first-base 1.5.1 → 1.5.2

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/.node-version CHANGED
@@ -1 +1 @@
1
- v20.11.1
1
+ v20.11.1
package/.npm-version CHANGED
@@ -1 +1 @@
1
- 10.2.4
1
+ 10.2.4
package/README.md CHANGED
@@ -1,142 +1,142 @@
1
- # `first-base`
2
-
3
- Integration testing for CLI applications.
4
-
5
- ## Usage Example
6
-
7
- ```js
8
- const { spawn } = require("first-base");
9
-
10
- test("something", async () => {
11
- const run = spawn("node", ["-i"]); // launch node REPL
12
- await run.outputContains("> "); // wait until `> ` is logged
13
- run.write("2 + 2\n"); // enter `2 + 2` and press enter
14
- await run.outputContains("4"); // wait until `4` is logged
15
- run.kill(); // Ctrl+C
16
- await run.completion; // Wait until process exits
17
- });
18
- ```
19
-
20
- ## API
21
-
22
- ### `spawn(command: string, args?: Array<string>, options?: Object) => RunContext`
23
-
24
- `args` and `options` are the same as [child_process.spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).
25
-
26
- Returns a `RunContext` object; see below.
27
-
28
- ### `RunContext#result`
29
-
30
- An object with the following properties on it:
31
-
32
- - `stdout` (`string`): All the data the process has written to STDOUT so far
33
- - `stderr` (`string`): All the data the process has written to STDERR so far
34
- - `code` (`number | null`): Exit status code, if the process has finished
35
- - `error` (`boolean`): If the process errored out, this is `true`
36
-
37
- This object gets updated over time as the process runs.
38
-
39
- #### Usage
40
-
41
- ```js
42
- const run = spawn("ls", { cwd: __dirname });
43
- console.log(run.result); // { stdout: '', stderr: '', code: null, error: false }
44
- await run.completion;
45
- console.log(run.result); // { stdout: 'README.md\npackage.json\nindex.js\n', stderr: '', code: 0, error: false }
46
- ```
47
-
48
- ### `RunContext#completion`
49
-
50
- A Promise that gets resolved when the process completes. You can `await` it in your tests.
51
-
52
- #### Usage
53
-
54
- ```js
55
- const run = spawn("ls", { cwd: __dirname });
56
- await run.completion; // Waits until the `ls` process finishes
57
- ```
58
-
59
- ### `RunContext#debug() => RunContext`
60
-
61
- Enables debug logging for the `RunContext` and returns it. Useful when your tests are failing and you want to understand what's going on.
62
-
63
- Returns itself so you can add it to a variable declaration easily.
64
-
65
- #### Usage
66
-
67
- ```js
68
- const run = spawn("ls", { cwd: __dirname }).debug();
69
- // The following messages are logged to the console over time:
70
- //
71
- // STDOUT: README.md\npackage.json\nindex.js
72
- // Process exited
73
- // { stdout: 'README.md\npackage.json\nindex.js', stderr: '', code: 0, error: false }
74
- ```
75
-
76
- ### `RunContext#outputContains(value: string | RegExp) => Promise<void>`
77
-
78
- Returns a Promise that will resolve once the process's output (combined STDOUT/STDERR) contains either the specified string or matches the specified RegExp. Ignores ANSI control characters.
79
-
80
- #### Usage
81
-
82
- ```js
83
- const run = spawn("node", ["-i"]); // start Node.js REPL
84
- await run.outputContains("> "); // Wait until prompt appears
85
- ```
86
-
87
- ### `RunContext#clearOutputContainsBuffer()`
88
-
89
- Clears the buffer of "seen" output as far as the `outputContains` method is concerned. Useful if the output already contains the specified value, and you want to wait until it appears a second time.
90
-
91
- #### Usage
92
-
93
- ```js
94
- const run = spawn("node", ["-i"]); // start Node.js REPL
95
- await run.outputContains("> "); // Wait until prompt appears
96
- run.write("2 + 2\n"); // Write 2 + 2 then press enter
97
- run.clearOutputContainsBuffer();
98
- await run.outputContains("> "); // Wait until prompt appears a second time. If we hadn't cleared the buffer, this would resolve immediately.
99
- ```
100
-
101
- ### `RunContext#write(data: string | Buffer)`
102
-
103
- Write some data into the process's STDIN stream.
104
-
105
- #### Usage
106
-
107
- ```js
108
- const run = spawn("node", ["-i"]); // start Node.js REPL
109
- await run.outputContains("> "); // Wait until prompt appears
110
- run.write("2 + 2\n"); // Write 2 + 2 then press enter
111
- await run.outputContains("4");
112
- ```
113
-
114
- ### `RunContext#close(stream: 'stdin' | 'stdout' | 'stderr')`
115
-
116
- Close one of the processes's associated stdio streams.
117
-
118
- #### Usage
119
-
120
- ```js
121
- const run = spawn("node", ["-i"]); // start Node.js REPL
122
- await run.outputContains("> "); // Wait until prompt appears
123
- run.close("stdin"); // Like pressing Ctrl+D; sends EOF
124
- await run.completion;
125
- ```
126
-
127
- ### `RunContext#kill(signal?: string)`
128
-
129
- Kills the process. If no signal is specified, it defaults to `"SIGINT"`.
130
-
131
- #### Usage
132
-
133
- ```js
134
- const run = spawn("node", ["-i"]);
135
- run.kill(); // Kill with SIGINT
136
- // OR:
137
- run.kill("SIGKILL"); // Kill with SIGKILL
138
- ```
139
-
140
- ## License
141
-
142
- MIT
1
+ # `first-base`
2
+
3
+ Integration testing for CLI applications.
4
+
5
+ ## Usage Example
6
+
7
+ ```js
8
+ const { spawn } = require("first-base");
9
+
10
+ test("something", async () => {
11
+ const run = spawn("node", ["-i"]); // launch node REPL
12
+ await run.outputContains("> "); // wait until `> ` is logged
13
+ run.write("2 + 2\n"); // enter `2 + 2` and press enter
14
+ await run.outputContains("4"); // wait until `4` is logged
15
+ run.kill(); // Ctrl+C
16
+ await run.completion; // Wait until process exits
17
+ });
18
+ ```
19
+
20
+ ## API
21
+
22
+ ### `spawn(command: string, args?: Array<string>, options?: Object) => RunContext`
23
+
24
+ `args` and `options` are the same as [child_process.spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).
25
+
26
+ Returns a `RunContext` object; see below.
27
+
28
+ ### `RunContext#result`
29
+
30
+ An object with the following properties on it:
31
+
32
+ - `stdout` (`string`): All the data the process has written to STDOUT so far
33
+ - `stderr` (`string`): All the data the process has written to STDERR so far
34
+ - `code` (`number | null`): Exit status code, if the process has finished
35
+ - `error` (`boolean`): If the process errored out, this is `true`
36
+
37
+ This object gets updated over time as the process runs.
38
+
39
+ #### Usage
40
+
41
+ ```js
42
+ const run = spawn("ls", { cwd: __dirname });
43
+ console.log(run.result); // { stdout: '', stderr: '', code: null, error: false }
44
+ await run.completion;
45
+ console.log(run.result); // { stdout: 'README.md\npackage.json\nindex.js\n', stderr: '', code: 0, error: false }
46
+ ```
47
+
48
+ ### `RunContext#completion`
49
+
50
+ A Promise that gets resolved when the process completes. You can `await` it in your tests.
51
+
52
+ #### Usage
53
+
54
+ ```js
55
+ const run = spawn("ls", { cwd: __dirname });
56
+ await run.completion; // Waits until the `ls` process finishes
57
+ ```
58
+
59
+ ### `RunContext#debug() => RunContext`
60
+
61
+ Enables debug logging for the `RunContext` and returns it. Useful when your tests are failing and you want to understand what's going on.
62
+
63
+ Returns itself so you can add it to a variable declaration easily.
64
+
65
+ #### Usage
66
+
67
+ ```js
68
+ const run = spawn("ls", { cwd: __dirname }).debug();
69
+ // The following messages are logged to the console over time:
70
+ //
71
+ // STDOUT: README.md\npackage.json\nindex.js
72
+ // Process exited
73
+ // { stdout: 'README.md\npackage.json\nindex.js', stderr: '', code: 0, error: false }
74
+ ```
75
+
76
+ ### `RunContext#outputContains(value: string | RegExp) => Promise<void>`
77
+
78
+ Returns a Promise that will resolve once the process's output (combined STDOUT/STDERR) contains either the specified string or matches the specified RegExp. Ignores ANSI control characters.
79
+
80
+ #### Usage
81
+
82
+ ```js
83
+ const run = spawn("node", ["-i"]); // start Node.js REPL
84
+ await run.outputContains("> "); // Wait until prompt appears
85
+ ```
86
+
87
+ ### `RunContext#clearOutputContainsBuffer()`
88
+
89
+ Clears the buffer of "seen" output as far as the `outputContains` method is concerned. Useful if the output already contains the specified value, and you want to wait until it appears a second time.
90
+
91
+ #### Usage
92
+
93
+ ```js
94
+ const run = spawn("node", ["-i"]); // start Node.js REPL
95
+ await run.outputContains("> "); // Wait until prompt appears
96
+ run.write("2 + 2\n"); // Write 2 + 2 then press enter
97
+ run.clearOutputContainsBuffer();
98
+ await run.outputContains("> "); // Wait until prompt appears a second time. If we hadn't cleared the buffer, this would resolve immediately.
99
+ ```
100
+
101
+ ### `RunContext#write(data: string | Buffer)`
102
+
103
+ Write some data into the process's STDIN stream.
104
+
105
+ #### Usage
106
+
107
+ ```js
108
+ const run = spawn("node", ["-i"]); // start Node.js REPL
109
+ await run.outputContains("> "); // Wait until prompt appears
110
+ run.write("2 + 2\n"); // Write 2 + 2 then press enter
111
+ await run.outputContains("4");
112
+ ```
113
+
114
+ ### `RunContext#close(stream: 'stdin' | 'stdout' | 'stderr')`
115
+
116
+ Close one of the processes's associated stdio streams.
117
+
118
+ #### Usage
119
+
120
+ ```js
121
+ const run = spawn("node", ["-i"]); // start Node.js REPL
122
+ await run.outputContains("> "); // Wait until prompt appears
123
+ run.close("stdin"); // Like pressing Ctrl+D; sends EOF
124
+ await run.completion;
125
+ ```
126
+
127
+ ### `RunContext#kill(signal?: string)`
128
+
129
+ Kills the process. If no signal is specified, it defaults to `"SIGINT"`.
130
+
131
+ #### Usage
132
+
133
+ ```js
134
+ const run = spawn("node", ["-i"]);
135
+ run.kill(); // Kill with SIGINT
136
+ // OR:
137
+ run.kill("SIGKILL"); // Kill with SIGKILL
138
+ ```
139
+
140
+ ## License
141
+
142
+ MIT
package/dist/index.d.ts CHANGED
@@ -1,66 +1,66 @@
1
- export type Options = {
2
- cwd?: string;
3
- env?: { [varName: string]: any };
4
- argv0?: string;
5
- detached?: boolean;
6
- uid?: number;
7
- gid?: number;
8
- shell?: boolean | string;
9
- windowsVerbatimArguments?: boolean;
10
- windowsHide?: boolean;
11
- pty?: boolean;
12
- };
13
-
14
- export type RunContext = {
15
- result: {
16
- stdout: string;
17
- stderr: string;
18
- code: null | number;
19
- error: boolean;
20
- };
21
- /**
22
- * Same as {@link RunContext.result}, but with {@link sanitizers} run on
23
- * stdout/stderr.
24
- */
25
- cleanResult(): {
26
- stdout: string;
27
- stderr: string;
28
- code: null | number;
29
- error: boolean;
30
- };
31
- completion: Promise<void>;
32
- debug(): RunContext;
33
- outputContains(value: string | RegExp): Promise<void>;
34
- clearOutputContainsBuffer(): void;
35
- // TODO: Should be string | Buffer, but idk how to use Buffer since they might not be using node types
36
- write(data: any): void;
37
- close(stream: "stdin" | "stdout" | "stderr"): void;
38
- kill(signal?: string): void;
39
- };
40
-
41
- export const spawn: ((cmd: string) => RunContext) &
42
- ((cmd: string, args: Array<string>) => RunContext) &
43
- ((cmd: string, options: Options) => RunContext) &
44
- ((cmd: string, args: Array<string>, options: Options) => RunContext);
45
-
46
- /**
47
- * An array of functions that will be run on stdout/stderr when calling
48
- * {@link RunContext.cleanResult}.
49
- *
50
- * By default, it contains 5 functions, which are run in order:
51
- *
52
- * - `stripAnsi`: Removes ANSI control characters
53
- * - `replaceRootDir`: Replaces eg `/home/suchipi/Code/first-base/src/index.js` with `<rootDir>/src/index.js`
54
- * - This function searches upwards for the root dir using a heuristic, and caches results in the {@link Map} `replaceRootDir.cache`.
55
- * - The heuristic is:
56
- * - Look upwards for a folder containing `.git` or `.hg`
57
- * - if none is found, look upwards for a folder containing `package-lock.json`, `.gitignore` or `.hgignore`,
58
- * - if none is found, look upwards for a folder containing `package.json` or `README.md`
59
- * - if none is found, consider the present working directory to be the root dir.
60
- * - `replaceCwd`: Replaces the current working directory with `<cwd>`
61
- * - `collapseStackTrace`: For Node.JS-style stack traces, replaces the long chain of "at ..." lines with a single "at somewhere" line
62
- * - `omitThrowLineNumber`: For Node.JS error source display, removes the line number
63
- *
64
- * You can remove them or replace them or add to them by mutating the `sanitizers` Array.
65
- */
66
- export const sanitizers: Array<(str: string) => string>;
1
+ export type Options = {
2
+ cwd?: string;
3
+ env?: { [varName: string]: any };
4
+ argv0?: string;
5
+ detached?: boolean;
6
+ uid?: number;
7
+ gid?: number;
8
+ shell?: boolean | string;
9
+ windowsVerbatimArguments?: boolean;
10
+ windowsHide?: boolean;
11
+ pty?: boolean;
12
+ };
13
+
14
+ export type RunContext = {
15
+ result: {
16
+ stdout: string;
17
+ stderr: string;
18
+ code: null | number;
19
+ error: boolean;
20
+ };
21
+ /**
22
+ * Same as {@link RunContext.result}, but with {@link sanitizers} run on
23
+ * stdout/stderr.
24
+ */
25
+ cleanResult(): {
26
+ stdout: string;
27
+ stderr: string;
28
+ code: null | number;
29
+ error: boolean;
30
+ };
31
+ completion: Promise<void>;
32
+ debug(): RunContext;
33
+ outputContains(value: string | RegExp): Promise<void>;
34
+ clearOutputContainsBuffer(): void;
35
+ // TODO: Should be string | Buffer, but idk how to use Buffer since they might not be using node types
36
+ write(data: any): void;
37
+ close(stream: "stdin" | "stdout" | "stderr"): void;
38
+ kill(signal?: string): void;
39
+ };
40
+
41
+ export const spawn: ((cmd: string) => RunContext) &
42
+ ((cmd: string, args: Array<string>) => RunContext) &
43
+ ((cmd: string, options: Options) => RunContext) &
44
+ ((cmd: string, args: Array<string>, options: Options) => RunContext);
45
+
46
+ /**
47
+ * An array of functions that will be run on stdout/stderr when calling
48
+ * {@link RunContext.cleanResult}.
49
+ *
50
+ * By default, it contains 5 functions, which are run in order:
51
+ *
52
+ * - `stripAnsi`: Removes ANSI control characters
53
+ * - `replaceRootDir`: Replaces eg `/home/suchipi/Code/first-base/src/index.js` with `<rootDir>/src/index.js`
54
+ * - This function searches upwards for the root dir using a heuristic, and caches results in the {@link Map} `replaceRootDir.cache`.
55
+ * - The heuristic is:
56
+ * - Look upwards for a folder containing `.git` or `.hg`
57
+ * - if none is found, look upwards for a folder containing `package-lock.json`, `.gitignore` or `.hgignore`,
58
+ * - if none is found, look upwards for a folder containing `package.json` or `README.md`
59
+ * - if none is found, consider the present working directory to be the root dir.
60
+ * - `replaceCwd`: Replaces the current working directory with `<cwd>`
61
+ * - `collapseStackTrace`: For Node.JS-style stack traces, replaces the long chain of "at ..." lines with a single "at somewhere" line
62
+ * - `omitThrowLineNumber`: For Node.JS error source display, removes the line number
63
+ *
64
+ * You can remove them or replace them or add to them by mutating the `sanitizers` Array.
65
+ */
66
+ export const sanitizers: Array<(str: string) => string>;
@@ -1,40 +1,40 @@
1
- // @flow
2
- export type Options = {
3
- cwd?: ?string,
4
- env?: ?{ [varName: string]: any },
5
- argv0?: ?string,
6
- detached?: ?boolean,
7
- uid?: ?number,
8
- gid?: ?number,
9
- shell?: ?boolean | string,
10
- windowsVerbatimArguments?: ?boolean,
11
- windowsHide?: ?boolean,
12
- pty?: ?boolean,
13
- };
14
-
15
- export type RunContext = {
16
- result: {
17
- stdout: string,
18
- stderr: string,
19
- code: null | number,
20
- error: boolean,
21
- },
22
- completion: Promise<void>,
23
- debug(): RunContext,
24
- outputContains(value: string | RegExp): Promise<void>,
25
- clearOutputContainsBuffer(): void,
26
- write(data: string | Buffer): void,
27
- close(stream: "stdin" | "stdout" | "stderr"): void,
28
- kill(signal?: string): void,
29
- };
30
-
31
- interface Exports {
32
- spawn(cmd: string): RunContext;
33
- spawn(cmd: string, args: Array<string>): RunContext;
34
- spawn(cmd: string, options: Options): RunContext;
35
- spawn(cmd: string, args: Array<string>, options: Options): RunContext;
36
-
37
- sanitizers: Array<(str: string) => string>;
38
- }
39
-
40
- declare module.exports: Exports;
1
+ // @flow
2
+ export type Options = {
3
+ cwd?: ?string,
4
+ env?: ?{ [varName: string]: any },
5
+ argv0?: ?string,
6
+ detached?: ?boolean,
7
+ uid?: ?number,
8
+ gid?: ?number,
9
+ shell?: ?boolean | string,
10
+ windowsVerbatimArguments?: ?boolean,
11
+ windowsHide?: ?boolean,
12
+ pty?: ?boolean,
13
+ };
14
+
15
+ export type RunContext = {
16
+ result: {
17
+ stdout: string,
18
+ stderr: string,
19
+ code: null | number,
20
+ error: boolean,
21
+ },
22
+ completion: Promise<void>,
23
+ debug(): RunContext,
24
+ outputContains(value: string | RegExp): Promise<void>,
25
+ clearOutputContainsBuffer(): void,
26
+ write(data: string | Buffer): void,
27
+ close(stream: "stdin" | "stdout" | "stderr"): void,
28
+ kill(signal?: string): void,
29
+ };
30
+
31
+ interface Exports {
32
+ spawn(cmd: string): RunContext;
33
+ spawn(cmd: string, args: Array<string>): RunContext;
34
+ spawn(cmd: string, options: Options): RunContext;
35
+ spawn(cmd: string, args: Array<string>, options: Options): RunContext;
36
+
37
+ sanitizers: Array<(str: string) => string>;
38
+ }
39
+
40
+ declare module.exports: Exports;
package/package.json CHANGED
@@ -1,36 +1,38 @@
1
- {
2
- "name": "first-base",
3
- "version": "1.5.1",
4
- "description": "Integration testing for CLI applications",
5
- "main": "dist/index.js",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/suchipi/first-base.git"
9
- },
10
- "author": "Lily Skye <me@suchipi.com>",
11
- "license": "MIT",
12
- "dependencies": {
13
- "node-pty": "^1.0.0",
14
- "strip-ansi": "^5.0.0",
15
- "nice-path": "^2.0.0"
16
- },
17
- "devDependencies": {
18
- "@babel/cli": "^7.23.4",
19
- "@babel/core": "^7.23.7",
20
- "@babel/preset-env": "^7.23.8",
21
- "babel-core": "^7.0.0-bridge.0",
22
- "babel-jest": "^29.7.0",
23
- "eslint": "^8.56.0",
24
- "eslint-config-unobtrusive": "^1.2.5",
25
- "eslint-plugin-import": "^2.29.1",
26
- "jest": "^29.7.0",
27
- "prettier": "^3.2.4"
28
- },
29
- "scripts": {
30
- "build": "mkdir -p dist; rm -rf dist/*; babel src --out-dir dist && cp src/index.js.flow dist/ && cp src/index.d.ts dist/",
31
- "test": "jest"
32
- },
33
- "jest": {
34
- "prettierPath": null
35
- }
36
- }
1
+ {
2
+ "name": "first-base",
3
+ "version": "1.5.2",
4
+ "description": "Integration testing for CLI applications",
5
+ "main": "dist/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/suchipi/first-base.git"
9
+ },
10
+ "author": "Lily Skye <me@suchipi.com>",
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "strip-ansi": "^5.0.0",
14
+ "nice-path": "^2.0.0"
15
+ },
16
+ "optionalDependencies": {
17
+ "node-pty": "^1.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "@babel/cli": "^7.23.4",
21
+ "@babel/core": "^7.23.7",
22
+ "@babel/preset-env": "^7.23.8",
23
+ "babel-core": "^7.0.0-bridge.0",
24
+ "babel-jest": "^29.7.0",
25
+ "eslint": "^8.56.0",
26
+ "eslint-config-unobtrusive": "^1.2.5",
27
+ "eslint-plugin-import": "^2.29.1",
28
+ "jest": "^29.7.0",
29
+ "prettier": "^3.2.4"
30
+ },
31
+ "scripts": {
32
+ "build": "mkdir -p dist; rm -rf dist/*; babel src --out-dir dist && cp src/index.js.flow dist/ && cp src/index.d.ts dist/",
33
+ "test": "jest"
34
+ },
35
+ "jest": {
36
+ "prettierPath": null
37
+ }
38
+ }