Skip to content

Commit 75505eb

Browse files
committed
Generate Node config types from schema
Derive OpenCCConfig typings from the JSON schema with json-schema-to-typescript instead of keeping a broad Record type. Keep regeneration on the npm script path and document it in the Node README.
1 parent 9fd996d commit 75505eb

7 files changed

Lines changed: 371 additions & 3 deletions

File tree

node/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,14 @@ npm install
298298
npm test
299299
```
300300

301+
The `OpenCCConfig` TypeScript declarations in `opencc.d.ts`,
302+
`opencc.d.cts`, and `opencc.d.mts` are generated from
303+
`data/config/opencc_config.schema.json`. Regenerate them after changing the
304+
schema:
305+
306+
```bash
307+
npm run generate:node-config-types
308+
```
309+
301310
To build prebuilt native addons for publishing, see
302311
[`node/PUBLISHING.md`](./PUBLISHING.md).

node/opencc.d.cts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1-
type OpenCCConfig = Record<string, unknown>;
1+
type OpenCCDict = OpenCCFileDict | OpenCCInlineDict | OpenCCGroupDict;
2+
type OpenCCSegmentation = OpenCCMmsegSegmentation | OpenCCPluginSegmentation;
3+
type OpenCCPluginSegmentation = {
4+
type: string;
5+
resources?: Record<string, string>;
6+
[key: string]: string | Record<string, string> | undefined;
7+
};
8+
9+
interface OpenCCConfig {
10+
name: string;
11+
normalization?: [OpenCCConversion, ...OpenCCConversion[]];
12+
segmentation?: OpenCCSegmentation;
13+
conversion_chain: [OpenCCConversion, ...OpenCCConversion[]];
14+
}
15+
interface OpenCCConversion {
16+
dict: OpenCCDict;
17+
}
18+
interface OpenCCFileDict {
19+
type: "text" | "ocd" | "ocd2";
20+
file: string;
21+
may_output_tofu?: boolean;
22+
}
23+
interface OpenCCInlineDict {
24+
type: "inline";
25+
entries: {
26+
[k: string]: string;
27+
};
28+
}
29+
interface OpenCCGroupDict {
30+
type: "group";
31+
match_policy: "short_circuit" | "union";
32+
dicts: [OpenCCDict, ...OpenCCDict[]];
33+
may_output_tofu?: boolean;
34+
}
35+
interface OpenCCMmsegSegmentation {
36+
type: "mmseg";
37+
dict: OpenCCDict;
38+
}
239

340
interface OpenCCOptions {
441
includeTofuRiskDictionaries?: boolean;

node/opencc.d.mts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1-
type OpenCCConfig = Record<string, unknown>;
1+
type OpenCCDict = OpenCCFileDict | OpenCCInlineDict | OpenCCGroupDict;
2+
type OpenCCSegmentation = OpenCCMmsegSegmentation | OpenCCPluginSegmentation;
3+
type OpenCCPluginSegmentation = {
4+
type: string;
5+
resources?: Record<string, string>;
6+
[key: string]: string | Record<string, string> | undefined;
7+
};
8+
9+
interface OpenCCConfig {
10+
name: string;
11+
normalization?: [OpenCCConversion, ...OpenCCConversion[]];
12+
segmentation?: OpenCCSegmentation;
13+
conversion_chain: [OpenCCConversion, ...OpenCCConversion[]];
14+
}
15+
interface OpenCCConversion {
16+
dict: OpenCCDict;
17+
}
18+
interface OpenCCFileDict {
19+
type: "text" | "ocd" | "ocd2";
20+
file: string;
21+
may_output_tofu?: boolean;
22+
}
23+
interface OpenCCInlineDict {
24+
type: "inline";
25+
entries: {
26+
[k: string]: string;
27+
};
28+
}
29+
interface OpenCCGroupDict {
30+
type: "group";
31+
match_policy: "short_circuit" | "union";
32+
dicts: [OpenCCDict, ...OpenCCDict[]];
33+
may_output_tofu?: boolean;
34+
}
35+
interface OpenCCMmsegSegmentation {
36+
type: "mmseg";
37+
dict: OpenCCDict;
38+
}
239

340
interface OpenCCOptions {
441
includeTofuRiskDictionaries?: boolean;

node/opencc.d.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1-
type OpenCCConfig = Record<string, unknown>;
1+
type OpenCCDict = OpenCCFileDict | OpenCCInlineDict | OpenCCGroupDict;
2+
type OpenCCSegmentation = OpenCCMmsegSegmentation | OpenCCPluginSegmentation;
3+
type OpenCCPluginSegmentation = {
4+
type: string;
5+
resources?: Record<string, string>;
6+
[key: string]: string | Record<string, string> | undefined;
7+
};
8+
9+
interface OpenCCConfig {
10+
name: string;
11+
normalization?: [OpenCCConversion, ...OpenCCConversion[]];
12+
segmentation?: OpenCCSegmentation;
13+
conversion_chain: [OpenCCConversion, ...OpenCCConversion[]];
14+
}
15+
interface OpenCCConversion {
16+
dict: OpenCCDict;
17+
}
18+
interface OpenCCFileDict {
19+
type: "text" | "ocd" | "ocd2";
20+
file: string;
21+
may_output_tofu?: boolean;
22+
}
23+
interface OpenCCInlineDict {
24+
type: "inline";
25+
entries: {
26+
[k: string]: string;
27+
};
28+
}
29+
interface OpenCCGroupDict {
30+
type: "group";
31+
match_policy: "short_circuit" | "union";
32+
dicts: [OpenCCDict, ...OpenCCDict[]];
33+
may_output_tofu?: boolean;
34+
}
35+
interface OpenCCMmsegSegmentation {
36+
type: "mmseg";
37+
dict: OpenCCDict;
38+
}
239

340
interface OpenCCOptions {
441
includeTofuRiskDictionaries?: boolean;

package-lock.json

Lines changed: 140 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"scripts": {
2727
"test": "node --test --test-reporter=spec node/test.js && node node/esm-test.mjs",
28+
"generate:node-config-types": "node scripts/generate-node-opencc-config-types.js",
2829
"prebuild": "prebuildify --napi --strip --postinstall \"node scripts/prepare-node-prebuild-artifacts.js\"",
2930
"prepare:scoped-packages": "node scripts/prepare-node-scoped-packages.js",
3031
"prepack": "node scripts/verify-node-main-package.js",
@@ -63,6 +64,7 @@
6364
"prebuilds/assets/*.ocd2",
6465
"scripts/prepare-node-prebuild-artifacts.js",
6566
"scripts/install.js",
67+
"scripts/generate-node-opencc-config-types.js",
6668
"scripts/prepare-node-scoped-packages.js",
6769
"scripts/verify-node-main-package.js",
6870
"scripts/verify-node-scoped-packages-published.js",
@@ -148,6 +150,7 @@
148150
"Traditional Chinese"
149151
],
150152
"devDependencies": {
153+
"json-schema-to-typescript": "^15.0.4",
151154
"prebuildify": "^6.0.1"
152155
},
153156
"dependencies": {

0 commit comments

Comments
 (0)