|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +const fs = require('fs').promises; |
| 7 | +const Bundler = require("@hyperjump/json-schema-bundle"); |
| 8 | + |
| 9 | +(async function () { |
| 10 | + bundle(`https://json-schema.org/draft/2019-09/schema`, 'draft09'); |
| 11 | + bundle(`https://json-schema.org/draft/2020-12/schema`, 'draft12'); |
| 12 | +}()); |
| 13 | + |
| 14 | +async function bundle(uri, filename) { |
| 15 | + const metaSchema = await Bundler.get(uri); |
| 16 | + const bundle = await Bundler.bundle(metaSchema); |
| 17 | + const jsonified = JSON.stringify(bundle, null, 2).replace(/"undefined": ""/g, '"$dynamicAnchor": "meta"'); |
| 18 | + const jsified = 'export default ' + printObject(JSON.parse(jsonified)); |
| 19 | + fs.writeFile(`./${filename}.json`, jsonified, 'utf8'); |
| 20 | + fs.writeFile(`./${filename}.js`, jsified, 'utf8'); |
| 21 | +} |
| 22 | + |
| 23 | +function printLiteral(value) { |
| 24 | + if (typeof value === 'string') { |
| 25 | + return `'${value}'`; |
| 26 | + } |
| 27 | + return value; |
| 28 | +} |
| 29 | + |
| 30 | +function printKey(value) { |
| 31 | + if (value.match(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/)) { |
| 32 | + return `${value}`; |
| 33 | + } |
| 34 | + return `'${value}'`; |
| 35 | +} |
| 36 | + |
| 37 | +function indent(level) { |
| 38 | + return '\t'.repeat(level); |
| 39 | +} |
| 40 | + |
| 41 | +function printObject(obj, indentLevel = 0) { |
| 42 | + const result = []; |
| 43 | + if (Array.isArray(obj)) { |
| 44 | + result.push(`[`); |
| 45 | + for (const item of obj) { |
| 46 | + if (typeof item === 'object' && item !== null) { |
| 47 | + result.push(`${indent(indentLevel + 1)}${printObject(item, indentLevel + 1)},`); |
| 48 | + } else { |
| 49 | + result.push(`${indent(indentLevel + 1)}${printLiteral(item)},`); |
| 50 | + } |
| 51 | + } |
| 52 | + result.push(`${indent(indentLevel)}]`); |
| 53 | + return result.join('\n'); |
| 54 | + } |
| 55 | + if (obj === null) { |
| 56 | + result.push(`null`); |
| 57 | + return result.join('\n'); |
| 58 | + } |
| 59 | + |
| 60 | + result.push(`{`); |
| 61 | + for (const [key, value] of Object.entries(obj)) { |
| 62 | + if (typeof value === 'object' && value !== null) { |
| 63 | + result.push(`${indent(indentLevel + 1)}${printKey(key)}: ${printObject(value, indentLevel + 1)},`); |
| 64 | + } else { |
| 65 | + result.push(`${indent(indentLevel + 1)}${printKey(key)}: ${printLiteral(value)},`); |
| 66 | + } |
| 67 | + } |
| 68 | + result.push(`${indent(indentLevel)}}`); |
| 69 | + return result.join('\n'); |
| 70 | +} |
0 commit comments