Skip to content

Commit 4af9c44

Browse files
committed
update
1 parent 0b44cbe commit 4af9c44

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
const fs = require('fs').promises;
7-
const Bundler = require("@hyperjump/json-schema-bundle");
6+
import { writeFile } from 'node:fs/promises';
7+
import Bundler from '@hyperjump/json-schema-bundle';
88

99
(async function () {
10-
bundle(`https://json-schema.org/draft/2019-09/schema`, 'draft-2019-09', 'https://json-schema.org/draft/2019-09');
11-
bundle(`https://json-schema.org/draft/2020-12/schema`, 'draft-2020-12', 'https://json-schema.org/draft/2020-12');
10+
bundle('https://json-schema.org/draft/2019-09/schema', 'draft-2019-09', 'https://json-schema.org/draft/2019-09');
11+
bundle('https://json-schema.org/draft/2020-12/schema', 'draft-2020-12', 'https://json-schema.org/draft/2020-12');
1212
}());
1313

1414
async function bundle(uri, filename, derivedURL) {
1515
const metaSchema = await Bundler.get(uri);
1616
let bundle = await Bundler.bundle(metaSchema);
1717
bundle = JSON.parse(JSON.stringify(bundle, null, 2).replace(/"undefined": ""/g, '"$dynamicAnchor": "meta"'));
18-
fs.writeFile(`./${filename}.json`, JSON.stringify(bundle, null, 2), 'utf8');
18+
await writeFile(`./${filename}.json`, JSON.stringify(bundle, null, 2), 'utf8');
1919
bundle = flattenDraftMetaSchema(bundle);
2020
const jsified = getCopyright(derivedURL) + 'export default ' + printObject(bundle);
21-
fs.writeFile(`./${filename}-flat.json`, JSON.stringify(bundle, null, 2), 'utf8');
22-
fs.writeFile(`./src/services/schemas/${filename}-flat.ts`, jsified, 'utf8');
21+
await writeFile(`./${filename}-flat.json`, JSON.stringify(bundle, null, 2), 'utf8');
22+
await writeFile(`./src/services/schemas/${filename}-flat.ts`, jsified, 'utf8');
2323
}
2424
function getCopyright(derivedURL) {
2525
return [
@@ -55,7 +55,7 @@ function indent(level) {
5555
function printObject(obj, indentLevel = 0) {
5656
const result = [];
5757
if (Array.isArray(obj)) {
58-
result.push(`[`);
58+
result.push('[');
5959
for (const item of obj) {
6060
if (typeof item === 'object' && item !== null) {
6161
result.push(`${indent(indentLevel + 1)}${printObject(item, indentLevel + 1)},`);
@@ -67,11 +67,11 @@ function printObject(obj, indentLevel = 0) {
6767
return result.join('\n');
6868
}
6969
if (obj === null) {
70-
result.push(`null`);
70+
result.push('null');
7171
return result.join('\n');
7272
}
7373

74-
result.push(`{`);
74+
result.push('{');
7575
for (const [key, value] of Object.entries(obj)) {
7676
if (typeof value === 'object' && value !== null) {
7777
result.push(`${indent(indentLevel + 1)}${printKey(key)}: ${printObject(value, indentLevel + 1)},`);
@@ -106,9 +106,9 @@ function replaceDynamicRefs(node, anchorName = DEFAULT_ANCHOR) {
106106
visit(node, (n, k) => {
107107
const v = n[k];
108108
if (k === '$dynamicRef' && v === '#' + anchorName) {
109-
n['$ref'] = '#';
110-
delete n['$dynamicRef'];
111-
};
109+
n.$ref = '#';
110+
delete n.$dynamicRef;
111+
}
112112
});
113113
}
114114

@@ -117,9 +117,9 @@ function replaceRecursiveRefs(node, anchorName = DEFAULT_ANCHOR) {
117117
visit(node, (n, k) => {
118118
const v = n[k];
119119
if (k === '$recursiveRef') {
120-
n['$ref'] = v;
121-
delete n['$recursiveRef'];
122-
};
120+
n.$ref = v;
121+
delete n.$recursiveRef;
122+
}
123123
});
124124
}
125125

@@ -130,7 +130,7 @@ function replaceOldRefs(node, anchorName = DEFAULT_ANCHOR) {
130130
if (k === '$ref' && typeof v === 'string' && v.startsWith(anchorName + '/')) {
131131
const segments = v.split('#');
132132
if (segments.length === 2) {
133-
n['$ref'] = `#${segments[1]}`;
133+
n.$ref = `#${segments[1]}`;
134134
}
135135
}
136136
});
@@ -149,7 +149,7 @@ function stripDynamicAnchors(node) {
149149
function collectVocabularies(schema) {
150150
const vocabularies = [];
151151
const defs = schema.$defs || {};
152-
for (const [key, value] of Object.entries(defs)) {
152+
for (const [, value] of Object.entries(defs)) {
153153
if (value && typeof value === 'object' && !Array.isArray(value) && value.$id && value.$dynamicAnchor === DEFAULT_ANCHOR && value.properties) {
154154
vocabularies.push(value);
155155
}
@@ -251,4 +251,4 @@ function flattenDraftMetaSchema(original) {
251251
}
252252

253253
return schema;
254-
}
254+
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
const fs = require('fs');
7-
const path = require('path');
6+
import fs from 'node:fs';
7+
import path from 'node:path';
8+
import { fileURLToPath } from 'node:url';
9+
10+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
811

912
function deleteRefs(dir) {
1013
const files = fs.readdirSync(dir);
11-
for (let file of files) {
14+
for (const file of files) {
1215
const filePath = path.join(dir, file);
1316
const stat = fs.statSync(filePath);
1417
if (stat.isDirectory()) {
@@ -27,6 +30,6 @@ function deleteRefs(dir) {
2730
}
2831
}
2932

30-
let location = path.join(__dirname, '..', 'lib');
33+
const location = path.join(__dirname, '..', 'lib');
3134
console.log('process ' + location);
3235
deleteRefs(location);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"prepack": "npm run clean && npm run compile && npm run test && npm run remove-sourcemap-refs",
4040
"compile": "tsc -p ./src",
4141
"clean": "rimraf lib",
42-
"remove-sourcemap-refs": "node ./build/remove-sourcemap-refs.cjs",
42+
"bundle-schemas": "node ./build/bundle-schemas.js",
43+
"remove-sourcemap-refs": "node ./build/remove-sourcemap-refs.js",
4344
"watch": "tsc -w -p ./src",
4445
"pretest": "npm run compile",
4546
"test": "node --test ./lib/esm/test/**/*.test.js",

0 commit comments

Comments
 (0)