@ldlework/workmark 1.4.1 → 1.4.2
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/dist/lib/parse.js +12 -1
- package/package.json +1 -1
- package/src/lib/parse.ts +18 -1
package/dist/lib/parse.js
CHANGED
|
@@ -138,8 +138,19 @@ function coerce(raw, schema) {
|
|
|
138
138
|
}
|
|
139
139
|
return out;
|
|
140
140
|
}
|
|
141
|
+
// --- 4. apply defaults ---------------------------------------------------
|
|
142
|
+
function applyDefaults(out, schema) {
|
|
143
|
+
const props = schema.properties ?? {};
|
|
144
|
+
const result = { ...out };
|
|
145
|
+
for (const [key, prop] of Object.entries(props)) {
|
|
146
|
+
if (result[key] === undefined && prop && "default" in prop) {
|
|
147
|
+
result[key] = prop.default;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
141
152
|
// --- orchestrator --------------------------------------------------------
|
|
142
153
|
export function parseArgs(argv, positional, schema) {
|
|
143
154
|
const s = schema;
|
|
144
|
-
return coerce(dispatch(tokenize(argv), positional, s), s);
|
|
155
|
+
return applyDefaults(coerce(dispatch(tokenize(argv), positional, s), s), s);
|
|
145
156
|
}
|
package/package.json
CHANGED
package/src/lib/parse.ts
CHANGED
|
@@ -18,6 +18,7 @@ type SchemaProp = {
|
|
|
18
18
|
items?: { type?: string; enum?: unknown[] };
|
|
19
19
|
enum?: unknown[];
|
|
20
20
|
anyOf?: unknown[];
|
|
21
|
+
default?: unknown;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
type SchemaObject = {
|
|
@@ -180,6 +181,22 @@ function coerce(
|
|
|
180
181
|
return out;
|
|
181
182
|
}
|
|
182
183
|
|
|
184
|
+
// --- 4. apply defaults ---------------------------------------------------
|
|
185
|
+
|
|
186
|
+
function applyDefaults(
|
|
187
|
+
out: Record<string, unknown>,
|
|
188
|
+
schema: SchemaObject,
|
|
189
|
+
): Record<string, unknown> {
|
|
190
|
+
const props = schema.properties ?? {};
|
|
191
|
+
const result = { ...out };
|
|
192
|
+
for (const [key, prop] of Object.entries(props)) {
|
|
193
|
+
if (result[key] === undefined && prop && "default" in prop) {
|
|
194
|
+
result[key] = prop.default;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
183
200
|
// --- orchestrator --------------------------------------------------------
|
|
184
201
|
|
|
185
202
|
export function parseArgs(
|
|
@@ -188,5 +205,5 @@ export function parseArgs(
|
|
|
188
205
|
schema: Record<string, unknown>,
|
|
189
206
|
): Record<string, unknown> {
|
|
190
207
|
const s = schema as SchemaObject;
|
|
191
|
-
return coerce(dispatch(tokenize(argv), positional, s), s);
|
|
208
|
+
return applyDefaults(coerce(dispatch(tokenize(argv), positional, s), s), s);
|
|
192
209
|
}
|