@augment-vir/node 31.73.5 → 32.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/dist/augments/fs/read-dir.js +9 -3
- package/dist/augments/path/contains.d.ts +11 -7
- package/dist/augments/path/contains.js +1 -1
- package/dist/augments/path/resolve-import.d.ts +4 -1
- package/dist/augments/path/resolve-import.js +7 -3
- package/dist/augments/terminal/run-cli-script.d.ts +5 -8
- package/dist/augments/terminal/run-cli-script.js +1 -8
- package/dist/augments/terminal/shell.d.ts +20 -11
- package/dist/augments/terminal/shell.js +10 -4
- package/package.json +6 -6
- package/src/augments/fs/read-dir.ts +12 -3
- package/src/augments/path/contains.ts +10 -6
- package/src/augments/path/resolve-import.ts +14 -10
- package/src/augments/terminal/run-cli-script.ts +5 -10
- package/src/augments/terminal/shell.ts +47 -27
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { readdir, stat } from 'node:fs/promises';
|
|
2
2
|
import { join, relative } from 'node:path';
|
|
3
|
-
async function internalReadDirPathsRecursive(dirPath, basePath) {
|
|
3
|
+
async function internalReadDirPathsRecursive({ dirPath, basePath, }) {
|
|
4
4
|
const dirContents = await readdir(dirPath);
|
|
5
5
|
const recursiveContents = (await Promise.all(dirContents.map(async (fileName) => {
|
|
6
6
|
const filePath = join(dirPath, fileName);
|
|
7
7
|
if ((await stat(filePath)).isDirectory()) {
|
|
8
|
-
return internalReadDirPathsRecursive(
|
|
8
|
+
return internalReadDirPathsRecursive({
|
|
9
|
+
dirPath: filePath,
|
|
10
|
+
basePath,
|
|
11
|
+
});
|
|
9
12
|
}
|
|
10
13
|
else {
|
|
11
14
|
return relative(basePath, filePath);
|
|
@@ -22,7 +25,10 @@ async function internalReadDirPathsRecursive(dirPath, basePath) {
|
|
|
22
25
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
23
26
|
*/
|
|
24
27
|
export async function readDirRecursive(dirPath) {
|
|
25
|
-
return await internalReadDirPathsRecursive(
|
|
28
|
+
return await internalReadDirPathsRecursive({
|
|
29
|
+
dirPath,
|
|
30
|
+
basePath: dirPath,
|
|
31
|
+
});
|
|
26
32
|
}
|
|
27
33
|
/**
|
|
28
34
|
* Reads all files within a single directory and filters them by the given extension or extensions.
|
|
@@ -6,11 +6,15 @@ import { type PartialWithUndefined } from '@augment-vir/common';
|
|
|
6
6
|
* @category Package : @augment-vir/node
|
|
7
7
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
8
8
|
*/
|
|
9
|
-
export declare function doesPathContain(potentialParentPath
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
export declare function doesPathContain({ potentialParentPath, potentialChildPath, options, }: Readonly<{
|
|
10
|
+
potentialParentPath: string;
|
|
11
|
+
potentialChildPath: string;
|
|
12
|
+
options?: PartialWithUndefined<{
|
|
13
|
+
/**
|
|
14
|
+
* Set this to `true` to return `true` if the paths are exactly equal.
|
|
15
|
+
*
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
allowSelf: boolean;
|
|
19
|
+
}>;
|
|
16
20
|
}>): boolean;
|
|
@@ -7,7 +7,7 @@ import { isOperatingSystem, OperatingSystem } from '../os/operating-system.js';
|
|
|
7
7
|
* @category Package : @augment-vir/node
|
|
8
8
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
9
9
|
*/
|
|
10
|
-
export function doesPathContain(potentialParentPath, potentialChildPath, options = {}) {
|
|
10
|
+
export function doesPathContain({ potentialParentPath, potentialChildPath, options = {}, }) {
|
|
11
11
|
if (!potentialParentPath || !potentialChildPath) {
|
|
12
12
|
return false;
|
|
13
13
|
}
|
|
@@ -8,4 +8,7 @@
|
|
|
8
8
|
* @returns `undefined` if no matches are found.
|
|
9
9
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
10
10
|
*/
|
|
11
|
-
export declare function resolveImportPath(importerFilePath
|
|
11
|
+
export declare function resolveImportPath({ importerFilePath, importPath, }: Readonly<{
|
|
12
|
+
importerFilePath: string;
|
|
13
|
+
importPath: string;
|
|
14
|
+
}>): string | undefined;
|
|
@@ -15,10 +15,14 @@ import { replaceWithWindowsPathIfNeeded } from './os-path.js';
|
|
|
15
15
|
* @returns `undefined` if no matches are found.
|
|
16
16
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
17
17
|
*/
|
|
18
|
-
export function resolveImportPath(importerFilePath, importPath) {
|
|
18
|
+
export function resolveImportPath({ importerFilePath, importPath, }) {
|
|
19
19
|
const foundTsconfig = readTsconfig(importerFilePath);
|
|
20
20
|
const mappedImportPath = foundTsconfig
|
|
21
|
-
? mapImportPath(
|
|
21
|
+
? mapImportPath({
|
|
22
|
+
importPath,
|
|
23
|
+
tsconfig: foundTsconfig.tsconfig,
|
|
24
|
+
tsconfigPath: foundTsconfig.path,
|
|
25
|
+
})
|
|
22
26
|
: importPath;
|
|
23
27
|
if (mappedImportPath.startsWith(sep) || mappedImportPath.startsWith('/')) {
|
|
24
28
|
/** Absolute import */
|
|
@@ -56,7 +60,7 @@ function mapFilePath(path) {
|
|
|
56
60
|
}
|
|
57
61
|
return path;
|
|
58
62
|
}
|
|
59
|
-
function mapImportPath(importPath, tsconfig, tsconfigPath) {
|
|
63
|
+
function mapImportPath({ importPath, tsconfig, tsconfigPath, }) {
|
|
60
64
|
const paths = tsconfig.options.paths;
|
|
61
65
|
if (!paths) {
|
|
62
66
|
return importPath;
|
|
@@ -16,11 +16,8 @@ export declare const ExtensionToRunner: Record<string, string | {
|
|
|
16
16
|
* @category Package : @augment-vir/node
|
|
17
17
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
18
18
|
*/
|
|
19
|
-
export declare function runCliScript(scriptPath
|
|
20
|
-
|
|
21
|
-
cliScriptFilePath: string
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* if there isn't one.
|
|
25
|
-
*/
|
|
26
|
-
binName: string | undefined): Promise<void>;
|
|
19
|
+
export declare function runCliScript({ scriptPath, cliScriptFilePath, binName, }: Readonly<{
|
|
20
|
+
scriptPath: string;
|
|
21
|
+
cliScriptFilePath: string;
|
|
22
|
+
binName: string | undefined;
|
|
23
|
+
}>): Promise<void>;
|
|
@@ -27,14 +27,7 @@ export const ExtensionToRunner = {
|
|
|
27
27
|
* @category Package : @augment-vir/node
|
|
28
28
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
29
29
|
*/
|
|
30
|
-
export async function runCliScript(scriptPath,
|
|
31
|
-
/** This should just be `__filename` (for CJS) or `import.meta.filename` (for ESM). */
|
|
32
|
-
cliScriptFilePath,
|
|
33
|
-
/**
|
|
34
|
-
* This should be the bin name of the package that is calling this function. Set to `undefined`
|
|
35
|
-
* if there isn't one.
|
|
36
|
-
*/
|
|
37
|
-
binName) {
|
|
30
|
+
export async function runCliScript({ scriptPath, cliScriptFilePath, binName, }) {
|
|
38
31
|
const args = extractRelevantArgs({
|
|
39
32
|
rawArgs: process.argv,
|
|
40
33
|
binName,
|
|
@@ -126,6 +126,20 @@ export declare class ShellTarget extends ListenTarget<ShellStdoutEvent | ShellSt
|
|
|
126
126
|
readonly childProcess: ChildProcess;
|
|
127
127
|
constructor(childProcess: ChildProcess);
|
|
128
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Options for {@link streamShellCommand}.
|
|
131
|
+
*
|
|
132
|
+
* @category Node : Terminal : Util
|
|
133
|
+
* @category Package : @augment-vir/node
|
|
134
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
135
|
+
*/
|
|
136
|
+
export type StreamShellCommandOptions = PartialWithUndefined<{
|
|
137
|
+
cwd: string;
|
|
138
|
+
env: NodeJS.ProcessEnv;
|
|
139
|
+
shell: string;
|
|
140
|
+
/** Automatically hook up stdout and stderr printing to the caller's console methods. */
|
|
141
|
+
hookUpToConsole: boolean;
|
|
142
|
+
}>;
|
|
129
143
|
/**
|
|
130
144
|
* Runs a shell command and returns a {@link ShellTarget} instance for directly hooking into shell
|
|
131
145
|
* events. This allows instant reactions to shell events but in a less convenient API compared to
|
|
@@ -137,7 +151,7 @@ export declare class ShellTarget extends ListenTarget<ShellStdoutEvent | ShellSt
|
|
|
137
151
|
* @see
|
|
138
152
|
* - {@link runShellCommand}: a higher level and more succinct way of running a shell command.
|
|
139
153
|
*/
|
|
140
|
-
export declare function streamShellCommand(command: string, cwd
|
|
154
|
+
export declare function streamShellCommand(command: string, { cwd, env, shell, hookUpToConsole, }?: StreamShellCommandOptions): ShellTarget;
|
|
141
155
|
/**
|
|
142
156
|
* Options for {@link runShellCommand}.
|
|
143
157
|
*
|
|
@@ -145,19 +159,14 @@ export declare function streamShellCommand(command: string, cwd?: string, shell?
|
|
|
145
159
|
* @category Package : @augment-vir/node
|
|
146
160
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
147
161
|
*/
|
|
148
|
-
export type RunShellCommandOptions = {
|
|
149
|
-
cwd?: string | undefined;
|
|
150
|
-
env?: NodeJS.ProcessEnv | undefined;
|
|
151
|
-
shell?: string | undefined;
|
|
152
|
-
/** Automatically hook up stdout and stderr printing to the caller's console methods. */
|
|
153
|
-
hookUpToConsole?: boolean | undefined;
|
|
162
|
+
export type RunShellCommandOptions = StreamShellCommandOptions & PartialWithUndefined<{
|
|
154
163
|
/** @default false */
|
|
155
|
-
rejectOnError
|
|
164
|
+
rejectOnError: boolean;
|
|
156
165
|
/** Callback to call whenever the shell logs to stdout. */
|
|
157
|
-
stdoutCallback
|
|
166
|
+
stdoutCallback: (stdout: string, childProcess: ChildProcess) => MaybePromise<void> | undefined;
|
|
158
167
|
/** Callback to call whenever the shell logs to stderr. */
|
|
159
|
-
stderrCallback
|
|
160
|
-
}
|
|
168
|
+
stderrCallback: (stderr: string, childProcess: ChildProcess) => MaybePromise<void> | undefined;
|
|
169
|
+
}>;
|
|
161
170
|
/**
|
|
162
171
|
* Runs a shell command and returns its output.
|
|
163
172
|
*
|
|
@@ -64,7 +64,7 @@ export class ShellTarget extends ListenTarget {
|
|
|
64
64
|
* @see
|
|
65
65
|
* - {@link runShellCommand}: a higher level and more succinct way of running a shell command.
|
|
66
66
|
*/
|
|
67
|
-
export function streamShellCommand(command, cwd, shell = 'bash',
|
|
67
|
+
export function streamShellCommand(command, { cwd, env = process.env, shell = 'bash', hookUpToConsole = false, } = {}) {
|
|
68
68
|
const stdio = hookUpToConsole ? [process.stdin] : undefined;
|
|
69
69
|
const childProcess = spawn(command, {
|
|
70
70
|
shell,
|
|
@@ -109,7 +109,9 @@ export function streamShellCommand(command, cwd, shell = 'bash', env = process.e
|
|
|
109
109
|
const exitCode = inputExitCode ?? undefined;
|
|
110
110
|
const exitSignal = inputExitSignal ?? undefined;
|
|
111
111
|
if ((exitCode !== undefined && exitCode !== 0) || exitSignal !== undefined) {
|
|
112
|
-
const execException = new Error(`Command failed: ${command}`)
|
|
112
|
+
const execException = Object.assign(new Error(`Command failed: ${command}`), {
|
|
113
|
+
cmd: command,
|
|
114
|
+
});
|
|
113
115
|
if (exitCode != undefined) {
|
|
114
116
|
execException.code = exitCode;
|
|
115
117
|
}
|
|
@@ -117,7 +119,6 @@ export function streamShellCommand(command, cwd, shell = 'bash', env = process.e
|
|
|
117
119
|
if (exitSignal != undefined) {
|
|
118
120
|
execException.signal = exitSignal;
|
|
119
121
|
}
|
|
120
|
-
execException.cmd = command;
|
|
121
122
|
execException.killed = childProcess.killed;
|
|
122
123
|
execException.cwd = cwd;
|
|
123
124
|
shellTarget.dispatch(new ShellErrorEvent({
|
|
@@ -147,7 +148,12 @@ export async function runShellCommand(command, options = {}) {
|
|
|
147
148
|
let stdout = '';
|
|
148
149
|
let stderr = '';
|
|
149
150
|
const errors = [];
|
|
150
|
-
const shellTarget = streamShellCommand(command,
|
|
151
|
+
const shellTarget = streamShellCommand(command, {
|
|
152
|
+
cwd: options.cwd,
|
|
153
|
+
shell: options.shell,
|
|
154
|
+
env: options.env,
|
|
155
|
+
hookUpToConsole: options.hookUpToConsole,
|
|
156
|
+
});
|
|
151
157
|
shellTarget.listen(ShellStdoutEvent, ({ detail: chunk }) => {
|
|
152
158
|
if (options.stdoutCallback) {
|
|
153
159
|
void options.stdoutCallback(chunk.toString(), shellTarget.childProcess);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/node",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "32.1.0",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
"test:update": "npm test"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@augment-vir/assert": "^
|
|
42
|
-
"@augment-vir/common": "^
|
|
43
|
-
"@date-vir/duration": "^8.
|
|
41
|
+
"@augment-vir/assert": "^32.1.0",
|
|
42
|
+
"@augment-vir/common": "^32.1.0",
|
|
43
|
+
"@date-vir/duration": "^8.6.1",
|
|
44
44
|
"ansi-styles": "^6.2.3",
|
|
45
45
|
"sanitize-filename": "^1.6.4",
|
|
46
46
|
"terminate": "^2.8.0",
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"typed-event-target": "^4.3.1"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@augment-vir/test": "^
|
|
53
|
-
"@types/node": "^
|
|
52
|
+
"@augment-vir/test": "^32.1.0",
|
|
53
|
+
"@types/node": "^26.0.1",
|
|
54
54
|
"@web/dev-server-esbuild": "^1.0.5",
|
|
55
55
|
"@web/test-runner": "^0.20.2",
|
|
56
56
|
"@web/test-runner-playwright": "^0.11.1",
|
|
@@ -2,14 +2,20 @@ import {readdir, stat} from 'node:fs/promises';
|
|
|
2
2
|
import {join, relative} from 'node:path';
|
|
3
3
|
import {type RequireExactlyOne} from 'type-fest';
|
|
4
4
|
|
|
5
|
-
async function internalReadDirPathsRecursive(
|
|
5
|
+
async function internalReadDirPathsRecursive({
|
|
6
|
+
dirPath,
|
|
7
|
+
basePath,
|
|
8
|
+
}: Readonly<{dirPath: string; basePath: string}>): Promise<string[]> {
|
|
6
9
|
const dirContents = await readdir(dirPath);
|
|
7
10
|
const recursiveContents: string[] = (
|
|
8
11
|
await Promise.all(
|
|
9
12
|
dirContents.map(async (fileName): Promise<string | ReadonlyArray<string>> => {
|
|
10
13
|
const filePath = join(dirPath, fileName);
|
|
11
14
|
if ((await stat(filePath)).isDirectory()) {
|
|
12
|
-
return internalReadDirPathsRecursive(
|
|
15
|
+
return internalReadDirPathsRecursive({
|
|
16
|
+
dirPath: filePath,
|
|
17
|
+
basePath,
|
|
18
|
+
});
|
|
13
19
|
} else {
|
|
14
20
|
return relative(basePath, filePath);
|
|
15
21
|
}
|
|
@@ -29,7 +35,10 @@ async function internalReadDirPathsRecursive(dirPath: string, basePath: string):
|
|
|
29
35
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
30
36
|
*/
|
|
31
37
|
export async function readDirRecursive(dirPath: string): Promise<string[]> {
|
|
32
|
-
return await internalReadDirPathsRecursive(
|
|
38
|
+
return await internalReadDirPathsRecursive({
|
|
39
|
+
dirPath,
|
|
40
|
+
basePath: dirPath,
|
|
41
|
+
});
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
/**
|
|
@@ -9,18 +9,22 @@ import {isOperatingSystem, OperatingSystem} from '../os/operating-system.js';
|
|
|
9
9
|
* @category Package : @augment-vir/node
|
|
10
10
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
11
11
|
*/
|
|
12
|
-
export function doesPathContain(
|
|
13
|
-
potentialParentPath
|
|
14
|
-
potentialChildPath
|
|
15
|
-
options
|
|
12
|
+
export function doesPathContain({
|
|
13
|
+
potentialParentPath,
|
|
14
|
+
potentialChildPath,
|
|
15
|
+
options = {},
|
|
16
|
+
}: Readonly<{
|
|
17
|
+
potentialParentPath: string;
|
|
18
|
+
potentialChildPath: string;
|
|
19
|
+
options?: PartialWithUndefined<{
|
|
16
20
|
/**
|
|
17
21
|
* Set this to `true` to return `true` if the paths are exactly equal.
|
|
18
22
|
*
|
|
19
23
|
* @default false
|
|
20
24
|
*/
|
|
21
25
|
allowSelf: boolean;
|
|
22
|
-
}
|
|
23
|
-
): boolean {
|
|
26
|
+
}>;
|
|
27
|
+
}>): boolean {
|
|
24
28
|
if (!potentialParentPath || !potentialChildPath) {
|
|
25
29
|
return false;
|
|
26
30
|
}
|
|
@@ -17,14 +17,18 @@ import {replaceWithWindowsPathIfNeeded} from './os-path.js';
|
|
|
17
17
|
* @returns `undefined` if no matches are found.
|
|
18
18
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
19
19
|
*/
|
|
20
|
-
export function resolveImportPath(
|
|
21
|
-
importerFilePath
|
|
22
|
-
importPath
|
|
23
|
-
): string | undefined {
|
|
20
|
+
export function resolveImportPath({
|
|
21
|
+
importerFilePath,
|
|
22
|
+
importPath,
|
|
23
|
+
}: Readonly<{importerFilePath: string; importPath: string}>): string | undefined {
|
|
24
24
|
const foundTsconfig = readTsconfig(importerFilePath);
|
|
25
25
|
|
|
26
26
|
const mappedImportPath = foundTsconfig
|
|
27
|
-
? mapImportPath(
|
|
27
|
+
? mapImportPath({
|
|
28
|
+
importPath,
|
|
29
|
+
tsconfig: foundTsconfig.tsconfig,
|
|
30
|
+
tsconfigPath: foundTsconfig.path,
|
|
31
|
+
})
|
|
28
32
|
: importPath;
|
|
29
33
|
|
|
30
34
|
if (mappedImportPath.startsWith(sep) || mappedImportPath.startsWith('/')) {
|
|
@@ -81,11 +85,11 @@ function mapFilePath(path: string): string {
|
|
|
81
85
|
return path;
|
|
82
86
|
}
|
|
83
87
|
|
|
84
|
-
function mapImportPath(
|
|
85
|
-
importPath
|
|
86
|
-
tsconfig
|
|
87
|
-
tsconfigPath
|
|
88
|
-
): string {
|
|
88
|
+
function mapImportPath({
|
|
89
|
+
importPath,
|
|
90
|
+
tsconfig,
|
|
91
|
+
tsconfigPath,
|
|
92
|
+
}: Readonly<{importPath: string; tsconfig: ParsedCommandLine; tsconfigPath: string}>): string {
|
|
89
93
|
const paths = tsconfig.options.paths;
|
|
90
94
|
if (!paths) {
|
|
91
95
|
return importPath;
|
|
@@ -30,16 +30,11 @@ export const ExtensionToRunner: Record<string, string | {npx: string}> = {
|
|
|
30
30
|
* @category Package : @augment-vir/node
|
|
31
31
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
32
32
|
*/
|
|
33
|
-
export async function runCliScript(
|
|
34
|
-
scriptPath
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* This should be the bin name of the package that is calling this function. Set to `undefined`
|
|
39
|
-
* if there isn't one.
|
|
40
|
-
*/
|
|
41
|
-
binName: string | undefined,
|
|
42
|
-
) {
|
|
33
|
+
export async function runCliScript({
|
|
34
|
+
scriptPath,
|
|
35
|
+
cliScriptFilePath,
|
|
36
|
+
binName,
|
|
37
|
+
}: Readonly<{scriptPath: string; cliScriptFilePath: string; binName: string | undefined}>) {
|
|
43
38
|
const args = extractRelevantArgs({
|
|
44
39
|
rawArgs: process.argv,
|
|
45
40
|
binName,
|
|
@@ -75,6 +75,21 @@ export class ShellTarget extends ListenTarget<
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Options for {@link streamShellCommand}.
|
|
80
|
+
*
|
|
81
|
+
* @category Node : Terminal : Util
|
|
82
|
+
* @category Package : @augment-vir/node
|
|
83
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
84
|
+
*/
|
|
85
|
+
export type StreamShellCommandOptions = PartialWithUndefined<{
|
|
86
|
+
cwd: string;
|
|
87
|
+
env: NodeJS.ProcessEnv;
|
|
88
|
+
shell: string;
|
|
89
|
+
/** Automatically hook up stdout and stderr printing to the caller's console methods. */
|
|
90
|
+
hookUpToConsole: boolean;
|
|
91
|
+
}>;
|
|
92
|
+
|
|
78
93
|
/**
|
|
79
94
|
* Runs a shell command and returns a {@link ShellTarget} instance for directly hooking into shell
|
|
80
95
|
* events. This allows instant reactions to shell events but in a less convenient API compared to
|
|
@@ -88,10 +103,12 @@ export class ShellTarget extends ListenTarget<
|
|
|
88
103
|
*/
|
|
89
104
|
export function streamShellCommand(
|
|
90
105
|
command: string,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
106
|
+
{
|
|
107
|
+
cwd,
|
|
108
|
+
env = process.env,
|
|
109
|
+
shell = 'bash',
|
|
110
|
+
hookUpToConsole = false,
|
|
111
|
+
}: StreamShellCommandOptions = {},
|
|
95
112
|
): ShellTarget {
|
|
96
113
|
const stdio = hookUpToConsole ? [process.stdin] : undefined;
|
|
97
114
|
|
|
@@ -147,8 +164,11 @@ export function streamShellCommand(
|
|
|
147
164
|
const exitSignal: NodeJS.Signals | undefined = inputExitSignal ?? undefined;
|
|
148
165
|
|
|
149
166
|
if ((exitCode !== undefined && exitCode !== 0) || exitSignal !== undefined) {
|
|
150
|
-
const execException: ExecException & {cwd?: string | undefined} =
|
|
151
|
-
`Command failed: ${command}
|
|
167
|
+
const execException: ExecException & {cwd?: string | undefined} = Object.assign(
|
|
168
|
+
new Error(`Command failed: ${command}`),
|
|
169
|
+
{
|
|
170
|
+
cmd: command,
|
|
171
|
+
},
|
|
152
172
|
);
|
|
153
173
|
if (exitCode != undefined) {
|
|
154
174
|
execException.code = exitCode;
|
|
@@ -157,7 +177,6 @@ export function streamShellCommand(
|
|
|
157
177
|
if (exitSignal != undefined) {
|
|
158
178
|
execException.signal = exitSignal;
|
|
159
179
|
}
|
|
160
|
-
execException.cmd = command;
|
|
161
180
|
execException.killed = childProcess.killed;
|
|
162
181
|
execException.cwd = cwd;
|
|
163
182
|
shellTarget.dispatch(
|
|
@@ -186,19 +205,21 @@ export function streamShellCommand(
|
|
|
186
205
|
* @category Package : @augment-vir/node
|
|
187
206
|
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
188
207
|
*/
|
|
189
|
-
export type RunShellCommandOptions =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
208
|
+
export type RunShellCommandOptions = StreamShellCommandOptions &
|
|
209
|
+
PartialWithUndefined<{
|
|
210
|
+
/** @default false */
|
|
211
|
+
rejectOnError: boolean;
|
|
212
|
+
/** Callback to call whenever the shell logs to stdout. */
|
|
213
|
+
stdoutCallback: (
|
|
214
|
+
stdout: string,
|
|
215
|
+
childProcess: ChildProcess,
|
|
216
|
+
) => MaybePromise<void> | undefined;
|
|
217
|
+
/** Callback to call whenever the shell logs to stderr. */
|
|
218
|
+
stderrCallback: (
|
|
219
|
+
stderr: string,
|
|
220
|
+
childProcess: ChildProcess,
|
|
221
|
+
) => MaybePromise<void> | undefined;
|
|
222
|
+
}>;
|
|
202
223
|
|
|
203
224
|
/**
|
|
204
225
|
* Runs a shell command and returns its output.
|
|
@@ -218,13 +239,12 @@ export async function runShellCommand(
|
|
|
218
239
|
let stderr = '';
|
|
219
240
|
const errors: Error[] = [];
|
|
220
241
|
|
|
221
|
-
const shellTarget = streamShellCommand(
|
|
222
|
-
|
|
223
|
-
options.
|
|
224
|
-
options.
|
|
225
|
-
options.
|
|
226
|
-
|
|
227
|
-
);
|
|
242
|
+
const shellTarget = streamShellCommand(command, {
|
|
243
|
+
cwd: options.cwd,
|
|
244
|
+
shell: options.shell,
|
|
245
|
+
env: options.env,
|
|
246
|
+
hookUpToConsole: options.hookUpToConsole,
|
|
247
|
+
});
|
|
228
248
|
|
|
229
249
|
shellTarget.listen(ShellStdoutEvent, ({detail: chunk}) => {
|
|
230
250
|
if (options.stdoutCallback) {
|