@amritk/mjst 0.12.2 → 0.12.3

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.
@@ -1,245 +1,206 @@
1
- const SOURCE_FORMATS = ['json', 'typebox', 'zod', 'valibot', 'effect'];
2
- // Boolean flags toggle on by presence and accept `--flag=false` to opt out.
3
- const BOOLEAN_KEYS = new Set([
4
- 'typesOnly',
5
- 'validators',
6
- 'examples',
7
- 'build',
8
- 'logWarnings',
9
- 'strict',
10
- 'stripUnknown',
11
- 'caseInsensitive',
12
- 'readonly',
13
- 'resolveRemote',
14
- 'allowPrivateHosts',
1
+ const SOURCE_FORMATS = ["json", "typebox", "zod", "valibot", "effect"];
2
+ const BOOLEAN_KEYS = /* @__PURE__ */ new Set([
3
+ "typesOnly",
4
+ "validators",
5
+ "examples",
6
+ "build",
7
+ "logWarnings",
8
+ "strict",
9
+ "stripUnknown",
10
+ "caseInsensitive",
11
+ "readonly",
12
+ "resolveRemote",
13
+ "allowPrivateHosts"
15
14
  ]);
16
- // Value flags consume the following argument (or `--flag=value`).
17
- const VALUE_KEYS = new Set([
18
- 'schema',
19
- 'schemaDir',
20
- 'outDir',
21
- 'outFile',
22
- 'input',
23
- 'export',
24
- 'helpers',
25
- 'typeSuffix',
26
- 'importExt',
27
- 'rootType',
15
+ const VALUE_KEYS = /* @__PURE__ */ new Set([
16
+ "schema",
17
+ "schemaDir",
18
+ "outDir",
19
+ "outFile",
20
+ "input",
21
+ "export",
22
+ "helpers",
23
+ "typeSuffix",
24
+ "importExt",
25
+ "rootType"
28
26
  ]);
29
- // Recognized flags that don't map into CliConfig because they're consumed
30
- // earlier in the pipeline (e.g. `--config` is read by `extractConfigPath` to
31
- // load the config file before flags are overlaid). They still take a value, so
32
- // list them here to keep the unknown-flag guard from rejecting them.
33
- const EXTERNAL_VALUE_KEYS = new Set(['config']);
34
- /** Normalizes a CLI flag name to its camelCase config key so both `--out-dir` and `--outDir` map to `outDir`. */
27
+ const EXTERNAL_VALUE_KEYS = /* @__PURE__ */ new Set(["config"]);
35
28
  const toCamelCase = (key) => key.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
36
29
  const parseHelpersValue = (value) => {
37
- if (value === 'package' || value === 'embedded')
38
- return value;
39
- return undefined;
30
+ if (value === "package" || value === "embedded")
31
+ return value;
32
+ return void 0;
40
33
  };
41
34
  const parseImportExtValue = (value) => {
42
- if (value === 'js' || value === 'ts')
43
- return value;
44
- return undefined;
35
+ if (value === "js" || value === "ts")
36
+ return value;
37
+ return void 0;
45
38
  };
46
- const parseInputValue = (value) => SOURCE_FORMATS.includes(value) ? value : undefined;
47
- /**
48
- * Appends the hosts in `value` (comma-separated) onto `allowedHosts`. Repeating
49
- * `--allowed-hosts` accumulates, so `--allowed-hosts a.com --allowed-hosts b.com`
50
- * and `--allowed-hosts a.com,b.com` are equivalent. Blank entries are dropped.
51
- */
39
+ const parseInputValue = (value) => SOURCE_FORMATS.includes(value) ? value : void 0;
52
40
  const appendAllowedHosts = (config, value) => {
53
- const hosts = value
54
- .split(',')
55
- .map((host) => host.trim())
56
- .filter((host) => host.length > 0);
57
- if (hosts.length === 0)
58
- return;
59
- config.allowedHosts = [...(config.allowedHosts ?? []), ...hosts];
41
+ const hosts = value.split(",").map((host) => host.trim()).filter((host) => host.length > 0);
42
+ if (hosts.length === 0)
43
+ return;
44
+ config.allowedHosts = [...config.allowedHosts ?? [], ...hosts];
60
45
  };
61
- /** Assigns a value-flag onto the config. Returns false for unknown keys. */
62
46
  const assignValue = (config, key, value) => {
63
- switch (key) {
64
- case 'schema':
65
- config.schema = value;
66
- return true;
67
- case 'schemaDir':
68
- config.schemaDir = value;
69
- return true;
70
- case 'outDir':
71
- config.outDir = value;
72
- return true;
73
- case 'outFile':
74
- config.outFile = value;
75
- return true;
76
- case 'export':
77
- config.export = value;
78
- return true;
79
- case 'typeSuffix':
80
- config.typeSuffix = value;
81
- return true;
82
- case 'rootType':
83
- config.rootType = value;
84
- return true;
85
- case 'input': {
86
- const parsed = parseInputValue(value);
87
- if (!parsed) {
88
- throw new Error(`Invalid --input value "${value}". Expected one of: ${SOURCE_FORMATS.join(', ')}.`);
89
- }
90
- config.input = parsed;
91
- return true;
92
- }
93
- case 'helpers': {
94
- const parsed = parseHelpersValue(value);
95
- if (!parsed) {
96
- throw new Error(`Invalid --helpers value "${value}". Expected one of: package, embedded.`);
97
- }
98
- config.helpers = parsed;
99
- return true;
100
- }
101
- case 'importExt': {
102
- const parsed = parseImportExtValue(value);
103
- if (!parsed) {
104
- throw new Error(`Invalid --import-ext value "${value}". Expected one of: js, ts.`);
105
- }
106
- config.importExt = parsed;
107
- return true;
108
- }
109
- default:
110
- return false;
47
+ switch (key) {
48
+ case "schema":
49
+ config.schema = value;
50
+ return true;
51
+ case "schemaDir":
52
+ config.schemaDir = value;
53
+ return true;
54
+ case "outDir":
55
+ config.outDir = value;
56
+ return true;
57
+ case "outFile":
58
+ config.outFile = value;
59
+ return true;
60
+ case "export":
61
+ config.export = value;
62
+ return true;
63
+ case "typeSuffix":
64
+ config.typeSuffix = value;
65
+ return true;
66
+ case "rootType":
67
+ config.rootType = value;
68
+ return true;
69
+ case "input": {
70
+ const parsed = parseInputValue(value);
71
+ if (!parsed) {
72
+ throw new Error(`Invalid --input value "${value}". Expected one of: ${SOURCE_FORMATS.join(", ")}.`);
73
+ }
74
+ config.input = parsed;
75
+ return true;
111
76
  }
77
+ case "helpers": {
78
+ const parsed = parseHelpersValue(value);
79
+ if (!parsed) {
80
+ throw new Error(`Invalid --helpers value "${value}". Expected one of: package, embedded.`);
81
+ }
82
+ config.helpers = parsed;
83
+ return true;
84
+ }
85
+ case "importExt": {
86
+ const parsed = parseImportExtValue(value);
87
+ if (!parsed) {
88
+ throw new Error(`Invalid --import-ext value "${value}". Expected one of: js, ts.`);
89
+ }
90
+ config.importExt = parsed;
91
+ return true;
92
+ }
93
+ default:
94
+ return false;
95
+ }
112
96
  };
113
- /** Assigns a boolean flag onto the config. Returns false for unknown keys. */
114
97
  const assignBoolean = (config, key, value) => {
115
- switch (key) {
116
- case 'typesOnly':
117
- config.typesOnly = value;
118
- return true;
119
- case 'validators':
120
- config.validators = value;
121
- return true;
122
- case 'examples':
123
- config.examples = value;
124
- return true;
125
- case 'build':
126
- config.build = value;
127
- return true;
128
- case 'logWarnings':
129
- config.logWarnings = value;
130
- return true;
131
- case 'strict':
132
- config.strict = value;
133
- return true;
134
- case 'stripUnknown':
135
- config.stripUnknown = value;
136
- return true;
137
- case 'caseInsensitive':
138
- config.caseInsensitive = value;
139
- return true;
140
- case 'readonly':
141
- config.readonly = value;
142
- return true;
143
- case 'resolveRemote':
144
- config.resolveRemote = value;
145
- return true;
146
- case 'allowPrivateHosts':
147
- config.allowPrivateHosts = value;
148
- return true;
149
- default:
150
- return false;
151
- }
98
+ switch (key) {
99
+ case "typesOnly":
100
+ config.typesOnly = value;
101
+ return true;
102
+ case "validators":
103
+ config.validators = value;
104
+ return true;
105
+ case "examples":
106
+ config.examples = value;
107
+ return true;
108
+ case "build":
109
+ config.build = value;
110
+ return true;
111
+ case "logWarnings":
112
+ config.logWarnings = value;
113
+ return true;
114
+ case "strict":
115
+ config.strict = value;
116
+ return true;
117
+ case "stripUnknown":
118
+ config.stripUnknown = value;
119
+ return true;
120
+ case "caseInsensitive":
121
+ config.caseInsensitive = value;
122
+ return true;
123
+ case "readonly":
124
+ config.readonly = value;
125
+ return true;
126
+ case "resolveRemote":
127
+ config.resolveRemote = value;
128
+ return true;
129
+ case "allowPrivateHosts":
130
+ config.allowPrivateHosts = value;
131
+ return true;
132
+ default:
133
+ return false;
134
+ }
152
135
  };
153
- /**
154
- * Parses command-line arguments into a partial CLI config.
155
- * Every flag accepts both kebab-case and camelCase (e.g. `--out-dir` and `--outDir`)
156
- * and supports either `--flag value` or `--flag=value` syntax. Boolean flags toggle
157
- * on by presence and accept `--flag=false` to opt out. Only flags that were explicitly
158
- * provided are returned so they can be layered on top of config file values.
159
- */
160
- export const parseCliArgs = (args) => {
161
- const config = {};
162
- for (let i = 0; i < args.length; i++) {
163
- const arg = args[i];
164
- if (!arg || !arg.startsWith('--')) {
165
- continue;
166
- }
167
- // Handle --flag=value syntax
168
- const equalsIndex = arg.indexOf('=');
169
- if (equalsIndex !== -1) {
170
- const key = toCamelCase(arg.slice(2, equalsIndex));
171
- const value = arg.slice(equalsIndex + 1);
172
- if (key === 'banner') {
173
- // --banner=false → false, --banner=true → true, --banner=<text> → custom string
174
- config.banner = value === 'false' ? false : value === 'true' ? true : value;
175
- }
176
- else if (key === 'allowedHosts') {
177
- appendAllowedHosts(config, value);
178
- }
179
- else if (BOOLEAN_KEYS.has(key)) {
180
- assignBoolean(config, key, value !== 'false');
181
- }
182
- else if (VALUE_KEYS.has(key)) {
183
- assignValue(config, key, value);
184
- }
185
- else if (!EXTERNAL_VALUE_KEYS.has(key)) {
186
- // An unrecognized flag is almost always a typo (e.g. `--strcit`). Silently
187
- // dropping it means the user gets non-strict output while believing they
188
- // asked for strict — fail loudly instead of guessing intent.
189
- throw new Error(`Unknown flag "--${arg.slice(2, equalsIndex)}".`);
190
- }
191
- continue;
192
- }
193
- const key = toCamelCase(arg.slice(2));
194
- const flagName = arg.slice(2);
195
- // --banner: presence alone enables the default message; an immediately
196
- // following non-flag argument is treated as a custom message string.
197
- if (key === 'banner') {
198
- const nextArg = args[i + 1];
199
- if (nextArg && !nextArg.startsWith('--')) {
200
- config.banner = nextArg;
201
- i++;
202
- }
203
- else {
204
- config.banner = true;
205
- }
206
- continue;
207
- }
208
- // --allowed-hosts takes a value (comma-separated) and accumulates when repeated.
209
- if (key === 'allowedHosts') {
210
- const value = args[i + 1];
211
- if (value === undefined || value.startsWith('--')) {
212
- throw new Error(`Flag "--${flagName}" expects a value.`);
213
- }
214
- appendAllowedHosts(config, value);
215
- i++;
216
- continue;
217
- }
218
- // Boolean flag: presence alone enables it, no value needed
219
- if (BOOLEAN_KEYS.has(key)) {
220
- assignBoolean(config, key, true);
221
- continue;
222
- }
223
- // Value flag: consume the next argument. It must be present and must not be
224
- // another flag — `--schema --out-dir dist` means `--schema` lost its value,
225
- // which would otherwise silently fall back to a default.
226
- if (VALUE_KEYS.has(key)) {
227
- const value = args[i + 1];
228
- if (value === undefined || value.startsWith('--')) {
229
- throw new Error(`Flag "--${flagName}" expects a value.`);
230
- }
231
- assignValue(config, key, value);
232
- i++;
233
- continue;
234
- }
235
- // A flag consumed elsewhere (e.g. `--config`) still swallows its value here.
236
- if (EXTERNAL_VALUE_KEYS.has(key)) {
237
- const value = args[i + 1];
238
- if (value !== undefined && !value.startsWith('--'))
239
- i++;
240
- continue;
241
- }
242
- throw new Error(`Unknown flag "--${flagName}".`);
136
+ const parseCliArgs = (args) => {
137
+ const config = {};
138
+ for (let i = 0; i < args.length; i++) {
139
+ const arg = args[i];
140
+ if (!arg || !arg.startsWith("--")) {
141
+ continue;
142
+ }
143
+ const equalsIndex = arg.indexOf("=");
144
+ if (equalsIndex !== -1) {
145
+ const key2 = toCamelCase(arg.slice(2, equalsIndex));
146
+ const value = arg.slice(equalsIndex + 1);
147
+ if (key2 === "banner") {
148
+ config.banner = value === "false" ? false : value === "true" ? true : value;
149
+ } else if (key2 === "allowedHosts") {
150
+ appendAllowedHosts(config, value);
151
+ } else if (BOOLEAN_KEYS.has(key2)) {
152
+ assignBoolean(config, key2, value !== "false");
153
+ } else if (VALUE_KEYS.has(key2)) {
154
+ assignValue(config, key2, value);
155
+ } else if (!EXTERNAL_VALUE_KEYS.has(key2)) {
156
+ throw new Error(`Unknown flag "--${arg.slice(2, equalsIndex)}".`);
157
+ }
158
+ continue;
159
+ }
160
+ const key = toCamelCase(arg.slice(2));
161
+ const flagName = arg.slice(2);
162
+ if (key === "banner") {
163
+ const nextArg = args[i + 1];
164
+ if (nextArg && !nextArg.startsWith("--")) {
165
+ config.banner = nextArg;
166
+ i++;
167
+ } else {
168
+ config.banner = true;
169
+ }
170
+ continue;
243
171
  }
244
- return config;
172
+ if (key === "allowedHosts") {
173
+ const value = args[i + 1];
174
+ if (value === void 0 || value.startsWith("--")) {
175
+ throw new Error(`Flag "--${flagName}" expects a value.`);
176
+ }
177
+ appendAllowedHosts(config, value);
178
+ i++;
179
+ continue;
180
+ }
181
+ if (BOOLEAN_KEYS.has(key)) {
182
+ assignBoolean(config, key, true);
183
+ continue;
184
+ }
185
+ if (VALUE_KEYS.has(key)) {
186
+ const value = args[i + 1];
187
+ if (value === void 0 || value.startsWith("--")) {
188
+ throw new Error(`Flag "--${flagName}" expects a value.`);
189
+ }
190
+ assignValue(config, key, value);
191
+ i++;
192
+ continue;
193
+ }
194
+ if (EXTERNAL_VALUE_KEYS.has(key)) {
195
+ const value = args[i + 1];
196
+ if (value !== void 0 && !value.startsWith("--"))
197
+ i++;
198
+ continue;
199
+ }
200
+ throw new Error(`Unknown flag "--${flagName}".`);
201
+ }
202
+ return config;
203
+ };
204
+ export {
205
+ parseCliArgs
245
206
  };
@@ -1,13 +1,9 @@
1
- import { readFile } from 'node:fs/promises';
2
- /**
3
- * Reads the CLI's own version from its `package.json`.
4
- *
5
- * Resolved relative to this module so it works whether the code runs from
6
- * `src/` (dev) or the bundled `dist/cli.js` (published) — `package.json`
7
- * sits one directory above both.
8
- */
9
- export const readVersion = async () => {
10
- const pkgUrl = new URL('../package.json', import.meta.url);
11
- const pkg = JSON.parse(await readFile(pkgUrl, 'utf-8'));
12
- return pkg.version ?? '0.0.0';
1
+ import { readFile } from "node:fs/promises";
2
+ const readVersion = async () => {
3
+ const pkgUrl = new URL("../package.json", import.meta.url);
4
+ const pkg = JSON.parse(await readFile(pkgUrl, "utf-8"));
5
+ return pkg.version ?? "0.0.0";
6
+ };
7
+ export {
8
+ readVersion
13
9
  };
@@ -1,9 +1,4 @@
1
- /**
2
- * Resolves the effective extension for relative import specifiers in the
3
- * generated output. An explicit `importExt` (flag or config file) always wins.
4
- * Otherwise the default is `'ts'` — the literal on-disk paths, loadable by
5
- * Bun, Node's type stripping, and tsc with `allowImportingTsExtensions` —
6
- * except under `build`, where tsc must compile the sources and can only emit
7
- * from `'js'` specifiers.
8
- */
9
- export const resolveImportExt = (config) => config.importExt ?? (config.build ? 'js' : 'ts');
1
+ const resolveImportExt = (config) => config.importExt ?? (config.build ? "js" : "ts");
2
+ export {
3
+ resolveImportExt
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/mjst",
3
- "version": "0.12.2",
3
+ "version": "0.12.3",
4
4
  "description": "Generate TypeScript parsers and type definitions from JSON Schemas.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -35,21 +35,21 @@
35
35
  "scripts": {
36
36
  "dev": "bun run --conditions=development ./src/cli.ts",
37
37
  "start": "bun run ./src/cli.ts",
38
- "build": "tsgo -p tsconfig.build.json && tsc-alias -p tsconfig.build.json -f",
38
+ "build": "tsgo -p tsconfig.build.json && tsc-alias -p tsconfig.build.json -f && node ../../scripts/strip-comments.mjs",
39
39
  "types:check": "tsgo -p . --noEmit",
40
40
  "test": "NODE_ENV=production vitest run --root ../.. cli",
41
41
  "generate-readme": "bun run ./scripts/generate-readme.ts"
42
42
  },
43
43
  "dependencies": {
44
- "@amritk/adapters": "0.3.2",
45
- "@amritk/generate-examples": "0.5.2",
46
- "@amritk/generate-parsers": "0.16.3",
47
- "@amritk/generate-validators": "0.11.8",
48
- "@amritk/generate-markdown": "0.4.2",
49
- "@amritk/helpers": "0.13.2",
50
- "@amritk/lint": "0.3.2",
51
- "@amritk/resolve-refs": "0.4.2",
52
- "@amritk/yaml": "0.3.2",
44
+ "@amritk/adapters": "0.3.3",
45
+ "@amritk/generate-examples": "0.5.3",
46
+ "@amritk/generate-parsers": "0.17.0",
47
+ "@amritk/generate-validators": "0.11.9",
48
+ "@amritk/generate-markdown": "0.4.3",
49
+ "@amritk/helpers": "0.13.3",
50
+ "@amritk/lint": "0.3.3",
51
+ "@amritk/resolve-refs": "0.4.3",
52
+ "@amritk/yaml": "0.3.3",
53
53
  "fast-glob": "^3.3.2",
54
54
  "yargs": "^17.7.2"
55
55
  },