politty 0.11.0 → 0.11.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/LICENSE +21 -0
- package/README.md +9 -2
- package/dist/{arg-registry-aHbip9Hs.d.ts → arg-registry-D558cYkD.d.ts} +10 -2
- package/dist/augment.d.ts +1 -1
- package/dist/cli.js +3 -3
- package/dist/completion/index.d.ts +2 -2
- package/dist/completion/index.js +1 -1
- package/dist/{completion-CqKX9sCR.js → completion-B1K87cU5.js} +4 -4
- package/dist/docs/index.d.ts +1 -1
- package/dist/docs/index.js +3 -2
- package/dist/{index-dRZSB_t3.d.ts → index-CeZ1yI46.d.ts} +2 -2
- package/dist/index.d.ts +69 -7
- package/dist/index.js +5 -5
- package/dist/prompt/clack/index.d.ts +1 -1
- package/dist/prompt/index.d.ts +1 -1
- package/dist/prompt/inquirer/index.d.ts +1 -1
- package/dist/{runner-Z7R_XvAV.js → runner-CEDtiI7D.js} +235 -27
- package/dist/{schema-extractor-DnOTAjV_.js → schema-extractor-wzJ1cGon.js} +15 -127
- package/dist/skill/index.d.ts +121 -3
- package/dist/skill/index.js +284 -116
- package/dist/subcommand-router-CTzHPGzF.js +135 -0
- package/package.json +7 -7
- package/dist/arg-registry-BeLLAW5-.js +0 -25
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { a as toCamelCase,
|
|
1
|
+
import { a as toCamelCase, n as getAllAliases, o as toKebabCase, r as getExtractedFields, t as extractFields } from "./schema-extractor-wzJ1cGon.js";
|
|
2
|
+
import { a as resolveSubcommandWithAlias, c as resolveSubCommandMeta, n as listSubCommands, o as isLazyCommand, r as resolveLazyCommand, t as listSubCommandNamesWithAliases } from "./subcommand-router-CTzHPGzF.js";
|
|
2
3
|
import { n as emptyLogs, r as mergeLogs, t as createLogCollector } from "./log-collector-DK32-73m.js";
|
|
3
4
|
import { a as symbols, i as styles } from "./logger-DbDkjdfO.js";
|
|
4
5
|
import { stripVTControlCharacters } from "node:util";
|
|
@@ -995,6 +996,30 @@ var DuplicateNegationError = class extends Error {
|
|
|
995
996
|
this.name = "DuplicateNegationError";
|
|
996
997
|
}
|
|
997
998
|
};
|
|
999
|
+
/**
|
|
1000
|
+
* Error thrown when a field name collides with a reserved, framework-injected
|
|
1001
|
+
* key on the final args object (e.g. `$source`).
|
|
1002
|
+
*/
|
|
1003
|
+
var ReservedFieldNameError = class extends Error {
|
|
1004
|
+
constructor(message) {
|
|
1005
|
+
super(message);
|
|
1006
|
+
this.name = "ReservedFieldNameError";
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
/**
|
|
1010
|
+
* Error thrown when a global field and a same-named local field have
|
|
1011
|
+
* different definitions (per `extractFields()`'s type bucket, whether the
|
|
1012
|
+
* field is positional, and enum values). Only exactly-matching definitions
|
|
1013
|
+
* are allowed to share a name across global/local schemas — anything else
|
|
1014
|
+
* is rejected at validation time rather than risking a value from one
|
|
1015
|
+
* schema silently flowing into the other.
|
|
1016
|
+
*/
|
|
1017
|
+
var FieldTypeConflictError = class extends Error {
|
|
1018
|
+
constructor(message) {
|
|
1019
|
+
super(message);
|
|
1020
|
+
this.name = "FieldTypeConflictError";
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
998
1023
|
|
|
999
1024
|
//#endregion
|
|
1000
1025
|
//#region src/validator/command-validator.ts
|
|
@@ -1196,6 +1221,29 @@ function checkReservedAliases(extracted, commandPath) {
|
|
|
1196
1221
|
return errors;
|
|
1197
1222
|
}
|
|
1198
1223
|
/**
|
|
1224
|
+
* Check for field names starting with `$`.
|
|
1225
|
+
*
|
|
1226
|
+
* The `$` prefix is reserved for framework-injected helpers on the final
|
|
1227
|
+
* args object (e.g. `$source`). It is also impractical as a real CLI flag
|
|
1228
|
+
* since an unquoted `$name` is expanded by the shell before it ever reaches
|
|
1229
|
+
* the program, so this is rejected outright rather than merely discouraged.
|
|
1230
|
+
*
|
|
1231
|
+
* Aliases can't start with `$` (schema extraction already restricts alias
|
|
1232
|
+
* characters to `[A-Za-z0-9-]`), and `cliName` is derived from `name` via
|
|
1233
|
+
* `toKebabCase`, which never strips or moves a leading `$` — so checking
|
|
1234
|
+
* `field.name` alone covers every way `$` could reach the final args object.
|
|
1235
|
+
*/
|
|
1236
|
+
function checkReservedFieldNames(extracted, commandPath) {
|
|
1237
|
+
const errors = [];
|
|
1238
|
+
for (const field of extracted.fields) if (field.name.startsWith("$")) errors.push({
|
|
1239
|
+
commandPath,
|
|
1240
|
+
type: "reserved_field_name",
|
|
1241
|
+
message: `Field "${field.name}" starts with "$", which is reserved for framework-injected helpers (e.g. $source).`,
|
|
1242
|
+
field: field.name
|
|
1243
|
+
});
|
|
1244
|
+
return errors;
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1199
1247
|
* Validate that no duplicate field names exist
|
|
1200
1248
|
*
|
|
1201
1249
|
* @param extracted - Extracted fields from schema
|
|
@@ -1262,6 +1310,28 @@ function validateReservedAliases(extracted, _hasSubCommands) {
|
|
|
1262
1310
|
}
|
|
1263
1311
|
}
|
|
1264
1312
|
/**
|
|
1313
|
+
* Validate that no field name starts with `$`
|
|
1314
|
+
*
|
|
1315
|
+
* The `$` prefix is reserved for framework-injected helpers on the final
|
|
1316
|
+
* args object (e.g. `$source`), and is unusable as a real CLI flag anyway
|
|
1317
|
+
* since an unquoted `$name` gets shell-expanded before the program sees it.
|
|
1318
|
+
*
|
|
1319
|
+
* Checking `field.name` alone is sufficient: aliases can't start with `$`
|
|
1320
|
+
* (schema extraction already restricts alias characters to `[A-Za-z0-9-]`),
|
|
1321
|
+
* and `cliName` is derived from `name` via `toKebabCase`, which never strips
|
|
1322
|
+
* or moves a leading `$`. See {@link checkReservedFieldNames}.
|
|
1323
|
+
*
|
|
1324
|
+
* @param extracted - Extracted fields from schema
|
|
1325
|
+
* @throws {ReservedFieldNameError} If a field name starts with "$"
|
|
1326
|
+
*/
|
|
1327
|
+
function validateReservedFieldNames(extracted) {
|
|
1328
|
+
const errors = checkReservedFieldNames(extracted, []);
|
|
1329
|
+
if (errors.length > 0) {
|
|
1330
|
+
const err = errors[0];
|
|
1331
|
+
throw new ReservedFieldNameError(err.message);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1265
1335
|
* Validate that custom boolean negation names do not collide with anything
|
|
1266
1336
|
*
|
|
1267
1337
|
* @param extracted - Extracted fields from schema
|
|
@@ -1288,21 +1358,76 @@ function validateCaseVariantCollisions(extracted) {
|
|
|
1288
1358
|
}
|
|
1289
1359
|
}
|
|
1290
1360
|
/**
|
|
1291
|
-
*
|
|
1292
|
-
*
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
*
|
|
1296
|
-
*
|
|
1361
|
+
* Check whether two same-named fields from different schemas (e.g. global
|
|
1362
|
+
* args and command args) have identical definitions. Only the facts
|
|
1363
|
+
* `extractFields()` already exposes are compared: the coarse type bucket,
|
|
1364
|
+
* whether the field is positional, and, for enum-like fields, the exact
|
|
1365
|
+
* set of allowed values. Anything `extractFields()` can't see (e.g. a
|
|
1366
|
+
* `.refine()`) is intentionally not compared — this is a coarse, cheap
|
|
1367
|
+
* equality check, not a full schema comparison.
|
|
1297
1368
|
*/
|
|
1298
|
-
function
|
|
1369
|
+
function fieldsAreIdentical(a, b) {
|
|
1370
|
+
if (a.type !== b.type) return false;
|
|
1371
|
+
if (a.positional !== b.positional) return false;
|
|
1372
|
+
const aEnum = a.enumValues;
|
|
1373
|
+
const bEnum = b.enumValues;
|
|
1374
|
+
if (!aEnum && !bEnum) return true;
|
|
1375
|
+
if (!aEnum || !bEnum) return false;
|
|
1376
|
+
const aSet = new Set(aEnum);
|
|
1377
|
+
const bSet = new Set(bEnum);
|
|
1378
|
+
if (aSet.size !== bSet.size) return false;
|
|
1379
|
+
for (const value of aSet) if (!bSet.has(value)) return false;
|
|
1380
|
+
return true;
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Check for cross-schema collisions between two schemas (e.g., global args
|
|
1384
|
+
* and command args): neither a case-variant collision (same canonical name,
|
|
1385
|
+
* different spelling) nor a same-named field with a different definition
|
|
1386
|
+
* (same spelling, but the two schemas don't agree on what values are valid).
|
|
1387
|
+
*/
|
|
1388
|
+
function checkCrossSchemaCollisions(extractedA, extractedB, commandPath) {
|
|
1389
|
+
const errors = [];
|
|
1299
1390
|
const canonicalMap = /* @__PURE__ */ new Map();
|
|
1300
|
-
for (const field of extractedA.fields) canonicalMap.set(toCamelCase(field.name), field
|
|
1391
|
+
for (const field of extractedA.fields) canonicalMap.set(toCamelCase(field.name), field);
|
|
1301
1392
|
for (const field of extractedB.fields) {
|
|
1302
1393
|
const camel = toCamelCase(field.name);
|
|
1303
1394
|
const existing = canonicalMap.get(camel);
|
|
1304
|
-
if (existing
|
|
1395
|
+
if (!existing) continue;
|
|
1396
|
+
if (existing.name !== field.name) {
|
|
1397
|
+
errors.push({
|
|
1398
|
+
commandPath,
|
|
1399
|
+
type: "case_variant_collision",
|
|
1400
|
+
message: `Global field "${existing.name}" and command field "${field.name}" are case variants of each other and would collide.`,
|
|
1401
|
+
field: field.name
|
|
1402
|
+
});
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
if (!fieldsAreIdentical(existing, field)) errors.push({
|
|
1406
|
+
commandPath,
|
|
1407
|
+
type: "field_type_conflict",
|
|
1408
|
+
message: `Global field "${existing.name}" and command field "${field.name}" share the same name but have different definitions.`,
|
|
1409
|
+
field: field.name
|
|
1410
|
+
});
|
|
1305
1411
|
}
|
|
1412
|
+
return errors;
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Validate that no cross-schema collisions exist between two schemas
|
|
1416
|
+
* (e.g., global args and command args): neither a case-variant collision
|
|
1417
|
+
* (same canonical name, different spelling) nor a same-named field with a
|
|
1418
|
+
* different definition (same spelling, but the two schemas don't agree on
|
|
1419
|
+
* what values are valid).
|
|
1420
|
+
*
|
|
1421
|
+
* @param extractedA - Extracted fields from first schema (e.g., global args)
|
|
1422
|
+
* @param extractedB - Extracted fields from second schema (e.g., command args)
|
|
1423
|
+
* @throws {CaseVariantCollisionError} If cross-schema case-variant collisions are found
|
|
1424
|
+
* @throws {FieldTypeConflictError} If a same-named field has a different definition on each schema
|
|
1425
|
+
*/
|
|
1426
|
+
function validateCrossSchemaCollisions(extractedA, extractedB) {
|
|
1427
|
+
const err = checkCrossSchemaCollisions(extractedA, extractedB, [])[0];
|
|
1428
|
+
if (!err) return;
|
|
1429
|
+
if (err.type === "case_variant_collision") throw new CaseVariantCollisionError(err.message);
|
|
1430
|
+
throw new FieldTypeConflictError(err.message);
|
|
1306
1431
|
}
|
|
1307
1432
|
/**
|
|
1308
1433
|
* Collect validation errors for a single command's schema (non-throwing)
|
|
@@ -1314,7 +1439,8 @@ function collectSchemaErrors(extracted, _hasSubCommands, commandPath) {
|
|
|
1314
1439
|
...checkDuplicateAliases(extracted, commandPath),
|
|
1315
1440
|
...checkDuplicateNegations(extracted, commandPath),
|
|
1316
1441
|
...checkPositionalConfig(extracted, commandPath),
|
|
1317
|
-
...checkReservedAliases(extracted, commandPath)
|
|
1442
|
+
...checkReservedAliases(extracted, commandPath),
|
|
1443
|
+
...checkReservedFieldNames(extracted, commandPath)
|
|
1318
1444
|
];
|
|
1319
1445
|
}
|
|
1320
1446
|
/**
|
|
@@ -1390,13 +1516,18 @@ async function validateCommand(command, options = {}) {
|
|
|
1390
1516
|
const commandPath = options.commandPath ?? [command.name];
|
|
1391
1517
|
const errors = [];
|
|
1392
1518
|
const hasSubCommands = command.subCommands ? Object.keys(command.subCommands).length > 0 : false;
|
|
1519
|
+
const globalExtracted = options.globalArgs ? extractFields(options.globalArgs) : void 0;
|
|
1393
1520
|
if (command.args) {
|
|
1394
1521
|
const extracted = extractFields(command.args);
|
|
1395
1522
|
errors.push(...collectSchemaErrors(extracted, hasSubCommands, commandPath));
|
|
1523
|
+
if (globalExtracted) errors.push(...checkCrossSchemaCollisions(globalExtracted, extracted, commandPath));
|
|
1396
1524
|
}
|
|
1397
1525
|
errors.push(...checkSubCommandAliasConflicts(command, commandPath));
|
|
1398
1526
|
if (command.subCommands) for (const [name, subCmd] of Object.entries(command.subCommands)) {
|
|
1399
|
-
const subResult = await validateCommand(await resolveLazyCommand(subCmd), {
|
|
1527
|
+
const subResult = await validateCommand(await resolveLazyCommand(subCmd), {
|
|
1528
|
+
commandPath: [...commandPath, name],
|
|
1529
|
+
...options.globalArgs ? { globalArgs: options.globalArgs } : {}
|
|
1530
|
+
});
|
|
1400
1531
|
if (!subResult.valid) errors.push(...subResult.errors);
|
|
1401
1532
|
}
|
|
1402
1533
|
if (errors.length === 0) return { valid: true };
|
|
@@ -1496,6 +1627,26 @@ function resolveLongOption(arg, lookup) {
|
|
|
1496
1627
|
//#endregion
|
|
1497
1628
|
//#region src/parser/argv-parser.ts
|
|
1498
1629
|
/**
|
|
1630
|
+
* Coerce a `--flag=value` string to a boolean for boolean-typed flags.
|
|
1631
|
+
* Values other than "true"/"false" are passed through unchanged so
|
|
1632
|
+
* downstream validation reports the invalid input instead of silently
|
|
1633
|
+
* guessing.
|
|
1634
|
+
*/
|
|
1635
|
+
function coerceBoolean(value) {
|
|
1636
|
+
if (value === "true") return true;
|
|
1637
|
+
if (value === "false") return false;
|
|
1638
|
+
return value;
|
|
1639
|
+
}
|
|
1640
|
+
/**
|
|
1641
|
+
* Matches tokens that look like a negative number (e.g. "-5", "-5.2", "-.5"),
|
|
1642
|
+
* as opposed to another flag. Used to decide whether a dash-prefixed token
|
|
1643
|
+
* should still be consumed as a value-taking option's value.
|
|
1644
|
+
*/
|
|
1645
|
+
const NEGATIVE_NUMBER_PATTERN = /^-\.?\d/;
|
|
1646
|
+
function looksLikeNegativeNumber(value) {
|
|
1647
|
+
return NEGATIVE_NUMBER_PATTERN.test(value);
|
|
1648
|
+
}
|
|
1649
|
+
/**
|
|
1499
1650
|
* Parse argv into a flat record
|
|
1500
1651
|
*
|
|
1501
1652
|
* Supports:
|
|
@@ -1568,7 +1719,10 @@ function parseArgv(argv, options = {}) {
|
|
|
1568
1719
|
}
|
|
1569
1720
|
const eqIndex = withoutDashes.indexOf("=");
|
|
1570
1721
|
if (eqIndex !== -1) {
|
|
1571
|
-
|
|
1722
|
+
const name = withoutDashes.slice(0, eqIndex);
|
|
1723
|
+
const value = withoutDashes.slice(eqIndex + 1);
|
|
1724
|
+
const resolvedName = aliasMap.get(name) ?? name;
|
|
1725
|
+
setOption(name, booleanFlags.has(resolvedName) ? coerceBoolean(value) : value);
|
|
1572
1726
|
i++;
|
|
1573
1727
|
} else {
|
|
1574
1728
|
const name = withoutDashes;
|
|
@@ -1578,7 +1732,7 @@ function parseArgv(argv, options = {}) {
|
|
|
1578
1732
|
i++;
|
|
1579
1733
|
} else {
|
|
1580
1734
|
const nextArg = argv[i + 1];
|
|
1581
|
-
if (nextArg !== void 0 && !nextArg.startsWith("-")) {
|
|
1735
|
+
if (nextArg !== void 0 && (!nextArg.startsWith("-") || looksLikeNegativeNumber(nextArg))) {
|
|
1582
1736
|
setOption(name, nextArg);
|
|
1583
1737
|
i += 2;
|
|
1584
1738
|
} else {
|
|
@@ -1593,7 +1747,10 @@ function parseArgv(argv, options = {}) {
|
|
|
1593
1747
|
const withoutDash = arg.slice(1);
|
|
1594
1748
|
const eqIndex = withoutDash.indexOf("=");
|
|
1595
1749
|
if (eqIndex !== -1) {
|
|
1596
|
-
|
|
1750
|
+
const name = withoutDash.slice(0, eqIndex);
|
|
1751
|
+
const value = withoutDash.slice(eqIndex + 1);
|
|
1752
|
+
const resolvedName = aliasMap.get(name) ?? name;
|
|
1753
|
+
setOption(name, booleanFlags.has(resolvedName) ? coerceBoolean(value) : value);
|
|
1597
1754
|
i++;
|
|
1598
1755
|
} else if (withoutDash.length === 1) {
|
|
1599
1756
|
const name = withoutDash;
|
|
@@ -1603,7 +1760,7 @@ function parseArgv(argv, options = {}) {
|
|
|
1603
1760
|
i++;
|
|
1604
1761
|
} else {
|
|
1605
1762
|
const nextArg = argv[i + 1];
|
|
1606
|
-
if (nextArg !== void 0 && !nextArg.startsWith("-")) {
|
|
1763
|
+
if (nextArg !== void 0 && (!nextArg.startsWith("-") || looksLikeNegativeNumber(nextArg))) {
|
|
1607
1764
|
setOption(name, nextArg);
|
|
1608
1765
|
i += 2;
|
|
1609
1766
|
} else {
|
|
@@ -1951,6 +2108,7 @@ function parseArgs(argv, command, options = {}) {
|
|
|
1951
2108
|
validateDuplicateNegations(extracted);
|
|
1952
2109
|
validatePositionalConfig(extracted);
|
|
1953
2110
|
validateReservedAliases(extracted, hasSubCommands);
|
|
2111
|
+
validateReservedFieldNames(extracted);
|
|
1954
2112
|
if (options.globalExtracted) validateCrossSchemaCollisions(options.globalExtracted, extracted);
|
|
1955
2113
|
}
|
|
1956
2114
|
}
|
|
@@ -2002,12 +2160,14 @@ function parseArgs(argv, command, options = {}) {
|
|
|
2002
2160
|
const parserOptions = buildParserOptions(extracted);
|
|
2003
2161
|
const parsed = parseArgv(commandArgv, parserOptions);
|
|
2004
2162
|
const rawArgs = mergeWithPositionals(parsed, extracted);
|
|
2163
|
+
const envFallbackFields = /* @__PURE__ */ new Set();
|
|
2005
2164
|
for (const field of extracted.fields) if (field.env && rawArgs[field.name] === void 0) {
|
|
2006
2165
|
const envNames = Array.isArray(field.env) ? field.env : [field.env];
|
|
2007
2166
|
for (const envName of envNames) {
|
|
2008
2167
|
const envValue = process.env[envName];
|
|
2009
2168
|
if (envValue !== void 0) {
|
|
2010
2169
|
rawArgs[field.name] = envValue;
|
|
2170
|
+
envFallbackFields.add(field.name);
|
|
2011
2171
|
break;
|
|
2012
2172
|
}
|
|
2013
2173
|
}
|
|
@@ -2035,7 +2195,8 @@ function parseArgs(argv, command, options = {}) {
|
|
|
2035
2195
|
unknownFlags,
|
|
2036
2196
|
unknownGlobalFlags: suppressedGlobalFlags,
|
|
2037
2197
|
extractedFields: extracted,
|
|
2038
|
-
rawGlobalArgs
|
|
2198
|
+
rawGlobalArgs,
|
|
2199
|
+
envFallbackFields
|
|
2039
2200
|
};
|
|
2040
2201
|
}
|
|
2041
2202
|
/**
|
|
@@ -2243,6 +2404,18 @@ const defaultLogger = {
|
|
|
2243
2404
|
warn: (message) => console.warn(message)
|
|
2244
2405
|
};
|
|
2245
2406
|
/**
|
|
2407
|
+
* Attach a non-enumerable `$source` helper to the final args object so it
|
|
2408
|
+
* stays invisible to `Object.keys`/`JSON.stringify`/spread (those only ever
|
|
2409
|
+
* see real field values) while still being reachable via property access
|
|
2410
|
+
* (including through `createDualCaseProxy`).
|
|
2411
|
+
*/
|
|
2412
|
+
function attachArgSource(target, sourceMap) {
|
|
2413
|
+
Object.defineProperty(target, "$source", {
|
|
2414
|
+
value: (name) => sourceMap.get(name) ?? sourceMap.get(toCamelCase(name)) ?? sourceMap.get(toKebabCase(name)) ?? "default",
|
|
2415
|
+
enumerable: false
|
|
2416
|
+
});
|
|
2417
|
+
}
|
|
2418
|
+
/**
|
|
2246
2419
|
* Run a command with the given arguments (programmatic/test usage)
|
|
2247
2420
|
*
|
|
2248
2421
|
* This function parses arguments, validates them, routes to subcommands,
|
|
@@ -2342,7 +2515,7 @@ async function runCommand(command, argv, options = {}) {
|
|
|
2342
2515
|
function isInternalSubcommandInvocation(command, argv, globalExtracted) {
|
|
2343
2516
|
const firstPositional = findFirstPositional(argv, globalExtracted);
|
|
2344
2517
|
if (!firstPositional || !firstPositional.startsWith("__")) return false;
|
|
2345
|
-
return
|
|
2518
|
+
return Object.hasOwn(command.subCommands ?? {}, firstPositional);
|
|
2346
2519
|
}
|
|
2347
2520
|
/**
|
|
2348
2521
|
* Run a CLI command as the main entry point
|
|
@@ -2656,6 +2829,8 @@ async function runCommandInternal(command, argv, options = {}) {
|
|
|
2656
2829
|
}
|
|
2657
2830
|
let validatedGlobalArgs = {};
|
|
2658
2831
|
const isCompletionInvocation = command.name === "__complete";
|
|
2832
|
+
const cliProvidedGlobalFields = new Set(Object.keys(accumulatedGlobalArgs));
|
|
2833
|
+
const envFallbackGlobalFields = /* @__PURE__ */ new Set();
|
|
2659
2834
|
if (options.globalArgs && options._globalExtracted && !isCompletionInvocation) {
|
|
2660
2835
|
for (const field of options._globalExtracted.fields) if (field.env && accumulatedGlobalArgs[field.name] === void 0) {
|
|
2661
2836
|
const envNames = Array.isArray(field.env) ? field.env : [field.env];
|
|
@@ -2663,13 +2838,14 @@ async function runCommandInternal(command, argv, options = {}) {
|
|
|
2663
2838
|
const envValue = process.env[envName];
|
|
2664
2839
|
if (envValue !== void 0) {
|
|
2665
2840
|
accumulatedGlobalArgs[field.name] = envValue;
|
|
2841
|
+
envFallbackGlobalFields.add(field.name);
|
|
2666
2842
|
break;
|
|
2667
2843
|
}
|
|
2668
2844
|
}
|
|
2669
2845
|
}
|
|
2670
2846
|
if (options.prompt) {
|
|
2671
2847
|
const resolved = await options.prompt(accumulatedGlobalArgs, options._globalExtracted);
|
|
2672
|
-
Object.
|
|
2848
|
+
for (const [key, value] of Object.entries(resolved)) if (value !== void 0) accumulatedGlobalArgs[key] = value;
|
|
2673
2849
|
}
|
|
2674
2850
|
const globalValidation = validateArgs(accumulatedGlobalArgs, options.globalArgs);
|
|
2675
2851
|
if (!globalValidation.success) {
|
|
@@ -2684,6 +2860,10 @@ async function runCommandInternal(command, argv, options = {}) {
|
|
|
2684
2860
|
validatedGlobalArgs = globalValidation.data;
|
|
2685
2861
|
}
|
|
2686
2862
|
if (!command.args) {
|
|
2863
|
+
const globalSourceMap = /* @__PURE__ */ new Map();
|
|
2864
|
+
for (const name of cliProvidedGlobalFields) globalSourceMap.set(name, "cli");
|
|
2865
|
+
for (const name of envFallbackGlobalFields) globalSourceMap.set(name, "env");
|
|
2866
|
+
attachArgSource(validatedGlobalArgs, globalSourceMap);
|
|
2687
2867
|
const proxiedGlobalArgs = createDualCaseProxy(validatedGlobalArgs);
|
|
2688
2868
|
if (options._globalExtracted && !isCompletionInvocation) await runEffects(proxiedGlobalArgs, options._globalExtracted, proxiedGlobalArgs);
|
|
2689
2869
|
collector?.stop();
|
|
@@ -2695,12 +2875,25 @@ async function runCommandInternal(command, argv, options = {}) {
|
|
|
2695
2875
|
});
|
|
2696
2876
|
}
|
|
2697
2877
|
let argsToValidate = parseResult.rawArgs;
|
|
2878
|
+
const promptResolvedFields = /* @__PURE__ */ new Set();
|
|
2698
2879
|
if (options.prompt && parseResult.extractedFields) {
|
|
2699
2880
|
const resolved = await options.prompt(argsToValidate, parseResult.extractedFields);
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2881
|
+
const nextArgsToValidate = { ...argsToValidate };
|
|
2882
|
+
for (const [key, value] of Object.entries(resolved)) if (value !== void 0) {
|
|
2883
|
+
nextArgsToValidate[key] = value;
|
|
2884
|
+
promptResolvedFields.add(key);
|
|
2885
|
+
}
|
|
2886
|
+
argsToValidate = nextArgsToValidate;
|
|
2887
|
+
}
|
|
2888
|
+
const globalPrefilledFields = /* @__PURE__ */ new Set();
|
|
2889
|
+
if (parseResult.extractedFields) {
|
|
2890
|
+
for (const field of parseResult.extractedFields.fields) if (!(Object.hasOwn(parseResult.rawArgs, field.name) || promptResolvedFields.has(field.name)) && cliProvidedGlobalFields.has(field.name) && Object.hasOwn(validatedGlobalArgs, field.name)) {
|
|
2891
|
+
argsToValidate = {
|
|
2892
|
+
...argsToValidate,
|
|
2893
|
+
[field.name]: validatedGlobalArgs[field.name]
|
|
2894
|
+
};
|
|
2895
|
+
globalPrefilledFields.add(field.name);
|
|
2896
|
+
}
|
|
2704
2897
|
}
|
|
2705
2898
|
const validationResult = validateArgs(argsToValidate, command.args);
|
|
2706
2899
|
if (!validationResult.success) {
|
|
@@ -2716,10 +2909,24 @@ async function runCommandInternal(command, argv, options = {}) {
|
|
|
2716
2909
|
const proxiedGlobalArgs = createDualCaseProxy(validatedGlobalArgs);
|
|
2717
2910
|
if (options._globalExtracted && !isCompletionInvocation) await runEffects(proxiedGlobalArgs, options._globalExtracted, proxiedGlobalArgs);
|
|
2718
2911
|
if (parseResult.extractedFields && !isCompletionInvocation) await runEffects(proxiedCommandArgs, parseResult.extractedFields, proxiedGlobalArgs);
|
|
2719
|
-
const
|
|
2912
|
+
const mergedPlainArgs = {
|
|
2720
2913
|
...proxiedGlobalArgs,
|
|
2721
2914
|
...proxiedCommandArgs
|
|
2722
|
-
}
|
|
2915
|
+
};
|
|
2916
|
+
const argSourceMap = /* @__PURE__ */ new Map();
|
|
2917
|
+
for (const name of cliProvidedGlobalFields) argSourceMap.set(name, "cli");
|
|
2918
|
+
for (const name of envFallbackGlobalFields) argSourceMap.set(name, "env");
|
|
2919
|
+
const localEnvFallbackFields = parseResult.envFallbackFields ?? /* @__PURE__ */ new Set();
|
|
2920
|
+
for (const field of parseResult.extractedFields?.fields ?? []) {
|
|
2921
|
+
if (globalPrefilledFields.has(field.name)) {
|
|
2922
|
+
argSourceMap.set(field.name, "cli");
|
|
2923
|
+
continue;
|
|
2924
|
+
}
|
|
2925
|
+
const localHasCliOrEnvValue = Object.hasOwn(parseResult.rawArgs, field.name) || localEnvFallbackFields.has(field.name);
|
|
2926
|
+
argSourceMap.set(field.name, localHasCliOrEnvValue ? localEnvFallbackFields.has(field.name) ? "env" : "cli" : "default");
|
|
2927
|
+
}
|
|
2928
|
+
attachArgSource(mergedPlainArgs, argSourceMap);
|
|
2929
|
+
const mergedArgs = createDualCaseProxy(mergedPlainArgs);
|
|
2723
2930
|
collector?.stop();
|
|
2724
2931
|
return await executeLifecycle(command, mergedArgs, {
|
|
2725
2932
|
handleSignals: options.handleSignals,
|
|
@@ -2752,6 +2959,7 @@ function extractAndValidateGlobal(options) {
|
|
|
2752
2959
|
validateDuplicateAliases(extracted);
|
|
2753
2960
|
validateDuplicateNegations(extracted);
|
|
2754
2961
|
validateReservedAliases(extracted, true);
|
|
2962
|
+
validateReservedFieldNames(extracted);
|
|
2755
2963
|
const positionalNames = extracted.fields.filter((f) => f.positional).map((f) => f.name);
|
|
2756
2964
|
if (positionalNames.length > 0) throw new Error(`Global options schema must not contain positional arguments. Found: ${positionalNames.join(", ")}`);
|
|
2757
2965
|
}
|
|
@@ -2759,5 +2967,5 @@ function extractAndValidateGlobal(options) {
|
|
|
2759
2967
|
}
|
|
2760
2968
|
|
|
2761
2969
|
//#endregion
|
|
2762
|
-
export {
|
|
2763
|
-
//# sourceMappingURL=runner-
|
|
2970
|
+
export { ReservedFieldNameError as C, createDualCaseProxy as D, renderMarkdown as E, ReservedAliasError as S, renderInline as T, DuplicateAliasError as _, parseArgv as a, FieldTypeConflictError as b, validateCommand as c, validateDuplicateFields as d, validateDuplicateNegations as f, CaseVariantCollisionError as g, validateReservedFieldNames as h, formatValidationErrors as i, validateCrossSchemaCollisions as l, validateReservedAliases as m, runMain as n, formatCommandValidationErrors as o, validatePositionalConfig as p, runner_exports as r, validateCaseVariantCollisions as s, runCommand as t, validateDuplicateAliases as u, DuplicateFieldError as v, generateHelp as w, PositionalConfigError as x, DuplicateNegationError as y };
|
|
2971
|
+
//# sourceMappingURL=runner-CEDtiI7D.js.map
|
|
@@ -1,135 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
|
|
3
|
-
//#region src/
|
|
3
|
+
//#region src/core/arg-registry.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Custom registry for politty argument metadata
|
|
6
|
+
* This avoids polluting Zod's GlobalMeta
|
|
6
7
|
*/
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function isLazyCommand(value) {
|
|
12
|
-
return typeof value === "object" && value !== null && LAZY_BRAND in value && value[LAZY_BRAND] === true;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Create a lazily-loaded subcommand with synchronous metadata.
|
|
16
|
-
*
|
|
17
|
-
* The `meta` command provides names, descriptions, and args schema
|
|
18
|
-
* for static analysis (completion scripts, help text) without loading
|
|
19
|
-
* the full command module.
|
|
20
|
-
*
|
|
21
|
-
* The `load` function is called only at execution time.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* import { lazy, defineCommand } from "politty";
|
|
26
|
-
*
|
|
27
|
-
* const cli = defineCommand({
|
|
28
|
-
* name: "mycli",
|
|
29
|
-
* subCommands: {
|
|
30
|
-
* deploy: lazy(
|
|
31
|
-
* defineCommand({
|
|
32
|
-
* name: "deploy",
|
|
33
|
-
* description: "Deploy the application",
|
|
34
|
-
* args: z.object({ env: arg(z.string()) }),
|
|
35
|
-
* }),
|
|
36
|
-
* () => import("./deploy.js").then((m) => m.deployCommand),
|
|
37
|
-
* ),
|
|
38
|
-
* },
|
|
39
|
-
* });
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
function lazy(meta, load) {
|
|
43
|
-
return {
|
|
44
|
-
[LAZY_BRAND]: true,
|
|
45
|
-
meta,
|
|
46
|
-
load
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Resolve synchronous metadata from a SubCommandValue.
|
|
51
|
-
* Returns null for legacy async subcommands whose metadata is unavailable.
|
|
52
|
-
*/
|
|
53
|
-
function resolveSubCommandMeta(subCmd) {
|
|
54
|
-
if (isLazyCommand(subCmd)) return subCmd.meta;
|
|
55
|
-
if (typeof subCmd === "function") return null;
|
|
56
|
-
return subCmd;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/executor/subcommand-router.ts
|
|
61
|
-
/**
|
|
62
|
-
* Resolve a lazy-loaded command (sync or async)
|
|
63
|
-
*
|
|
64
|
-
* @param cmd - The command or lazy loader function
|
|
65
|
-
* @returns The resolved command
|
|
66
|
-
*/
|
|
67
|
-
async function resolveLazyCommand(cmd) {
|
|
68
|
-
if (isLazyCommand(cmd)) return await cmd.load();
|
|
69
|
-
if (typeof cmd === "function") return await cmd();
|
|
70
|
-
return cmd;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Resolve a subcommand by name (including alias lookup) and return both the
|
|
74
|
-
* resolved command and the canonical name if accessed via alias.
|
|
75
|
-
*
|
|
76
|
-
* This avoids a redundant alias scan when the caller needs both pieces of info.
|
|
77
|
-
*/
|
|
78
|
-
async function resolveSubcommandWithAlias(command, name) {
|
|
79
|
-
if (!command.subCommands) return;
|
|
80
|
-
const subCmd = command.subCommands[name];
|
|
81
|
-
if (subCmd) return {
|
|
82
|
-
command: await resolveLazyCommand(subCmd),
|
|
83
|
-
aliasFor: void 0
|
|
84
|
-
};
|
|
85
|
-
const canonicalName = resolveSubCommandAlias(command, name);
|
|
86
|
-
if (canonicalName) return {
|
|
87
|
-
command: await resolveLazyCommand(command.subCommands[canonicalName]),
|
|
88
|
-
aliasFor: canonicalName
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Resolve an alias to the canonical subcommand name.
|
|
93
|
-
* Returns the canonical name if the given name is an alias, or undefined.
|
|
94
|
-
*
|
|
95
|
-
* Note: Aliases are only recognized for eagerly-defined commands and
|
|
96
|
-
* `lazy()` commands (which carry synchronous metadata). Pure async
|
|
97
|
-
* subcommand functions do not expose metadata synchronously, so their
|
|
98
|
-
* aliases cannot be resolved without loading the module.
|
|
99
|
-
*
|
|
100
|
-
* @param command - The parent command
|
|
101
|
-
* @param alias - The alias to look up
|
|
102
|
-
* @returns The canonical subcommand name, or undefined
|
|
103
|
-
*/
|
|
104
|
-
function resolveSubCommandAlias(command, alias) {
|
|
105
|
-
if (!command.subCommands) return void 0;
|
|
106
|
-
for (const [name, subCmd] of Object.entries(command.subCommands)) if (resolveSubCommandMeta(subCmd)?.aliases?.includes(alias)) return name;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Build a set of all recognized subcommand names including aliases.
|
|
110
|
-
*
|
|
111
|
-
* @param command - The parent command
|
|
112
|
-
* @returns Set of all names (canonical + aliases)
|
|
113
|
-
*/
|
|
114
|
-
function listSubCommandNamesWithAliases(command) {
|
|
115
|
-
const names = /* @__PURE__ */ new Set();
|
|
116
|
-
if (!command.subCommands) return names;
|
|
117
|
-
for (const [name, subCmd] of Object.entries(command.subCommands)) {
|
|
118
|
-
names.add(name);
|
|
119
|
-
const meta = resolveSubCommandMeta(subCmd);
|
|
120
|
-
if (meta?.aliases) for (const alias of meta.aliases) names.add(alias);
|
|
121
|
-
}
|
|
122
|
-
return names;
|
|
8
|
+
const argRegistry = z.registry();
|
|
9
|
+
function arg(schema, meta) {
|
|
10
|
+
if (meta) argRegistry.add(schema, meta);
|
|
11
|
+
return schema;
|
|
123
12
|
}
|
|
124
13
|
/**
|
|
125
|
-
*
|
|
14
|
+
* Get metadata for a schema from the registry
|
|
126
15
|
*
|
|
127
|
-
* @param
|
|
128
|
-
* @returns
|
|
16
|
+
* @param schema - The Zod schema
|
|
17
|
+
* @returns The metadata if registered, undefined otherwise
|
|
129
18
|
*/
|
|
130
|
-
function
|
|
131
|
-
|
|
132
|
-
return Object.keys(command.subCommands);
|
|
19
|
+
function getArgMeta$1(schema) {
|
|
20
|
+
return argRegistry.get(schema);
|
|
133
21
|
}
|
|
134
22
|
|
|
135
23
|
//#endregion
|
|
@@ -596,5 +484,5 @@ function getExtractedFields(command) {
|
|
|
596
484
|
}
|
|
597
485
|
|
|
598
486
|
//#endregion
|
|
599
|
-
export { toCamelCase as a,
|
|
600
|
-
//# sourceMappingURL=schema-extractor-
|
|
487
|
+
export { toCamelCase as a, getUnknownKeysMode as i, getAllAliases as n, toKebabCase as o, getExtractedFields as r, arg as s, extractFields as t };
|
|
488
|
+
//# sourceMappingURL=schema-extractor-wzJ1cGon.js.map
|