@hla4ts/fom-codegen 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli.ts +179 -179
- package/src/generated/index.ts +25 -25
- package/src/generated/xsd-types.ts +63 -63
- package/src/xsd-parser.ts +404 -404
- package/src/xsd-type-generator.ts +452 -452
- package/src/xsd-types-adapter.ts +177 -177
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import { resolve } from "node:path";
|
|
3
|
-
import { pathToFileURL } from "node:url";
|
|
4
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
5
|
-
import { FomRegistry } from "./registry.ts";
|
|
6
|
-
import type { FomModel, FomOutputFormat, FomOutputOptions } from "./types.ts";
|
|
7
|
-
import { renderFomXml } from "./xml-writer.ts";
|
|
8
|
-
import { parseFomXml } from "./xml-parser.ts";
|
|
9
|
-
import { generateFomTypescript } from "./ts-codegen.ts";
|
|
10
|
-
|
|
11
|
-
type CliArgs = {
|
|
12
|
-
entry: string;
|
|
13
|
-
out?: string;
|
|
14
|
-
format: FomOutputFormat;
|
|
15
|
-
schemaLocation?: string;
|
|
16
|
-
includeModelIdentification: boolean;
|
|
17
|
-
indent?: string;
|
|
18
|
-
fromXml?: string;
|
|
19
|
-
outTs?: string;
|
|
20
|
-
tsMode?: "model" | "registry";
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function parseArgs(argv: string[]): CliArgs {
|
|
24
|
-
const args: CliArgs = {
|
|
25
|
-
entry: "",
|
|
26
|
-
format: "omt",
|
|
27
|
-
includeModelIdentification: true,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
for (let i = 0; i < argv.length; i += 1) {
|
|
31
|
-
const arg = argv[i];
|
|
32
|
-
switch (arg) {
|
|
33
|
-
case "--entry":
|
|
34
|
-
args.entry = argv[++i] ?? "";
|
|
35
|
-
break;
|
|
36
|
-
case "--from-xml":
|
|
37
|
-
args.fromXml = argv[++i];
|
|
38
|
-
break;
|
|
39
|
-
case "--out-ts":
|
|
40
|
-
args.outTs = argv[++i];
|
|
41
|
-
break;
|
|
42
|
-
case "--ts-mode":
|
|
43
|
-
args.tsMode = argv[++i] as "model" | "registry";
|
|
44
|
-
break;
|
|
45
|
-
case "--out":
|
|
46
|
-
args.out = argv[++i];
|
|
47
|
-
break;
|
|
48
|
-
case "--format":
|
|
49
|
-
args.format = (argv[++i] as FomOutputFormat) ?? "omt";
|
|
50
|
-
break;
|
|
51
|
-
case "--schema":
|
|
52
|
-
args.schemaLocation = argv[++i];
|
|
53
|
-
break;
|
|
54
|
-
case "--no-model-identification":
|
|
55
|
-
args.includeModelIdentification = false;
|
|
56
|
-
break;
|
|
57
|
-
case "--indent":
|
|
58
|
-
args.indent = argv[++i];
|
|
59
|
-
break;
|
|
60
|
-
case "--fdd":
|
|
61
|
-
args.format = "fdd";
|
|
62
|
-
break;
|
|
63
|
-
case "--omt":
|
|
64
|
-
args.format = "omt";
|
|
65
|
-
break;
|
|
66
|
-
case "--help":
|
|
67
|
-
printUsage();
|
|
68
|
-
process.exit(0);
|
|
69
|
-
default:
|
|
70
|
-
if (!arg.startsWith("--") && !args.entry) {
|
|
71
|
-
args.entry = arg;
|
|
72
|
-
}
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!args.entry && !args.fromXml) {
|
|
78
|
-
printUsage();
|
|
79
|
-
process.exit(1);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return args;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function printUsage(): void {
|
|
86
|
-
const text = [
|
|
87
|
-
"Usage: hla4ts-fom --entry <file> [options]",
|
|
88
|
-
"",
|
|
89
|
-
"Options:",
|
|
90
|
-
" --entry <file> Module that exports a registry or model",
|
|
91
|
-
" --out <file> Output XML file (stdout if omitted)",
|
|
92
|
-
" --format <omt|fdd> Output format (default: omt)",
|
|
93
|
-
" --omt Shortcut for --format omt",
|
|
94
|
-
" --fdd Shortcut for --format fdd",
|
|
95
|
-
" --from-xml <file> Parse XML and emit TypeScript",
|
|
96
|
-
" --out-ts <file> Output TypeScript file",
|
|
97
|
-
" --ts-mode <model|registry> TypeScript output mode (default: model)",
|
|
98
|
-
" --schema <value> Override xsi:schemaLocation",
|
|
99
|
-
" --indent <value> Indent string (default: two spaces)",
|
|
100
|
-
" --no-model-identification Skip modelIdentification block",
|
|
101
|
-
];
|
|
102
|
-
console.log(text.join("\n"));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function resolveModel(mod: Record<string, unknown>): FomModel {
|
|
106
|
-
if (isRegistry(mod.registry)) {
|
|
107
|
-
return (mod.registry as FomRegistry).buildModel();
|
|
108
|
-
}
|
|
109
|
-
if (isRegistry(mod.default)) {
|
|
110
|
-
return (mod.default as FomRegistry).buildModel();
|
|
111
|
-
}
|
|
112
|
-
if (typeof mod.buildFom === "function") {
|
|
113
|
-
return mod.buildFom() as FomModel;
|
|
114
|
-
}
|
|
115
|
-
if (isFomModel(mod.default)) {
|
|
116
|
-
return mod.default as FomModel;
|
|
117
|
-
}
|
|
118
|
-
if (isFomModel(mod.fom)) {
|
|
119
|
-
return mod.fom as FomModel;
|
|
120
|
-
}
|
|
121
|
-
throw new Error("Entry module did not export a registry or FomModel.");
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function isRegistry(value: unknown): value is FomRegistry {
|
|
125
|
-
return Boolean(
|
|
126
|
-
value &&
|
|
127
|
-
typeof value === "object" &&
|
|
128
|
-
typeof (value as FomRegistry).buildModel === "function" &&
|
|
129
|
-
typeof (value as FomRegistry).toXml === "function"
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function isFomModel(value: unknown): value is FomModel {
|
|
134
|
-
return Boolean(
|
|
135
|
-
value &&
|
|
136
|
-
typeof value === "object" &&
|
|
137
|
-
Array.isArray((value as FomModel).objectClasses) &&
|
|
138
|
-
Array.isArray((value as FomModel).interactionClasses) &&
|
|
139
|
-
Array.isArray((value as FomModel).dataTypes)
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async function main(): Promise<void> {
|
|
144
|
-
const args = parseArgs(process.argv.slice(2));
|
|
145
|
-
if (args.fromXml) {
|
|
146
|
-
const xml = readFileSync(args.fromXml, "utf8");
|
|
147
|
-
const model = parseFomXml(xml);
|
|
148
|
-
const ts = generateFomTypescript(model, {
|
|
149
|
-
mode: args.tsMode ?? "model",
|
|
150
|
-
});
|
|
151
|
-
if (args.outTs) {
|
|
152
|
-
writeFileSync(args.outTs, ts);
|
|
153
|
-
} else {
|
|
154
|
-
console.log(ts);
|
|
155
|
-
}
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const entryUrl = pathToFileURL(resolve(args.entry)).href;
|
|
160
|
-
const mod = (await import(entryUrl)) as Record<string, unknown>;
|
|
161
|
-
const model = resolveModel(mod);
|
|
162
|
-
const options: FomOutputOptions = {
|
|
163
|
-
format: args.format,
|
|
164
|
-
schemaLocation: args.schemaLocation,
|
|
165
|
-
includeModelIdentification: args.includeModelIdentification,
|
|
166
|
-
indent: args.indent,
|
|
167
|
-
};
|
|
168
|
-
const xml = renderFomXml(model, options);
|
|
169
|
-
if (args.out) {
|
|
170
|
-
writeFileSync(args.out, xml);
|
|
171
|
-
} else {
|
|
172
|
-
console.log(xml);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
main().catch((error) => {
|
|
177
|
-
console.error(`[hla4ts-fom] ${error instanceof Error ? error.message : String(error)}`);
|
|
178
|
-
process.exit(1);
|
|
179
|
-
});
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { FomRegistry } from "./registry.ts";
|
|
6
|
+
import type { FomModel, FomOutputFormat, FomOutputOptions } from "./types.ts";
|
|
7
|
+
import { renderFomXml } from "./xml-writer.ts";
|
|
8
|
+
import { parseFomXml } from "./xml-parser.ts";
|
|
9
|
+
import { generateFomTypescript } from "./ts-codegen.ts";
|
|
10
|
+
|
|
11
|
+
type CliArgs = {
|
|
12
|
+
entry: string;
|
|
13
|
+
out?: string;
|
|
14
|
+
format: FomOutputFormat;
|
|
15
|
+
schemaLocation?: string;
|
|
16
|
+
includeModelIdentification: boolean;
|
|
17
|
+
indent?: string;
|
|
18
|
+
fromXml?: string;
|
|
19
|
+
outTs?: string;
|
|
20
|
+
tsMode?: "model" | "registry";
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv: string[]): CliArgs {
|
|
24
|
+
const args: CliArgs = {
|
|
25
|
+
entry: "",
|
|
26
|
+
format: "omt",
|
|
27
|
+
includeModelIdentification: true,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
31
|
+
const arg = argv[i];
|
|
32
|
+
switch (arg) {
|
|
33
|
+
case "--entry":
|
|
34
|
+
args.entry = argv[++i] ?? "";
|
|
35
|
+
break;
|
|
36
|
+
case "--from-xml":
|
|
37
|
+
args.fromXml = argv[++i];
|
|
38
|
+
break;
|
|
39
|
+
case "--out-ts":
|
|
40
|
+
args.outTs = argv[++i];
|
|
41
|
+
break;
|
|
42
|
+
case "--ts-mode":
|
|
43
|
+
args.tsMode = argv[++i] as "model" | "registry";
|
|
44
|
+
break;
|
|
45
|
+
case "--out":
|
|
46
|
+
args.out = argv[++i];
|
|
47
|
+
break;
|
|
48
|
+
case "--format":
|
|
49
|
+
args.format = (argv[++i] as FomOutputFormat) ?? "omt";
|
|
50
|
+
break;
|
|
51
|
+
case "--schema":
|
|
52
|
+
args.schemaLocation = argv[++i];
|
|
53
|
+
break;
|
|
54
|
+
case "--no-model-identification":
|
|
55
|
+
args.includeModelIdentification = false;
|
|
56
|
+
break;
|
|
57
|
+
case "--indent":
|
|
58
|
+
args.indent = argv[++i];
|
|
59
|
+
break;
|
|
60
|
+
case "--fdd":
|
|
61
|
+
args.format = "fdd";
|
|
62
|
+
break;
|
|
63
|
+
case "--omt":
|
|
64
|
+
args.format = "omt";
|
|
65
|
+
break;
|
|
66
|
+
case "--help":
|
|
67
|
+
printUsage();
|
|
68
|
+
process.exit(0);
|
|
69
|
+
default:
|
|
70
|
+
if (!arg.startsWith("--") && !args.entry) {
|
|
71
|
+
args.entry = arg;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!args.entry && !args.fromXml) {
|
|
78
|
+
printUsage();
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return args;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function printUsage(): void {
|
|
86
|
+
const text = [
|
|
87
|
+
"Usage: hla4ts-fom --entry <file> [options]",
|
|
88
|
+
"",
|
|
89
|
+
"Options:",
|
|
90
|
+
" --entry <file> Module that exports a registry or model",
|
|
91
|
+
" --out <file> Output XML file (stdout if omitted)",
|
|
92
|
+
" --format <omt|fdd> Output format (default: omt)",
|
|
93
|
+
" --omt Shortcut for --format omt",
|
|
94
|
+
" --fdd Shortcut for --format fdd",
|
|
95
|
+
" --from-xml <file> Parse XML and emit TypeScript",
|
|
96
|
+
" --out-ts <file> Output TypeScript file",
|
|
97
|
+
" --ts-mode <model|registry> TypeScript output mode (default: model)",
|
|
98
|
+
" --schema <value> Override xsi:schemaLocation",
|
|
99
|
+
" --indent <value> Indent string (default: two spaces)",
|
|
100
|
+
" --no-model-identification Skip modelIdentification block",
|
|
101
|
+
];
|
|
102
|
+
console.log(text.join("\n"));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function resolveModel(mod: Record<string, unknown>): FomModel {
|
|
106
|
+
if (isRegistry(mod.registry)) {
|
|
107
|
+
return (mod.registry as FomRegistry).buildModel();
|
|
108
|
+
}
|
|
109
|
+
if (isRegistry(mod.default)) {
|
|
110
|
+
return (mod.default as FomRegistry).buildModel();
|
|
111
|
+
}
|
|
112
|
+
if (typeof mod.buildFom === "function") {
|
|
113
|
+
return mod.buildFom() as FomModel;
|
|
114
|
+
}
|
|
115
|
+
if (isFomModel(mod.default)) {
|
|
116
|
+
return mod.default as FomModel;
|
|
117
|
+
}
|
|
118
|
+
if (isFomModel(mod.fom)) {
|
|
119
|
+
return mod.fom as FomModel;
|
|
120
|
+
}
|
|
121
|
+
throw new Error("Entry module did not export a registry or FomModel.");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isRegistry(value: unknown): value is FomRegistry {
|
|
125
|
+
return Boolean(
|
|
126
|
+
value &&
|
|
127
|
+
typeof value === "object" &&
|
|
128
|
+
typeof (value as FomRegistry).buildModel === "function" &&
|
|
129
|
+
typeof (value as FomRegistry).toXml === "function"
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function isFomModel(value: unknown): value is FomModel {
|
|
134
|
+
return Boolean(
|
|
135
|
+
value &&
|
|
136
|
+
typeof value === "object" &&
|
|
137
|
+
Array.isArray((value as FomModel).objectClasses) &&
|
|
138
|
+
Array.isArray((value as FomModel).interactionClasses) &&
|
|
139
|
+
Array.isArray((value as FomModel).dataTypes)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function main(): Promise<void> {
|
|
144
|
+
const args = parseArgs(process.argv.slice(2));
|
|
145
|
+
if (args.fromXml) {
|
|
146
|
+
const xml = readFileSync(args.fromXml, "utf8");
|
|
147
|
+
const model = parseFomXml(xml);
|
|
148
|
+
const ts = generateFomTypescript(model, {
|
|
149
|
+
mode: args.tsMode ?? "model",
|
|
150
|
+
});
|
|
151
|
+
if (args.outTs) {
|
|
152
|
+
writeFileSync(args.outTs, ts);
|
|
153
|
+
} else {
|
|
154
|
+
console.log(ts);
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const entryUrl = pathToFileURL(resolve(args.entry)).href;
|
|
160
|
+
const mod = (await import(entryUrl)) as Record<string, unknown>;
|
|
161
|
+
const model = resolveModel(mod);
|
|
162
|
+
const options: FomOutputOptions = {
|
|
163
|
+
format: args.format,
|
|
164
|
+
schemaLocation: args.schemaLocation,
|
|
165
|
+
includeModelIdentification: args.includeModelIdentification,
|
|
166
|
+
indent: args.indent,
|
|
167
|
+
};
|
|
168
|
+
const xml = renderFomXml(model, options);
|
|
169
|
+
if (args.out) {
|
|
170
|
+
writeFileSync(args.out, xml);
|
|
171
|
+
} else {
|
|
172
|
+
console.log(xml);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
main().catch((error) => {
|
|
177
|
+
console.error(`[hla4ts-fom] ${error instanceof Error ? error.message : String(error)}`);
|
|
178
|
+
process.exit(1);
|
|
179
|
+
});
|
package/src/generated/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generated XSD Types - Re-exported for convenience
|
|
3
|
-
*
|
|
4
|
-
* This module re-exports the generated TypeScript types from the XSD schema.
|
|
5
|
-
* These types provide autocomplete and type checking that matches XSD validation.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export * from "./xsd-types.ts";
|
|
9
|
-
|
|
10
|
-
// Re-export commonly used types with shorter names for convenience
|
|
11
|
-
export type {
|
|
12
|
-
ModelIdentification as XsdModelIdentification,
|
|
13
|
-
ObjectClass as XsdObjectClass,
|
|
14
|
-
InteractionClass as XsdInteractionClass,
|
|
15
|
-
Attribute as XsdAttribute,
|
|
16
|
-
Parameter as XsdParameter,
|
|
17
|
-
} from "./xsd-types.ts";
|
|
18
|
-
|
|
19
|
-
export type {
|
|
20
|
-
SharingEnumeration as XsdSharingType,
|
|
21
|
-
OrderEnumeration as XsdOrderType,
|
|
22
|
-
UpdateEnumeration as XsdUpdateType,
|
|
23
|
-
OwnershipEnumeration as XsdOwnershipType,
|
|
24
|
-
OMTypeEnumeration as XsdOMType,
|
|
25
|
-
} from "./xsd-types.ts";
|
|
1
|
+
/**
|
|
2
|
+
* Generated XSD Types - Re-exported for convenience
|
|
3
|
+
*
|
|
4
|
+
* This module re-exports the generated TypeScript types from the XSD schema.
|
|
5
|
+
* These types provide autocomplete and type checking that matches XSD validation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from "./xsd-types.ts";
|
|
9
|
+
|
|
10
|
+
// Re-export commonly used types with shorter names for convenience
|
|
11
|
+
export type {
|
|
12
|
+
ModelIdentification as XsdModelIdentification,
|
|
13
|
+
ObjectClass as XsdObjectClass,
|
|
14
|
+
InteractionClass as XsdInteractionClass,
|
|
15
|
+
Attribute as XsdAttribute,
|
|
16
|
+
Parameter as XsdParameter,
|
|
17
|
+
} from "./xsd-types.ts";
|
|
18
|
+
|
|
19
|
+
export type {
|
|
20
|
+
SharingEnumeration as XsdSharingType,
|
|
21
|
+
OrderEnumeration as XsdOrderType,
|
|
22
|
+
UpdateEnumeration as XsdUpdateType,
|
|
23
|
+
OwnershipEnumeration as XsdOwnershipType,
|
|
24
|
+
OMTypeEnumeration as XsdOMType,
|
|
25
|
+
} from "./xsd-types.ts";
|