-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
61 lines (55 loc) · 1.64 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
const fs = require('fs')
const util = require('util')
const path = require('path')
const handlebars = require('handlebars')
const yaml = require('js-yaml')
const Ajv = require('ajv/dist/2020')
const addFormats = require('ajv-formats')
const helpers = require('handlebars-helpers')({ handlebars })
const SCHEMA = require('./schema.json')
const ajv = new Ajv({
allowUnionTypes: true,
strict: false,
strictTypes: true,
strictTuples: true,
validateSchema: true,
validateFormats: true,
allErrors: true
})
addFormats(ajv)
/*
* Validate the data against the schema
*/
const validateFunction = ajv.compile(SCHEMA)
console.error('Reading YAML data file...')
const DATA = yaml.load(fs.readFileSync(path.resolve(__dirname, 'data.yaml'), 'utf8'))
console.error('Validating the data against the schema...')
if (!validateFunction(DATA)) {
console.error('Validation failed')
for (const error of validateFunction.errors) {
console.error(`- [${error.instancePath}]: ${error.message}`)
}
process.exit(1)
}
for (const item of DATA) {
for (const subitem of DATA) {
if (!util.isDeepStrictEqual(item, subitem) && item.url === subitem.url) {
console.error(`Duplicated URL: ${item.url}`)
process.exit(1)
}
}
}
/*
* Generate the README from the data
*/
console.error('Reading template...')
const template = fs.readFileSync(path.resolve(__dirname, 'template.hbs'), 'utf8')
console.error('Generating README...')
const output = handlebars.compile(template)({
data: DATA,
reverseSortBy: (...args) => {
return helpers.sortBy(...args).reverse()
}
})
fs.writeFileSync(path.resolve(__dirname, 'README.md'), output)
console.error('Done!')