soda-nodejs 0.4.3 → 0.5.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/cjs/utils/spawnAsync.d.ts +11 -0
- package/dist/cjs/utils/spawnAsync.js +20 -0
- package/dist/cjs/utils/spawnAsync.js.map +2 -2
- package/dist/esm/utils/spawnAsync.d.ts +11 -0
- package/dist/esm/utils/spawnAsync.js +29 -0
- package/dist/esm/utils/spawnAsync.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/spawnAsync.ts +37 -0
|
@@ -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,12 +19,30 @@ 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) {
|
|
27
41
|
const promise = new Promise((resolve, reject) => {
|
|
42
|
+
if (Array.isArray(args))
|
|
43
|
+
options = { ...defaultOptions, ...options };
|
|
44
|
+
else
|
|
45
|
+
args = { ...defaultOptions, ...args };
|
|
28
46
|
const child = (0, import_child_process.spawn)(command, args, options);
|
|
29
47
|
promise.child = child;
|
|
30
48
|
child.on("exit", (code) => {
|
|
@@ -39,6 +57,8 @@ function spawnAsync(command, args, options) {
|
|
|
39
57
|
}
|
|
40
58
|
// Annotate the CommonJS export names for ESM import in node:
|
|
41
59
|
0 && (module.exports = {
|
|
60
|
+
getDefaultOptions,
|
|
61
|
+
setDefaultOptions,
|
|
42
62
|
spawnAsync
|
|
43
63
|
});
|
|
44
64
|
//# 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 const child = spawn(command, args, options)\n promise.child = child\n child.on(\"exit\", code => {\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 return promise\n}\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 const promise = new Promise<any>((resolve, reject) => {\n if (Array.isArray(args)) options = { ...defaultOptions, ...options }\n else args = { ...defaultOptions, ...args }\n const child = spawn(command, args, options)\n promise.child = child\n child.on(\"exit\", code => {\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 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,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,UAAM,YAAQ,4BAAM,SAAS,MAAM,OAAO;AAC1C,YAAQ,QAAQ;AAChB,UAAM,GAAG,QAAQ,UAAQ;AACrB,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,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,10 +1,39 @@
|
|
|
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) {
|
|
7
29
|
const promise = new Promise((resolve, reject) => {
|
|
30
|
+
if (Array.isArray(args)) options = {
|
|
31
|
+
...defaultOptions,
|
|
32
|
+
...options
|
|
33
|
+
};else args = {
|
|
34
|
+
...defaultOptions,
|
|
35
|
+
...args
|
|
36
|
+
};
|
|
8
37
|
const child = spawn(command, args, options);
|
|
9
38
|
promise.child = child;
|
|
10
39
|
child.on("exit", code => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["spawn","
|
|
1
|
+
{"version":3,"names":["spawn","defaultOptions","getDefaultOptions","setDefaultOptions","options","spawnAsync","command","args","promise","Promise","resolve","reject","Array","isArray","child","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 const promise = new Promise<any>((resolve, reject) => {\n if (Array.isArray(args)) options = { ...defaultOptions, ...options }\n else args = { ...defaultOptions, ...args }\n const child = spawn(command, args, options)\n promise.child = child\n child.on(\"exit\", code => {\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 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,MAAMI,OAAO,GAAG,IAAIC,OAAO,CAAM,CAACC,OAAO,EAAEC,MAAM,KAAK;IAClD,IAAIC,KAAK,CAACC,OAAO,CAACN,IAAI,CAAC,EAAEH,OAAO,GAAG;MAAE,GAAGH,cAAc;MAAE,GAAGG;IAAQ,CAAC,MAC/DG,IAAI,GAAG;MAAE,GAAGN,cAAc;MAAE,GAAGM;IAAK,CAAC;IAC1C,MAAMO,KAAK,GAAGd,KAAK,CAACM,OAAO,EAAEC,IAAI,EAAEH,OAAO,CAAC;IAC3CI,OAAO,CAACM,KAAK,GAAGA,KAAK;IACrBA,KAAK,CAACC,EAAE,CAAC,MAAM,EAAEC,IAAI,IAAI;MACrB,IAAIA,IAAI,KAAK,CAAC,EAAE,OAAON,OAAO,CAACI,KAAK,CAAC;MACrCG,OAAO,CAACC,KAAK,CAAE,IAAGZ,OAAQ,8BAA6BU,IAAK,EAAC,CAAC;MAC9DL,MAAM,CAAC,IAAIQ,KAAK,CAAE,IAAGb,OAAQ,8BAA6BU,IAAK,EAAC,CAAC,CAAC;MAClE;IACJ,CAAC,CAAC;EACN,CAAC,CAAiC;EAClC,OAAOR,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,
|
|
@@ -102,6 +137,8 @@ export function spawnAsync(command: string, args: readonly string[], options: Sp
|
|
|
102
137
|
*/
|
|
103
138
|
export function spawnAsync(command: string, args?: any, options?: any): Promise<any> {
|
|
104
139
|
const promise = new Promise<any>((resolve, reject) => {
|
|
140
|
+
if (Array.isArray(args)) options = { ...defaultOptions, ...options }
|
|
141
|
+
else args = { ...defaultOptions, ...args }
|
|
105
142
|
const child = spawn(command, args, options)
|
|
106
143
|
promise.child = child
|
|
107
144
|
child.on("exit", code => {
|