generated from gethyperos/hypr-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
109 lines (88 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Repository index generator
//
// List of available icons:
// https://ionic.io/ionicons
//
const fs = require('fs')
const Validator = require('jsonschema').Validator;
const validator = new Validator();
const console = require('consola')
function readJson(path) {
return JSON.parse(fs.readFileSync(path, 'utf8'))
}
function writeJson(path, data) {
fs.writeFileSync(path, JSON.stringify(data, null, 2))
}
function enforceCategories() {
const Categories = readJson('./categories.json')
const Schema = readJson('./schemas/categories.schema.json')
const validationResult = validator.validate(Categories, Schema)
if (validationResult.errors.length > 0) {
console.error(`Invalid categories.json`)
validationResult.errors.forEach( err => {
console.info(`${err.stack ?? err.property + ' ' + err.nessage}`)
})
process.exit(1)
} else {
console.success(`categories.json is valid`)
}
}
function generateAppIndex() {
try {
const Apps = []
const UniqueIds = []
const categories = readJson('./categories.json')
fs.readdirSync('./Apps').forEach(path => {
if (!fs.lstatSync(`./Apps/${path}`).isDirectory) { return }
const appManifest = readJson(`./Apps/${path}/app.json`)
const Schema = readJson('./schemas/app.schema.json')
const validationResult = validator.validate(appManifest, Schema)
appManifest.App.categories.forEach( category => {
const foundCategory = categories.filter( c => c.name === category )
if (foundCategory.length < 1) {
console.warn(`App "${appManifest.App.name}" has invalid category: ${category}`)
process.exit(1)
}
})
if (UniqueIds.includes(appManifest.App.id)) {
console.warn(`Duplicate app id: ${appManifest.App.id} for: ${path}/app.json`)
process.exit(1)
}
try {
fs.readFileSync(`Apps/${path}/${appManifest.App.icon}`)
} catch (e) {
console.warn(`App "${appManifest.App.name}" has invalid icon path!
${appManifest.App.icon} (Icon not found)
Missing icon doesn\'t prevent your app from validating, but it won\'t be merged.`
)
}
UniqueIds.push(appManifest.App.id)
Apps.push({
id: appManifest.App.id,
name: appManifest.App.name,
description: appManifest.App.description,
directory: path,
icon: `Apps/${path}/${appManifest.App.icon}`,
manifest: `Apps/${path}/app.json`,
metadata: `Apps/${path}/metadata/app.md`,
categories: appManifest.App.categories
})
if (validationResult.errors.length > 0) {
console.error(`Invalid app manifest: ${path}/app.json`)
validationResult.errors.forEach( err => {
console.info(`${err.stack ?? err.property + ' ' + err.nessage}`)
})
process.exit(1)
} else {
console.success(`App "${appManifest.App.name}" is valid`)
}
appManifest.App.directory = path
fs.writeFileSync(`Apps/${path}/app.json`, JSON.stringify(appManifest, null, 2))
})
writeJson( './index.json', Apps.sort() )
} catch(e) {
console.error(`${e}`)
}
}
enforceCategories()
generateAppIndex()