cli-forge 0.1.0 → 0.3.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 +3 -0
- package/bin/cli.js +10 -0
- package/bin/cli.js.map +1 -0
- package/bin/commands/generate-documentation.d.ts +27 -0
- package/bin/commands/generate-documentation.js +124 -0
- package/bin/commands/generate-documentation.js.map +1 -0
- package/bin/commands/init.d.ts +22 -0
- package/bin/commands/init.js +94 -0
- package/bin/commands/init.js.map +1 -0
- package/bin/utils/fs.d.ts +1 -0
- package/bin/utils/fs.js +15 -0
- package/bin/utils/fs.js.map +1 -0
- package/package.json +23 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +3 -0
- package/src/index.js.map +1 -1
- package/src/lib/cli-forge.d.ts +88 -7
- package/src/lib/cli-forge.js +158 -61
- package/src/lib/cli-forge.js.map +1 -1
- package/src/lib/documentation.d.ts +36 -0
- package/src/lib/documentation.js +21 -0
- package/src/lib/documentation.js.map +1 -0
package/bin/cli.d.ts
ADDED
package/bin/cli.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const src_1 = require("../src");
|
|
5
|
+
const generate_documentation_1 = require("./commands/generate-documentation");
|
|
6
|
+
const init_1 = require("./commands/init");
|
|
7
|
+
const mycli = (0, init_1.withInit)((0, generate_documentation_1.withGenerateDocumentation)((0, src_1.cli)('cli-forge')));
|
|
8
|
+
exports.default = mycli;
|
|
9
|
+
mycli.forge();
|
|
10
|
+
//# sourceMappingURL=cli.js.map
|
package/bin/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../../packages/cli-forge/bin/cli.ts"],"names":[],"mappings":";;;AAEA,gCAA6B;AAC7B,8EAA8E;AAC9E,0CAA2C;AAE3C,MAAM,KAAK,GAAG,IAAA,eAAQ,EAAC,IAAA,kDAAyB,EAAC,IAAA,SAAG,EAAC,WAAW,CAAC,CAAC,CAAC,CAAC;AAEpE,kBAAe,KAAK,CAAC;AAErB,KAAK,CAAC,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ParsedArgs } from '@cli-forge/parser';
|
|
2
|
+
import { CLI } from '../../src';
|
|
3
|
+
type GenerateDocsArgs = {
|
|
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 & {
|
|
18
|
+
cli: string;
|
|
19
|
+
} & {
|
|
20
|
+
output: string;
|
|
21
|
+
} & {
|
|
22
|
+
format: string;
|
|
23
|
+
} & {
|
|
24
|
+
export: string;
|
|
25
|
+
}>;
|
|
26
|
+
export declare function withGenerateDocumentation<T extends ParsedArgs>(cli: CLI<T>): CLI<T, T>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withGenerateDocumentationArgs = withGenerateDocumentationArgs;
|
|
4
|
+
exports.withGenerateDocumentation = withGenerateDocumentation;
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const src_1 = require("../../src");
|
|
8
|
+
const documentation_1 = require("../../src/lib/documentation");
|
|
9
|
+
const fs_1 = require("../utils/fs");
|
|
10
|
+
function withGenerateDocumentationArgs(cmd) {
|
|
11
|
+
return cmd
|
|
12
|
+
.positional('cli', {
|
|
13
|
+
type: 'string',
|
|
14
|
+
description: 'Path to the cli that docs should be generated for.',
|
|
15
|
+
required: true,
|
|
16
|
+
})
|
|
17
|
+
.option('output', {
|
|
18
|
+
alias: ['o'],
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'Where should the documentation be output?',
|
|
21
|
+
default: 'docs',
|
|
22
|
+
})
|
|
23
|
+
.option('format', {
|
|
24
|
+
type: 'string',
|
|
25
|
+
description: 'What format should the documentation be output in? (json, md)',
|
|
26
|
+
default: 'md',
|
|
27
|
+
})
|
|
28
|
+
.option('export', {
|
|
29
|
+
type: 'string',
|
|
30
|
+
description: 'The name of the export that contains the CLI instance. By default, docs will be generated for the default export.',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function withGenerateDocumentation(cli) {
|
|
34
|
+
return cli.command('generate-documentation', {
|
|
35
|
+
description: 'Generate documentation for the given CLI',
|
|
36
|
+
builder: withGenerateDocumentationArgs,
|
|
37
|
+
handler: async (args) => {
|
|
38
|
+
if (args.cli.startsWith('./')) {
|
|
39
|
+
args.cli = (0, node_path_1.join)(process.cwd(), args.cli);
|
|
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 src_1.CLI)) {
|
|
44
|
+
throw new Error(`${args.cli}${args.export ? '#' + args.export : ''} is not a CLI.`);
|
|
45
|
+
}
|
|
46
|
+
const documentation = (0, documentation_1.generateDocumentation)(cli);
|
|
47
|
+
if (args.format === 'md') {
|
|
48
|
+
await generateMarkdownDocumentation(documentation, args);
|
|
49
|
+
}
|
|
50
|
+
else if (args.format === 'json') {
|
|
51
|
+
const outfile = args.output.endsWith('json')
|
|
52
|
+
? args.output
|
|
53
|
+
: (0, node_path_1.join)(args.output, cli.name + '.json');
|
|
54
|
+
const outdir = (0, node_path_1.dirname)(outfile);
|
|
55
|
+
(0, fs_1.ensureDirSync)(outdir);
|
|
56
|
+
(0, node_fs_1.writeFileSync)(outfile, JSON.stringify(documentation, null, 2));
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function generateMarkdownDocumentation(docs, args) {
|
|
62
|
+
const md = await importMarkdownFactory();
|
|
63
|
+
await generateMarkdownForSingleCommand(docs, (0, node_path_1.join)(args.output, docs.name), md);
|
|
64
|
+
}
|
|
65
|
+
async function generateMarkdownForSingleCommand(docs, out, md) {
|
|
66
|
+
const subcommands = docs.subcommands;
|
|
67
|
+
const outdir = subcommands.length ? out : (0, node_path_1.dirname)(out);
|
|
68
|
+
const outname = subcommands.length ? 'index' : docs.name;
|
|
69
|
+
(0, fs_1.ensureDirSync)(outdir);
|
|
70
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(outdir, outname + '.md'), md.h1(docs.name, ...[
|
|
71
|
+
docs.description,
|
|
72
|
+
getPositionalArgsFragment(docs.positionals, md),
|
|
73
|
+
getFlagArgsFragment(docs.options, md),
|
|
74
|
+
getSubcommandsFragment(docs.subcommands, md),
|
|
75
|
+
].filter(isTruthy)));
|
|
76
|
+
for (const subcommand of docs.subcommands) {
|
|
77
|
+
await generateMarkdownForSingleCommand(subcommand, (0, node_path_1.join)(outdir, subcommand.name), md);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function formatOption(option, md) {
|
|
81
|
+
return md.h3(option.key, ...[
|
|
82
|
+
md.bold('Type:') +
|
|
83
|
+
' ' +
|
|
84
|
+
(option.type === 'array'
|
|
85
|
+
? `${option.items}[]`
|
|
86
|
+
: option.type),
|
|
87
|
+
option.description,
|
|
88
|
+
option.default ? md.bold('Default:') + ' ' + option.default : undefined,
|
|
89
|
+
option.required ? md.bold('Required') : undefined,
|
|
90
|
+
option.alias?.length
|
|
91
|
+
? md.h4('Aliases', md.ul(...option.alias))
|
|
92
|
+
: undefined,
|
|
93
|
+
].filter(isTruthy));
|
|
94
|
+
}
|
|
95
|
+
function getPositionalArgsFragment(positionals, md) {
|
|
96
|
+
if (positionals?.length === 0) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
return md.h2('Positional Arguments', ...positionals.map((positional) => formatOption(positional, md)));
|
|
100
|
+
}
|
|
101
|
+
function getFlagArgsFragment(options, md) {
|
|
102
|
+
if (Object.keys(options).length === 0) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
return md.h2('Flags', ...Object.values(options).map((option) => formatOption(option, md)));
|
|
106
|
+
}
|
|
107
|
+
function getSubcommandsFragment(subcommands, md) {
|
|
108
|
+
if (subcommands.length === 0) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
return md.h2('Subcommands', ...subcommands.map((subcommand) => md.link(`./${subcommand.name}`, subcommand.name)));
|
|
112
|
+
}
|
|
113
|
+
function isTruthy(value) {
|
|
114
|
+
return !!value;
|
|
115
|
+
}
|
|
116
|
+
async function importMarkdownFactory() {
|
|
117
|
+
try {
|
|
118
|
+
return await Promise.resolve().then(() => require('markdown-factory'));
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
throw new Error('Could not find markdown-factory. Please install it to generate markdown documentation.');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=generate-documentation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-documentation.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/commands/generate-documentation.ts"],"names":[],"mappings":";;AAqBA,sEA0BC;AAED,8DA4BC;AA3ED,qCAAwC;AACxC,yCAA0C;AAE1C,mCAAgC;AAChC,+DAGqC;AACrC,oCAA4C;AAW5C,SAAgB,6BAA6B,CAC3C,GAAiC;IAEjC,OAAO,GAAG;SACP,UAAU,CAAC,KAAK,EAAE;QACjB,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,oDAAoD;QACjE,QAAQ,EAAE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CACF,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAI,KAA2B;IAC9C,OAAO,CAAC,CAAC,KAAK,CAAC;AACjB,CAAC;AAED,KAAK,UAAU,qBAAqB;IAClC,IAAI,CAAC;QACH,OAAO,2CAAa,kBAAkB,EAAC,CAAC;IAC1C,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ParsedArgs } from '@cli-forge/parser';
|
|
2
|
+
import { CLI } from '../../src';
|
|
3
|
+
type InitArgs = {
|
|
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 & {
|
|
9
|
+
cliName: string;
|
|
10
|
+
} & {
|
|
11
|
+
output: string;
|
|
12
|
+
} & {
|
|
13
|
+
format: string;
|
|
14
|
+
}, T & {
|
|
15
|
+
cliName: string;
|
|
16
|
+
} & {
|
|
17
|
+
output: string;
|
|
18
|
+
} & {
|
|
19
|
+
format: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function withInit<T extends ParsedArgs>(cli: CLI<T>): CLI<T, T>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withInitArgs = withInitArgs;
|
|
4
|
+
exports.withInit = withInit;
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const fs_1 = require("../utils/fs");
|
|
8
|
+
const package_json_1 = require("../../package.json");
|
|
9
|
+
const node_child_process_1 = require("node:child_process");
|
|
10
|
+
function withInitArgs(cmd) {
|
|
11
|
+
return cmd
|
|
12
|
+
.positional('cliName', {
|
|
13
|
+
type: 'string',
|
|
14
|
+
description: 'Name of the CLI to generate.',
|
|
15
|
+
required: true,
|
|
16
|
+
})
|
|
17
|
+
.option('output', {
|
|
18
|
+
alias: ['o'],
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'Where should the CLI be created?',
|
|
21
|
+
})
|
|
22
|
+
.option('format', {
|
|
23
|
+
type: 'string',
|
|
24
|
+
default: 'ts',
|
|
25
|
+
description: 'What format should the CLI be in? (js, ts)',
|
|
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
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const COMMON_CONTENTS = (name) => `const myCLI = cli('${name}')
|
|
59
|
+
.command('hello', {
|
|
60
|
+
builder: (args) => args.positional('name', {type: 'string'}),
|
|
61
|
+
handler: (args) => {
|
|
62
|
+
console.log('hello', args.name);
|
|
63
|
+
}
|
|
64
|
+
})`;
|
|
65
|
+
const JS_CLI_CONTENTS = (name) => `const { cli } = require('cli-forge');
|
|
66
|
+
|
|
67
|
+
${COMMON_CONTENTS(name)}
|
|
68
|
+
|
|
69
|
+
module.exports = myCLI;
|
|
70
|
+
|
|
71
|
+
if (require.main === module) {
|
|
72
|
+
myCLI.forge();
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
const TS_CLI_CONTENTS = (name) => `import { cli } from 'cli-forge';
|
|
76
|
+
|
|
77
|
+
${COMMON_CONTENTS(name)}
|
|
78
|
+
|
|
79
|
+
export default myCLI;
|
|
80
|
+
|
|
81
|
+
if (require.main === module) {
|
|
82
|
+
myCLI.forge();
|
|
83
|
+
}
|
|
84
|
+
`;
|
|
85
|
+
function readJsonOr(filePath, alt) {
|
|
86
|
+
try {
|
|
87
|
+
const contents = (0, node_fs_1.readFileSync)(filePath, 'utf-8');
|
|
88
|
+
return JSON.parse(contents);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return alt;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/commands/init.ts"],"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AE,EAAE,CAAC;;EAExC,eAAe,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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function ensureDirSync(dir: string): void;
|
package/bin/utils/fs.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureDirSync = ensureDirSync;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
function ensureDirSync(dir) {
|
|
6
|
+
try {
|
|
7
|
+
(0, node_fs_1.mkdirSync)(dir, { recursive: true });
|
|
8
|
+
}
|
|
9
|
+
catch (e) {
|
|
10
|
+
if (!(0, node_fs_1.existsSync)(dir)) {
|
|
11
|
+
throw new Error(`Could not create directory: ${dir}`, { cause: e });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=fs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/bin/utils/fs.ts"],"names":[],"mappings":";;AAEA,sCAQC;AAVD,qCAAgD;AAEhD,SAAgB,aAAa,CAAC,GAAW;IACvC,IAAI,CAAC;QACH,IAAA,mBAAS,EAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,IAAA,oBAAU,EAAC,GAAG,CAAC,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,+BAA+B,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QACtE,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-forge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"tslib": "^2.3.0"
|
|
5
|
+
"tslib": "^2.3.0",
|
|
6
|
+
"@cli-forge/parser": "0.3.0"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"markdown-factory": "0.2.0"
|
|
10
|
+
},
|
|
11
|
+
"peerDependenciesMeta": {
|
|
12
|
+
"markdown-factory": {
|
|
13
|
+
"optional": true
|
|
14
|
+
}
|
|
6
15
|
},
|
|
7
16
|
"type": "commonjs",
|
|
8
17
|
"main": "./src/index.js",
|
|
9
18
|
"typings": "./src/index.d.ts",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"directory": "packages/cli-forge",
|
|
23
|
+
"url": "https://github.com/AgentEnder/cli-forge"
|
|
24
|
+
},
|
|
25
|
+
"bin": {
|
|
26
|
+
"cli-forge": "./bin/cli.js"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
10
31
|
"types": "./src/index.d.ts"
|
|
11
32
|
}
|
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = void 0;
|
|
3
4
|
const tslib_1 = require("tslib");
|
|
4
5
|
tslib_1.__exportStar(require("./lib/cli-forge"), exports);
|
|
6
|
+
var cli_forge_1 = require("./lib/cli-forge");
|
|
7
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return cli_forge_1.default; } });
|
|
5
8
|
//# 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,0DAAgC;AAChC,6CAA0C;AAAjC,oGAAA,OAAO,OAAA"}
|
package/src/lib/cli-forge.d.ts
CHANGED
|
@@ -1,35 +1,116 @@
|
|
|
1
1
|
import { ArrayOptionConfig, OptionConfig, ParsedArgs } from '@cli-forge/parser';
|
|
2
|
-
type CLICommandOptions<TInitial extends ParsedArgs, TArgs extends TInitial> = {
|
|
2
|
+
export type CLICommandOptions<TInitial extends ParsedArgs, TArgs extends TInitial> = {
|
|
3
3
|
description?: string;
|
|
4
4
|
builder?: (parser: CLI<TInitial>) => CLI<TArgs>;
|
|
5
|
-
handler: (args: TArgs) => void
|
|
5
|
+
handler: (args: TArgs) => void | Promise<void>;
|
|
6
6
|
};
|
|
7
|
-
export
|
|
7
|
+
export type ArgsOf<T extends CLI> = T extends {
|
|
8
|
+
configuration: {
|
|
9
|
+
handler: (args: infer TArgs) => void;
|
|
10
|
+
};
|
|
11
|
+
} ? TArgs : never;
|
|
12
|
+
/**
|
|
13
|
+
* The base class for a CLI application. This class is used to define the structure of the CLI.
|
|
14
|
+
*
|
|
15
|
+
* {@link cli} is provided as a small helper function to create a new CLI instance.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { cli } from 'cli-forge';
|
|
20
|
+
*
|
|
21
|
+
* cli('basic-cli').command('hello', {
|
|
22
|
+
* builder: (args) =>
|
|
23
|
+
* args.option('name', {
|
|
24
|
+
* type: 'string',
|
|
25
|
+
* }),
|
|
26
|
+
* handler: (args) => {
|
|
27
|
+
* console.log(`Hello, ${args.name}!`);
|
|
28
|
+
* }).forge();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class CLI<T extends ParsedArgs = ParsedArgs, T2 extends T = T> {
|
|
8
32
|
name: string;
|
|
9
|
-
|
|
33
|
+
configuration?: CLICommandOptions<T, T2> | undefined;
|
|
10
34
|
private commands;
|
|
11
35
|
private commandChain;
|
|
12
36
|
private requiresCommand;
|
|
13
37
|
private parser;
|
|
14
|
-
|
|
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);
|
|
43
|
+
/**
|
|
44
|
+
* Registers a new command with the CLI.
|
|
45
|
+
* @param key What should the new command be called?
|
|
46
|
+
* @param options Settings for the new command. See {@link CLICommandOptions}.
|
|
47
|
+
* @returns Updated CLI instance with the new command registered.
|
|
48
|
+
*/
|
|
15
49
|
command<TArgs extends T>(key: string, options: CLICommandOptions<T, TArgs>): this;
|
|
50
|
+
/**
|
|
51
|
+
* Registers a new option for the CLI command. This option will be accessible
|
|
52
|
+
* within the command handler, as well as any subcommands.
|
|
53
|
+
*
|
|
54
|
+
* @param name The name of the option.
|
|
55
|
+
* @param config Configuration for the option. See {@link OptionConfig}.
|
|
56
|
+
* @returns Updated CLI instance with the new option registered.
|
|
57
|
+
*/
|
|
16
58
|
option<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<T & { [key in TOption]: TOptionConfig["coerce"] extends (value: string) => infer TCoerce ? TCoerce : {
|
|
17
59
|
string: string;
|
|
18
60
|
number: number;
|
|
19
61
|
boolean: boolean;
|
|
20
62
|
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig["items"] extends "string" ? string : number : never)[];
|
|
21
63
|
}[TOptionConfig["type"]]; }>;
|
|
22
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Registers a new positional argument for the CLI command. This argument will be accessible
|
|
66
|
+
* within the command handler, as well as any subcommands.
|
|
67
|
+
* @param name The name of the positional argument.
|
|
68
|
+
* @param config Configuration for the positional argument. See {@link OptionConfig}.
|
|
69
|
+
* @returns Updated CLI instance with the new positional argument registered.
|
|
70
|
+
*/
|
|
71
|
+
positional<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<T & { [key in TOption]: TOptionConfig["coerce"] extends (value: string) => infer TCoerce ? TCoerce : {
|
|
23
72
|
string: string;
|
|
24
73
|
number: number;
|
|
25
74
|
boolean: boolean;
|
|
26
75
|
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig["items"] extends "string" ? string : number : never)[];
|
|
27
76
|
}[TOptionConfig["type"]]; }>;
|
|
77
|
+
/**
|
|
78
|
+
* Requires a command to be provided when executing the CLI. Useful if your parent command
|
|
79
|
+
* cannot be executed on its own.
|
|
80
|
+
* @returns Updated CLI instance.
|
|
81
|
+
*/
|
|
28
82
|
demandCommand(): this;
|
|
83
|
+
/**
|
|
84
|
+
* Gets help text for the current command as a string.
|
|
85
|
+
* @returns Help text for the current command.
|
|
86
|
+
*/
|
|
29
87
|
formatHelp(): string;
|
|
88
|
+
/**
|
|
89
|
+
* Prints help text for the current command to the console.
|
|
90
|
+
*/
|
|
30
91
|
printHelp(): void;
|
|
92
|
+
/**
|
|
93
|
+
* Runs the current command.
|
|
94
|
+
* @param cmd The command to run.
|
|
95
|
+
* @param args The arguments to pass to the command.
|
|
96
|
+
*/
|
|
31
97
|
runCommand<T extends ParsedArgs>(cmd: CLI<T>, args: T): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Parses argv and executes the CLI
|
|
100
|
+
* @param args argv. Defaults to process.argv.slice(2)
|
|
101
|
+
* @returns Promise that resolves when the handler completes.
|
|
102
|
+
*/
|
|
32
103
|
forge(args?: string[]): Promise<void>;
|
|
104
|
+
getParser(): import("@cli-forge/parser").ReadonlyArgvParser<T & {
|
|
105
|
+
help: boolean;
|
|
106
|
+
}>;
|
|
107
|
+
getSubcommands(): Readonly<Record<string, CLI>>;
|
|
108
|
+
clone(): CLI<T, T2>;
|
|
33
109
|
}
|
|
34
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Constructs a CLI instance. See {@link CLI} for more information.
|
|
112
|
+
* @param name Name for the top level CLI
|
|
113
|
+
* @returns
|
|
114
|
+
*/
|
|
115
|
+
export declare function cli(name: string): CLI<ParsedArgs, ParsedArgs>;
|
|
35
116
|
export default cli;
|
package/src/lib/cli-forge.js
CHANGED
|
@@ -2,65 +2,128 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CLI = void 0;
|
|
4
4
|
exports.cli = cli;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
5
|
const parser_1 = require("@cli-forge/parser");
|
|
6
|
+
/**
|
|
7
|
+
* The base class for a CLI application. This class is used to define the structure of the CLI.
|
|
8
|
+
*
|
|
9
|
+
* {@link cli} is provided as a small helper function to create a new CLI instance.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { cli } from 'cli-forge';
|
|
14
|
+
*
|
|
15
|
+
* cli('basic-cli').command('hello', {
|
|
16
|
+
* builder: (args) =>
|
|
17
|
+
* args.option('name', {
|
|
18
|
+
* type: 'string',
|
|
19
|
+
* }),
|
|
20
|
+
* handler: (args) => {
|
|
21
|
+
* console.log(`Hello, ${args.name}!`);
|
|
22
|
+
* }).forge();
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
7
25
|
class CLI {
|
|
26
|
+
name;
|
|
27
|
+
configuration;
|
|
28
|
+
commands = {};
|
|
29
|
+
commandChain = [];
|
|
30
|
+
requiresCommand = false;
|
|
31
|
+
parser = new parser_1.ArgvParser({
|
|
32
|
+
unmatchedParser: (arg) => {
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
34
|
+
let currentCommand = this;
|
|
35
|
+
for (const command of this.commandChain) {
|
|
36
|
+
currentCommand = currentCommand.commands[command];
|
|
37
|
+
}
|
|
38
|
+
const command = currentCommand.commands[arg];
|
|
39
|
+
if (command && command.configuration) {
|
|
40
|
+
command.configuration.builder?.(command);
|
|
41
|
+
this.commandChain.push(arg);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
},
|
|
46
|
+
}).option('help', {
|
|
47
|
+
type: 'boolean',
|
|
48
|
+
alias: ['h'],
|
|
49
|
+
description: 'Show help for the current command',
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* @param name What should the name of the cli command be?
|
|
53
|
+
* @param configuration Configuration for the current CLI command.
|
|
54
|
+
*/
|
|
8
55
|
constructor(name, configuration) {
|
|
9
56
|
this.name = name;
|
|
10
57
|
this.configuration = configuration;
|
|
11
|
-
this.commands = {};
|
|
12
|
-
this.commandChain = [];
|
|
13
|
-
this.requiresCommand = false;
|
|
14
|
-
this.parser = new parser_1.ArgvParser({
|
|
15
|
-
unmatchedParser: (arg, tokens, parser) => {
|
|
16
|
-
var _a, _b;
|
|
17
|
-
let currentCommand = this;
|
|
18
|
-
for (const command of this.commandChain) {
|
|
19
|
-
currentCommand = currentCommand.commands[command];
|
|
20
|
-
}
|
|
21
|
-
const command = currentCommand.commands[arg];
|
|
22
|
-
if (command && command.configuration) {
|
|
23
|
-
(_b = (_a = command.configuration).builder) === null || _b === void 0 ? void 0 : _b.call(_a, command);
|
|
24
|
-
this.commandChain.push(arg);
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
return false;
|
|
28
|
-
},
|
|
29
|
-
}).option('help', {
|
|
30
|
-
type: 'boolean',
|
|
31
|
-
alias: ['-h'],
|
|
32
|
-
description: 'Show help for the current command',
|
|
33
|
-
});
|
|
34
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Registers a new command with the CLI.
|
|
61
|
+
* @param key What should the new command be called?
|
|
62
|
+
* @param options Settings for the new command. See {@link CLICommandOptions}.
|
|
63
|
+
* @returns Updated CLI instance with the new command registered.
|
|
64
|
+
*/
|
|
35
65
|
command(key, options) {
|
|
36
66
|
if (key === '$0') {
|
|
37
|
-
this.configuration =
|
|
67
|
+
this.configuration = {
|
|
68
|
+
...this.configuration,
|
|
69
|
+
builder: options.builder,
|
|
70
|
+
handler: options.handler,
|
|
71
|
+
description: options.description,
|
|
72
|
+
};
|
|
38
73
|
}
|
|
39
74
|
this.commands[key] = new CLI(key, options);
|
|
40
75
|
this.commands[key].parser = this.parser;
|
|
41
76
|
return this;
|
|
42
77
|
}
|
|
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
|
+
*/
|
|
43
86
|
option(name, config) {
|
|
44
87
|
this.parser.option(name, config);
|
|
45
88
|
return this;
|
|
46
89
|
}
|
|
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
|
+
*/
|
|
47
97
|
positional(name, config) {
|
|
48
|
-
this.parser.
|
|
98
|
+
this.parser.positional(name, config);
|
|
49
99
|
return this;
|
|
50
100
|
}
|
|
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
|
+
*/
|
|
51
106
|
demandCommand() {
|
|
52
107
|
this.requiresCommand = true;
|
|
53
108
|
return this;
|
|
54
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Gets help text for the current command as a string.
|
|
112
|
+
* @returns Help text for the current command.
|
|
113
|
+
*/
|
|
55
114
|
formatHelp() {
|
|
56
|
-
var _a, _b;
|
|
57
115
|
const help = [];
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
58
117
|
let command = this;
|
|
59
118
|
for (const key of this.commandChain) {
|
|
60
119
|
command = command.commands[key];
|
|
61
120
|
}
|
|
62
|
-
help.push(`Usage: ${[
|
|
63
|
-
|
|
121
|
+
help.push(`Usage: ${[
|
|
122
|
+
this.name,
|
|
123
|
+
...this.commandChain,
|
|
124
|
+
...command.parser.configuredPositionals.map((p) => `[${p.key}]`),
|
|
125
|
+
].join(' ')}`);
|
|
126
|
+
if (command.configuration?.description) {
|
|
64
127
|
help.push(command.configuration.description);
|
|
65
128
|
}
|
|
66
129
|
if (Object.keys(command.commands).length > 0) {
|
|
@@ -69,7 +132,7 @@ class CLI {
|
|
|
69
132
|
}
|
|
70
133
|
for (const key in command.commands) {
|
|
71
134
|
const subcommand = command.commands[key];
|
|
72
|
-
help.push(` ${key}${
|
|
135
|
+
help.push(` ${key}${subcommand.configuration?.description
|
|
73
136
|
? ' - ' + subcommand.configuration.description
|
|
74
137
|
: ''}`);
|
|
75
138
|
}
|
|
@@ -87,46 +150,80 @@ class CLI {
|
|
|
87
150
|
}
|
|
88
151
|
return help.join('\n');
|
|
89
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Prints help text for the current command to the console.
|
|
155
|
+
*/
|
|
90
156
|
printHelp() {
|
|
91
157
|
console.log(this.formatHelp());
|
|
92
158
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Runs the current command.
|
|
161
|
+
* @param cmd The command to run.
|
|
162
|
+
* @param args The arguments to pass to the command.
|
|
163
|
+
*/
|
|
164
|
+
async runCommand(cmd, args) {
|
|
165
|
+
try {
|
|
166
|
+
if (cmd.requiresCommand) {
|
|
167
|
+
throw new Error(`${[this.name, ...this.commandChain].join(' ')} requires a command`);
|
|
100
168
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.printHelp();
|
|
169
|
+
if (cmd.configuration?.handler) {
|
|
170
|
+
await cmd.configuration.handler(args);
|
|
104
171
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
forge() {
|
|
108
|
-
return tslib_1.__awaiter(this, arguments, void 0, function* (args = process.argv.slice(2)) {
|
|
109
|
-
var _a, _b, _c;
|
|
110
|
-
// Parsing the args does two things:
|
|
111
|
-
// - builds argv to pass to handler
|
|
112
|
-
// - fills the command chain + registers commands
|
|
113
|
-
const argv = this.parser.parse(args);
|
|
114
|
-
let currentCommand = this;
|
|
115
|
-
for (const command of this.commandChain) {
|
|
116
|
-
currentCommand = currentCommand.commands[command];
|
|
172
|
+
else {
|
|
173
|
+
throw new Error(`${[this.name, ...this.commandChain].join(' ')} is not implemented.`);
|
|
117
174
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
process.exitCode = 1;
|
|
178
|
+
console.error(e);
|
|
179
|
+
this.printHelp();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Parses argv and executes the CLI
|
|
184
|
+
* @param args argv. Defaults to process.argv.slice(2)
|
|
185
|
+
* @returns Promise that resolves when the handler completes.
|
|
186
|
+
*/
|
|
187
|
+
async forge(args = process.argv.slice(2)) {
|
|
188
|
+
// Parsing the args does two things:
|
|
189
|
+
// - builds argv to pass to handler
|
|
190
|
+
// - fills the command chain + registers commands
|
|
191
|
+
const argv = this.parser.parse(args);
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
193
|
+
let currentCommand = this;
|
|
194
|
+
for (const command of this.commandChain) {
|
|
195
|
+
currentCommand = currentCommand.commands[command];
|
|
196
|
+
}
|
|
197
|
+
if (argv.help) {
|
|
198
|
+
this.printHelp();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const finalArgV = currentCommand === this
|
|
202
|
+
? (this.configuration?.builder?.(this).parser ?? this.parser).parse(args)
|
|
203
|
+
: argv;
|
|
204
|
+
await this.runCommand(currentCommand, finalArgV);
|
|
205
|
+
}
|
|
206
|
+
getParser() {
|
|
207
|
+
return this.parser.asReadonly();
|
|
208
|
+
}
|
|
209
|
+
getSubcommands() {
|
|
210
|
+
return this.commands;
|
|
211
|
+
}
|
|
212
|
+
clone() {
|
|
213
|
+
const clone = new CLI(this.name, this.configuration);
|
|
214
|
+
clone.commands = { ...this.commands };
|
|
215
|
+
clone.commandChain = [...this.commandChain];
|
|
216
|
+
clone.requiresCommand = this.requiresCommand;
|
|
217
|
+
clone.parser = this.parser.clone();
|
|
218
|
+
return clone;
|
|
127
219
|
}
|
|
128
220
|
}
|
|
129
221
|
exports.CLI = CLI;
|
|
222
|
+
/**
|
|
223
|
+
* Constructs a CLI instance. See {@link CLI} for more information.
|
|
224
|
+
* @param name Name for the top level CLI
|
|
225
|
+
* @returns
|
|
226
|
+
*/
|
|
130
227
|
function cli(name) {
|
|
131
228
|
return new CLI(name);
|
|
132
229
|
}
|
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":";;;
|
|
1
|
+
{"version":3,"file":"cli-forge.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/cli-forge.ts"],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AC,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;QAC7C,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAS,CAAC;QAC1C,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AA5QD,kBA4QC;AAED;;;;GAIG;AACH,SAAgB,GAAG,CAAC,IAAY;IAC9B,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;AACvB,CAAC;AAED,kBAAe,GAAG,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { OptionConfig } from '@cli-forge/parser';
|
|
2
|
+
import { CLI } from './cli-forge';
|
|
3
|
+
export type Documentation = {
|
|
4
|
+
name: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
options: Readonly<Record<string, OptionConfig & {
|
|
7
|
+
key: string;
|
|
8
|
+
}>>;
|
|
9
|
+
positionals: readonly Readonly<OptionConfig & {
|
|
10
|
+
key: string;
|
|
11
|
+
}>[];
|
|
12
|
+
subcommands: Documentation[];
|
|
13
|
+
};
|
|
14
|
+
export declare function generateDocumentation(cli: CLI): {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string | undefined;
|
|
17
|
+
options: Readonly<{
|
|
18
|
+
unmatched: OptionConfig<any> & {
|
|
19
|
+
key: string;
|
|
20
|
+
position?: number;
|
|
21
|
+
};
|
|
22
|
+
'--'?: (OptionConfig<any> & {
|
|
23
|
+
key: string;
|
|
24
|
+
position?: number;
|
|
25
|
+
}) | undefined;
|
|
26
|
+
help: OptionConfig<any> & {
|
|
27
|
+
key: string;
|
|
28
|
+
position?: number;
|
|
29
|
+
};
|
|
30
|
+
}>;
|
|
31
|
+
positionals: readonly Readonly<OptionConfig<any> & {
|
|
32
|
+
key: string;
|
|
33
|
+
position?: number;
|
|
34
|
+
}>[];
|
|
35
|
+
subcommands: Documentation[];
|
|
36
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateDocumentation = generateDocumentation;
|
|
4
|
+
function generateDocumentation(cli) {
|
|
5
|
+
// Ensure current command's options are built.
|
|
6
|
+
if (cli.configuration?.builder) {
|
|
7
|
+
cli.configuration.builder(cli);
|
|
8
|
+
}
|
|
9
|
+
const parser = cli.getParser();
|
|
10
|
+
const options = parser.configuredOptions;
|
|
11
|
+
const positionals = parser.configuredPositionals;
|
|
12
|
+
const subcommands = Object.values(cli.getSubcommands()).map((cmd) => generateDocumentation(cmd.clone()));
|
|
13
|
+
return {
|
|
14
|
+
name: cli.name,
|
|
15
|
+
description: cli.configuration?.description,
|
|
16
|
+
options,
|
|
17
|
+
positionals,
|
|
18
|
+
subcommands,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=documentation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"documentation.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/documentation.ts"],"names":[],"mappings":";;AAWA,sDAoBC;AApBD,SAAgB,qBAAqB,CAAC,GAAQ;IAC5C,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,CAAC,CAC5C,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,WAAW,EAAE,GAAG,CAAC,aAAa,EAAE,WAAW;QAC3C,OAAO;QACP,WAAW;QACX,WAAW;KACZ,CAAC;AACJ,CAAC"}
|