@oclif/test 4.0.1-beta.2 → 4.0.1-dev.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/lib/index.d.ts +1 -1
- package/lib/index.js +58 -52
- package/package.json +3 -5
package/lib/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare function captureOutput<T>(fn: () => Promise<unknown>, opts?: Capt
|
|
|
9
9
|
stderr: string;
|
|
10
10
|
stdout: string;
|
|
11
11
|
}>;
|
|
12
|
-
export declare function runCommand<T>(args: string
|
|
12
|
+
export declare function runCommand<T>(args: string[], loadOpts?: Interfaces.LoadOptions, captureOpts?: CaptureOptions): Promise<{
|
|
13
13
|
error?: Error & Partial<Errors.CLIError>;
|
|
14
14
|
result?: T;
|
|
15
15
|
stderr: string;
|
package/lib/index.js
CHANGED
|
@@ -8,7 +8,48 @@ const core_1 = require("@oclif/core");
|
|
|
8
8
|
const ansis_1 = __importDefault(require("ansis"));
|
|
9
9
|
const debug_1 = __importDefault(require("debug"));
|
|
10
10
|
const node_path_1 = require("node:path");
|
|
11
|
-
const debug = (0, debug_1.default)('
|
|
11
|
+
const debug = (0, debug_1.default)('test');
|
|
12
|
+
const RECORD_OPTIONS = {
|
|
13
|
+
print: false,
|
|
14
|
+
stripAnsi: true,
|
|
15
|
+
};
|
|
16
|
+
const originals = {
|
|
17
|
+
stderr: process.stderr.write,
|
|
18
|
+
stdout: process.stdout.write,
|
|
19
|
+
};
|
|
20
|
+
const output = {
|
|
21
|
+
stderr: [],
|
|
22
|
+
stdout: [],
|
|
23
|
+
};
|
|
24
|
+
function mockedStdout(str, encoding, cb) {
|
|
25
|
+
output.stdout.push(str);
|
|
26
|
+
if (!RECORD_OPTIONS.print)
|
|
27
|
+
return true;
|
|
28
|
+
if (typeof encoding === 'string') {
|
|
29
|
+
return originals.stdout.bind(process.stdout)(str, encoding, cb);
|
|
30
|
+
}
|
|
31
|
+
return originals.stdout.bind(process.stdout)(str, cb);
|
|
32
|
+
}
|
|
33
|
+
function mockedStderr(str, encoding, cb) {
|
|
34
|
+
output.stderr.push(str);
|
|
35
|
+
if (!RECORD_OPTIONS.print)
|
|
36
|
+
return true;
|
|
37
|
+
if (typeof encoding === 'string') {
|
|
38
|
+
return originals.stdout.bind(process.stderr)(str, encoding, cb);
|
|
39
|
+
}
|
|
40
|
+
return originals.stdout.bind(process.stderr)(str, cb);
|
|
41
|
+
}
|
|
42
|
+
const restore = () => {
|
|
43
|
+
process.stderr.write = originals.stderr;
|
|
44
|
+
process.stdout.write = originals.stdout;
|
|
45
|
+
};
|
|
46
|
+
const reset = () => {
|
|
47
|
+
output.stderr = [];
|
|
48
|
+
output.stdout = [];
|
|
49
|
+
};
|
|
50
|
+
const toString = (str) => RECORD_OPTIONS.stripAnsi ? ansis_1.default.strip(str.toString()) : str.toString();
|
|
51
|
+
const getStderr = () => output.stderr.map((b) => toString(b)).join('');
|
|
52
|
+
const getStdout = () => output.stdout.map((b) => toString(b)).join('');
|
|
12
53
|
function traverseFilePathUntil(filename, predicate) {
|
|
13
54
|
let current = filename;
|
|
14
55
|
while (!predicate(current)) {
|
|
@@ -16,48 +57,18 @@ function traverseFilePathUntil(filename, predicate) {
|
|
|
16
57
|
}
|
|
17
58
|
return current;
|
|
18
59
|
}
|
|
19
|
-
function findRoot() {
|
|
20
|
-
return (process.env.OCLIF_TEST_ROOT ??
|
|
21
|
-
// eslint-disable-next-line unicorn/prefer-module
|
|
22
|
-
Object.values(require.cache).find((m) => m?.children.includes(module))?.filename ??
|
|
23
|
-
traverseFilePathUntil(
|
|
24
|
-
// eslint-disable-next-line unicorn/prefer-module
|
|
25
|
-
require.main?.path ?? module.path, (p) => !(p.includes('node_modules') || p.includes('.pnpm') || p.includes('.yarn'))));
|
|
26
|
-
}
|
|
27
60
|
function makeLoadOptions(loadOpts) {
|
|
28
|
-
return loadOpts ?? {
|
|
61
|
+
return (loadOpts ?? {
|
|
62
|
+
root: traverseFilePathUntil(
|
|
63
|
+
// eslint-disable-next-line unicorn/prefer-module
|
|
64
|
+
require.main?.path ?? module.path, (p) => !(p.includes('node_modules') || p.includes('.pnpm') || p.includes('.yarn'))),
|
|
65
|
+
});
|
|
29
66
|
}
|
|
30
67
|
async function captureOutput(fn, opts) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
stderr: process.stderr.write,
|
|
36
|
-
stdout: process.stdout.write,
|
|
37
|
-
};
|
|
38
|
-
const output = {
|
|
39
|
-
stderr: [],
|
|
40
|
-
stdout: [],
|
|
41
|
-
};
|
|
42
|
-
const toString = (str) => (stripAnsi ? ansis_1.default.strip(str.toString()) : str.toString());
|
|
43
|
-
const getStderr = () => output.stderr.map((b) => toString(b)).join('');
|
|
44
|
-
const getStdout = () => output.stdout.map((b) => toString(b)).join('');
|
|
45
|
-
const mock = (std) => (str, encoding, cb) => {
|
|
46
|
-
output[std].push(str);
|
|
47
|
-
if (print) {
|
|
48
|
-
if (encoding !== null && typeof encoding === 'function') {
|
|
49
|
-
cb = encoding;
|
|
50
|
-
encoding = undefined;
|
|
51
|
-
}
|
|
52
|
-
originals[std].apply(process[std], [str, encoding, cb]);
|
|
53
|
-
}
|
|
54
|
-
else if (typeof cb === 'function')
|
|
55
|
-
cb();
|
|
56
|
-
return true;
|
|
57
|
-
};
|
|
58
|
-
process.stdout.write = mock('stdout');
|
|
59
|
-
process.stderr.write = mock('stderr');
|
|
60
|
-
process.env.NODE_ENV = 'test';
|
|
68
|
+
RECORD_OPTIONS.print = opts?.print ?? false;
|
|
69
|
+
RECORD_OPTIONS.stripAnsi = opts?.stripAnsi ?? true;
|
|
70
|
+
process.stderr.write = mockedStderr;
|
|
71
|
+
process.stdout.write = mockedStdout;
|
|
61
72
|
try {
|
|
62
73
|
const result = await fn();
|
|
63
74
|
return {
|
|
@@ -68,32 +79,27 @@ async function captureOutput(fn, opts) {
|
|
|
68
79
|
}
|
|
69
80
|
catch (error) {
|
|
70
81
|
return {
|
|
71
|
-
...(error instanceof core_1.Errors.CLIError && { error
|
|
72
|
-
...(error instanceof Error && { error
|
|
82
|
+
...(error instanceof core_1.Errors.CLIError && { error }),
|
|
83
|
+
...(error instanceof Error && { error }),
|
|
73
84
|
stderr: getStderr(),
|
|
74
85
|
stdout: getStdout(),
|
|
75
86
|
};
|
|
76
87
|
}
|
|
77
88
|
finally {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
process.env.NODE_ENV = originals.NODE_ENV;
|
|
89
|
+
restore();
|
|
90
|
+
reset();
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
exports.captureOutput = captureOutput;
|
|
84
94
|
async function runCommand(args, loadOpts, captureOpts) {
|
|
85
95
|
const loadOptions = makeLoadOptions(loadOpts);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const finalArgs = id === '.' ? rest : argsArray;
|
|
89
|
-
debug('loadOpts: %O', loadOptions);
|
|
90
|
-
debug('args: %O', finalArgs);
|
|
91
|
-
return captureOutput(async () => (0, core_1.run)(finalArgs, loadOptions), captureOpts);
|
|
96
|
+
debug('loadOpts: %O', loadOpts);
|
|
97
|
+
return captureOutput(async () => (0, core_1.run)(args, loadOptions), captureOpts);
|
|
92
98
|
}
|
|
93
99
|
exports.runCommand = runCommand;
|
|
94
100
|
async function runHook(hook, options, loadOpts, recordOpts) {
|
|
95
101
|
const loadOptions = makeLoadOptions(loadOpts);
|
|
96
|
-
debug('loadOpts: %O',
|
|
102
|
+
debug('loadOpts: %O', loadOpts);
|
|
97
103
|
return captureOutput(async () => {
|
|
98
104
|
const config = await core_1.Config.load(loadOptions);
|
|
99
105
|
return config.runHook(hook, options);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oclif/test",
|
|
3
3
|
"description": "test helpers for oclif components",
|
|
4
|
-
"version": "4.0.1-
|
|
4
|
+
"version": "4.0.1-dev.0",
|
|
5
5
|
"author": "Salesforce",
|
|
6
6
|
"bugs": "https://github.com/oclif/test/issues",
|
|
7
7
|
"dependencies": {
|
|
@@ -9,17 +9,15 @@
|
|
|
9
9
|
"debug": "^4.3.4"
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@oclif/core": "^4.0.0-beta.
|
|
12
|
+
"@oclif/core": "^4.0.0-beta.6"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@commitlint/config-conventional": "^18.6.3",
|
|
16
16
|
"@oclif/core": "^4.0.0-beta.6",
|
|
17
17
|
"@oclif/prettier-config": "^0.2.1",
|
|
18
|
-
"@types/chai": "^4.3.16",
|
|
19
18
|
"@types/debug": "^4.1.12",
|
|
20
19
|
"@types/mocha": "^10",
|
|
21
20
|
"@types/node": "^18",
|
|
22
|
-
"chai": "^5.1.1",
|
|
23
21
|
"commitlint": "^18.6.1",
|
|
24
22
|
"eslint": "^8.57.0",
|
|
25
23
|
"eslint-config-oclif": "^5.2.0",
|
|
@@ -30,7 +28,7 @@
|
|
|
30
28
|
"mocha": "^10",
|
|
31
29
|
"prettier": "^3.2.5",
|
|
32
30
|
"shx": "^0.3.3",
|
|
33
|
-
"
|
|
31
|
+
"ts-node": "^10.9.2",
|
|
34
32
|
"typescript": "^5.4.5"
|
|
35
33
|
},
|
|
36
34
|
"engines": {
|