forked from frictionlessdata/datapackage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
110 lines (81 loc) · 2.61 KB
/
build.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
110
const util = require('util')
const fs = require('fs-extra')
const yaml = require('js-yaml')
const nodePath = require('path')
const glob = util.promisify(require('glob'))
const $RefParser = require('json-schema-ref-parser')
const csvParseSync = require('csv-parse/lib/sync');
const excludeOnOutput = ['dictionary.json']
// Helpers
function prepareDirectories() {
const dirs = [
'build/schemas',
'build/specs',
]
// Ensure directories
for (const dir of dirs) {
fs.emptyDirSync(dir)
}
}
function compileDictionary() {
// Init dictionary
const dictionary = {
'$schema': 'http://json-schema.org/draft-04/schema#',
definitions: {}
}
// Fill dictionary
for (const file of glob.sync('schemas/dictionary/*.yml')) {
const contents = fs.readFileSync(file)
Object.assign(dictionary.definitions, yaml.safeLoad(contents))
}
// Save dictionary
const contents = JSON.stringify(dictionary, null, 2)
fs.writeFileSync('schemas/dictionary.json', contents)
}
async function buildSchemas() {
// Dereference and save every schema
for (const file of glob.sync('schemas/*.json')) {
const basename = nodePath.basename(file)
if (excludeOnOutput.includes(basename)) continue
const rawSchema = JSON.parse(fs.readFileSync(file))
const schema = await $RefParser.dereference(rawSchema)
const contents = JSON.stringify(schema, null, 2)
fs.writeFileSync(`build/schemas/${basename}`, contents)
console.log(`[+] build/schemas/${basename}.json`)
}
}
function buildRegistry() {
// Copy csv registry
fs.copyFileSync('schemas/registry.csv', 'build/schemas/registry.csv')
console.log(`[+] build/schemas/registry.csv`)
// Convert csv registy to json
const registry = csvParseSync(fs.readFileSync('schemas/registry.csv'), {columns: true})
const contents = JSON.stringify(registry, null, 2)
fs.writeFileSync(`build/schemas/registry.json`, contents)
console.log(`[+] build/schemas/registry.json`)
}
function buildSpecs() {
// Convert specs files for `lektor`
for (const file of glob.sync('specs/*.md')) {
let name = nodePath.parse(file).name
if (name === 'index') name = ''
fs.ensureDirSync(`build/specs/${name}`)
fs.copyFileSync(file, `build/specs/${name}/contents.lr`)
console.log(`[+] build/schemas/specs/${name}/contents.lr`)
}
}
// Main script
async function main() {
prepareDirectories()
compileDictionary()
await buildSchemas()
buildRegistry()
buildSpecs()
}
process.on('warning', (warning) => {
console.error(warning)
process.exit(1)
})
main()
.then(() => console.log('Specs build is completed!'))
.catch((error) => {throw error})