cli-forge 0.4.0 → 0.6.0
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/bin/cli.js +5 -1
- package/bin/cli.js.map +1 -1
- package/bin/commands/generate-documentation.d.ts +2 -0
- package/bin/commands/generate-documentation.js +121 -17
- package/bin/commands/generate-documentation.js.map +1 -1
- package/bin/commands/init.d.ts +4 -0
- package/bin/commands/init.js +33 -4
- package/bin/commands/init.js.map +1 -1
- package/package.json +10 -4
- package/src/index.d.ts +1 -0
- package/src/index.js +3 -1
- package/src/index.js.map +1 -1
- package/src/lib/cli-forge.d.ts +112 -11
- package/src/lib/cli-forge.js +245 -42
- package/src/lib/cli-forge.js.map +1 -1
- package/src/lib/documentation.d.ts +7 -14
- package/src/lib/documentation.js +15 -2
- package/src/lib/documentation.js.map +1 -1
- package/src/lib/interactive-shell.d.ts +13 -0
- package/src/lib/interactive-shell.js +85 -0
- package/src/lib/interactive-shell.js.map +1 -0
- package/src/lib/test-harness.d.ts +36 -0
- package/src/lib/test-harness.js +37 -0
- package/src/lib/test-harness.js.map +1 -0
- package/src/lib/utils.d.ts +10 -0
- package/src/lib/utils.js +110 -0
- package/src/lib/utils.js.map +1 -0
package/bin/cli.js
CHANGED
|
@@ -6,5 +6,9 @@ const generate_documentation_1 = require("./commands/generate-documentation");
|
|
|
6
6
|
const init_1 = require("./commands/init");
|
|
7
7
|
const mycli = (0, src_1.cli)('cli-forge').commands(generate_documentation_1.generateDocumentationCommand, init_1.initCommand);
|
|
8
8
|
exports.default = mycli;
|
|
9
|
-
|
|
9
|
+
if (require.main === module) {
|
|
10
|
+
(async () => {
|
|
11
|
+
await mycli.forge();
|
|
12
|
+
})();
|
|
13
|
+
}
|
|
10
14
|
//# sourceMappingURL=cli.js.map
|
package/bin/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../../packages/cli-forge/bin/cli.ts"],"names":[],"mappings":";;;AAEA,gCAA6B;AAC7B,8EAAiF;AACjF,0CAA8C;AAE9C,MAAM,KAAK,GAAG,IAAA,SAAG,EAAC,WAAW,CAAC,CAAC,QAAQ,CACrC,qDAA4B,EAC5B,kBAAW,CACZ,CAAC;AAEF,kBAAe,KAAK,CAAC;AAErB,KAAK,CAAC,KAAK,EAAE,CAAC"}
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../../packages/cli-forge/bin/cli.ts"],"names":[],"mappings":";;;AAEA,gCAA6B;AAC7B,8EAAiF;AACjF,0CAA8C;AAE9C,MAAM,KAAK,GAAG,IAAA,SAAG,EAAC,WAAW,CAAC,CAAC,QAAQ,CACrC,qDAA4B,EAC5B,kBAAW,CACZ,CAAC;AAEF,kBAAe,KAAK,CAAC;AAErB,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC5B,CAAC,KAAK,IAAI,EAAE;QACV,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC,CAAC,EAAE,CAAC;AACP,CAAC"}
|
|
@@ -4,10 +4,12 @@ exports.generateDocumentationCommand = void 0;
|
|
|
4
4
|
exports.withGenerateDocumentationArgs = withGenerateDocumentationArgs;
|
|
5
5
|
const node_fs_1 = require("node:fs");
|
|
6
6
|
const node_path_1 = require("node:path");
|
|
7
|
+
const posix_1 = require("node:path/posix");
|
|
7
8
|
const src_1 = require("../../src");
|
|
8
9
|
const documentation_1 = require("../../src/lib/documentation");
|
|
9
10
|
const cli_forge_1 = require("../../src/lib/cli-forge");
|
|
10
11
|
const fs_1 = require("../utils/fs");
|
|
12
|
+
const node_url_1 = require("node:url");
|
|
11
13
|
function withGenerateDocumentationArgs(cmd) {
|
|
12
14
|
return cmd
|
|
13
15
|
.positional('cli', {
|
|
@@ -23,26 +25,25 @@ function withGenerateDocumentationArgs(cmd) {
|
|
|
23
25
|
})
|
|
24
26
|
.option('format', {
|
|
25
27
|
type: 'string',
|
|
26
|
-
description: 'What format should the documentation be output in?
|
|
28
|
+
description: 'What format should the documentation be output in?',
|
|
27
29
|
default: 'md',
|
|
30
|
+
choices: ['json', 'md'],
|
|
28
31
|
})
|
|
29
32
|
.option('export', {
|
|
30
33
|
type: 'string',
|
|
31
34
|
description: 'The name of the export that contains the CLI instance. By default, docs will be generated for the default export.',
|
|
35
|
+
})
|
|
36
|
+
.option('tsconfig', {
|
|
37
|
+
type: 'string',
|
|
38
|
+
description: 'Specifies the `tsconfig` used when loading typescript based CLIs.',
|
|
32
39
|
});
|
|
33
40
|
}
|
|
34
41
|
exports.generateDocumentationCommand = (0, src_1.default)('generate-documentation', {
|
|
35
42
|
description: 'Generate documentation for the given CLI',
|
|
36
43
|
builder: (b) => withGenerateDocumentationArgs(b),
|
|
37
44
|
handler: async (args) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
const cliModule = await Promise.resolve(`${args.cli}`).then(s => require(s));
|
|
42
|
-
const cli = cliModule[args.export || 'default'] ?? cliModule;
|
|
43
|
-
if (!(cli instanceof cli_forge_1.InternalCLI)) {
|
|
44
|
-
throw new Error(`${args.cli}${args.export ? '#' + args.export : ''} is not a CLI.`);
|
|
45
|
-
}
|
|
45
|
+
const cliModule = await loadCLIModule(args);
|
|
46
|
+
let cli = readCLIFromModule(cliModule, args);
|
|
46
47
|
const documentation = (0, documentation_1.generateDocumentation)(cli);
|
|
47
48
|
if (args.format === 'md') {
|
|
48
49
|
await generateMarkdownDocumentation(documentation, args);
|
|
@@ -59,25 +60,28 @@ exports.generateDocumentationCommand = (0, src_1.default)('generate-documentatio
|
|
|
59
60
|
});
|
|
60
61
|
async function generateMarkdownDocumentation(docs, args) {
|
|
61
62
|
const md = await importMarkdownFactory();
|
|
62
|
-
await generateMarkdownForSingleCommand(docs,
|
|
63
|
+
await generateMarkdownForSingleCommand(docs, args.output, args.output, md);
|
|
63
64
|
}
|
|
64
|
-
async function generateMarkdownForSingleCommand(docs, out, md) {
|
|
65
|
+
async function generateMarkdownForSingleCommand(docs, out, docsRoot, md) {
|
|
65
66
|
const subcommands = docs.subcommands;
|
|
66
67
|
const outdir = subcommands.length ? out : (0, node_path_1.dirname)(out);
|
|
67
68
|
const outname = subcommands.length ? 'index' : docs.name;
|
|
68
69
|
(0, fs_1.ensureDirSync)(outdir);
|
|
69
70
|
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(outdir, outname + '.md'), md.h1(docs.name, ...[
|
|
71
|
+
[md.bold('Usage:'), md.code(docs.usage)].join(' '),
|
|
70
72
|
docs.description,
|
|
71
73
|
getPositionalArgsFragment(docs.positionals, md),
|
|
72
74
|
getFlagArgsFragment(docs.options, md),
|
|
73
|
-
getSubcommandsFragment(docs.subcommands, md),
|
|
75
|
+
getSubcommandsFragment(docs.subcommands, outdir, docsRoot, md),
|
|
76
|
+
getExamplesFragment(docs.examples, md),
|
|
74
77
|
].filter(isTruthy)));
|
|
75
78
|
for (const subcommand of docs.subcommands) {
|
|
76
|
-
await generateMarkdownForSingleCommand(subcommand, (0, node_path_1.join)(outdir, subcommand.name), md);
|
|
79
|
+
await generateMarkdownForSingleCommand(subcommand, (0, node_path_1.join)(outdir, subcommand.name), docsRoot, md);
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
82
|
function formatOption(option, md) {
|
|
80
|
-
return md.h3(option.key, ...[
|
|
83
|
+
return md.h3(option.deprecated ? md.strikethrough(option.key) : option.key, ...[
|
|
84
|
+
option.deprecated ? md.bold(md.italics('Deprecated')) : undefined,
|
|
81
85
|
md.bold('Type:') +
|
|
82
86
|
' ' +
|
|
83
87
|
(option.type === 'array'
|
|
@@ -85,7 +89,18 @@ function formatOption(option, md) {
|
|
|
85
89
|
: option.type),
|
|
86
90
|
option.description,
|
|
87
91
|
option.default ? md.bold('Default:') + ' ' + option.default : undefined,
|
|
88
|
-
|
|
92
|
+
// No need to show required if it's required and has a default, as its not actually required to pass.
|
|
93
|
+
option.required && !option.default ? md.bold('Required') : undefined,
|
|
94
|
+
option.choices
|
|
95
|
+
? md.bold('Valid values:') +
|
|
96
|
+
' ' +
|
|
97
|
+
(() => {
|
|
98
|
+
const choicesAsString = (typeof option.choices === 'function'
|
|
99
|
+
? option.choices()
|
|
100
|
+
: option.choices).map((t) => t.toString());
|
|
101
|
+
return choicesAsString.join(', ');
|
|
102
|
+
})()
|
|
103
|
+
: undefined,
|
|
89
104
|
option.alias?.length
|
|
90
105
|
? md.h4('Aliases', md.ul(...option.alias))
|
|
91
106
|
: undefined,
|
|
@@ -103,11 +118,12 @@ function getFlagArgsFragment(options, md) {
|
|
|
103
118
|
}
|
|
104
119
|
return md.h2('Flags', ...Object.values(options).map((option) => formatOption(option, md)));
|
|
105
120
|
}
|
|
106
|
-
function getSubcommandsFragment(subcommands, md) {
|
|
121
|
+
function getSubcommandsFragment(subcommands, outdir, docsRoot, md) {
|
|
107
122
|
if (subcommands.length === 0) {
|
|
108
123
|
return undefined;
|
|
109
124
|
}
|
|
110
|
-
return md.h2('Subcommands', ...subcommands.map((subcommand) => md.link(
|
|
125
|
+
return md.h2('Subcommands', ...subcommands.map((subcommand) => md.link('./' +
|
|
126
|
+
(0, posix_1.join)((0, posix_1.normalize)((0, node_path_1.relative)(docsRoot, outdir)), subcommand.name + '.md'), subcommand.name)));
|
|
111
127
|
}
|
|
112
128
|
function isTruthy(value) {
|
|
113
129
|
return !!value;
|
|
@@ -120,4 +136,92 @@ async function importMarkdownFactory() {
|
|
|
120
136
|
throw new Error('Could not find markdown-factory. Please install it to generate markdown documentation.');
|
|
121
137
|
}
|
|
122
138
|
}
|
|
139
|
+
function isCLI(obj) {
|
|
140
|
+
if (obj instanceof cli_forge_1.InternalCLI) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
if (typeof obj !== 'object' || !obj) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
if (!('constructor' in obj)) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
if (!('name' in obj.constructor)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return obj.constructor.name === cli_forge_1.InternalCLI.name;
|
|
153
|
+
}
|
|
154
|
+
function getExamplesFragment(examples, md) {
|
|
155
|
+
if (examples.length === 0) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
return md.h2('Examples', ...examples.map((example) => md.codeBlock(example, 'shell')));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Reads the CLI instance from the provided module. For some reason,
|
|
162
|
+
* when importing a module that uses `export default` in typescript
|
|
163
|
+
* the default export is nested under a `default` property on the module...
|
|
164
|
+
* We for code that looks like `export default 5`, on import we get back
|
|
165
|
+
* `{ default: {default: 5}}`. To work around this, and make things work
|
|
166
|
+
* we try to read the CLI instance from the module in a few different ways.
|
|
167
|
+
*
|
|
168
|
+
* @param cliModule The imported module
|
|
169
|
+
* @param exportSpecifier The name of the export to read the CLI from. Defaults to `default`.
|
|
170
|
+
* @returns A CLI instance.
|
|
171
|
+
*/
|
|
172
|
+
function readCLIFromModule(cliModule, args) {
|
|
173
|
+
let cli = cliModule;
|
|
174
|
+
if (args.export) {
|
|
175
|
+
cli = cliModule[args.export] ?? cliModule['default']?.[args.export];
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
cli =
|
|
179
|
+
cliModule['default']?.['default'] ?? cliModule['default'] ?? cliModule;
|
|
180
|
+
}
|
|
181
|
+
if (!isCLI(cli)) {
|
|
182
|
+
throw new Error(`${args.cli}${args.export ? '#' + args.export : ''} is not a CLI.`);
|
|
183
|
+
}
|
|
184
|
+
return cli;
|
|
185
|
+
}
|
|
186
|
+
async function loadCLIModule(args) {
|
|
187
|
+
if (!(0, node_path_1.isAbsolute)(args.cli)) {
|
|
188
|
+
args.cli = (0, node_path_1.join)(process.cwd(), args.cli);
|
|
189
|
+
}
|
|
190
|
+
const cliPath = [
|
|
191
|
+
args.cli,
|
|
192
|
+
`${args.cli}.ts`,
|
|
193
|
+
`${args.cli}.js`,
|
|
194
|
+
`${args.cli}.cjs`,
|
|
195
|
+
`${args.cli}.mjs`,
|
|
196
|
+
(0, node_path_1.join)(args.cli, 'index.ts'),
|
|
197
|
+
(0, node_path_1.join)(args.cli, 'index.js'),
|
|
198
|
+
(0, node_path_1.join)(args.cli, 'index.cjs'),
|
|
199
|
+
(0, node_path_1.join)(args.cli, 'index.mjs'),
|
|
200
|
+
].find((f) => (0, node_fs_1.existsSync)(f));
|
|
201
|
+
if (!cliPath) {
|
|
202
|
+
throw new Error(`Could not find CLI module at ${args.cli}
|
|
203
|
+
|
|
204
|
+
Ensure that the path is correct and that the CLI module exists.`);
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const tsx = (await Promise.resolve().then(() => require(
|
|
208
|
+
//@ts-ignore
|
|
209
|
+
'tsx/esm/api')));
|
|
210
|
+
return tsx.tsImport(cliPath, {
|
|
211
|
+
tsconfig: args.tsconfig,
|
|
212
|
+
parentURL: (0, node_url_1.pathToFileURL)(__dirname).toString(),
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
try {
|
|
217
|
+
return await Promise.resolve(`${cliPath}`).then(s => require(s));
|
|
218
|
+
}
|
|
219
|
+
catch (e) {
|
|
220
|
+
if (cliPath.endsWith('.ts')) {
|
|
221
|
+
console.warn('[cli-forge]: Generating docs for a typescript CLI requires installing `tsx` as a dev dependency, targeting the build artifacts instead, or otherwise registering a typescript loader with node.');
|
|
222
|
+
}
|
|
223
|
+
throw e;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
123
227
|
//# sourceMappingURL=generate-documentation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate-documentation.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/commands/generate-documentation.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"generate-documentation.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/commands/generate-documentation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ACb,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,CAAC,GAAG;;sEAEU,CAAC,CAAC;IACtE,CAAC;IAED,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,CAAC;QACX,YAAY;QACZ,aAAa,EACd,CAAgD,CAAC;QAClD,OAAO,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE;YAC3B,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,SAAS,EAAE,IAAA,wBAAa,EAAC,SAAS,CAAC,CAAC,QAAQ,EAAE;SAC/C,CAAC,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,CAAC;YACH,OAAO,yBAAa,OAAO,yBAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC5B,OAAO,CAAC,IAAI,CACV,iMAAiM,CAClM,CAAC;YACJ,CAAC;YACD,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/bin/commands/init.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export declare function withInitArgs<T extends ParsedArgs>(cmd: CLI<T>): CLI<T &
|
|
|
6
6
|
output: string;
|
|
7
7
|
} & {
|
|
8
8
|
format: string;
|
|
9
|
+
} & {
|
|
10
|
+
initialVersion: string;
|
|
9
11
|
}>;
|
|
10
12
|
export declare const initCommand: CLI<{
|
|
11
13
|
unmatched: string[];
|
|
@@ -16,4 +18,6 @@ export declare const initCommand: CLI<{
|
|
|
16
18
|
output: string;
|
|
17
19
|
} & {
|
|
18
20
|
format: string;
|
|
21
|
+
} & {
|
|
22
|
+
initialVersion: string;
|
|
19
23
|
}>;
|
package/bin/commands/init.js
CHANGED
|
@@ -5,10 +5,22 @@ exports.withInitArgs = withInitArgs;
|
|
|
5
5
|
const node_child_process_1 = require("node:child_process");
|
|
6
6
|
const node_fs_1 = require("node:fs");
|
|
7
7
|
const node_path_1 = require("node:path");
|
|
8
|
-
const
|
|
8
|
+
const CLI_FORGE_PACKAGE_JSON = require("../../package.json");
|
|
9
9
|
const src_1 = require("../../src");
|
|
10
10
|
// import { CLI } from '../../src/lib/cli-forge';
|
|
11
11
|
const fs_1 = require("../utils/fs");
|
|
12
|
+
const CLI_FORGE_VERSION = CLI_FORGE_PACKAGE_JSON.version;
|
|
13
|
+
const DEV_PEER_DEPS = Object.entries(CLI_FORGE_PACKAGE_JSON.peerDependencies).reduce((acc, [dep, version]) => {
|
|
14
|
+
if (
|
|
15
|
+
// The dev prop doesn't actually do anything for npm/pnpm/yarn,
|
|
16
|
+
// but we are using it to mark when a peer dep is only used at dev time.
|
|
17
|
+
// In these cases, we can safely add them to the devDependencies of the
|
|
18
|
+
// generated CLI.
|
|
19
|
+
CLI_FORGE_PACKAGE_JSON.peerDependenciesMeta[dep]?.dev) {
|
|
20
|
+
acc[dep] = version;
|
|
21
|
+
}
|
|
22
|
+
return acc;
|
|
23
|
+
}, {});
|
|
12
24
|
function withInitArgs(cmd) {
|
|
13
25
|
return cmd
|
|
14
26
|
.positional('cliName', {
|
|
@@ -24,7 +36,13 @@ function withInitArgs(cmd) {
|
|
|
24
36
|
.option('format', {
|
|
25
37
|
type: 'string',
|
|
26
38
|
default: 'ts',
|
|
27
|
-
description: 'What format should the CLI be in?
|
|
39
|
+
description: 'What format should the CLI be in?',
|
|
40
|
+
choices: ['js', 'ts'],
|
|
41
|
+
})
|
|
42
|
+
.option('initialVersion', {
|
|
43
|
+
type: 'string',
|
|
44
|
+
default: '0.0.1',
|
|
45
|
+
description: 'Initial version used when creating the package.json for the new CLI.',
|
|
28
46
|
});
|
|
29
47
|
}
|
|
30
48
|
exports.initCommand = (0, src_1.default)('init', {
|
|
@@ -35,11 +53,22 @@ exports.initCommand = (0, src_1.default)('init', {
|
|
|
35
53
|
(0, fs_1.ensureDirSync)(args.output);
|
|
36
54
|
const packageJsonPath = (0, node_path_1.join)(args.output, 'package.json');
|
|
37
55
|
const cliPath = (0, node_path_1.join)(args.output, 'bin', `${args.cliName}.${args.format}`);
|
|
38
|
-
const packageJsonContent = readJsonOr(packageJsonPath, {
|
|
56
|
+
const packageJsonContent = readJsonOr(packageJsonPath, {
|
|
57
|
+
name: args.cliName,
|
|
58
|
+
version: args.initialVersion,
|
|
59
|
+
});
|
|
39
60
|
packageJsonContent.bin ??= {};
|
|
40
61
|
packageJsonContent.bin[args.cliName] = (0, node_path_1.relative)(args.output, cliPath);
|
|
41
62
|
packageJsonContent.dependencies ??= {};
|
|
42
|
-
packageJsonContent.dependencies['cli-forge'] ??=
|
|
63
|
+
packageJsonContent.dependencies['cli-forge'] ??= CLI_FORGE_VERSION;
|
|
64
|
+
if (args.format === 'ts') {
|
|
65
|
+
const latestTypescriptVersion = (0, node_child_process_1.execSync)('npm show typescript version').toString();
|
|
66
|
+
packageJsonContent.devDependencies = {
|
|
67
|
+
typescript: latestTypescriptVersion,
|
|
68
|
+
...DEV_PEER_DEPS,
|
|
69
|
+
...packageJsonContent.devDependencies,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
43
72
|
(0, node_fs_1.writeFileSync)(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
|
|
44
73
|
(0, fs_1.ensureDirSync)((0, node_path_1.dirname)(cliPath));
|
|
45
74
|
(0, node_fs_1.writeFileSync)(cliPath, args.format === 'ts'
|
package/bin/commands/init.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/commands/init.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/commands/init.ts"],"names":[],"mappings":";;;AA6BA,oCAwBC;AAnDD,2DAA8C;AAC9C,qCAAkE;AAClE,yCAAoD;AAEpD,6DAA6D;AAC7D,mCAAqC;AACrC,iDAAiD;AACjD,oCAA4C;AAE5C,MAAM,iBAAiB,GAAG,sBAAs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C;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-forge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.0",
|
|
6
|
-
"@cli-forge/parser": "0.
|
|
6
|
+
"@cli-forge/parser": "0.6.0"
|
|
7
7
|
},
|
|
8
8
|
"peerDependencies": {
|
|
9
|
-
"markdown-factory": "0.2.0"
|
|
9
|
+
"markdown-factory": "0.2.0",
|
|
10
|
+
"tsx": "4.19.0"
|
|
10
11
|
},
|
|
11
12
|
"peerDependenciesMeta": {
|
|
12
13
|
"markdown-factory": {
|
|
13
|
-
"optional": true
|
|
14
|
+
"optional": true,
|
|
15
|
+
"dev": true
|
|
16
|
+
},
|
|
17
|
+
"tsx": {
|
|
18
|
+
"optional": true,
|
|
19
|
+
"dev": true
|
|
14
20
|
}
|
|
15
21
|
},
|
|
16
22
|
"type": "commonjs",
|
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cli = exports.default = void 0;
|
|
3
|
+
exports.TestHarness = exports.cli = exports.default = void 0;
|
|
4
4
|
var cli_forge_1 = require("./lib/cli-forge");
|
|
5
5
|
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return cli_forge_1.default; } });
|
|
6
6
|
Object.defineProperty(exports, "cli", { enumerable: true, get: function () { return cli_forge_1.cli; } });
|
|
7
|
+
var test_harness_1 = require("./lib/test-harness");
|
|
8
|
+
Object.defineProperty(exports, "TestHarness", { enumerable: true, get: function () { return test_harness_1.TestHarness; } });
|
|
7
9
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/cli-forge/src/index.ts"],"names":[],"mappings":";;;AAAA,6CAA6D;AAApD,oGAAA,OAAO,OAAA;AAAE,gGAAA,GAAG,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/cli-forge/src/index.ts"],"names":[],"mappings":";;;AAAA,6CAA6D;AAApD,oGAAA,OAAO,OAAA;AAAE,gGAAA,GAAG,OAAA;AACrB,mDAAiD;AAAxC,2GAAA,WAAW,OAAA"}
|
package/src/lib/cli-forge.d.ts
CHANGED
|
@@ -5,11 +5,45 @@ export interface CLIHandlerContext {
|
|
|
5
5
|
export type CLICommandOptions<TInitial extends ParsedArgs, TArgs extends TInitial = TInitial> = {
|
|
6
6
|
description?: string;
|
|
7
7
|
builder?: (parser: CLI<TInitial>) => CLI<TArgs>;
|
|
8
|
-
handler
|
|
8
|
+
handler?: (args: TArgs, context: CLIHandlerContext) => void | Promise<void>;
|
|
9
|
+
usage?: string;
|
|
10
|
+
examples?: string[];
|
|
9
11
|
};
|
|
10
12
|
export type Command<TInitial extends ParsedArgs = any, TArgs extends TInitial = TInitial> = ({
|
|
11
13
|
name: string;
|
|
12
14
|
} & CLICommandOptions<TInitial, TArgs>) | CLI<TArgs>;
|
|
15
|
+
/**
|
|
16
|
+
* Error Handler for CLI applications. Error handlers should re-throw the error if they cannot handle it.
|
|
17
|
+
*
|
|
18
|
+
* @param e The error that was thrown.
|
|
19
|
+
* @param actions Actions that can be taken by the error handler. Prefer using these over process.exit for better support of interactive shells.
|
|
20
|
+
*/
|
|
21
|
+
export type ErrorHandler = (e: unknown, actions: {
|
|
22
|
+
/**
|
|
23
|
+
* Exits the process immediately.
|
|
24
|
+
* @param code
|
|
25
|
+
*/
|
|
26
|
+
exit: (code?: number) => void;
|
|
27
|
+
}) => void;
|
|
28
|
+
/**
|
|
29
|
+
* The interface for a CLI application or subcommands.
|
|
30
|
+
*
|
|
31
|
+
* {@link cli} is provided as a small helper function to create a new CLI instance.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { cli } from 'cli-forge';
|
|
36
|
+
*
|
|
37
|
+
* cli('basic-cli').command('hello', {
|
|
38
|
+
* builder: (args) =>
|
|
39
|
+
* args.option('name', {
|
|
40
|
+
* type: 'string',
|
|
41
|
+
* }),
|
|
42
|
+
* handler: (args) => {
|
|
43
|
+
* console.log(`Hello, ${args.name}!`);
|
|
44
|
+
* }).forge();
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
13
47
|
export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
|
|
14
48
|
command<TCommandArgs extends TArgs>(cmd: Command<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
15
49
|
/**
|
|
@@ -29,6 +63,21 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
|
|
|
29
63
|
* @param commands Several commands to register. Can be the result of a call to {@link cli} or a configuration object.
|
|
30
64
|
*/
|
|
31
65
|
commands(...commands: Command[]): CLI<TArgs>;
|
|
66
|
+
/**
|
|
67
|
+
* Enables the ability to run CLI commands that contain subcommands as an interactive shell.
|
|
68
|
+
* This presents as a small shell that only knows the current command and its subcommands.
|
|
69
|
+
* Any flags already consumed by the command will be passed to every subcommand invocation.
|
|
70
|
+
*/
|
|
71
|
+
enableInteractiveShell(): CLI<TArgs>;
|
|
72
|
+
/**
|
|
73
|
+
* Registers a custom global error handler for the CLI. This handler will be called when an error is thrown
|
|
74
|
+
* during the execution of the CLI and not otherwise handled. Error handlers should re-throw the error if they
|
|
75
|
+
* cannot handle it, s.t. the next error handler can attempt to handle it.
|
|
76
|
+
*
|
|
77
|
+
* @param handler Typically called with an Error object, but you should be prepared to handle any type of error.
|
|
78
|
+
* @param actions Actions that can be taken by the error handler. Prefer using these over process.exit for better support of interactive shells.
|
|
79
|
+
*/
|
|
80
|
+
errorHandler(handler: ErrorHandler): CLI<TArgs>;
|
|
32
81
|
/**
|
|
33
82
|
* Registers a new option for the CLI command. This option will be accessible
|
|
34
83
|
* within the command handler, as well as any subcommands.
|
|
@@ -60,12 +109,44 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
|
|
|
60
109
|
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig['items'] extends 'string' ? string : number : never)[];
|
|
61
110
|
}[TOptionConfig['type']];
|
|
62
111
|
}>;
|
|
112
|
+
/**
|
|
113
|
+
* Adds support for reading CLI options from environment variables.
|
|
114
|
+
* @param prefix The prefix to use when looking up environment variables. Defaults to the command name.
|
|
115
|
+
*/
|
|
116
|
+
env(prefix?: string): CLI<TArgs>;
|
|
117
|
+
/**
|
|
118
|
+
* Sets a group of options as mutually exclusive. If more than one option is provided, there will be a validation error.
|
|
119
|
+
* @param options The options that should be mutually exclusive.
|
|
120
|
+
*/
|
|
121
|
+
conflicts(...options: [string, string, ...string[]]): CLI<TArgs>;
|
|
122
|
+
/**
|
|
123
|
+
* Sets a group of options as mutually inclusive. If one option is provided, all other options must also be provided.
|
|
124
|
+
* @param option The option that implies the other options.
|
|
125
|
+
* @param impliedOptions The options which become required when the option is provided.
|
|
126
|
+
*/
|
|
127
|
+
implies(option: string, ...impliedOptions: string[]): CLI<TArgs>;
|
|
63
128
|
/**
|
|
64
129
|
* Requires a command to be provided when executing the CLI. Useful if your parent command
|
|
65
130
|
* cannot be executed on its own.
|
|
66
131
|
* @returns Updated CLI instance.
|
|
67
132
|
*/
|
|
68
133
|
demandCommand(): CLI<TArgs>;
|
|
134
|
+
/**
|
|
135
|
+
* Sets the usage text for the CLI. This text will be displayed in place of the default usage text
|
|
136
|
+
* @param usageText Text displayed in place of the default usage text for `--help` and in generated docs.
|
|
137
|
+
*/
|
|
138
|
+
usage(usageText: string): CLI<TArgs>;
|
|
139
|
+
/**
|
|
140
|
+
* Sets the description for the CLI. This text will be displayed in the help text and generated docs.
|
|
141
|
+
* @param examples Examples to display in the help text and generated docs.
|
|
142
|
+
*/
|
|
143
|
+
examples(...examples: string[]): CLI<TArgs>;
|
|
144
|
+
/**
|
|
145
|
+
* Allows overriding the version displayed when passing `--version`. Defaults to crawling
|
|
146
|
+
* the file system to get the package.json of the currently executing command.
|
|
147
|
+
* @param override
|
|
148
|
+
*/
|
|
149
|
+
version(override?: string): CLI<TArgs>;
|
|
69
150
|
/**
|
|
70
151
|
* Prints help text to stdout.
|
|
71
152
|
*/
|
|
@@ -75,7 +156,7 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
|
|
|
75
156
|
* @param args argv. Defaults to process.argv.slice(2)
|
|
76
157
|
* @returns Promise that resolves when the handler completes.
|
|
77
158
|
*/
|
|
78
|
-
forge(args?: string[]): Promise<
|
|
159
|
+
forge(args?: string[]): Promise<TArgs>;
|
|
79
160
|
}
|
|
80
161
|
/**
|
|
81
162
|
* The base class for a CLI application. This class is used to define the structure of the CLI.
|
|
@@ -98,26 +179,40 @@ export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
|
|
|
98
179
|
*/
|
|
99
180
|
export declare class InternalCLI<TArgs extends ParsedArgs = ParsedArgs> implements CLI<TArgs> {
|
|
100
181
|
name: string;
|
|
101
|
-
|
|
102
|
-
|
|
182
|
+
/**
|
|
183
|
+
* For internal use only. Stick to properties available on {@link CLI}.
|
|
184
|
+
*/
|
|
185
|
+
registeredCommands: Record<string, InternalCLI<any>>;
|
|
186
|
+
/**
|
|
187
|
+
* For internal use only. Stick to properties available on {@link CLI}.
|
|
188
|
+
*/
|
|
189
|
+
commandChain: string[];
|
|
103
190
|
private requiresCommand;
|
|
104
191
|
private _configuration?;
|
|
192
|
+
private _versionOverride?;
|
|
193
|
+
private registeredErrorHandlers;
|
|
194
|
+
get configuration(): CLICommandOptions<any, any> | undefined;
|
|
195
|
+
private set configuration(value);
|
|
105
196
|
private parser;
|
|
106
197
|
/**
|
|
107
198
|
* @param name What should the name of the cli command be?
|
|
108
199
|
* @param configuration Configuration for the current CLI command.
|
|
109
200
|
*/
|
|
110
|
-
constructor(name: string);
|
|
111
|
-
get configuration(): CLICommandOptions<any, any> | undefined;
|
|
112
|
-
private set configuration(value);
|
|
201
|
+
constructor(name: string, rootCommandConfiguration?: CLICommandOptions<TArgs>);
|
|
113
202
|
withRootCommandConfiguration<TRootCommandArgs extends TArgs>(configuration: CLICommandOptions<TArgs, TRootCommandArgs>): InternalCLI<TArgs>;
|
|
114
203
|
command<TCommandArgs extends TArgs>(cmd: Command<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
115
204
|
command<TCommandArgs extends TArgs>(key: string, options: CLICommandOptions<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
116
|
-
commands(commands: Command[]):
|
|
117
|
-
commands(...commands: Command[]):
|
|
205
|
+
commands(commands: Command[]): CLI<TArgs>;
|
|
206
|
+
commands(...commands: Command[]): CLI<TArgs>;
|
|
118
207
|
option<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): any;
|
|
119
208
|
positional<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): any;
|
|
209
|
+
conflicts(...args: [string, string, ...string[]]): CLI<TArgs>;
|
|
210
|
+
implies(option: string, ...impliedOptions: string[]): CLI<TArgs>;
|
|
211
|
+
env(prefix?: string): this;
|
|
120
212
|
demandCommand(): this;
|
|
213
|
+
usage(usageText: string): this;
|
|
214
|
+
examples(...examples: string[]): this;
|
|
215
|
+
version(version?: string): this;
|
|
121
216
|
/**
|
|
122
217
|
* Gets help text for the current command as a string.
|
|
123
218
|
* @returns Help text for the current command.
|
|
@@ -132,15 +227,21 @@ export declare class InternalCLI<TArgs extends ParsedArgs = ParsedArgs> implemen
|
|
|
132
227
|
* @param cmd The command to run.
|
|
133
228
|
* @param args The arguments to pass to the command.
|
|
134
229
|
*/
|
|
135
|
-
runCommand<T extends ParsedArgs>(cmd: InternalCLI<T>, args: T): Promise<void>;
|
|
230
|
+
runCommand<T extends ParsedArgs>(cmd: InternalCLI<T>, args: T, originalArgV: string[]): Promise<void>;
|
|
231
|
+
enableInteractiveShell(): this;
|
|
232
|
+
private versionHandler;
|
|
233
|
+
private withErrorHandlers;
|
|
234
|
+
errorHandler(handler: ErrorHandler): this;
|
|
136
235
|
/**
|
|
137
236
|
* Parses argv and executes the CLI
|
|
138
237
|
* @param args argv. Defaults to process.argv.slice(2)
|
|
139
238
|
* @returns Promise that resolves when the handler completes.
|
|
140
239
|
*/
|
|
141
|
-
forge(args?: string[])
|
|
240
|
+
forge: (args?: string[]) => Promise<Awaited<TArgs>>;
|
|
142
241
|
getParser(): import("@cli-forge/parser").ReadonlyArgvParser<TArgs & {
|
|
143
242
|
help: boolean;
|
|
243
|
+
} & {
|
|
244
|
+
version: boolean;
|
|
144
245
|
}>;
|
|
145
246
|
getSubcommands(): Readonly<Record<string, InternalCLI>>;
|
|
146
247
|
clone(): InternalCLI<TArgs>;
|