politty 0.11.2 → 0.11.5
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/README.md +20 -4
- package/dist/{arg-registry-D558cYkD.d.ts → arg-registry-SrNIsq-d.d.ts} +41 -10
- package/dist/augment.d.ts +1 -2
- package/dist/cli-main.d.ts +1 -0
- package/dist/cli-main.js +86 -0
- package/dist/cli.js +10 -49
- package/dist/compile-cache-NnTkjJMV.js +101 -0
- package/dist/compile-cache.d.ts +79 -0
- package/dist/compile-cache.js +3 -0
- package/dist/completion/index.d.ts +2 -2
- package/dist/completion/index.js +1 -1
- package/dist/{completion-B1K87cU5.js → completion-D9SKGJad.js} +5 -4
- package/dist/docs/index.d.ts +26 -13
- package/dist/docs/index.js +2 -2
- package/dist/{index-CeZ1yI46.d.ts → index-qqwo5WZT.d.ts} +29 -18
- package/dist/index.d.ts +76 -5
- package/dist/index.js +4 -3
- package/dist/prompt/clack/index.d.ts +1 -2
- package/dist/prompt/index.d.ts +1 -2
- package/dist/prompt/inquirer/index.d.ts +1 -2
- package/dist/runner-BGs_QMJM.js +3 -0
- package/dist/{runner-CEDtiI7D.js → runner-xN7248Ph.js} +26 -31
- package/dist/skill/index.d.ts +37 -3
- package/dist/skill/index.js +35 -10
- package/dist/src-BiCtSNrn.js +193 -0
- package/package.json +16 -12
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import "./schema-extractor-wzJ1cGon.js";
|
|
2
|
+
import "./completion-D9SKGJad.js";
|
|
3
|
+
import "./runner-xN7248Ph.js";
|
|
4
|
+
import "./logger-DbDkjdfO.js";
|
|
5
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
//#region src/compile-cache-shim.ts
|
|
10
|
+
/**
|
|
11
|
+
* Generator for compile-cache bin shims.
|
|
12
|
+
*
|
|
13
|
+
* Emits the minimal entry file described in docs/recipes.md ("Faster
|
|
14
|
+
* Startup"): a shim that enables the Node.js on-disk compile cache via
|
|
15
|
+
* `politty/compile-cache` and then loads the real CLI with a dynamic
|
|
16
|
+
* import. Meant to be wired into a `postbuild` or `prepack` script via
|
|
17
|
+
* `politty generate-shim` so the shim never has to live in source.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Marker identifying files this generator wrote (overwrite guard). Must be
|
|
21
|
+
* shim-specific: other politty artifacts (e.g. completion scripts) also say
|
|
22
|
+
* "Generated by politty", and those must never be treated as overwritable.
|
|
23
|
+
*/
|
|
24
|
+
const SHIM_MARKER = "Generated by politty: compile-cache bin shim";
|
|
25
|
+
/** Entry candidates tried, in order, when `entry` is omitted. */
|
|
26
|
+
const DEFAULT_ENTRY_CANDIDATES = [
|
|
27
|
+
"./cli.js",
|
|
28
|
+
"./cli.mjs",
|
|
29
|
+
"./index.js",
|
|
30
|
+
"./index.mjs"
|
|
31
|
+
];
|
|
32
|
+
/**
|
|
33
|
+
* Read the nearest `package.json`, walking up from `startDir` to the
|
|
34
|
+
* filesystem root. A malformed file is a real error and propagates.
|
|
35
|
+
*/
|
|
36
|
+
function readNearestPackageJson(startDir) {
|
|
37
|
+
let dir = resolve(startDir);
|
|
38
|
+
for (;;) {
|
|
39
|
+
const candidate = join(dir, "package.json");
|
|
40
|
+
if (existsSync(candidate)) return {
|
|
41
|
+
pkg: JSON.parse(readFileSync(candidate, "utf8")),
|
|
42
|
+
dir
|
|
43
|
+
};
|
|
44
|
+
const parent = dirname(dir);
|
|
45
|
+
if (parent === dir) return void 0;
|
|
46
|
+
dir = parent;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* `bin` entries as `[name, path]` pairs: the object form's entries in
|
|
51
|
+
* declaration order, or the string form as a single pair named after the
|
|
52
|
+
* package (npm's own convention for `"bin": "path"`).
|
|
53
|
+
*/
|
|
54
|
+
function binEntries(pkg) {
|
|
55
|
+
if (!pkg) return [];
|
|
56
|
+
if (typeof pkg.bin === "string" && pkg.bin) {
|
|
57
|
+
const name = defaultProgramName(pkg);
|
|
58
|
+
return name ? [[name, pkg.bin]] : [];
|
|
59
|
+
}
|
|
60
|
+
if (typeof pkg.bin === "object" && pkg.bin !== null) return Object.entries(pkg.bin).filter((pair) => typeof pair[1] === "string" && pair[1] !== "");
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Derive the fallback program name from a package.json: the first `bin`
|
|
65
|
+
* command name when present (that is the name users type in their shell),
|
|
66
|
+
* else the package name without its scope.
|
|
67
|
+
*/
|
|
68
|
+
function defaultProgramName(pkg) {
|
|
69
|
+
if (!pkg) return void 0;
|
|
70
|
+
if (typeof pkg.bin === "object" && pkg.bin !== null) {
|
|
71
|
+
const [first] = Object.keys(pkg.bin);
|
|
72
|
+
if (first) return first;
|
|
73
|
+
}
|
|
74
|
+
if (typeof pkg.name === "string" && pkg.name) {
|
|
75
|
+
const name = pkg.name;
|
|
76
|
+
return name.startsWith("@") ? name.split("/")[1] ?? name : name;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Pick the default entry: the first {@link DEFAULT_ENTRY_CANDIDATES} that
|
|
81
|
+
* exists next to the shim (never the shim itself).
|
|
82
|
+
*/
|
|
83
|
+
function defaultEntry(outputPath) {
|
|
84
|
+
const dir = dirname(outputPath);
|
|
85
|
+
const self = basename(outputPath);
|
|
86
|
+
for (const candidate of DEFAULT_ENTRY_CANDIDATES) {
|
|
87
|
+
if (basename(candidate) === self) continue;
|
|
88
|
+
if (existsSync(join(dir, candidate))) return candidate;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function shimContent(program, entry) {
|
|
92
|
+
return [
|
|
93
|
+
"#!/usr/bin/env node",
|
|
94
|
+
"",
|
|
95
|
+
`// ${SHIM_MARKER}.`,
|
|
96
|
+
"// Enables the Node.js on-disk compile cache (V8 code cache) before the",
|
|
97
|
+
"// CLI module graph is compiled, so warm starts skip recompilation",
|
|
98
|
+
"// (Node >= 22.8.0; silent no-op otherwise).",
|
|
99
|
+
"try {",
|
|
100
|
+
" const { enableCompileCache } = await import(\"politty/compile-cache\");",
|
|
101
|
+
` enableCompileCache(${JSON.stringify(program)});`,
|
|
102
|
+
"} catch {",
|
|
103
|
+
" // politty is not resolvable from here (e.g. a fully bundled CLI) —",
|
|
104
|
+
" // start without the compile cache rather than failing the CLI.",
|
|
105
|
+
"}",
|
|
106
|
+
`await import(${JSON.stringify(entry)});`,
|
|
107
|
+
""
|
|
108
|
+
].join("\n");
|
|
109
|
+
}
|
|
110
|
+
function toArray(value) {
|
|
111
|
+
if (value === void 0) return [];
|
|
112
|
+
return Array.isArray(value) ? value : [value];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Generate executable compile-cache bin shims.
|
|
116
|
+
*
|
|
117
|
+
* With no explicit paths, one shim is generated per `bin` entry of the
|
|
118
|
+
* nearest `package.json`, each importing a conventional built module next
|
|
119
|
+
* to it — so a bare `politty generate-shim` in a `postbuild` script is
|
|
120
|
+
* usually enough. Explicit `entry` values are paired in order with the
|
|
121
|
+
* `bin` entries (or with explicit `out` values of the same count).
|
|
122
|
+
*
|
|
123
|
+
* The generated files are ESM (they use top-level `await import`), so `.js`
|
|
124
|
+
* output requires `"type": "module"` in the package; use a `.mjs` extension
|
|
125
|
+
* otherwise. Refuses to overwrite an existing file it did not generate, so
|
|
126
|
+
* a `bin` path still pointing at the real CLI entry fails loudly instead of
|
|
127
|
+
* clobbering the build output.
|
|
128
|
+
*/
|
|
129
|
+
function generateCompileCacheShim(options = {}) {
|
|
130
|
+
const cwd = options.cwd ?? process.cwd();
|
|
131
|
+
const found = readNearestPackageJson(cwd);
|
|
132
|
+
const pkg = found?.pkg;
|
|
133
|
+
const entries = toArray(options.entry);
|
|
134
|
+
const outs = toArray(options.out);
|
|
135
|
+
const bins = binEntries(pkg);
|
|
136
|
+
let outputPaths;
|
|
137
|
+
if (outs.length > 0) {
|
|
138
|
+
if (entries.length > 0 && entries.length !== outs.length) throw new Error(`--entry count (${entries.length}) does not match --out count (${outs.length}); pass one --out per --entry or omit --out entirely.`);
|
|
139
|
+
outputPaths = outs.map((out) => isAbsolute(out) ? out : resolve(cwd, out));
|
|
140
|
+
} else {
|
|
141
|
+
if (bins.length === 0 || found === void 0) throw new Error("Cannot derive the output path: no bin entry in package.json. Pass --out explicitly.");
|
|
142
|
+
if (entries.length > 0 && entries.length !== bins.length) throw new Error(`--entry count (${entries.length}) does not match the package's bin entries (${bins.length}); pass one --entry per bin (in declaration order) or matching --out paths.`);
|
|
143
|
+
outputPaths = bins.map(([, path]) => resolve(found.dir, path));
|
|
144
|
+
}
|
|
145
|
+
if (new Set(outputPaths).size !== outputPaths.length) throw new Error("Duplicate output paths; each shim needs a distinct output file. Check the --out values (or the bin paths in package.json when --out is omitted).");
|
|
146
|
+
const binNameByPath = /* @__PURE__ */ new Map();
|
|
147
|
+
if (found) for (const [name, path] of bins) binNameByPath.set(resolve(found.dir, path), name);
|
|
148
|
+
const warnings = [];
|
|
149
|
+
const plans = outputPaths.map((outputPath, index) => {
|
|
150
|
+
const matchedBinName = binNameByPath.get(outputPath);
|
|
151
|
+
const program = options.program ?? matchedBinName ?? defaultProgramName(pkg);
|
|
152
|
+
if (!program) throw new Error("Cannot derive a program name: no bin or name in package.json. Pass --program explicitly.");
|
|
153
|
+
if (options.program === void 0 && matchedBinName === void 0 && outs.length > 0) warnings.push(`Warning: ${formatShimPath(outputPath, cwd)} does not match any bin path in package.json, so its cache directory uses the program name ${JSON.stringify(program)}. Pass --program to override.`);
|
|
154
|
+
if (outputPath.endsWith(".cjs")) throw new Error("The generated shim is an ES module and cannot be written to a .cjs file. Use a .mjs (or .js in a \"type\": \"module\" package) output path.");
|
|
155
|
+
if (extname(outputPath) === "") throw new Error("The generated shim is an ES module and cannot be written to an extensionless file (it would not load on every supported Node.js version). Use a .mjs (or .js in a \"type\": \"module\" package) output path.");
|
|
156
|
+
if (outputPath.endsWith(".js") && pkg?.type !== "module") throw new Error(pkg === void 0 ? "The generated shim is an ES module, and no package.json was found to confirm .js files are treated as ESM. Write it to a .mjs file instead (e.g. --out dist/bin.mjs)." : "The generated shim is an ES module, but the package is not \"type\": \"module\". Write it to a .mjs file instead (e.g. --out dist/bin.mjs).");
|
|
157
|
+
const entry = entries[index] ?? defaultEntry(outputPath);
|
|
158
|
+
if (entry === void 0) throw new Error(`Cannot derive an entry: none of ${DEFAULT_ENTRY_CANDIDATES.join(", ")} exist next to ${outputPath}. Pass --entry explicitly.`);
|
|
159
|
+
if (!entry.startsWith("./") && !entry.startsWith("../") && !entry.startsWith("file:")) throw new Error(`Invalid entry ${entry}: pass a specifier relative to the shim file ("./..." or "../...") or a file: URL.`);
|
|
160
|
+
let entryPath;
|
|
161
|
+
if (entry.startsWith("file:")) try {
|
|
162
|
+
entryPath = fileURLToPath(entry);
|
|
163
|
+
} catch {
|
|
164
|
+
entryPath = void 0;
|
|
165
|
+
}
|
|
166
|
+
else entryPath = resolve(dirname(outputPath), entry);
|
|
167
|
+
if (entryPath === outputPath) throw new Error(`The shim at ${outputPath} would import itself (entry ${entry}). Point bin at a separate shim path (e.g. dist/bin.js) or pass a different --entry.`);
|
|
168
|
+
if (existsSync(outputPath) && !readFileSync(outputPath, "utf8").includes(SHIM_MARKER)) throw new Error(`Refusing to overwrite ${outputPath}: it exists and was not generated by politty. If bin currently points at your real CLI entry, point it at a separate shim path (e.g. dist/bin.js) or pass --out explicitly.`);
|
|
169
|
+
return {
|
|
170
|
+
outputPath,
|
|
171
|
+
program,
|
|
172
|
+
entry
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
for (const warning of warnings) console.warn(warning);
|
|
176
|
+
for (const { outputPath, program, entry } of plans) {
|
|
177
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
178
|
+
writeFileSync(outputPath, shimContent(program, entry));
|
|
179
|
+
chmodSync(outputPath, 493);
|
|
180
|
+
}
|
|
181
|
+
return plans;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Format a generated path for CLI output: relative to `cwd` when inside it.
|
|
185
|
+
*/
|
|
186
|
+
function formatShimPath(outputPath, cwd) {
|
|
187
|
+
const rel = relative(cwd, outputPath);
|
|
188
|
+
return rel && !rel.startsWith("..") ? rel : outputPath;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
export { generateCompileCacheShim as n, formatShimPath as t };
|
|
193
|
+
//# sourceMappingURL=src-BiCtSNrn.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "politty",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.5",
|
|
4
4
|
"description": "A lightweight CLI framework inspired by citty with zod v4 registry integration for type-safe metadata management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"argument-parser",
|
|
@@ -35,6 +35,10 @@
|
|
|
35
35
|
"types": "./dist/augment.d.ts",
|
|
36
36
|
"default": "./dist/augment.js"
|
|
37
37
|
},
|
|
38
|
+
"./compile-cache": {
|
|
39
|
+
"types": "./dist/compile-cache.d.ts",
|
|
40
|
+
"default": "./dist/compile-cache.js"
|
|
41
|
+
},
|
|
38
42
|
"./docs": {
|
|
39
43
|
"types": "./dist/docs/index.d.ts",
|
|
40
44
|
"default": "./dist/docs/index.js"
|
|
@@ -68,19 +72,19 @@
|
|
|
68
72
|
"@clack/prompts": "1.2.0",
|
|
69
73
|
"@inquirer/prompts": "8.5.2",
|
|
70
74
|
"@quansync/fs": "1.0.0",
|
|
71
|
-
"@types/node": "25.9.
|
|
72
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
73
|
-
"@vitest/coverage-v8": "4.1.
|
|
74
|
-
"knip": "6.
|
|
75
|
-
"lefthook": "2.1.
|
|
75
|
+
"@types/node": "25.9.5",
|
|
76
|
+
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
77
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
78
|
+
"knip": "6.26.0",
|
|
79
|
+
"lefthook": "2.1.10",
|
|
76
80
|
"organize-imports-cli": "1.0.2",
|
|
77
|
-
"oxfmt": "0.
|
|
78
|
-
"oxlint": "1.
|
|
81
|
+
"oxfmt": "0.58.0",
|
|
82
|
+
"oxlint": "1.73.0",
|
|
79
83
|
"quansync": "1.0.0",
|
|
80
|
-
"tsdown": "0.22.
|
|
81
|
-
"tsx": "4.
|
|
82
|
-
"typescript": "
|
|
83
|
-
"vitest": "4.1.
|
|
84
|
+
"tsdown": "0.22.7",
|
|
85
|
+
"tsx": "4.23.1",
|
|
86
|
+
"typescript": "7.0.2",
|
|
87
|
+
"vitest": "4.1.10",
|
|
84
88
|
"zod": "4.4.3"
|
|
85
89
|
},
|
|
86
90
|
"peerDependencies": {
|