@vltpkg/promise-spawn 1.0.0-rc.22 → 1.0.0-rc.24

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.
@@ -0,0 +1,153 @@
1
+ import type { ChildProcess, IOType, SpawnOptions, StdioOptions } from 'node:child_process';
2
+ export type IOTypeNoPipe = Exclude<IOType, IOTypePipe>;
3
+ export type IOTypePipe = 'overlapped' | 'pipe' | null | undefined;
4
+ export interface PromiseSpawnOptions extends SpawnOptions {
5
+ stdioString?: boolean;
6
+ acceptFail?: boolean;
7
+ }
8
+ export interface PromiseSpawnOptionsString extends PromiseSpawnOptions {
9
+ stdioString?: true;
10
+ }
11
+ export interface PromiseSpawnOptionsBuffer extends PromiseSpawnOptions {
12
+ stdioString: false;
13
+ }
14
+ export interface PromiseSpawnOptionsStdin extends PromiseSpawnOptions {
15
+ stdio?: Exclude<IOTypePipe, null> | [
16
+ stdin?: IOTypePipe,
17
+ stdout?: Exclude<StdioOptions, IOType>[number],
18
+ stderr?: Exclude<StdioOptions, IOType>[number]
19
+ ];
20
+ }
21
+ export interface PromiseSpawnOptionsNoStdin extends PromiseSpawnOptions {
22
+ stdio: IOTypeNoPipe | IOTypeNoPipe[] | [
23
+ stdin: IOTypeNoPipe | number,
24
+ stdout?: Exclude<StdioOptions, IOType>[number],
25
+ stderr?: Exclude<StdioOptions, IOType>[number]
26
+ ];
27
+ }
28
+ export interface PromiseSpawnOptionsStdout extends PromiseSpawnOptions {
29
+ stdio?: Exclude<IOTypePipe, null> | [
30
+ stdin: Exclude<StdioOptions, IOType>[number] | undefined,
31
+ stdout?: IOTypePipe,
32
+ stderr?: Exclude<StdioOptions, IOType>[number]
33
+ ];
34
+ }
35
+ export interface PromiseSpawnOptionsStdoutString extends PromiseSpawnOptionsStdout {
36
+ stdioString?: true;
37
+ }
38
+ export interface PromiseSpawnOptionsStdoutBuffer extends PromiseSpawnOptionsStdout {
39
+ stdioString: false;
40
+ }
41
+ export interface PromiseSpawnOptionsNoStdout extends PromiseSpawnOptions {
42
+ stdio: IOTypeNoPipe | IOTypeNoPipe[] | [
43
+ stdin: Exclude<StdioOptions, IOType>[number] | undefined,
44
+ stdout?: IOTypeNoPipe,
45
+ stderr?: Exclude<StdioOptions, IOType>[number]
46
+ ];
47
+ }
48
+ export interface PromiseSpawnOptionsStderr extends PromiseSpawnOptions {
49
+ stdio?: Exclude<IOTypePipe, null> | [
50
+ stdin: Exclude<StdioOptions, IOType>[number] | undefined,
51
+ stdout: Exclude<StdioOptions, IOType>[number] | undefined,
52
+ stderr?: IOTypePipe
53
+ ];
54
+ }
55
+ export interface PromiseSpawnOptionsStderrString extends PromiseSpawnOptionsStderr {
56
+ stdioString?: true;
57
+ }
58
+ export interface PromiseSpawnOptionsStderrBuffer extends PromiseSpawnOptionsStderr {
59
+ stdioString: false;
60
+ }
61
+ export interface PromiseSpawnOptionsNoStderr extends PromiseSpawnOptions {
62
+ stdio: IOTypeNoPipe | IOTypeNoPipe[] | [
63
+ stdin: Exclude<StdioOptions, IOType>[number] | undefined,
64
+ stdout: Exclude<StdioOptions, IOType>[number] | undefined,
65
+ stderr: IOTypeNoPipe
66
+ ];
67
+ }
68
+ export interface SpawnResult {
69
+ command: string;
70
+ args: string[];
71
+ cwd: string;
72
+ status: number | null;
73
+ signal: NodeJS.Signals | null;
74
+ stdout: Buffer | string | null;
75
+ stderr: Buffer | string | null;
76
+ }
77
+ export interface SpawnResultString extends SpawnResult {
78
+ stdout: string | null;
79
+ stderr: string | null;
80
+ }
81
+ export interface SpawnResultBuffer extends SpawnResult {
82
+ stdout: Buffer | null;
83
+ stderr: Buffer | null;
84
+ }
85
+ export interface SpawnResultStdout extends SpawnResult {
86
+ stdout: Buffer | string;
87
+ }
88
+ export interface SpawnResultStdoutString extends SpawnResultString {
89
+ stdout: string;
90
+ }
91
+ export interface SpawnResultStdoutBuffer extends SpawnResultBuffer {
92
+ stdout: Buffer;
93
+ }
94
+ export interface SpawnResultNoStdout extends SpawnResult {
95
+ stdout: null;
96
+ }
97
+ export interface SpawnResultStderr extends SpawnResult {
98
+ stderr: Buffer | string;
99
+ }
100
+ export interface SpawnResultStderrString extends SpawnResultString {
101
+ stderr: string;
102
+ }
103
+ export interface SpawnResultStderrBuffer extends SpawnResultBuffer {
104
+ stderr: Buffer;
105
+ }
106
+ export interface SpawnResultNoStderr extends SpawnResult {
107
+ stderr: null;
108
+ }
109
+ export interface SpawnResultNoStdio extends SpawnResult {
110
+ stderr: null;
111
+ stdout: null;
112
+ }
113
+ export interface SpawnResultStdioStrings extends SpawnResult {
114
+ stdout: string;
115
+ stderr: string;
116
+ }
117
+ export interface SpawnResultStdioBuffers extends SpawnResult {
118
+ stdout: Buffer;
119
+ stderr: Buffer;
120
+ }
121
+ export type Override<T, R> = Omit<T, keyof R> & R;
122
+ export type SpawnResultByOptions<T extends PromiseSpawnOptions> = Override<SpawnResult, {
123
+ stdout: T extends PromiseSpawnOptionsNoStdout ? null : T extends PromiseSpawnOptionsStdoutBuffer ? Buffer : T extends PromiseSpawnOptionsStdoutString ? string : T extends PromiseSpawnOptionsBuffer ? Buffer | null : T extends PromiseSpawnOptionsString ? string | null : Buffer | string | null;
124
+ stderr: T extends PromiseSpawnOptionsNoStderr ? null : T extends PromiseSpawnOptionsStderrBuffer ? Buffer : T extends PromiseSpawnOptionsStderrString ? string : T extends PromiseSpawnOptionsBuffer ? Buffer | null : T extends PromiseSpawnOptionsString ? string | null : Buffer | string | null;
125
+ }>;
126
+ export type ChildProcessByOptions<T extends PromiseSpawnOptions> = Override<ChildProcess, {
127
+ stdin: T extends PromiseSpawnOptionsNoStdin ? null : Exclude<ChildProcess['stdin'], null>;
128
+ stdout: T extends PromiseSpawnOptionsNoStdout ? null : Exclude<ChildProcess['stdout'], null>;
129
+ stderr: T extends PromiseSpawnOptionsNoStderr ? null : Exclude<ChildProcess['stderr'], null>;
130
+ }>;
131
+ /**
132
+ * Subtype of Promise returned by {@link promiseSpawn}.
133
+ *
134
+ * Resolution value is inferred from the provided options.
135
+ */
136
+ export declare class SpawnPromise<O extends PromiseSpawnOptions, T extends object = object> extends Promise<SpawnResultByOptions<O> & T> {
137
+ [Symbol.toStringTag]: string;
138
+ /** The spawned process this promise references */
139
+ process: ChildProcessByOptions<O>;
140
+ /** Expose the child process stdin, if available */
141
+ stdin: ChildProcessByOptions<O>['stdin'];
142
+ /**
143
+ * Set static `Symbol.species` back to the base Promise class so that
144
+ * v8 doesn't get confused by the changed constructor signature.
145
+ */
146
+ static get [Symbol.species](): PromiseConstructor;
147
+ constructor(command: string, args: string[], opts: O, extra?: T);
148
+ }
149
+ /**
150
+ * Spawn the specified command, and return a promise that resolves when
151
+ * the process closes or has an error.
152
+ */
153
+ export declare function promiseSpawn<O extends PromiseSpawnOptions = PromiseSpawnOptionsStderrString & PromiseSpawnOptionsStdoutString, E extends object = object>(command: string, args: string[], opts?: O, extra?: E): SpawnPromise<O, E>;
package/dist/index.js ADDED
@@ -0,0 +1,106 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { spawn } from 'node:child_process';
3
+ const isPipe = (stdio = 'pipe', fd) => stdio === 'pipe' || stdio === 'overlapped' || stdio === null ? true
4
+ : Array.isArray(stdio) ? isPipe(stdio[fd], fd)
5
+ : false;
6
+ function stdioResult(stdout, stderr, o) {
7
+ return {
8
+ stdout: (!isPipe(o.stdio, 1) ? null
9
+ : o.stdioString !== false ?
10
+ Buffer.concat(stdout).toString().trim()
11
+ : Buffer.concat(stdout)),
12
+ stderr: (!isPipe(o.stdio, 2) ? null
13
+ : o.stdioString !== false ?
14
+ Buffer.concat(stderr).toString().trim()
15
+ : Buffer.concat(stderr)),
16
+ };
17
+ }
18
+ /**
19
+ * Subtype of Promise returned by {@link promiseSpawn}.
20
+ *
21
+ * Resolution value is inferred from the provided options.
22
+ */
23
+ export class SpawnPromise extends Promise {
24
+ [Symbol.toStringTag] = 'SpawnPromise';
25
+ /** The spawned process this promise references */
26
+ process;
27
+ /** Expose the child process stdin, if available */
28
+ stdin;
29
+ /**
30
+ * Set static `Symbol.species` back to the base Promise class so that
31
+ * v8 doesn't get confused by the changed constructor signature.
32
+ */
33
+ static get [Symbol.species]() {
34
+ return Promise;
35
+ }
36
+ constructor(command, args, opts, extra = {}) {
37
+ let proc;
38
+ super((res, rej) => {
39
+ proc = spawn(command, args, opts);
40
+ const stdout = [];
41
+ const stderr = [];
42
+ // Don't use the spread operator on successResult or errorResult
43
+ // as that kills any typechecking for the known keys.
44
+ const reject = (er, status, signal) => {
45
+ const stdio = stdioResult(stdout, stderr, opts);
46
+ const errorResult = {
47
+ command,
48
+ args,
49
+ stdout: stdio.stdout,
50
+ stderr: stdio.stderr,
51
+ cwd: opts.cwd?.toString() ?? process.cwd(),
52
+ };
53
+ if (er !== undefined) {
54
+ errorResult.cause = er;
55
+ }
56
+ if (status !== undefined) {
57
+ errorResult.status = status;
58
+ }
59
+ /* c8 ignore next 3 - hard to test on win32 so coverage fails */
60
+ if (signal !== undefined) {
61
+ errorResult.signal = signal;
62
+ }
63
+ Object.assign(errorResult, extra);
64
+ rej(error('command failed', errorResult));
65
+ };
66
+ const resolve = (status, signal) => {
67
+ const stdio = stdioResult(stdout, stderr, opts);
68
+ const successResult = {
69
+ command,
70
+ args,
71
+ status,
72
+ signal,
73
+ stdout: stdio.stdout,
74
+ stderr: stdio.stderr,
75
+ cwd: opts.cwd?.toString() ?? process.cwd(),
76
+ };
77
+ Object.assign(successResult, extra);
78
+ res(successResult);
79
+ };
80
+ proc.on('error', reject);
81
+ proc.stdout
82
+ ?.on('data', c => stdout.push(c))
83
+ .on('error', reject);
84
+ proc.stderr
85
+ ?.on('data', c => stderr.push(c))
86
+ .on('error', reject);
87
+ proc.on('close', (status, signal) => {
88
+ if ((status || signal) && !opts.acceptFail) {
89
+ reject(undefined, status, signal);
90
+ }
91
+ else {
92
+ resolve(status, signal);
93
+ }
94
+ });
95
+ });
96
+ this.process = proc;
97
+ this.stdin = proc.stdin;
98
+ }
99
+ }
100
+ /**
101
+ * Spawn the specified command, and return a promise that resolves when
102
+ * the process closes or has an error.
103
+ */
104
+ export function promiseSpawn(command, args, opts = {}, extra = {}) {
105
+ return new SpawnPromise(command, args, opts, extra);
106
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/promise-spawn",
3
3
  "description": "Spawn a process and return a promise that resolves when it finishes",
4
- "version": "1.0.0-rc.22",
4
+ "version": "1.0.0-rc.24",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -12,7 +12,7 @@
12
12
  "email": "support@vlt.sh"
13
13
  },
14
14
  "dependencies": {
15
- "@vltpkg/error-cause": "1.0.0-rc.22"
15
+ "@vltpkg/error-cause": "1.0.0-rc.24"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@eslint/js": "^9.39.1",
@@ -44,13 +44,13 @@
44
44
  "extends": "../../tap-config.yaml"
45
45
  },
46
46
  "prettier": "../../.prettierrc.js",
47
- "module": "./src/index.ts",
47
+ "module": "./dist/index.js",
48
48
  "type": "module",
49
49
  "exports": {
50
50
  "./package.json": "./package.json",
51
51
  ".": {
52
52
  "import": {
53
- "default": "./src/index.ts"
53
+ "default": "./dist/index.js"
54
54
  }
55
55
  }
56
56
  },