| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const { compile } = require('json-schema-to-typescript'); |
| 6 | |
| 7 | const rootDir = |
| 8 | process.env.OPENCC_CONFIG_TYPES_ROOT || path.resolve(__dirname, '..'); |
| 9 | const schemaPath = path.join(rootDir, 'data/config/opencc_config.schema.json'); |
| 10 | const declarationFiles = [ |
| 11 | 'node/opencc.d.ts', |
| 12 | 'node/opencc.d.cts', |
| 13 | 'node/opencc.d.mts', |
| 14 | ].map((file) => path.join(rootDir, file)); |
| 15 | |
| 16 | const definitionTitles = { |
| 17 | segmentation: 'OpenCCSegmentation', |
| 18 | mmseg_segmentation: 'OpenCCMmsegSegmentation', |
| 19 | plugin_segmentation: 'OpenCCPluginSegmentation', |
| 20 | conversion: 'OpenCCConversion', |
| 21 | dict: 'OpenCCDict', |
| 22 | file_dict: 'OpenCCFileDict', |
| 23 | inline_dict: 'OpenCCInlineDict', |
| 24 | group_dict: 'OpenCCGroupDict', |
| 25 | }; |
| 26 | |
| 27 | function loadSchema() { |
| 28 | const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8')); |
| 29 | schema.title = 'OpenCCConfig'; |
| 30 | |
| 31 | for (const [name, title] of Object.entries(definitionTitles)) { |
| 32 | schema.definitions[name].title = title; |
| 33 | } |
| 34 | |
| 35 | schema.definitions.plugin_segmentation.tsType = [ |
| 36 | '{', |
| 37 | 'type: string;', |
| 38 | 'resources?: Record<string, string>;', |
| 39 | '[key: string]: string | Record<string, string> | undefined;', |
| 40 | '}', |
| 41 | ].join(' '); |
| 42 | |
| 43 | return schema; |
| 44 | } |
| 45 | |
| 46 | function normalizeGeneratedTypes(types) { |
| 47 | return types |
| 48 | .trim() |
| 49 | .replace(/^[ \t]*\/\*\*[\s\S]*?\*\/\n/gm, '') |
| 50 | .replace(/^export /gm, '') |
| 51 | .replace(/^\n+|\n+$/g, ''); |
| 52 | } |
| 53 | |
| 54 | async function generatedBlock() { |
| 55 | const generatedTypes = await compile(loadSchema(), 'OpenCCConfig', { |
| 56 | bannerComment: '', |
| 57 | maxItems: -1, |
| 58 | unreachableDefinitions: true, |
| 59 | }); |
| 60 | |
| 61 | return normalizeGeneratedTypes(generatedTypes); |
| 62 | } |
| 63 | |
| 64 | function replaceGeneratedBlock(filePath, block) { |
| 65 | const source = fs.readFileSync(filePath, 'utf8'); |
| 66 | |
| 67 | const optionsStart = source.indexOf('\ninterface OpenCCOptions {'); |
| 68 | if (optionsStart !== -1) { |
| 69 | return `${block}\n${source.slice(optionsStart)}`; |
| 70 | } |
| 71 | |
| 72 | const originalConfigType = 'type OpenCCConfig = Record<string, unknown>;'; |
| 73 | if (source.startsWith(originalConfigType)) { |
| 74 | return `${block}${source.slice(originalConfigType.length)}`; |
| 75 | } |
| 76 | |
| 77 | throw new Error(`Unable to locate generated block in ${filePath}`); |
| 78 | } |
| 79 | |
| 80 | async function main() { |
| 81 | const block = await generatedBlock(); |
| 82 | let hasDiff = false; |
| 83 | |
| 84 | for (const filePath of declarationFiles) { |
| 85 | const next = replaceGeneratedBlock(filePath, block); |
| 86 | const current = fs.readFileSync(filePath, 'utf8'); |
| 87 | if (next !== current) { |
| 88 | hasDiff = true; |
| 89 | if (process.argv.includes('--check')) { |
| 90 | console.error(`${path.relative(rootDir, filePath)} is not up to date.`); |
| 91 | } else { |
| 92 | fs.writeFileSync(filePath, next); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (process.argv.includes('--check') && hasDiff) { |
| 98 | process.exit(1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | main().catch((error) => { |
| 103 | console.error(error); |
| 104 | process.exit(1); |
| 105 | }); |