forked from mouseless/json-concepts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
138 lines (120 loc) · 3.97 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const path = require('path');
const fs = require('fs');
const cb = require('code-blocks');
fs.rmdirSync('./.dist', { recursive: true });
// 01-spec-dir or 01-spec-file.md
const specDirOrFile = /^[0-9]{2}-[a-z-]*([.]md)?$/;
const specFiles = files('./specs', entry => specDirOrFile.test(entry.name));
const specs = fromFileToTwoLevelHierarchy(specFiles, _ => {
return {
section: _.file.parent.name.substring(3, _.file.parent.name.length),
name: _.file.name.substring(3, _.file.name.length - 3),
path: _.file.path
};
});
const testCases = fromSpecToTestCase(specs, _ => {
return {
path: `./.dist/test-cases/${_.spec.section}/${_.spec.name}/${_.block.info.name}`,
content: _.block.value
};
});
// example-folder or example.json
// - exclude hidden e.g. .gitignore
// - exclude drafts e.g. xx-not-yet
const exampleDirOrFile = /^(?!\.)(?!xx-)[a-z-]*(([.][a-z-]*)?[.]json)?$/;
const exampleFiles = files('./examples', entry => exampleDirOrFile.test(entry.name));
const examples = fromFileToTwoLevelHierarchy(exampleFiles, _ => {
return {
group: _.file.parent.name,
name: _.file.name,
path: _.file.path
};
});
// copy specs to .dist/specs
for (const spec of specs) {
const targetPath = `./.dist/specs/${spec.section}/${spec.name}.md`;
mkdir(targetPath);
fs.copyFileSync(spec.path, targetPath);
}
// extract code blocks to .dist/test-cases
for (const testCase of testCases) {
if (fs.existsSync(testCase.path)) {
throw new Error(`'${testCase.path}' already exists`);
}
mkdir(testCase.path);
fs.writeFileSync(testCase.path, testCase.content);
}
// copy examples to .dist/examples
for (const example of examples) {
const targetPath = `./.dist/examples/${example.group}/${example.name}`;
mkdir(targetPath);
fs.copyFileSync(example.path, targetPath);
}
function mkdir(targetPath) {
const dir = path.dirname(targetPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
function files(root, filter, _parent) {
const entries = fs
.readdirSync(root, { withFileTypes: true })
.filter(file => filter(file) && file.name != 'node_modules');
if (entries.length == 0) {
return {};
}
const result = {};
for (const file of entries) {
if (file.isDirectory()) {
result[file.name] = {
type: 'directory',
path: `${root}/${file.name}`,
name: file.name,
parent: _parent
};
result[file.name].children = files(`${root}/${file.name}`, filter, result[file.name]);
} else {
result[file.name] = {
type: 'file',
path: `${root}/${file.name}`,
name: file.name,
parent: _parent
};
}
}
return result;
}
function fromFileToTwoLevelHierarchy(files, map) {
const result = [];
for (const parent of Object.values(files)) {
if (parent.children) {
for (const child of Object.values(parent.children)) {
result.push(map({ file: child }));
}
}
}
return result;
}
function fromSpecToTestCase(specs, map) {
const result = [];
specs.forEach(spec => {
const blocks = cb.fromFileSync(spec.path);
for (const block of blocks.filter(b => b.info.name)) {
if (block.lang === 'json') {
try {
JSON.parse(block.value);
} catch (e) {
console.error(`\x1b[31m` +
`invalid json '${block.info.name}' ` +
`at ${block.position.start.line}-${block.position.end.line} ` +
`in '${block.source.file}'.\n` +
`${e}` +
`\x1b[37m`);
process.exit(1);
}
}
result.push(map({ spec, block }));
}
});
return result;
}