-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathbundle-schemas.cjs
More file actions
70 lines (62 loc) · 2.4 KB
/
bundle-schemas.cjs
File metadata and controls
70 lines (62 loc) · 2.4 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const fs = require('fs').promises;
const Bundler = require("@hyperjump/json-schema-bundle");
(async function () {
bundle(`https://json-schema.org/draft/2019-09/schema`, 'draft09');
bundle(`https://json-schema.org/draft/2020-12/schema`, 'draft12');
}());
async function bundle(uri, filename) {
const metaSchema = await Bundler.get(uri);
const bundle = await Bundler.bundle(metaSchema);
const jsonified = JSON.stringify(bundle, null, 2).replace(/"undefined": ""/g, '"$dynamicAnchor": "meta"');
const jsified = 'export default ' + printObject(JSON.parse(jsonified));
fs.writeFile(`./${filename}.json`, jsonified, 'utf8');
fs.writeFile(`./${filename}.js`, jsified, 'utf8');
}
function printLiteral(value) {
if (typeof value === 'string') {
return `'${value}'`;
}
return value;
}
function printKey(value) {
if (value.match(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/)) {
return `${value}`;
}
return `'${value}'`;
}
function indent(level) {
return '\t'.repeat(level);
}
function printObject(obj, indentLevel = 0) {
const result = [];
if (Array.isArray(obj)) {
result.push(`[`);
for (const item of obj) {
if (typeof item === 'object' && item !== null) {
result.push(`${indent(indentLevel + 1)}${printObject(item, indentLevel + 1)},`);
} else {
result.push(`${indent(indentLevel + 1)}${printLiteral(item)},`);
}
}
result.push(`${indent(indentLevel)}]`);
return result.join('\n');
}
if (obj === null) {
result.push(`null`);
return result.join('\n');
}
result.push(`{`);
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && value !== null) {
result.push(`${indent(indentLevel + 1)}${printKey(key)}: ${printObject(value, indentLevel + 1)},`);
} else {
result.push(`${indent(indentLevel + 1)}${printKey(key)}: ${printLiteral(value)},`);
}
}
result.push(`${indent(indentLevel)}}`);
return result.join('\n');
}