soda-nodejs 0.4.3 → 0.5.1
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/cjs/utils/spawnAsync.d.ts +11 -0
- package/dist/cjs/utils/spawnAsync.js +23 -2
- package/dist/cjs/utils/spawnAsync.js.map +2 -2
- package/dist/esm/utils/spawnAsync.d.ts +11 -0
- package/dist/esm/utils/spawnAsync.js +32 -2
- package/dist/esm/utils/spawnAsync.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/spawnAsync.ts +41 -3
|
@@ -5,6 +5,17 @@ import { Readable, Writable } from "stream";
|
|
|
5
5
|
export interface PromiseWithChildProcess<T> extends Promise<T> {
|
|
6
6
|
child: T;
|
|
7
7
|
}
|
|
8
|
+
export type Options = SpawnOptionsWithoutStdio | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe> | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull> | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe> | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe> | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull> | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull> | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe> | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull> | SpawnOptions;
|
|
9
|
+
/**
|
|
10
|
+
* @description get default options for spawnAsync
|
|
11
|
+
* @returns {Options} options
|
|
12
|
+
*/
|
|
13
|
+
export declare function getDefaultOptions(): Options;
|
|
14
|
+
/**
|
|
15
|
+
* @description set default options for spawnAsync
|
|
16
|
+
* @param {Options | ((prev: Options) => Options)} options
|
|
17
|
+
*/
|
|
18
|
+
export declare function setDefaultOptions(options: Options | ((prev: Options) => Options)): SpawnOptions | undefined;
|
|
8
19
|
export declare function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>;
|
|
9
20
|
export declare function spawnAsync(command: string, options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>;
|
|
10
21
|
export declare function spawnAsync(command: string, options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>;
|
|
@@ -19,14 +19,32 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/utils/spawnAsync.ts
|
|
20
20
|
var spawnAsync_exports = {};
|
|
21
21
|
__export(spawnAsync_exports, {
|
|
22
|
+
getDefaultOptions: () => getDefaultOptions,
|
|
23
|
+
setDefaultOptions: () => setDefaultOptions,
|
|
22
24
|
spawnAsync: () => spawnAsync
|
|
23
25
|
});
|
|
24
26
|
module.exports = __toCommonJS(spawnAsync_exports);
|
|
25
27
|
var import_child_process = require("child_process");
|
|
28
|
+
var defaultOptions = {};
|
|
29
|
+
function getDefaultOptions() {
|
|
30
|
+
return defaultOptions;
|
|
31
|
+
}
|
|
32
|
+
function setDefaultOptions(options) {
|
|
33
|
+
if (typeof options === "function") {
|
|
34
|
+
defaultOptions = options(defaultOptions);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
defaultOptions = options;
|
|
38
|
+
return defaultOptions;
|
|
39
|
+
}
|
|
26
40
|
function spawnAsync(command, args, options) {
|
|
41
|
+
let child;
|
|
27
42
|
const promise = new Promise((resolve, reject) => {
|
|
28
|
-
|
|
29
|
-
|
|
43
|
+
if (Array.isArray(args))
|
|
44
|
+
options = { ...defaultOptions, ...options };
|
|
45
|
+
else
|
|
46
|
+
args = { ...defaultOptions, ...args };
|
|
47
|
+
child = (0, import_child_process.spawn)(command, args, options);
|
|
30
48
|
child.on("exit", (code) => {
|
|
31
49
|
if (code === 0)
|
|
32
50
|
return resolve(child);
|
|
@@ -35,10 +53,13 @@ function spawnAsync(command, args, options) {
|
|
|
35
53
|
return;
|
|
36
54
|
});
|
|
37
55
|
});
|
|
56
|
+
promise.child = child;
|
|
38
57
|
return promise;
|
|
39
58
|
}
|
|
40
59
|
// Annotate the CommonJS export names for ESM import in node:
|
|
41
60
|
0 && (module.exports = {
|
|
61
|
+
getDefaultOptions,
|
|
62
|
+
setDefaultOptions,
|
|
42
63
|
spawnAsync
|
|
43
64
|
});
|
|
44
65
|
//# sourceMappingURL=spawnAsync.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/utils/spawnAsync.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n ChildProcess,\n ChildProcessByStdio,\n ChildProcessWithoutNullStreams,\n SpawnOptions,\n SpawnOptionsWithStdioTuple,\n SpawnOptionsWithoutStdio,\n StdioNull,\n StdioPipe,\n spawn,\n} from \"child_process\"\nimport { Readable, Writable } from \"stream\"\n\nexport interface PromiseWithChildProcess<T> extends Promise<T> {\n child: T\n}\n\nexport function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>\nexport function spawnAsync(command: string, options: SpawnOptions): PromiseWithChildProcess<ChildProcess>\nexport function spawnAsync(\n command: string,\n args?: readonly string[],\n options?: SpawnOptionsWithoutStdio,\n): PromiseWithChildProcess<ChildProcessWithoutNullStreams>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>\nexport function spawnAsync(command: string, args: readonly string[], options: SpawnOptions): PromiseWithChildProcess<ChildProcess>\n\n/**\n * @description wait for the command to exit\n * @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.\n */\nexport function spawnAsync(command: string, args?: any, options?: any): Promise<any> {\n const promise = new Promise<any>((resolve, reject) => {\n
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAUO;
|
|
4
|
+
"sourcesContent": ["import {\n ChildProcess,\n ChildProcessByStdio,\n ChildProcessWithoutNullStreams,\n SpawnOptions,\n SpawnOptionsWithStdioTuple,\n SpawnOptionsWithoutStdio,\n StdioNull,\n StdioPipe,\n spawn,\n} from \"child_process\"\nimport { Readable, Writable } from \"stream\"\n\nexport interface PromiseWithChildProcess<T> extends Promise<T> {\n child: T\n}\n\nexport type Options =\n | SpawnOptionsWithoutStdio\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>\n | SpawnOptions\n\nlet defaultOptions: Options = {}\n\n/**\n * @description get default options for spawnAsync\n * @returns {Options} options\n */\nexport function getDefaultOptions(): Options {\n return defaultOptions\n}\n\n/**\n * @description set default options for spawnAsync\n * @param {Options | ((prev: Options) => Options)} options\n */\nexport function setDefaultOptions(options: Options | ((prev: Options) => Options)) {\n if (typeof options === \"function\") {\n defaultOptions = options(defaultOptions)\n return\n }\n defaultOptions = options\n return defaultOptions\n}\n\nexport function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>\nexport function spawnAsync(command: string, options: SpawnOptions): PromiseWithChildProcess<ChildProcess>\nexport function spawnAsync(\n command: string,\n args?: readonly string[],\n options?: SpawnOptionsWithoutStdio,\n): PromiseWithChildProcess<ChildProcessWithoutNullStreams>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>\nexport function spawnAsync(command: string, args: readonly string[], options: SpawnOptions): PromiseWithChildProcess<ChildProcess>\n\n/**\n * @description wait for the command to exit\n * @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.\n */\nexport function spawnAsync(command: string, args?: any, options?: any): Promise<any> {\n let child: any\n const promise = new Promise<any>((resolve, reject) => {\n if (Array.isArray(args)) options = { ...defaultOptions, ...options }\n else args = { ...defaultOptions, ...args }\n child = spawn(command, args, options)\n child.on(\"exit\", (code: number) => {\n if (code === 0) return resolve(child)\n console.error(`\"${command}\" Command failed with code ${code}`)\n reject(new Error(`\"${command}\" Command failed with code ${code}`))\n return\n })\n }) as PromiseWithChildProcess<any>\n promise.child = child\n return promise\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAUO;AAmBP,IAAI,iBAA0B,CAAC;AAMxB,SAAS,oBAA6B;AACzC,SAAO;AACX;AAMO,SAAS,kBAAkB,SAAiD;AAC/E,MAAI,OAAO,YAAY,YAAY;AAC/B,qBAAiB,QAAQ,cAAc;AACvC;AAAA,EACJ;AACA,mBAAiB;AACjB,SAAO;AACX;AAuFO,SAAS,WAAW,SAAiB,MAAY,SAA6B;AACjF,MAAI;AACJ,QAAM,UAAU,IAAI,QAAa,CAAC,SAAS,WAAW;AAClD,QAAI,MAAM,QAAQ,IAAI;AAAG,gBAAU,EAAE,GAAG,gBAAgB,GAAG,QAAQ;AAAA;AAC9D,aAAO,EAAE,GAAG,gBAAgB,GAAG,KAAK;AACzC,gBAAQ,4BAAM,SAAS,MAAM,OAAO;AACpC,UAAM,GAAG,QAAQ,CAAC,SAAiB;AAC/B,UAAI,SAAS;AAAG,eAAO,QAAQ,KAAK;AACpC,cAAQ,MAAM,IAAI,qCAAqC,MAAM;AAC7D,aAAO,IAAI,MAAM,IAAI,qCAAqC,MAAM,CAAC;AACjE;AAAA,IACJ,CAAC;AAAA,EACL,CAAC;AACD,UAAQ,QAAQ;AAChB,SAAO;AACX;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,6 +5,17 @@ import { Readable, Writable } from "stream";
|
|
|
5
5
|
export interface PromiseWithChildProcess<T> extends Promise<T> {
|
|
6
6
|
child: T;
|
|
7
7
|
}
|
|
8
|
+
export type Options = SpawnOptionsWithoutStdio | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe> | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull> | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe> | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe> | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull> | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull> | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe> | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull> | SpawnOptions;
|
|
9
|
+
/**
|
|
10
|
+
* @description get default options for spawnAsync
|
|
11
|
+
* @returns {Options} options
|
|
12
|
+
*/
|
|
13
|
+
export declare function getDefaultOptions(): Options;
|
|
14
|
+
/**
|
|
15
|
+
* @description set default options for spawnAsync
|
|
16
|
+
* @param {Options | ((prev: Options) => Options)} options
|
|
17
|
+
*/
|
|
18
|
+
export declare function setDefaultOptions(options: Options | ((prev: Options) => Options)): SpawnOptions | undefined;
|
|
8
19
|
export declare function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>;
|
|
9
20
|
export declare function spawnAsync(command: string, options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>;
|
|
10
21
|
export declare function spawnAsync(command: string, options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>;
|
|
@@ -1,12 +1,41 @@
|
|
|
1
1
|
import { spawn } from "child_process";
|
|
2
|
+
let defaultOptions = {};
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description get default options for spawnAsync
|
|
6
|
+
* @returns {Options} options
|
|
7
|
+
*/
|
|
8
|
+
export function getDefaultOptions() {
|
|
9
|
+
return defaultOptions;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @description set default options for spawnAsync
|
|
14
|
+
* @param {Options | ((prev: Options) => Options)} options
|
|
15
|
+
*/
|
|
16
|
+
export function setDefaultOptions(options) {
|
|
17
|
+
if (typeof options === "function") {
|
|
18
|
+
defaultOptions = options(defaultOptions);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
defaultOptions = options;
|
|
22
|
+
return defaultOptions;
|
|
23
|
+
}
|
|
2
24
|
/**
|
|
3
25
|
* @description wait for the command to exit
|
|
4
26
|
* @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.
|
|
5
27
|
*/
|
|
6
28
|
export function spawnAsync(command, args, options) {
|
|
29
|
+
let child;
|
|
7
30
|
const promise = new Promise((resolve, reject) => {
|
|
8
|
-
|
|
9
|
-
|
|
31
|
+
if (Array.isArray(args)) options = {
|
|
32
|
+
...defaultOptions,
|
|
33
|
+
...options
|
|
34
|
+
};else args = {
|
|
35
|
+
...defaultOptions,
|
|
36
|
+
...args
|
|
37
|
+
};
|
|
38
|
+
child = spawn(command, args, options);
|
|
10
39
|
child.on("exit", code => {
|
|
11
40
|
if (code === 0) return resolve(child);
|
|
12
41
|
console.error(`"${command}" Command failed with code ${code}`);
|
|
@@ -14,6 +43,7 @@ export function spawnAsync(command, args, options) {
|
|
|
14
43
|
return;
|
|
15
44
|
});
|
|
16
45
|
});
|
|
46
|
+
promise.child = child;
|
|
17
47
|
return promise;
|
|
18
48
|
}
|
|
19
49
|
//# sourceMappingURL=spawnAsync.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["spawn","spawnAsync","command","args","
|
|
1
|
+
{"version":3,"names":["spawn","defaultOptions","getDefaultOptions","setDefaultOptions","options","spawnAsync","command","args","child","promise","Promise","resolve","reject","Array","isArray","on","code","console","error","Error"],"sources":["../../../src/utils/spawnAsync.ts"],"sourcesContent":["import {\n ChildProcess,\n ChildProcessByStdio,\n ChildProcessWithoutNullStreams,\n SpawnOptions,\n SpawnOptionsWithStdioTuple,\n SpawnOptionsWithoutStdio,\n StdioNull,\n StdioPipe,\n spawn,\n} from \"child_process\"\nimport { Readable, Writable } from \"stream\"\n\nexport interface PromiseWithChildProcess<T> extends Promise<T> {\n child: T\n}\n\nexport type Options =\n | SpawnOptionsWithoutStdio\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>\n | SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>\n | SpawnOptions\n\nlet defaultOptions: Options = {}\n\n/**\n * @description get default options for spawnAsync\n * @returns {Options} options\n */\nexport function getDefaultOptions(): Options {\n return defaultOptions\n}\n\n/**\n * @description set default options for spawnAsync\n * @param {Options | ((prev: Options) => Options)} options\n */\nexport function setDefaultOptions(options: Options | ((prev: Options) => Options)) {\n if (typeof options === \"function\") {\n defaultOptions = options(defaultOptions)\n return\n }\n defaultOptions = options\n return defaultOptions\n}\n\nexport function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>\nexport function spawnAsync(\n command: string,\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>\nexport function spawnAsync(command: string, options: SpawnOptions): PromiseWithChildProcess<ChildProcess>\nexport function spawnAsync(\n command: string,\n args?: readonly string[],\n options?: SpawnOptionsWithoutStdio,\n): PromiseWithChildProcess<ChildProcessWithoutNullStreams>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>\nexport function spawnAsync(\n command: string,\n args: readonly string[],\n options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,\n): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>\nexport function spawnAsync(command: string, args: readonly string[], options: SpawnOptions): PromiseWithChildProcess<ChildProcess>\n\n/**\n * @description wait for the command to exit\n * @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.\n */\nexport function spawnAsync(command: string, args?: any, options?: any): Promise<any> {\n let child: any\n const promise = new Promise<any>((resolve, reject) => {\n if (Array.isArray(args)) options = { ...defaultOptions, ...options }\n else args = { ...defaultOptions, ...args }\n child = spawn(command, args, options)\n child.on(\"exit\", (code: number) => {\n if (code === 0) return resolve(child)\n console.error(`\"${command}\" Command failed with code ${code}`)\n reject(new Error(`\"${command}\" Command failed with code ${code}`))\n return\n })\n }) as PromiseWithChildProcess<any>\n promise.child = child\n return promise\n}\n"],"mappings":"AAAA,SASIA,KAAK,QACF,eAAe;AAmBtB,IAAIC,cAAuB,GAAG,CAAC,CAAC;;AAEhC;AACA;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAAA,EAAY;EACzC,OAAOD,cAAc;AACzB;;AAEA;AACA;AACA;AACA;AACA,OAAO,SAASE,iBAAiBA,CAACC,OAA+C,EAAE;EAC/E,IAAI,OAAOA,OAAO,KAAK,UAAU,EAAE;IAC/BH,cAAc,GAAGG,OAAO,CAACH,cAAc,CAAC;IACxC;EACJ;EACAA,cAAc,GAAGG,OAAO;EACxB,OAAOH,cAAc;AACzB;AAmFA;AACA;AACA;AACA;AACA,OAAO,SAASI,UAAUA,CAACC,OAAe,EAAEC,IAAU,EAAEH,OAAa,EAAgB;EACjF,IAAII,KAAU;EACd,MAAMC,OAAO,GAAG,IAAIC,OAAO,CAAM,CAACC,OAAO,EAAEC,MAAM,KAAK;IAClD,IAAIC,KAAK,CAACC,OAAO,CAACP,IAAI,CAAC,EAAEH,OAAO,GAAG;MAAE,GAAGH,cAAc;MAAE,GAAGG;IAAQ,CAAC,MAC/DG,IAAI,GAAG;MAAE,GAAGN,cAAc;MAAE,GAAGM;IAAK,CAAC;IAC1CC,KAAK,GAAGR,KAAK,CAACM,OAAO,EAAEC,IAAI,EAAEH,OAAO,CAAC;IACrCI,KAAK,CAACO,EAAE,CAAC,MAAM,EAAGC,IAAY,IAAK;MAC/B,IAAIA,IAAI,KAAK,CAAC,EAAE,OAAOL,OAAO,CAACH,KAAK,CAAC;MACrCS,OAAO,CAACC,KAAK,CAAE,IAAGZ,OAAQ,8BAA6BU,IAAK,EAAC,CAAC;MAC9DJ,MAAM,CAAC,IAAIO,KAAK,CAAE,IAAGb,OAAQ,8BAA6BU,IAAK,EAAC,CAAC,CAAC;MAClE;IACJ,CAAC,CAAC;EACN,CAAC,CAAiC;EAClCP,OAAO,CAACD,KAAK,GAAGA,KAAK;EACrB,OAAOC,OAAO;AAClB"}
|
package/package.json
CHANGED
package/src/utils/spawnAsync.ts
CHANGED
|
@@ -15,6 +15,41 @@ export interface PromiseWithChildProcess<T> extends Promise<T> {
|
|
|
15
15
|
child: T
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
export type Options =
|
|
19
|
+
| SpawnOptionsWithoutStdio
|
|
20
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>
|
|
21
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>
|
|
22
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>
|
|
23
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>
|
|
24
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>
|
|
25
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>
|
|
26
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>
|
|
27
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>
|
|
28
|
+
| SpawnOptions
|
|
29
|
+
|
|
30
|
+
let defaultOptions: Options = {}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @description get default options for spawnAsync
|
|
34
|
+
* @returns {Options} options
|
|
35
|
+
*/
|
|
36
|
+
export function getDefaultOptions(): Options {
|
|
37
|
+
return defaultOptions
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @description set default options for spawnAsync
|
|
42
|
+
* @param {Options | ((prev: Options) => Options)} options
|
|
43
|
+
*/
|
|
44
|
+
export function setDefaultOptions(options: Options | ((prev: Options) => Options)) {
|
|
45
|
+
if (typeof options === "function") {
|
|
46
|
+
defaultOptions = options(defaultOptions)
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
defaultOptions = options
|
|
50
|
+
return defaultOptions
|
|
51
|
+
}
|
|
52
|
+
|
|
18
53
|
export function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>
|
|
19
54
|
export function spawnAsync(
|
|
20
55
|
command: string,
|
|
@@ -101,15 +136,18 @@ export function spawnAsync(command: string, args: readonly string[], options: Sp
|
|
|
101
136
|
* @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.
|
|
102
137
|
*/
|
|
103
138
|
export function spawnAsync(command: string, args?: any, options?: any): Promise<any> {
|
|
139
|
+
let child: any
|
|
104
140
|
const promise = new Promise<any>((resolve, reject) => {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
child
|
|
141
|
+
if (Array.isArray(args)) options = { ...defaultOptions, ...options }
|
|
142
|
+
else args = { ...defaultOptions, ...args }
|
|
143
|
+
child = spawn(command, args, options)
|
|
144
|
+
child.on("exit", (code: number) => {
|
|
108
145
|
if (code === 0) return resolve(child)
|
|
109
146
|
console.error(`"${command}" Command failed with code ${code}`)
|
|
110
147
|
reject(new Error(`"${command}" Command failed with code ${code}`))
|
|
111
148
|
return
|
|
112
149
|
})
|
|
113
150
|
}) as PromiseWithChildProcess<any>
|
|
151
|
+
promise.child = child
|
|
114
152
|
return promise
|
|
115
153
|
}
|