cli-forge 0.3.0 → 0.5.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.d.ts +4 -1
- package/bin/cli.js +6 -2
- package/bin/cli.js.map +1 -1
- package/bin/commands/generate-documentation.d.ts +3 -18
- package/bin/commands/generate-documentation.js +75 -38
- package/bin/commands/generate-documentation.js.map +1 -1
- package/bin/commands/init.d.ts +6 -9
- package/bin/commands/init.js +35 -34
- package/bin/commands/init.js.map +1 -1
- package/package.json +2 -2
- package/src/index.d.ts +2 -2
- package/src/index.js +4 -3
- package/src/index.js.map +1 -1
- package/src/lib/cli-forge.d.ts +138 -41
- package/src/lib/cli-forge.js +193 -69
- package/src/lib/cli-forge.js.map +1 -1
- package/src/lib/documentation.d.ts +6 -2
- package/src/lib/documentation.js +12 -2
- package/src/lib/documentation.js.map +1 -1
- 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/bin/cli.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
declare const mycli: import("../src").CLI<
|
|
2
|
+
declare const mycli: import("../src").CLI<{
|
|
3
|
+
unmatched: string[];
|
|
4
|
+
'--'?: string[];
|
|
5
|
+
}>;
|
|
3
6
|
export default mycli;
|
package/bin/cli.js
CHANGED
|
@@ -4,7 +4,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
const src_1 = require("../src");
|
|
5
5
|
const generate_documentation_1 = require("./commands/generate-documentation");
|
|
6
6
|
const init_1 = require("./commands/init");
|
|
7
|
-
const mycli = (0,
|
|
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,
|
|
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"}
|
|
@@ -1,20 +1,6 @@
|
|
|
1
1
|
import type { ParsedArgs } from '@cli-forge/parser';
|
|
2
|
-
import { CLI } from '../../src';
|
|
3
|
-
|
|
4
|
-
cli: string;
|
|
5
|
-
output: string;
|
|
6
|
-
format: string;
|
|
7
|
-
export: string;
|
|
8
|
-
};
|
|
9
|
-
export declare function withGenerateDocumentationArgs<T extends ParsedArgs>(cmd: CLI<T, T & GenerateDocsArgs>): CLI<T & {
|
|
10
|
-
cli: string;
|
|
11
|
-
} & {
|
|
12
|
-
output: string;
|
|
13
|
-
} & {
|
|
14
|
-
format: string;
|
|
15
|
-
} & {
|
|
16
|
-
export: string;
|
|
17
|
-
}, T & {
|
|
2
|
+
import { CLI } from '../../src/lib/cli-forge';
|
|
3
|
+
export declare function withGenerateDocumentationArgs<T extends ParsedArgs>(cmd: CLI<T>): CLI<T & {
|
|
18
4
|
cli: string;
|
|
19
5
|
} & {
|
|
20
6
|
output: string;
|
|
@@ -23,5 +9,4 @@ export declare function withGenerateDocumentationArgs<T extends ParsedArgs>(cmd:
|
|
|
23
9
|
} & {
|
|
24
10
|
export: string;
|
|
25
11
|
}>;
|
|
26
|
-
export declare
|
|
27
|
-
export {};
|
|
12
|
+
export declare const generateDocumentationCommand: CLI;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateDocumentationCommand = void 0;
|
|
3
4
|
exports.withGenerateDocumentationArgs = withGenerateDocumentationArgs;
|
|
4
|
-
exports.withGenerateDocumentation = withGenerateDocumentation;
|
|
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");
|
|
10
|
+
const cli_forge_1 = require("../../src/lib/cli-forge");
|
|
9
11
|
const fs_1 = require("../utils/fs");
|
|
10
12
|
function withGenerateDocumentationArgs(cmd) {
|
|
11
13
|
return cmd
|
|
@@ -22,63 +24,65 @@ function withGenerateDocumentationArgs(cmd) {
|
|
|
22
24
|
})
|
|
23
25
|
.option('format', {
|
|
24
26
|
type: 'string',
|
|
25
|
-
description: 'What format should the documentation be output in?
|
|
27
|
+
description: 'What format should the documentation be output in?',
|
|
26
28
|
default: 'md',
|
|
29
|
+
choices: ['json', 'md'],
|
|
27
30
|
})
|
|
28
31
|
.option('export', {
|
|
29
32
|
type: 'string',
|
|
30
33
|
description: 'The name of the export that contains the CLI instance. By default, docs will be generated for the default export.',
|
|
31
34
|
});
|
|
32
35
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
}
|
|
36
|
+
exports.generateDocumentationCommand = (0, src_1.default)('generate-documentation', {
|
|
37
|
+
description: 'Generate documentation for the given CLI',
|
|
38
|
+
builder: (b) => withGenerateDocumentationArgs(b),
|
|
39
|
+
handler: async (args) => {
|
|
40
|
+
if (args.cli.startsWith('./') || args.cli.startsWith('../')) {
|
|
41
|
+
args.cli = (0, node_path_1.join)(process.cwd(), args.cli);
|
|
42
|
+
}
|
|
43
|
+
const cliModule = await Promise.resolve(`${args.cli}`).then(s => require(s));
|
|
44
|
+
const cli = cliModule[args.export || 'default'] ?? cliModule;
|
|
45
|
+
if (!isCLI(cli)) {
|
|
46
|
+
throw new Error(`${args.cli}${args.export ? '#' + args.export : ''} is not a CLI.`);
|
|
47
|
+
}
|
|
48
|
+
const documentation = (0, documentation_1.generateDocumentation)(cli);
|
|
49
|
+
if (args.format === 'md') {
|
|
50
|
+
await generateMarkdownDocumentation(documentation, args);
|
|
51
|
+
}
|
|
52
|
+
else if (args.format === 'json') {
|
|
53
|
+
const outfile = args.output.endsWith('json')
|
|
54
|
+
? args.output
|
|
55
|
+
: (0, node_path_1.join)(args.output, cli.name + '.json');
|
|
56
|
+
const outdir = (0, node_path_1.dirname)(outfile);
|
|
57
|
+
(0, fs_1.ensureDirSync)(outdir);
|
|
58
|
+
(0, node_fs_1.writeFileSync)(outfile, JSON.stringify(documentation, null, 2));
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
});
|
|
61
62
|
async function generateMarkdownDocumentation(docs, args) {
|
|
62
63
|
const md = await importMarkdownFactory();
|
|
63
|
-
await generateMarkdownForSingleCommand(docs,
|
|
64
|
+
await generateMarkdownForSingleCommand(docs, args.output, args.output, md);
|
|
64
65
|
}
|
|
65
|
-
async function generateMarkdownForSingleCommand(docs, out, md) {
|
|
66
|
+
async function generateMarkdownForSingleCommand(docs, out, docsRoot, md) {
|
|
66
67
|
const subcommands = docs.subcommands;
|
|
67
68
|
const outdir = subcommands.length ? out : (0, node_path_1.dirname)(out);
|
|
68
69
|
const outname = subcommands.length ? 'index' : docs.name;
|
|
69
70
|
(0, fs_1.ensureDirSync)(outdir);
|
|
70
71
|
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(outdir, outname + '.md'), md.h1(docs.name, ...[
|
|
72
|
+
[md.bold('Usage:'), md.code(docs.usage)].join(' '),
|
|
71
73
|
docs.description,
|
|
72
74
|
getPositionalArgsFragment(docs.positionals, md),
|
|
73
75
|
getFlagArgsFragment(docs.options, md),
|
|
74
|
-
getSubcommandsFragment(docs.subcommands, md),
|
|
76
|
+
getSubcommandsFragment(docs.subcommands, outdir, docsRoot, md),
|
|
77
|
+
getExamplesFragment(docs.examples, md),
|
|
75
78
|
].filter(isTruthy)));
|
|
76
79
|
for (const subcommand of docs.subcommands) {
|
|
77
|
-
await generateMarkdownForSingleCommand(subcommand, (0, node_path_1.join)(outdir, subcommand.name), md);
|
|
80
|
+
await generateMarkdownForSingleCommand(subcommand, (0, node_path_1.join)(outdir, subcommand.name), docsRoot, md);
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
function formatOption(option, md) {
|
|
81
|
-
return md.h3(option.key, ...[
|
|
84
|
+
return md.h3(option.deprecated ? md.strikethrough(option.key) : option.key, ...[
|
|
85
|
+
option.deprecated ? md.bold(md.italics('Deprecated')) : undefined,
|
|
82
86
|
md.bold('Type:') +
|
|
83
87
|
' ' +
|
|
84
88
|
(option.type === 'array'
|
|
@@ -86,7 +90,18 @@ function formatOption(option, md) {
|
|
|
86
90
|
: option.type),
|
|
87
91
|
option.description,
|
|
88
92
|
option.default ? md.bold('Default:') + ' ' + option.default : undefined,
|
|
89
|
-
|
|
93
|
+
// No need to show required if it's required and has a default, as its not actually required to pass.
|
|
94
|
+
option.required && !option.default ? md.bold('Required') : undefined,
|
|
95
|
+
option.choices
|
|
96
|
+
? md.bold('Valid values:') +
|
|
97
|
+
' ' +
|
|
98
|
+
(() => {
|
|
99
|
+
const choicesAsString = (typeof option.choices === 'function'
|
|
100
|
+
? option.choices()
|
|
101
|
+
: option.choices).map((t) => t.toString());
|
|
102
|
+
return choicesAsString.join(', ');
|
|
103
|
+
})()
|
|
104
|
+
: undefined,
|
|
90
105
|
option.alias?.length
|
|
91
106
|
? md.h4('Aliases', md.ul(...option.alias))
|
|
92
107
|
: undefined,
|
|
@@ -104,11 +119,12 @@ function getFlagArgsFragment(options, md) {
|
|
|
104
119
|
}
|
|
105
120
|
return md.h2('Flags', ...Object.values(options).map((option) => formatOption(option, md)));
|
|
106
121
|
}
|
|
107
|
-
function getSubcommandsFragment(subcommands, md) {
|
|
122
|
+
function getSubcommandsFragment(subcommands, outdir, docsRoot, md) {
|
|
108
123
|
if (subcommands.length === 0) {
|
|
109
124
|
return undefined;
|
|
110
125
|
}
|
|
111
|
-
return md.h2('Subcommands', ...subcommands.map((subcommand) => md.link(
|
|
126
|
+
return md.h2('Subcommands', ...subcommands.map((subcommand) => md.link('./' +
|
|
127
|
+
(0, posix_1.join)((0, posix_1.normalize)((0, node_path_1.relative)(docsRoot, outdir)), subcommand.name + '.md'), subcommand.name)));
|
|
112
128
|
}
|
|
113
129
|
function isTruthy(value) {
|
|
114
130
|
return !!value;
|
|
@@ -121,4 +137,25 @@ async function importMarkdownFactory() {
|
|
|
121
137
|
throw new Error('Could not find markdown-factory. Please install it to generate markdown documentation.');
|
|
122
138
|
}
|
|
123
139
|
}
|
|
140
|
+
function isCLI(obj) {
|
|
141
|
+
if (obj instanceof cli_forge_1.InternalCLI) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
if (typeof obj !== 'object' || !obj) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (!('constructor' in obj)) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (!('name' in obj.constructor)) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return obj.constructor.name === cli_forge_1.InternalCLI.name;
|
|
154
|
+
}
|
|
155
|
+
function getExamplesFragment(examples, md) {
|
|
156
|
+
if (examples.length === 0) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
return md.h2('Examples', ...examples.map((example) => md.codeBlock(example, 'shell')));
|
|
160
|
+
}
|
|
124
161
|
//# 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.ts"],"names":[],"mappings":";;;AAuBA,sEA0BC;AA/CD,qCAAwC;AACxC,yCAAoD;AACpD,2CAAuE;AAEvE,mCAA4B;AAC5B,+DAGqC;AACrC,uDAA2D;AAC3D,oCAA4C;AAW5C,SAAgB,6BAA6B,CAC3C,GAAW;IAEX,OAAO,GAAG;SACP,UAAU,CAAC,KAAK,EAAE;QACjB,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,oDAAoD;QACjE,QAAQ,EAAE,IAAI;KACf,CAAC;SACD,MAAM,CAAC,QAAQ,EAAE;QAChB,KAAK,EAAE,CAAC,GAAG,CAAC;QACZ,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,2CAA2C;QACxD,OAAO,EAAE,MAAM;KAChB,CAAC;SACD,MAAM,CAAC,QAAQ,EAAE;QAChB,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,oDAAoD;QACjE,OAAO,EAAE,IAAI;QACb,OAAO,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC;KACxB,CAAC;SACD,MAAM,CAAC,QAAQ,EAAE;QAChB,IAAI,EAAE,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,KAAK,QAAQ,IAAI,CAAC,GAAG,EAAE,CAAC;QACpC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,CAAC,CAAC,aAAa,IAAI,GAAG,CAAC,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,CAAC,CAAC,MAAM,IAAI,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;QACjC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,uBAAW,CAAC,IAAI,CAAC;AACnD,CAAC;AAED,SAAS,mBAAmB,CAC1B,QAAkB,EAClB,EAAqC;IAErC,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,EAAE,CAAC,EAAE,CACV,UAAU,EACV,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAC7D,CAAC;AACJ,CAAC"}
|
package/bin/commands/init.d.ts
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
import type { ParsedArgs } from '@cli-forge/parser';
|
|
2
2
|
import { CLI } from '../../src';
|
|
3
|
-
|
|
4
|
-
cliName: string;
|
|
5
|
-
output: string;
|
|
6
|
-
format: string;
|
|
7
|
-
};
|
|
8
|
-
export declare function withInitArgs<T extends ParsedArgs>(cmd: CLI<T, T & InitArgs>): CLI<T & {
|
|
3
|
+
export declare function withInitArgs<T extends ParsedArgs>(cmd: CLI<T>): CLI<T & {
|
|
9
4
|
cliName: string;
|
|
10
5
|
} & {
|
|
11
6
|
output: string;
|
|
12
7
|
} & {
|
|
13
8
|
format: string;
|
|
14
|
-
}
|
|
9
|
+
}>;
|
|
10
|
+
export declare const initCommand: CLI<{
|
|
11
|
+
unmatched: string[];
|
|
12
|
+
'--'?: string[];
|
|
13
|
+
} & {
|
|
15
14
|
cliName: string;
|
|
16
15
|
} & {
|
|
17
16
|
output: string;
|
|
18
17
|
} & {
|
|
19
18
|
format: string;
|
|
20
19
|
}>;
|
|
21
|
-
export declare function withInit<T extends ParsedArgs>(cli: CLI<T>): CLI<T, T>;
|
|
22
|
-
export {};
|
package/bin/commands/init.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initCommand = void 0;
|
|
3
4
|
exports.withInitArgs = withInitArgs;
|
|
4
|
-
|
|
5
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
6
|
const node_fs_1 = require("node:fs");
|
|
6
7
|
const node_path_1 = require("node:path");
|
|
7
|
-
const fs_1 = require("../utils/fs");
|
|
8
8
|
const package_json_1 = require("../../package.json");
|
|
9
|
-
const
|
|
9
|
+
const src_1 = require("../../src");
|
|
10
|
+
// import { CLI } from '../../src/lib/cli-forge';
|
|
11
|
+
const fs_1 = require("../utils/fs");
|
|
10
12
|
function withInitArgs(cmd) {
|
|
11
13
|
return cmd
|
|
12
14
|
.positional('cliName', {
|
|
@@ -22,39 +24,38 @@ function withInitArgs(cmd) {
|
|
|
22
24
|
.option('format', {
|
|
23
25
|
type: 'string',
|
|
24
26
|
default: 'ts',
|
|
25
|
-
description: 'What format should the CLI be in?
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
function withInit(cli) {
|
|
29
|
-
return cli.command('init', {
|
|
30
|
-
description: 'Generate a new CLI',
|
|
31
|
-
builder: withInitArgs,
|
|
32
|
-
handler: async (args) => {
|
|
33
|
-
args.output ??= process.cwd();
|
|
34
|
-
(0, fs_1.ensureDirSync)(args.output);
|
|
35
|
-
const packageJsonPath = (0, node_path_1.join)(args.output, 'package.json');
|
|
36
|
-
const cliPath = (0, node_path_1.join)(args.output, 'bin', `${args.cliName}.${args.format}`);
|
|
37
|
-
const packageJsonContent = readJsonOr(packageJsonPath, { name: args.cliName });
|
|
38
|
-
packageJsonContent.bin ??= {};
|
|
39
|
-
packageJsonContent.bin[args.cliName] = (0, node_path_1.relative)(args.output, cliPath);
|
|
40
|
-
packageJsonContent.dependencies ??= {};
|
|
41
|
-
packageJsonContent.dependencies['cli-forge'] ??= package_json_1.version;
|
|
42
|
-
(0, node_fs_1.writeFileSync)(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
|
|
43
|
-
(0, fs_1.ensureDirSync)((0, node_path_1.dirname)(cliPath));
|
|
44
|
-
(0, node_fs_1.writeFileSync)(cliPath, args.format === 'ts'
|
|
45
|
-
? TS_CLI_CONTENTS(args.cliName)
|
|
46
|
-
: JS_CLI_CONTENTS(args.cliName));
|
|
47
|
-
const installCommand = (0, node_fs_1.existsSync)((0, node_path_1.join)(args.output, 'yarn.lock'))
|
|
48
|
-
? 'yarn'
|
|
49
|
-
: (0, node_fs_1.existsSync)((0, node_path_1.join)(args.output, 'pnpm-lock.yaml'))
|
|
50
|
-
? 'pnpm'
|
|
51
|
-
: (0, node_fs_1.existsSync)((0, node_path_1.join)(args.output, 'bun.lockb'))
|
|
52
|
-
? 'bun'
|
|
53
|
-
: 'npm';
|
|
54
|
-
(0, node_child_process_1.execSync)(`${installCommand} install`);
|
|
55
|
-
},
|
|
27
|
+
description: 'What format should the CLI be in?',
|
|
28
|
+
choices: ['js', 'ts'],
|
|
56
29
|
});
|
|
57
30
|
}
|
|
31
|
+
exports.initCommand = (0, src_1.default)('init', {
|
|
32
|
+
description: 'Generate a new CLI',
|
|
33
|
+
builder: (b) => withInitArgs(b),
|
|
34
|
+
handler: async (args) => {
|
|
35
|
+
args.output ??= process.cwd();
|
|
36
|
+
(0, fs_1.ensureDirSync)(args.output);
|
|
37
|
+
const packageJsonPath = (0, node_path_1.join)(args.output, 'package.json');
|
|
38
|
+
const cliPath = (0, node_path_1.join)(args.output, 'bin', `${args.cliName}.${args.format}`);
|
|
39
|
+
const packageJsonContent = readJsonOr(packageJsonPath, { name: args.cliName });
|
|
40
|
+
packageJsonContent.bin ??= {};
|
|
41
|
+
packageJsonContent.bin[args.cliName] = (0, node_path_1.relative)(args.output, cliPath);
|
|
42
|
+
packageJsonContent.dependencies ??= {};
|
|
43
|
+
packageJsonContent.dependencies['cli-forge'] ??= package_json_1.version;
|
|
44
|
+
(0, node_fs_1.writeFileSync)(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
|
|
45
|
+
(0, fs_1.ensureDirSync)((0, node_path_1.dirname)(cliPath));
|
|
46
|
+
(0, node_fs_1.writeFileSync)(cliPath, args.format === 'ts'
|
|
47
|
+
? TS_CLI_CONTENTS(args.cliName)
|
|
48
|
+
: JS_CLI_CONTENTS(args.cliName));
|
|
49
|
+
const installCommand = (0, node_fs_1.existsSync)((0, node_path_1.join)(args.output, 'yarn.lock'))
|
|
50
|
+
? 'yarn'
|
|
51
|
+
: (0, node_fs_1.existsSync)((0, node_path_1.join)(args.output, 'pnpm-lock.yaml'))
|
|
52
|
+
? 'pnpm'
|
|
53
|
+
: (0, node_fs_1.existsSync)((0, node_path_1.join)(args.output, 'bun.lockb'))
|
|
54
|
+
? 'bun'
|
|
55
|
+
: 'npm';
|
|
56
|
+
(0, node_child_process_1.execSync)(`${installCommand} install`);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
58
59
|
const COMMON_CONTENTS = (name) => `const myCLI = cli('${name}')
|
|
59
60
|
.command('hello', {
|
|
60
61
|
builder: (args) => args.positional('name', {type: 'string'}),
|
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":";;;AAWA,oCAkBC;AA3BD,2DAA8C;AAC9C,qCAAkE;AAClE,yCAAoD;AAEpD,qDAAkE;AAClE,mCAAq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Ae,CAAC,IAAI,CAAC;;;;;;;CAOtB,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC;;EAExC,eAAe,CAAC,IAAI,CAAC;;;;;;;CAOtB,CAAC;AAEF,SAAS,UAAU,CAAI,QAAgB,EAAE,GAAM;IAC7C,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,IAAA,sBAAY,EAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACjD,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC;IACb,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
1
|
+
export { default, cli, CLI, Command } from './lib/cli-forge';
|
|
2
|
+
export { TestHarness } from './lib/test-harness';
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("./lib/cli-forge"), exports);
|
|
3
|
+
exports.TestHarness = exports.cli = exports.default = void 0;
|
|
6
4
|
var cli_forge_1 = require("./lib/cli-forge");
|
|
7
5
|
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return cli_forge_1.default; } });
|
|
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; } });
|
|
8
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":"
|
|
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
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { ArrayOptionConfig, OptionConfig, ParsedArgs } from '@cli-forge/parser';
|
|
2
|
-
export
|
|
2
|
+
export interface CLIHandlerContext {
|
|
3
|
+
command: CLI<any>;
|
|
4
|
+
}
|
|
5
|
+
export type CLICommandOptions<TInitial extends ParsedArgs, TArgs extends TInitial = TInitial> = {
|
|
3
6
|
description?: string;
|
|
4
7
|
builder?: (parser: CLI<TInitial>) => CLI<TArgs>;
|
|
5
|
-
handler
|
|
8
|
+
handler?: (args: TArgs, context: CLIHandlerContext) => void | Promise<void>;
|
|
9
|
+
usage?: string;
|
|
10
|
+
examples?: string[];
|
|
6
11
|
};
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
} ? TArgs : never;
|
|
12
|
+
export type Command<TInitial extends ParsedArgs = any, TArgs extends TInitial = TInitial> = ({
|
|
13
|
+
name: string;
|
|
14
|
+
} & CLICommandOptions<TInitial, TArgs>) | CLI<TArgs>;
|
|
12
15
|
/**
|
|
13
|
-
* The
|
|
16
|
+
* The interface for a CLI application or subcommands.
|
|
14
17
|
*
|
|
15
18
|
* {@link cli} is provided as a small helper function to create a new CLI instance.
|
|
16
19
|
*
|
|
@@ -28,25 +31,25 @@ export type ArgsOf<T extends CLI> = T extends {
|
|
|
28
31
|
* }).forge();
|
|
29
32
|
* ```
|
|
30
33
|
*/
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
configuration?: CLICommandOptions<T, T2> | undefined;
|
|
34
|
-
private commands;
|
|
35
|
-
private commandChain;
|
|
36
|
-
private requiresCommand;
|
|
37
|
-
private parser;
|
|
38
|
-
/**
|
|
39
|
-
* @param name What should the name of the cli command be?
|
|
40
|
-
* @param configuration Configuration for the current CLI command.
|
|
41
|
-
*/
|
|
42
|
-
constructor(name: string, configuration?: CLICommandOptions<T, T2> | undefined);
|
|
34
|
+
export interface CLI<TArgs extends ParsedArgs = ParsedArgs> {
|
|
35
|
+
command<TCommandArgs extends TArgs>(cmd: Command<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
43
36
|
/**
|
|
44
37
|
* Registers a new command with the CLI.
|
|
45
38
|
* @param key What should the new command be called?
|
|
46
39
|
* @param options Settings for the new command. See {@link CLICommandOptions}.
|
|
47
40
|
* @returns Updated CLI instance with the new command registered.
|
|
48
41
|
*/
|
|
49
|
-
command<
|
|
42
|
+
command<TCommandArgs extends TArgs>(key: string, options: CLICommandOptions<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
43
|
+
/**
|
|
44
|
+
* Registers multiple subcommands with the CLI.
|
|
45
|
+
* @param commands Several commands to register. Can be the result of a call to {@link cli} or a configuration object.
|
|
46
|
+
*/
|
|
47
|
+
commands(commands: Command[]): CLI<TArgs>;
|
|
48
|
+
/**
|
|
49
|
+
* Registers multiple subcommands with the CLI.
|
|
50
|
+
* @param commands Several commands to register. Can be the result of a call to {@link cli} or a configuration object.
|
|
51
|
+
*/
|
|
52
|
+
commands(...commands: Command[]): CLI<TArgs>;
|
|
50
53
|
/**
|
|
51
54
|
* Registers a new option for the CLI command. This option will be accessible
|
|
52
55
|
* within the command handler, as well as any subcommands.
|
|
@@ -55,12 +58,14 @@ export declare class CLI<T extends ParsedArgs = ParsedArgs, T2 extends T = T> {
|
|
|
55
58
|
* @param config Configuration for the option. See {@link OptionConfig}.
|
|
56
59
|
* @returns Updated CLI instance with the new option registered.
|
|
57
60
|
*/
|
|
58
|
-
option<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<
|
|
59
|
-
string:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
option<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<TArgs & {
|
|
62
|
+
[key in TOption]: TOptionConfig['coerce'] extends (value: string) => infer TCoerce ? TCoerce : {
|
|
63
|
+
string: string;
|
|
64
|
+
number: number;
|
|
65
|
+
boolean: boolean;
|
|
66
|
+
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig['items'] extends 'string' ? string : number : never)[];
|
|
67
|
+
}[TOptionConfig['type']];
|
|
68
|
+
}>;
|
|
64
69
|
/**
|
|
65
70
|
* Registers a new positional argument for the CLI command. This argument will be accessible
|
|
66
71
|
* within the command handler, as well as any subcommands.
|
|
@@ -68,18 +73,109 @@ export declare class CLI<T extends ParsedArgs = ParsedArgs, T2 extends T = T> {
|
|
|
68
73
|
* @param config Configuration for the positional argument. See {@link OptionConfig}.
|
|
69
74
|
* @returns Updated CLI instance with the new positional argument registered.
|
|
70
75
|
*/
|
|
71
|
-
positional<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<
|
|
72
|
-
string:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
positional<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<TArgs & {
|
|
77
|
+
[key in TOption]: TOptionConfig['coerce'] extends (value: string) => infer TCoerce ? TCoerce : {
|
|
78
|
+
string: string;
|
|
79
|
+
number: number;
|
|
80
|
+
boolean: boolean;
|
|
81
|
+
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig['items'] extends 'string' ? string : number : never)[];
|
|
82
|
+
}[TOptionConfig['type']];
|
|
83
|
+
}>;
|
|
84
|
+
/**
|
|
85
|
+
* Adds support for reading CLI options from environment variables.
|
|
86
|
+
* @param prefix The prefix to use when looking up environment variables. Defaults to the command name.
|
|
87
|
+
*/
|
|
88
|
+
env(prefix?: string): CLI<TArgs>;
|
|
89
|
+
/**
|
|
90
|
+
* Sets a group of options as mutually exclusive. If more than one option is provided, there will be a validation error.
|
|
91
|
+
* @param options The options that should be mutually exclusive.
|
|
92
|
+
*/
|
|
93
|
+
conflicts(...options: [string, string, ...string[]]): CLI<TArgs>;
|
|
94
|
+
/**
|
|
95
|
+
* Sets a group of options as mutually inclusive. If one option is provided, all other options must also be provided.
|
|
96
|
+
* @param option The option that implies the other options.
|
|
97
|
+
* @param impliedOptions The options which become required when the option is provided.
|
|
98
|
+
*/
|
|
99
|
+
implies(option: string, ...impliedOptions: string[]): CLI<TArgs>;
|
|
77
100
|
/**
|
|
78
101
|
* Requires a command to be provided when executing the CLI. Useful if your parent command
|
|
79
102
|
* cannot be executed on its own.
|
|
80
103
|
* @returns Updated CLI instance.
|
|
81
104
|
*/
|
|
105
|
+
demandCommand(): CLI<TArgs>;
|
|
106
|
+
/**
|
|
107
|
+
* Sets the usage text for the CLI. This text will be displayed in place of the default usage text
|
|
108
|
+
* @param usageText Text displayed in place of the default usage text for `--help` and in generated docs.
|
|
109
|
+
*/
|
|
110
|
+
usage(usageText: string): CLI<TArgs>;
|
|
111
|
+
/**
|
|
112
|
+
* Sets the description for the CLI. This text will be displayed in the help text and generated docs.
|
|
113
|
+
* @param examples Examples to display in the help text and generated docs.
|
|
114
|
+
*/
|
|
115
|
+
examples(...examples: string[]): CLI<TArgs>;
|
|
116
|
+
/**
|
|
117
|
+
* Prints help text to stdout.
|
|
118
|
+
*/
|
|
119
|
+
printHelp(): void;
|
|
120
|
+
/**
|
|
121
|
+
* Parses argv and executes the CLI
|
|
122
|
+
* @param args argv. Defaults to process.argv.slice(2)
|
|
123
|
+
* @returns Promise that resolves when the handler completes.
|
|
124
|
+
*/
|
|
125
|
+
forge(args?: string[]): Promise<TArgs>;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The base class for a CLI application. This class is used to define the structure of the CLI.
|
|
129
|
+
*
|
|
130
|
+
* {@link cli} is provided as a small helper function to create a new CLI instance.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* import { cli } from 'cli-forge';
|
|
135
|
+
*
|
|
136
|
+
* cli('basic-cli').command('hello', {
|
|
137
|
+
* builder: (args) =>
|
|
138
|
+
* args.option('name', {
|
|
139
|
+
* type: 'string',
|
|
140
|
+
* }),
|
|
141
|
+
* handler: (args) => {
|
|
142
|
+
* console.log(`Hello, ${args.name}!`);
|
|
143
|
+
* }).forge();
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export declare class InternalCLI<TArgs extends ParsedArgs = ParsedArgs> implements CLI<TArgs> {
|
|
147
|
+
name: string;
|
|
148
|
+
/**
|
|
149
|
+
* For internal use only. Stick to properties available on {@link CLI}.
|
|
150
|
+
*/
|
|
151
|
+
registeredCommands: Record<string, InternalCLI<any>>;
|
|
152
|
+
/**
|
|
153
|
+
* For internal use only. Stick to properties available on {@link CLI}.
|
|
154
|
+
*/
|
|
155
|
+
commandChain: string[];
|
|
156
|
+
private requiresCommand;
|
|
157
|
+
private _configuration?;
|
|
158
|
+
get configuration(): CLICommandOptions<any, any> | undefined;
|
|
159
|
+
private set configuration(value);
|
|
160
|
+
private parser;
|
|
161
|
+
/**
|
|
162
|
+
* @param name What should the name of the cli command be?
|
|
163
|
+
* @param configuration Configuration for the current CLI command.
|
|
164
|
+
*/
|
|
165
|
+
constructor(name: string);
|
|
166
|
+
withRootCommandConfiguration<TRootCommandArgs extends TArgs>(configuration: CLICommandOptions<TArgs, TRootCommandArgs>): InternalCLI<TArgs>;
|
|
167
|
+
command<TCommandArgs extends TArgs>(cmd: Command<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
168
|
+
command<TCommandArgs extends TArgs>(key: string, options: CLICommandOptions<TArgs, TCommandArgs>): CLI<TArgs>;
|
|
169
|
+
commands(commands: Command[]): CLI<TArgs>;
|
|
170
|
+
commands(...commands: Command[]): CLI<TArgs>;
|
|
171
|
+
option<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): any;
|
|
172
|
+
positional<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): any;
|
|
173
|
+
conflicts(...args: [string, string, ...string[]]): CLI<TArgs>;
|
|
174
|
+
implies(option: string, ...impliedOptions: string[]): CLI<TArgs>;
|
|
175
|
+
env(prefix?: string): this;
|
|
82
176
|
demandCommand(): this;
|
|
177
|
+
usage(usageText: string): this;
|
|
178
|
+
examples(...examples: string[]): this;
|
|
83
179
|
/**
|
|
84
180
|
* Gets help text for the current command as a string.
|
|
85
181
|
* @returns Help text for the current command.
|
|
@@ -94,23 +190,24 @@ export declare class CLI<T extends ParsedArgs = ParsedArgs, T2 extends T = T> {
|
|
|
94
190
|
* @param cmd The command to run.
|
|
95
191
|
* @param args The arguments to pass to the command.
|
|
96
192
|
*/
|
|
97
|
-
runCommand<T extends ParsedArgs>(cmd:
|
|
193
|
+
runCommand<T extends ParsedArgs>(cmd: InternalCLI<T>, args: T): Promise<void>;
|
|
98
194
|
/**
|
|
99
195
|
* Parses argv and executes the CLI
|
|
100
196
|
* @param args argv. Defaults to process.argv.slice(2)
|
|
101
197
|
* @returns Promise that resolves when the handler completes.
|
|
102
198
|
*/
|
|
103
|
-
forge(args?: string[]): Promise<
|
|
104
|
-
getParser(): import("@cli-forge/parser").ReadonlyArgvParser<
|
|
199
|
+
forge(args?: string[]): Promise<TArgs>;
|
|
200
|
+
getParser(): import("@cli-forge/parser").ReadonlyArgvParser<TArgs & {
|
|
105
201
|
help: boolean;
|
|
106
202
|
}>;
|
|
107
|
-
getSubcommands(): Readonly<Record<string,
|
|
108
|
-
clone():
|
|
203
|
+
getSubcommands(): Readonly<Record<string, InternalCLI>>;
|
|
204
|
+
clone(): InternalCLI<TArgs>;
|
|
109
205
|
}
|
|
110
206
|
/**
|
|
111
|
-
* Constructs a CLI instance. See {@link
|
|
207
|
+
* Constructs a CLI instance. See {@link InternalCLI} for more information.
|
|
112
208
|
* @param name Name for the top level CLI
|
|
113
|
-
* @
|
|
209
|
+
* @param rootCommandConfiguration Configuration used when running the bare CLI. e.g. npx my-cli, rather than npx my-cli [cmd]
|
|
210
|
+
* @returns A {@link InternalCLI} instance.
|
|
114
211
|
*/
|
|
115
|
-
export declare function cli(name: string): CLI<
|
|
212
|
+
export declare function cli<TArgs extends ParsedArgs>(name: string, rootCommandConfiguration?: CLICommandOptions<ParsedArgs, TArgs>): CLI<TArgs>;
|
|
116
213
|
export default cli;
|
package/src/lib/cli-forge.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.InternalCLI = void 0;
|
|
4
4
|
exports.cli = cli;
|
|
5
5
|
const parser_1 = require("@cli-forge/parser");
|
|
6
6
|
/**
|
|
@@ -22,20 +22,32 @@ const parser_1 = require("@cli-forge/parser");
|
|
|
22
22
|
* }).forge();
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
|
-
class
|
|
25
|
+
class InternalCLI {
|
|
26
26
|
name;
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* For internal use only. Stick to properties available on {@link CLI}.
|
|
29
|
+
*/
|
|
30
|
+
registeredCommands = {};
|
|
31
|
+
/**
|
|
32
|
+
* For internal use only. Stick to properties available on {@link CLI}.
|
|
33
|
+
*/
|
|
29
34
|
commandChain = [];
|
|
30
35
|
requiresCommand = false;
|
|
36
|
+
_configuration;
|
|
37
|
+
get configuration() {
|
|
38
|
+
return this._configuration;
|
|
39
|
+
}
|
|
40
|
+
set configuration(value) {
|
|
41
|
+
this._configuration = value;
|
|
42
|
+
}
|
|
31
43
|
parser = new parser_1.ArgvParser({
|
|
32
44
|
unmatchedParser: (arg) => {
|
|
33
45
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
34
46
|
let currentCommand = this;
|
|
35
47
|
for (const command of this.commandChain) {
|
|
36
|
-
currentCommand = currentCommand.
|
|
48
|
+
currentCommand = currentCommand.registeredCommands[command];
|
|
37
49
|
}
|
|
38
|
-
const command = currentCommand.
|
|
50
|
+
const command = currentCommand.registeredCommands[arg];
|
|
39
51
|
if (command && command.configuration) {
|
|
40
52
|
command.configuration.builder?.(command);
|
|
41
53
|
this.commandChain.push(arg);
|
|
@@ -52,61 +64,95 @@ class CLI {
|
|
|
52
64
|
* @param name What should the name of the cli command be?
|
|
53
65
|
* @param configuration Configuration for the current CLI command.
|
|
54
66
|
*/
|
|
55
|
-
constructor(name
|
|
67
|
+
constructor(name) {
|
|
56
68
|
this.name = name;
|
|
69
|
+
}
|
|
70
|
+
withRootCommandConfiguration(configuration) {
|
|
57
71
|
this.configuration = configuration;
|
|
72
|
+
this.requiresCommand = false;
|
|
73
|
+
return this;
|
|
58
74
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
command(keyOrCommand, options) {
|
|
76
|
+
if (typeof keyOrCommand === 'string') {
|
|
77
|
+
const key = keyOrCommand;
|
|
78
|
+
if (!options) {
|
|
79
|
+
throw new Error('options must be provided when calling `command` with a string');
|
|
80
|
+
}
|
|
81
|
+
if (key === '$0') {
|
|
82
|
+
this.configuration = {
|
|
83
|
+
...this.configuration,
|
|
84
|
+
builder: options.builder,
|
|
85
|
+
handler: options.handler,
|
|
86
|
+
description: options.description,
|
|
87
|
+
};
|
|
88
|
+
this.requiresCommand = false;
|
|
89
|
+
}
|
|
90
|
+
this.registeredCommands[key] = new InternalCLI(key).withRootCommandConfiguration(options);
|
|
91
|
+
this.registeredCommands[key].parser = this.parser;
|
|
92
|
+
}
|
|
93
|
+
else if (keyOrCommand instanceof InternalCLI) {
|
|
94
|
+
const cmd = keyOrCommand;
|
|
95
|
+
this.registeredCommands[cmd.name] = cmd;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const { name, ...configuration } = keyOrCommand;
|
|
99
|
+
this.command(name, configuration);
|
|
100
|
+
}
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
commands(...a0) {
|
|
104
|
+
const commands = a0.flat();
|
|
105
|
+
for (const val of commands) {
|
|
106
|
+
if (val instanceof InternalCLI) {
|
|
107
|
+
this.registeredCommands[val.name] = val;
|
|
108
|
+
// Include any options that were defined via cli(...).option() instead of via builder
|
|
109
|
+
this.parser.augment(val.parser);
|
|
110
|
+
val.parser = this.parser;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const { name, ...configuration } = val;
|
|
114
|
+
this.command(name, configuration);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
76
117
|
return this;
|
|
77
118
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Registers a new option for the CLI command. This option will be accessible
|
|
80
|
-
* within the command handler, as well as any subcommands.
|
|
81
|
-
*
|
|
82
|
-
* @param name The name of the option.
|
|
83
|
-
* @param config Configuration for the option. See {@link OptionConfig}.
|
|
84
|
-
* @returns Updated CLI instance with the new option registered.
|
|
85
|
-
*/
|
|
86
119
|
option(name, config) {
|
|
87
120
|
this.parser.option(name, config);
|
|
121
|
+
// Interface modifies the return type to reflect new params, cast is necessay.... I think 🤔
|
|
88
122
|
return this;
|
|
89
123
|
}
|
|
90
|
-
/**
|
|
91
|
-
* Registers a new positional argument for the CLI command. This argument will be accessible
|
|
92
|
-
* within the command handler, as well as any subcommands.
|
|
93
|
-
* @param name The name of the positional argument.
|
|
94
|
-
* @param config Configuration for the positional argument. See {@link OptionConfig}.
|
|
95
|
-
* @returns Updated CLI instance with the new positional argument registered.
|
|
96
|
-
*/
|
|
97
124
|
positional(name, config) {
|
|
98
125
|
this.parser.positional(name, config);
|
|
126
|
+
// Interface modifies the return type to reflect new params, cast is necessay.... I think 🤔
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
conflicts(...args) {
|
|
130
|
+
this.parser.conflicts(...args);
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
implies(option, ...impliedOptions) {
|
|
134
|
+
this.parser.implies(option, ...impliedOptions);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
env(prefix = (0, parser_1.fromCamelOrDashedCaseToConstCase)(this.name)) {
|
|
138
|
+
this.parser.env(prefix);
|
|
99
139
|
return this;
|
|
100
140
|
}
|
|
101
|
-
/**
|
|
102
|
-
* Requires a command to be provided when executing the CLI. Useful if your parent command
|
|
103
|
-
* cannot be executed on its own.
|
|
104
|
-
* @returns Updated CLI instance.
|
|
105
|
-
*/
|
|
106
141
|
demandCommand() {
|
|
107
142
|
this.requiresCommand = true;
|
|
108
143
|
return this;
|
|
109
144
|
}
|
|
145
|
+
usage(usageText) {
|
|
146
|
+
this.configuration ??= {};
|
|
147
|
+
this.configuration.usage = usageText;
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
examples(...examples) {
|
|
151
|
+
this.configuration ??= {};
|
|
152
|
+
this.configuration.examples ??= [];
|
|
153
|
+
this.configuration.examples.push(...examples);
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
110
156
|
/**
|
|
111
157
|
* Gets help text for the current command as a string.
|
|
112
158
|
* @returns Help text for the current command.
|
|
@@ -116,22 +162,24 @@ class CLI {
|
|
|
116
162
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
117
163
|
let command = this;
|
|
118
164
|
for (const key of this.commandChain) {
|
|
119
|
-
command = command.
|
|
165
|
+
command = command.registeredCommands[key];
|
|
120
166
|
}
|
|
121
|
-
help.push(`Usage: ${
|
|
122
|
-
this.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
167
|
+
help.push(`Usage: ${this.configuration?.usage
|
|
168
|
+
? this.configuration.usage
|
|
169
|
+
: [
|
|
170
|
+
this.name,
|
|
171
|
+
...this.commandChain,
|
|
172
|
+
...command.parser.configuredPositionals.map((p) => p.required ? `<${p.key}>` : `[${p.key}]`),
|
|
173
|
+
].join(' ')}`);
|
|
126
174
|
if (command.configuration?.description) {
|
|
127
175
|
help.push(command.configuration.description);
|
|
128
176
|
}
|
|
129
|
-
if (Object.keys(command.
|
|
177
|
+
if (Object.keys(command.registeredCommands).length > 0) {
|
|
130
178
|
help.push('');
|
|
131
179
|
help.push('Commands:');
|
|
132
180
|
}
|
|
133
|
-
for (const key in command.
|
|
134
|
-
const subcommand = command.
|
|
181
|
+
for (const key in command.registeredCommands) {
|
|
182
|
+
const subcommand = command.registeredCommands[key];
|
|
135
183
|
help.push(` ${key}${subcommand.configuration?.description
|
|
136
184
|
? ' - ' + subcommand.configuration.description
|
|
137
185
|
: ''}`);
|
|
@@ -140,11 +188,55 @@ class CLI {
|
|
|
140
188
|
help.push('');
|
|
141
189
|
help.push('Options:');
|
|
142
190
|
}
|
|
191
|
+
function getOptionParts(key, option) {
|
|
192
|
+
const parts = [];
|
|
193
|
+
if (option.description) {
|
|
194
|
+
parts.push(option.description);
|
|
195
|
+
}
|
|
196
|
+
if (option.choices) {
|
|
197
|
+
const choices = typeof option.choices === 'function'
|
|
198
|
+
? option.choices()
|
|
199
|
+
: option.choices;
|
|
200
|
+
parts.push(`(${choices.join(', ')})`);
|
|
201
|
+
}
|
|
202
|
+
if (option.default) {
|
|
203
|
+
parts.push('[default: ' + option.default + ']');
|
|
204
|
+
}
|
|
205
|
+
else if (option.required) {
|
|
206
|
+
parts.push('[required]');
|
|
207
|
+
}
|
|
208
|
+
if (option.deprecated) {
|
|
209
|
+
parts.push('[deprecated: ' + option.deprecated + ']');
|
|
210
|
+
}
|
|
211
|
+
return parts;
|
|
212
|
+
}
|
|
213
|
+
const allParts = [];
|
|
143
214
|
for (const key in this.parser.configuredOptions) {
|
|
144
215
|
const option = this.parser.configuredOptions[key];
|
|
145
|
-
|
|
216
|
+
allParts.push([key, ...getOptionParts(key, option)]);
|
|
217
|
+
}
|
|
218
|
+
const paddingValues = [];
|
|
219
|
+
for (let i = 0; i < allParts.length; i++) {
|
|
220
|
+
for (let j = 0; j < allParts[i].length; j++) {
|
|
221
|
+
if (!paddingValues[j]) {
|
|
222
|
+
paddingValues[j] = 0;
|
|
223
|
+
}
|
|
224
|
+
paddingValues[j] = Math.max(paddingValues[j], allParts[i][j].length);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
for (const [key, ...parts] of allParts) {
|
|
228
|
+
help.push(` --${key.padEnd(paddingValues[0])}${parts.length ? ' - ' : ''}${parts
|
|
229
|
+
.map((part, i) => part.padEnd(paddingValues[i + 1]))
|
|
230
|
+
.join(' ')}`);
|
|
146
231
|
}
|
|
147
|
-
if (
|
|
232
|
+
if (command.configuration?.examples?.length) {
|
|
233
|
+
help.push('');
|
|
234
|
+
help.push('Examples:');
|
|
235
|
+
for (const example of command.configuration.examples) {
|
|
236
|
+
help.push(` \`${example}\``);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (Object.keys(command.registeredCommands).length > 0) {
|
|
148
240
|
help.push(' ');
|
|
149
241
|
help.push(`Run \`${[this.name, ...this.commandChain].join(' ')} [command] --help\` for more information on a command`);
|
|
150
242
|
}
|
|
@@ -167,7 +259,9 @@ class CLI {
|
|
|
167
259
|
throw new Error(`${[this.name, ...this.commandChain].join(' ')} requires a command`);
|
|
168
260
|
}
|
|
169
261
|
if (cmd.configuration?.handler) {
|
|
170
|
-
await cmd.configuration.handler(args
|
|
262
|
+
await cmd.configuration.handler(args, {
|
|
263
|
+
command: cmd,
|
|
264
|
+
});
|
|
171
265
|
}
|
|
172
266
|
else {
|
|
173
267
|
throw new Error(`${[this.name, ...this.commandChain].join(' ')} is not implemented.`);
|
|
@@ -184,48 +278,78 @@ class CLI {
|
|
|
184
278
|
* @param args argv. Defaults to process.argv.slice(2)
|
|
185
279
|
* @returns Promise that resolves when the handler completes.
|
|
186
280
|
*/
|
|
187
|
-
async forge(args = process.argv
|
|
281
|
+
async forge(args = (0, parser_1.hideBin)(process.argv)) {
|
|
188
282
|
// Parsing the args does two things:
|
|
189
283
|
// - builds argv to pass to handler
|
|
190
284
|
// - fills the command chain + registers commands
|
|
191
|
-
|
|
285
|
+
let argv;
|
|
286
|
+
let validationFailedError;
|
|
287
|
+
try {
|
|
288
|
+
argv = this.parser.parse(args);
|
|
289
|
+
}
|
|
290
|
+
catch (e) {
|
|
291
|
+
if (e instanceof parser_1.ValidationFailedError) {
|
|
292
|
+
argv = e.partialArgV;
|
|
293
|
+
validationFailedError = e;
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
throw e;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
192
299
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
193
300
|
let currentCommand = this;
|
|
194
301
|
for (const command of this.commandChain) {
|
|
195
|
-
currentCommand = currentCommand.
|
|
302
|
+
currentCommand = currentCommand.registeredCommands[command];
|
|
196
303
|
}
|
|
197
304
|
if (argv.help) {
|
|
198
305
|
this.printHelp();
|
|
199
|
-
return;
|
|
306
|
+
return argv;
|
|
307
|
+
}
|
|
308
|
+
else if (validationFailedError) {
|
|
309
|
+
this.printHelp();
|
|
310
|
+
throw validationFailedError;
|
|
200
311
|
}
|
|
201
312
|
const finalArgV = currentCommand === this
|
|
202
|
-
? (this.configuration?.builder?.(this)
|
|
313
|
+
? (this.configuration?.builder?.(this)
|
|
314
|
+
?.parser ?? this.parser).parse(args)
|
|
203
315
|
: argv;
|
|
204
316
|
await this.runCommand(currentCommand, finalArgV);
|
|
317
|
+
return finalArgV;
|
|
205
318
|
}
|
|
206
319
|
getParser() {
|
|
207
320
|
return this.parser.asReadonly();
|
|
208
321
|
}
|
|
209
322
|
getSubcommands() {
|
|
210
|
-
return this.
|
|
323
|
+
return this.registeredCommands;
|
|
211
324
|
}
|
|
212
325
|
clone() {
|
|
213
|
-
const clone = new
|
|
214
|
-
|
|
326
|
+
const clone = new InternalCLI(this.name);
|
|
327
|
+
if (this.configuration) {
|
|
328
|
+
clone.withRootCommandConfiguration(this.configuration);
|
|
329
|
+
}
|
|
330
|
+
clone.registeredCommands = { ...this.registeredCommands };
|
|
215
331
|
clone.commandChain = [...this.commandChain];
|
|
216
332
|
clone.requiresCommand = this.requiresCommand;
|
|
217
333
|
clone.parser = this.parser.clone();
|
|
218
334
|
return clone;
|
|
219
335
|
}
|
|
220
336
|
}
|
|
221
|
-
exports.
|
|
337
|
+
exports.InternalCLI = InternalCLI;
|
|
222
338
|
/**
|
|
223
|
-
* Constructs a CLI instance. See {@link
|
|
339
|
+
* Constructs a CLI instance. See {@link InternalCLI} for more information.
|
|
224
340
|
* @param name Name for the top level CLI
|
|
225
|
-
* @
|
|
341
|
+
* @param rootCommandConfiguration Configuration used when running the bare CLI. e.g. npx my-cli, rather than npx my-cli [cmd]
|
|
342
|
+
* @returns A {@link InternalCLI} instance.
|
|
226
343
|
*/
|
|
227
|
-
function cli(name) {
|
|
228
|
-
|
|
344
|
+
function cli(name, rootCommandConfiguration) {
|
|
345
|
+
const cli = new InternalCLI(name);
|
|
346
|
+
if (rootCommandConfiguration) {
|
|
347
|
+
cli.withRootCommandConfiguration(rootCommandConfiguration);
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
cli.demandCommand();
|
|
351
|
+
}
|
|
352
|
+
return cli;
|
|
229
353
|
}
|
|
230
354
|
exports.default = cli;
|
|
231
355
|
//# sourceMappingURL=cli-forge.js.map
|
package/src/lib/cli-forge.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli-forge.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/cli-forge.ts"],"names":[],"mappings":";;;AA4TA,kBAEC;AA9TD,8CAK2B;AAiB3B;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAa,GAAG;IA8BL;IACA;IA9BD,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from '@cli-forge/parser';
|
|
2
|
-
import {
|
|
2
|
+
import { InternalCLI } from './cli-forge';
|
|
3
3
|
export type Documentation = {
|
|
4
4
|
name: string;
|
|
5
5
|
description?: string;
|
|
6
|
+
usage: string;
|
|
7
|
+
examples: string[];
|
|
6
8
|
options: Readonly<Record<string, OptionConfig & {
|
|
7
9
|
key: string;
|
|
8
10
|
}>>;
|
|
@@ -11,9 +13,11 @@ export type Documentation = {
|
|
|
11
13
|
}>[];
|
|
12
14
|
subcommands: Documentation[];
|
|
13
15
|
};
|
|
14
|
-
export declare function generateDocumentation(cli:
|
|
16
|
+
export declare function generateDocumentation(cli: InternalCLI, commandChain?: string[]): {
|
|
15
17
|
name: string;
|
|
16
18
|
description: string | undefined;
|
|
19
|
+
usage: string;
|
|
20
|
+
examples: string[];
|
|
17
21
|
options: Readonly<{
|
|
18
22
|
unmatched: OptionConfig<any> & {
|
|
19
23
|
key: string;
|
package/src/lib/documentation.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateDocumentation = generateDocumentation;
|
|
4
|
-
function generateDocumentation(cli) {
|
|
4
|
+
function generateDocumentation(cli, commandChain = []) {
|
|
5
5
|
// Ensure current command's options are built.
|
|
6
6
|
if (cli.configuration?.builder) {
|
|
7
7
|
cli.configuration.builder(cli);
|
|
@@ -9,10 +9,20 @@ function generateDocumentation(cli) {
|
|
|
9
9
|
const parser = cli.getParser();
|
|
10
10
|
const options = parser.configuredOptions;
|
|
11
11
|
const positionals = parser.configuredPositionals;
|
|
12
|
-
const subcommands = Object.values(cli.getSubcommands()).map((cmd) => generateDocumentation(cmd.clone()));
|
|
12
|
+
const subcommands = Object.values(cli.getSubcommands()).map((cmd) => generateDocumentation(cmd.clone(), [...commandChain, cli.name]));
|
|
13
13
|
return {
|
|
14
14
|
name: cli.name,
|
|
15
15
|
description: cli.configuration?.description,
|
|
16
|
+
usage: cli.configuration?.usage
|
|
17
|
+
? commandChain.length
|
|
18
|
+
? [...commandChain, cli.configuration.usage].join(' ')
|
|
19
|
+
: cli.configuration?.usage
|
|
20
|
+
: [
|
|
21
|
+
...commandChain,
|
|
22
|
+
cli.name,
|
|
23
|
+
...positionals.map((p) => (p.required ? `<${p.key}>` : `[${p.key}]`)),
|
|
24
|
+
].join(' '),
|
|
25
|
+
examples: cli.configuration?.examples ?? [],
|
|
16
26
|
options,
|
|
17
27
|
positionals,
|
|
18
28
|
subcommands,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentation.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/documentation.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"documentation.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/documentation.ts"],"names":[],"mappings":";;AAaA,sDAiCC;AAjCD,SAAgB,qBAAqB,CACnC,GAAgB,EAChB,eAAyB,EAAE;IAE3B,8CAA8C;IAC9C,IAAI,GAAG,CAAC,aAAa,EAAE,OAAO,EAAE,CAAC;QAC/B,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;IACD,MAAM,MAAM,GAAG,GAAG,CAAC,SAAS,EAAE,CAAC;IAE/B,MAAM,OAAO,GAAG,MAAM,CAAC,iBAAiB,CAAC;IACzC,MAAM,WAAW,GAAG,MAAM,CAAC,qBAAqB,CAAC;IACjD,MAAM,WAAW,GAAoB,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC,CAAC,GAAG,CAC1E,CAAC,GAAG,EAAE,EAAE,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,YAAY,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CACzE,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,WAAW,EAAE,GAAG,CAAC,aAAa,EAAE,WAAW;QAC3C,KAAK,EAAE,GAAG,CAAC,aAAa,EAAE,KAAK;YAC7B,CAAC,CAAC,YAAY,CAAC,MAAM;gBACnB,CAAC,CAAC,CAAC,GAAG,YAAY,EAAE,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;gBACtD,CAAC,CAAC,GAAG,CAAC,aAAa,EAAE,KAAK;YAC5B,CAAC,CAAC;gBACE,GAAG,YAAY;gBACf,GAAG,CAAC,IAAI;gBACR,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;aACtE,CAAC,IAAI,CAAC,GAAG,CAAC;QACf,QAAQ,EAAE,GAAG,CAAC,aAAa,EAAE,QAAQ,IAAI,EAAE;QAC3C,OAAO;QACP,WAAW;QACX,WAAW;KACZ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ParsedArgs } from '@cli-forge/parser';
|
|
2
|
+
import { CLI } from './cli-forge';
|
|
3
|
+
export type TestHarnessParseResult<T extends ParsedArgs> = {
|
|
4
|
+
/**
|
|
5
|
+
* Parsed arguments. Note the the typing of this is based on the CLI typings,
|
|
6
|
+
* but no runtime validation outside of the configured validation checks on
|
|
7
|
+
* individual options will be performed. If you want to validate the arguments,
|
|
8
|
+
* you should do so in your test or configure a `validate` callback for the option.
|
|
9
|
+
*/
|
|
10
|
+
args: T;
|
|
11
|
+
/**
|
|
12
|
+
* The command chain that was resolved during parsing. This is used for testing
|
|
13
|
+
* that the correct command is ran when resolving a subcommand. A test that checks
|
|
14
|
+
* this may look like:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const harness = new TestHarness(cli);
|
|
18
|
+
* const { args, commandChain } = await harness.parse(['hello', '--name=sir']);
|
|
19
|
+
* expect(commandChain).toEqual(['hello']);
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The above test would check that the `hello` command was resolved when parsing
|
|
23
|
+
* the argstring, and since only one command's handler will ever be called, this
|
|
24
|
+
* can be used to ensure that the correct command is ran.
|
|
25
|
+
*/
|
|
26
|
+
commandChain: string[];
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Utility for testing CLI instances. Can check argument parsing and validation, including
|
|
30
|
+
* command chain resolution.
|
|
31
|
+
*/
|
|
32
|
+
export declare class TestHarness<T extends ParsedArgs> {
|
|
33
|
+
private cli;
|
|
34
|
+
constructor(cli: CLI<T>);
|
|
35
|
+
parse(args: string[]): Promise<TestHarnessParseResult<T>>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TestHarness = void 0;
|
|
4
|
+
const cli_forge_1 = require("./cli-forge");
|
|
5
|
+
/**
|
|
6
|
+
* Utility for testing CLI instances. Can check argument parsing and validation, including
|
|
7
|
+
* command chain resolution.
|
|
8
|
+
*/
|
|
9
|
+
class TestHarness {
|
|
10
|
+
cli;
|
|
11
|
+
constructor(cli) {
|
|
12
|
+
if (cli instanceof cli_forge_1.InternalCLI) {
|
|
13
|
+
this.cli = cli;
|
|
14
|
+
mockHandler(cli);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
throw new Error('TestHarness can only be used with CLI instances created by `cli`.');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async parse(args) {
|
|
21
|
+
const argv = await this.cli.forge(args);
|
|
22
|
+
return {
|
|
23
|
+
args: argv,
|
|
24
|
+
commandChain: this.cli.commandChain,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.TestHarness = TestHarness;
|
|
29
|
+
function mockHandler(cli) {
|
|
30
|
+
if (cli.configuration?.handler) {
|
|
31
|
+
cli.configuration.handler = () => { };
|
|
32
|
+
}
|
|
33
|
+
for (const command in cli.registeredCommands) {
|
|
34
|
+
mockHandler(cli.registeredCommands[command]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=test-harness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-harness.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/test-harness.ts"],"names":[],"mappings":";;;AACA,2CAA+C;AA6B/C;;;GAGG;AACH,MAAa,WAAW;IACd,GAAG,CAAiB;IAE5B,YAAY,GAAW;QACrB,IAAI,GAAG,YAAY,uBAAW,EAAE,CAAC;YAC/B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;YACf,WAAW,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CACb,mEAAmE,CACpE,CAAC;QACJ,CAAC;IACH,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAAc;QACxB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAExC,OAAO;YACL,IAAI,EAAE,IAAI;YACV,YAAY,EAAE,IAAI,CAAC,GAAG,CAAC,YAAY;SACpC,CAAC;IACJ,CAAC;CACF;AAtBD,kCAsBC;AAED,SAAS,WAAW,CAAC,GAAgB;IACnC,IAAI,GAAG,CAAC,aAAa,EAAE,OAAO,EAAE,CAAC;QAC/B,GAAG,CAAC,aAAa,CAAC,OAAO,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;IACvC,CAAC;IACD,KAAK,MAAM,OAAO,IAAI,GAAG,CAAC,kBAAkB,EAAE,CAAC;QAC7C,WAAW,CAAC,GAAG,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC"}
|