cli-forge 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +5 -1
- package/bin/cli.js.map +1 -1
- package/bin/commands/generate-documentation.d.ts +2 -0
- package/bin/commands/generate-documentation.js +121 -17
- package/bin/commands/generate-documentation.js.map +1 -1
- package/bin/commands/init.d.ts +4 -0
- package/bin/commands/init.js +33 -4
- package/bin/commands/init.js.map +1 -1
- package/package.json +10 -4
- package/src/index.d.ts +1 -0
- package/src/index.js +3 -1
- package/src/index.js.map +1 -1
- package/src/lib/cli-forge.d.ts +112 -11
- package/src/lib/cli-forge.js +245 -42
- package/src/lib/cli-forge.js.map +1 -1
- package/src/lib/documentation.d.ts +7 -14
- package/src/lib/documentation.js +15 -2
- package/src/lib/documentation.js.map +1 -1
- package/src/lib/interactive-shell.d.ts +13 -0
- package/src/lib/interactive-shell.js +85 -0
- package/src/lib/interactive-shell.js.map +1 -0
- package/src/lib/test-harness.d.ts +36 -0
- package/src/lib/test-harness.js +37 -0
- package/src/lib/test-harness.js.map +1 -0
- package/src/lib/utils.d.ts +10 -0
- package/src/lib/utils.js +110 -0
- package/src/lib/utils.js.map +1 -0
|
@@ -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"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function getCallingFile(): string | undefined;
|
|
2
|
+
export declare function getParentPackageJson(searchPath: string): {
|
|
3
|
+
name: string;
|
|
4
|
+
version: string;
|
|
5
|
+
bin?: {
|
|
6
|
+
[cmd: string]: string;
|
|
7
|
+
};
|
|
8
|
+
dependencies?: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
export declare function stringToArgs(str: string): string[];
|
package/src/lib/utils.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCallingFile = getCallingFile;
|
|
4
|
+
exports.getParentPackageJson = getParentPackageJson;
|
|
5
|
+
exports.stringToArgs = stringToArgs;
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
function getCallingFile() {
|
|
9
|
+
// Since this function lives in a utility file, the parent file
|
|
10
|
+
// would just be the file that invokes getCallingFile... We actually
|
|
11
|
+
// want the parent of the file that calls this, which is this file's
|
|
12
|
+
// grandparent.
|
|
13
|
+
let grandparentFile;
|
|
14
|
+
// Overrides prepare stack trace to be an identity fn, to get the callsites
|
|
15
|
+
// associated with the current error.
|
|
16
|
+
const _pst = Error.prepareStackTrace;
|
|
17
|
+
Error.prepareStackTrace = function (err, stack) {
|
|
18
|
+
return stack;
|
|
19
|
+
};
|
|
20
|
+
try {
|
|
21
|
+
const err = new Error();
|
|
22
|
+
const callsites = err.stack;
|
|
23
|
+
let currentfile = callsites.shift().getFileName();
|
|
24
|
+
let parentfile;
|
|
25
|
+
while (callsites.length) {
|
|
26
|
+
const callerfile = callsites.shift().getFileName();
|
|
27
|
+
// We've reached the parent file
|
|
28
|
+
if (currentfile !== callerfile) {
|
|
29
|
+
// We've reached the grandparent file
|
|
30
|
+
if (parentfile && parentfile !== callerfile) {
|
|
31
|
+
grandparentFile = parentfile;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
parentfile = callerfile;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
Error.prepareStackTrace = _pst;
|
|
40
|
+
}
|
|
41
|
+
return grandparentFile;
|
|
42
|
+
}
|
|
43
|
+
function getParentPackageJson(searchPath) {
|
|
44
|
+
let currentPath = searchPath;
|
|
45
|
+
let packageJsonPath;
|
|
46
|
+
while (true) {
|
|
47
|
+
const packagePath = (0, path_1.join)(currentPath, 'package.json');
|
|
48
|
+
if ((0, fs_1.existsSync)(packagePath)) {
|
|
49
|
+
packageJsonPath = packagePath;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
const nextPath = (0, path_1.resolve)(currentPath, '..');
|
|
53
|
+
if (nextPath === currentPath) {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
currentPath = nextPath;
|
|
57
|
+
}
|
|
58
|
+
if (!packageJsonPath) {
|
|
59
|
+
throw new Error('Could not find package.json');
|
|
60
|
+
}
|
|
61
|
+
return require(packageJsonPath);
|
|
62
|
+
}
|
|
63
|
+
function stringToArgs(str) {
|
|
64
|
+
const quotePairs = new Map([
|
|
65
|
+
['"', '"'],
|
|
66
|
+
["'", "'"],
|
|
67
|
+
['`', '`'],
|
|
68
|
+
]);
|
|
69
|
+
const escapeChars = new Set(['\\']);
|
|
70
|
+
let activeQuote;
|
|
71
|
+
const args = [];
|
|
72
|
+
let currentArg = '';
|
|
73
|
+
let prev;
|
|
74
|
+
for (let i = 0; i < str.length; i++) {
|
|
75
|
+
const char = str[i];
|
|
76
|
+
if (activeQuote) {
|
|
77
|
+
while (true) {
|
|
78
|
+
if (i >= str.length) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
else if (str[i] === quotePairs.get(activeQuote) &&
|
|
82
|
+
!(prev && escapeChars.has(prev))) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
if (!escapeChars.has(str[i])) {
|
|
86
|
+
currentArg += str[i];
|
|
87
|
+
}
|
|
88
|
+
prev = str[i];
|
|
89
|
+
i++;
|
|
90
|
+
}
|
|
91
|
+
activeQuote = undefined;
|
|
92
|
+
}
|
|
93
|
+
else if (quotePairs.has(char) && !(prev && escapeChars.has(prev))) {
|
|
94
|
+
activeQuote = char;
|
|
95
|
+
}
|
|
96
|
+
else if (char === ' ' &&
|
|
97
|
+
prev !== ' ' &&
|
|
98
|
+
!(prev && escapeChars.has(prev))) {
|
|
99
|
+
args.push(currentArg);
|
|
100
|
+
currentArg = '';
|
|
101
|
+
}
|
|
102
|
+
else if (!escapeChars.has(char) || (prev && escapeChars.has(prev))) {
|
|
103
|
+
currentArg += char;
|
|
104
|
+
}
|
|
105
|
+
prev = char;
|
|
106
|
+
}
|
|
107
|
+
args.push(currentArg);
|
|
108
|
+
return args;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../../packages/cli-forge/src/lib/utils.ts"],"names":[],"mappings":";;AAGA,wCAuCC;AAED,oDAiCC;AAED,oCAiDC;AAhID,2BAAgC;AAChC,+BAAqC;AAErC,SAAgB,cAAc;IAC5B,+DAA+D;IAC/D,oEAAoE;IACpE,oEAAoE;IACpE,eAAe;IACf,IAAI,eAAmC,CAAC;IAExC,2EAA2E;IAC3E,qCAAqC;IACrC,MAAM,IAAI,GAAG,KAAK,CAAC,iBAAiB,CAAC;IACrC,KAAK,CAAC,iBAAiB,GAAG,UAAU,GAAG,EAAE,KAAK;QAC5C,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,KAAK,EAAE,CAAC;QACxB,MAAM,SAAS,GAAG,GAAG,CAAC,KAAiC,CAAC;QAExD,IAAI,WAAW,GAAG,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