cli-forge 0.1.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/README.md +11 -0
- package/package.json +11 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +5 -0
- package/src/index.js.map +1 -0
- package/src/lib/cli-forge.d.ts +35 -0
- package/src/lib/cli-forge.js +134 -0
- package/src/lib/cli-forge.js.map +1 -0
package/README.md
ADDED
package/package.json
ADDED
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/cli-forge';
|
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/cli-forge/src/index.ts"],"names":[],"mappings":";;;AAAA,0DAAgC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ArrayOptionConfig, OptionConfig, ParsedArgs } from '@cli-forge/parser';
|
|
2
|
+
type CLICommandOptions<TInitial extends ParsedArgs, TArgs extends TInitial> = {
|
|
3
|
+
description?: string;
|
|
4
|
+
builder?: (parser: CLI<TInitial>) => CLI<TArgs>;
|
|
5
|
+
handler: (args: TArgs) => void;
|
|
6
|
+
};
|
|
7
|
+
export declare class CLI<T extends ParsedArgs> {
|
|
8
|
+
name: string;
|
|
9
|
+
protected configuration?: CLICommandOptions<T, T> | undefined;
|
|
10
|
+
private commands;
|
|
11
|
+
private commandChain;
|
|
12
|
+
private requiresCommand;
|
|
13
|
+
private parser;
|
|
14
|
+
constructor(name: string, configuration?: CLICommandOptions<T, T> | undefined);
|
|
15
|
+
command<TArgs extends T>(key: string, options: CLICommandOptions<T, TArgs>): this;
|
|
16
|
+
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
|
+
string: string;
|
|
18
|
+
number: number;
|
|
19
|
+
boolean: boolean;
|
|
20
|
+
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig["items"] extends "string" ? string : number : never)[];
|
|
21
|
+
}[TOptionConfig["type"]]; }>;
|
|
22
|
+
positional<TOption extends string, TOptionConfig extends OptionConfig>(name: TOption, config: TOptionConfig): CLI<T & { [key in TOption]: {
|
|
23
|
+
string: string;
|
|
24
|
+
number: number;
|
|
25
|
+
boolean: boolean;
|
|
26
|
+
array: (TOptionConfig extends ArrayOptionConfig<string | number> ? TOptionConfig["items"] extends "string" ? string : number : never)[];
|
|
27
|
+
}[TOptionConfig["type"]]; }>;
|
|
28
|
+
demandCommand(): this;
|
|
29
|
+
formatHelp(): string;
|
|
30
|
+
printHelp(): void;
|
|
31
|
+
runCommand<T extends ParsedArgs>(cmd: CLI<T>, args: T): Promise<void>;
|
|
32
|
+
forge(args?: string[]): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export declare function cli(name: string): CLI<ParsedArgs>;
|
|
35
|
+
export default cli;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CLI = void 0;
|
|
4
|
+
exports.cli = cli;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const parser_1 = require("@cli-forge/parser");
|
|
7
|
+
class CLI {
|
|
8
|
+
constructor(name, configuration) {
|
|
9
|
+
this.name = name;
|
|
10
|
+
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
|
+
}
|
|
35
|
+
command(key, options) {
|
|
36
|
+
if (key === '$0') {
|
|
37
|
+
this.configuration = Object.assign(Object.assign({}, this.configuration), { builder: options.builder, handler: options.handler, description: options.description });
|
|
38
|
+
}
|
|
39
|
+
this.commands[key] = new CLI(key, options);
|
|
40
|
+
this.commands[key].parser = this.parser;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
option(name, config) {
|
|
44
|
+
this.parser.option(name, config);
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
positional(name, config) {
|
|
48
|
+
this.parser.option(name, config);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
demandCommand() {
|
|
52
|
+
this.requiresCommand = true;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
formatHelp() {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
const help = [];
|
|
58
|
+
let command = this;
|
|
59
|
+
for (const key of this.commandChain) {
|
|
60
|
+
command = command.commands[key];
|
|
61
|
+
}
|
|
62
|
+
help.push(`Usage: ${[this.name, ...this.commandChain].join(' ')}`);
|
|
63
|
+
if ((_a = command.configuration) === null || _a === void 0 ? void 0 : _a.description) {
|
|
64
|
+
help.push(command.configuration.description);
|
|
65
|
+
}
|
|
66
|
+
if (Object.keys(command.commands).length > 0) {
|
|
67
|
+
help.push('');
|
|
68
|
+
help.push('Commands:');
|
|
69
|
+
}
|
|
70
|
+
for (const key in command.commands) {
|
|
71
|
+
const subcommand = command.commands[key];
|
|
72
|
+
help.push(` ${key}${((_b = subcommand.configuration) === null || _b === void 0 ? void 0 : _b.description)
|
|
73
|
+
? ' - ' + subcommand.configuration.description
|
|
74
|
+
: ''}`);
|
|
75
|
+
}
|
|
76
|
+
if (Object.keys(this.parser.configuredOptions).length > 0) {
|
|
77
|
+
help.push('');
|
|
78
|
+
help.push('Options:');
|
|
79
|
+
}
|
|
80
|
+
for (const key in this.parser.configuredOptions) {
|
|
81
|
+
const option = this.parser.configuredOptions[key];
|
|
82
|
+
help.push(` --${key}${option.description ? ' - ' + option.description : ''}`);
|
|
83
|
+
}
|
|
84
|
+
if (Object.keys(command.commands).length > 0) {
|
|
85
|
+
help.push(' ');
|
|
86
|
+
help.push(`Run \`${[this.name, ...this.commandChain].join(' ')} [command] --help\` for more information on a command`);
|
|
87
|
+
}
|
|
88
|
+
return help.join('\n');
|
|
89
|
+
}
|
|
90
|
+
printHelp() {
|
|
91
|
+
console.log(this.formatHelp());
|
|
92
|
+
}
|
|
93
|
+
runCommand(cmd, args) {
|
|
94
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
if (cmd.requiresCommand) {
|
|
97
|
+
throw new Error(`${[this.name, ...this.commandChain].join(' ')} requires a command`);
|
|
98
|
+
}
|
|
99
|
+
yield cmd.configuration.handler(args);
|
|
100
|
+
}
|
|
101
|
+
catch (_a) {
|
|
102
|
+
process.exitCode = 1;
|
|
103
|
+
this.printHelp();
|
|
104
|
+
}
|
|
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];
|
|
117
|
+
}
|
|
118
|
+
if (argv.help) {
|
|
119
|
+
this.printHelp();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const finalArgV = currentCommand === this
|
|
123
|
+
? ((_c = (_b = (_a = this.configuration) === null || _a === void 0 ? void 0 : _a.builder) === null || _b === void 0 ? void 0 : _b.call(_a, this).parser) !== null && _c !== void 0 ? _c : this.parser).parse(args)
|
|
124
|
+
: argv;
|
|
125
|
+
yield this.runCommand(currentCommand, finalArgV);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.CLI = CLI;
|
|
130
|
+
function cli(name) {
|
|
131
|
+
return new CLI(name);
|
|
132
|
+
}
|
|
133
|
+
exports.default = cli;
|
|
134
|
+
//# sourceMappingURL=cli-forge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli-forge.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/cli-forge.ts"],"names":[],"mappings":";;;AAuMA,kBAEC;;AAzMD,8CAK2B;AAQ3B,MAAa,GAAG;IAwBd,YACS,IAAY,EACT,aAAuC;QAD1C,SAAI,GAAJ,IAAI,CAAQ;QACT,kBAAa,GAAb,aAAa,CAA0B;QAzB3C,aAAQ,GAA6B,EAAE,CAAC;QACxC,iBAAY,GAAa,EAAE,CAAC;QAC5B,oBAAe,GAAY,KAAK,CAAC;QACjC,WAAM,GAAG,IAAI,mBAAU,CAAI;YACjC,eAAe,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;;gBACvC,IAAI,cAAc,GAAa,IAAI,CAAC;gBACpC,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;oBACxC,cAAc,GAAG,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;gBACpD,CAAC;gBACD,MAAM,OAAO,GAAG,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;gBAC7C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