xshell 0.0.11 → 0.0.15

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/process.js CHANGED
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.call_node = exports.call = exports.start = exports.exe_node = exports.fp_root = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const child_process_1 = require("child_process");
6
- const stream_buffers_1 = require("stream-buffers");
7
6
  const iconv_lite_1 = (0, tslib_1.__importDefault)(require("iconv-lite"));
8
7
  require("./prototype");
9
8
  const utils_1 = require("./utils");
@@ -13,14 +12,14 @@ exports.exe_node = process.execPath.to_slash();
13
12
  - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)
14
13
  - args: `[]` arguments list
15
14
  - options
16
- - cwd?: `'D:/'`
15
+ - cwd?: `fp_root`
17
16
  - env?: `process.env` overwrite/add to process.env
18
17
  - encoding?: `'utf-8'` child output encoding
19
18
  - print?: `true` print option (with details)
20
19
  - stdio?: `'pipe'` when 'ignore' then ignore stdio processing
21
20
  - detached?: `false` whether to break the connection with child (ignore stdio, unref)
22
21
  */
23
- function start(exe, args = [], { cwd = 'd:/', encoding = 'utf-8', print = true, stdio = 'pipe', detached = false, env, } = {}) {
22
+ function start(exe, args = [], { cwd = exports.fp_root, encoding = 'utf-8', print = true, stdio = 'pipe', detached = false, env, } = {}) {
24
23
  const options = {
25
24
  cwd,
26
25
  shell: false,
@@ -51,21 +50,24 @@ function start(exe, args = [], { cwd = 'd:/', encoding = 'utf-8', print = true,
51
50
  if (stdio === 'ignore')
52
51
  return child;
53
52
  if (encoding !== 'binary') {
54
- child.stdout = child.stdout.pipe(iconv_lite_1.default.decodeStream(encoding));
55
- child.stderr = child.stderr.pipe(iconv_lite_1.default.decodeStream(encoding));
53
+ if (encoding === 'utf-8') {
54
+ child.stdout.setEncoding('utf-8');
55
+ child.stderr.setEncoding('utf-8');
56
+ }
57
+ else {
58
+ child.stdout = child.stdout.pipe(iconv_lite_1.default.decodeStream(encoding));
59
+ child.stderr = child.stderr.pipe(iconv_lite_1.default.decodeStream(encoding));
60
+ }
56
61
  if (print) {
57
- child.stdout.on('data', (chunk) => {
58
- process.stdout.write(chunk);
59
- });
60
- child.stderr.on('data', (chunk) => {
61
- process.stderr.write(chunk);
62
- });
62
+ child.stdout.pipe(process.stdout, { end: false });
63
+ child.stderr.pipe(process.stderr, { end: false });
63
64
  }
64
65
  }
65
66
  return child;
66
67
  }
67
68
  exports.start = start;
68
- async function call(exe, args = [], { encoding = 'utf-8', print = true, init_buffer_size, throw_code = true, } = {}) {
69
+ async function call(exe, args = [], options = {}) {
70
+ const { encoding = 'utf-8', print = true, throw_code = true, } = options;
69
71
  const print_options = typeof print === 'boolean' ?
70
72
  {
71
73
  command: print,
@@ -79,59 +81,48 @@ async function call(exe, args = [], { encoding = 'utf-8', print = true, init_buf
79
81
  const cmd = `${exe} ${args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ')}`;
80
82
  if (print_options.command)
81
83
  console.log(cmd.blue);
82
- let child = start(exe, args, Object.assign(arguments[2] || {}, { print: false }));
84
+ options.print = false;
85
+ let child = start(exe, args, options);
83
86
  // --- collect output
84
- let stdout;
85
- let stderr;
86
- if (encoding !== 'binary') {
87
- stdout = '';
88
- stderr = '';
89
- child.stdout.on('data', chunk => {
90
- if (print_options.stdout)
91
- process.stdout.write(chunk);
92
- stdout += chunk;
93
- });
94
- child.stderr.on('data', chunk => {
95
- if (print_options.stderr)
96
- process.stderr.write(chunk);
97
- stderr += chunk;
98
- });
99
- }
100
- else {
101
- const stream_buffer_options = init_buffer_size ? {
102
- initialSize: init_buffer_size,
103
- incrementAmount: init_buffer_size,
104
- } : {};
105
- stdout = new stream_buffers_1.WritableStreamBuffer(stream_buffer_options);
106
- stderr = new stream_buffers_1.WritableStreamBuffer(stream_buffer_options);
107
- if (print_options.stdout)
108
- child.stdout.pipe(stdout);
109
- if (print_options.stderr)
110
- child.stderr.pipe(stderr);
111
- }
87
+ let stdouts = [];
88
+ let stderrs = [];
112
89
  let code, signal;
113
90
  await Promise.all([
114
- new Promise(resolve => {
115
- child.stdout.on('end', () => { resolve(); });
116
- }),
117
- new Promise(resolve => {
118
- child.stderr.on('end', () => { resolve(); });
119
- }),
120
91
  new Promise(resolve => {
121
92
  child.on('exit', (_code, _signal) => {
122
93
  code = _code;
123
94
  signal = _signal;
124
95
  resolve();
125
96
  });
126
- })
97
+ }),
98
+ (async () => {
99
+ for await (const chunk of child.stdout) {
100
+ if (encoding !== 'binary' && print_options.stdout)
101
+ process.stdout.write(chunk);
102
+ stdouts.push(chunk);
103
+ }
104
+ })(),
105
+ (async () => {
106
+ for await (const chunk of child.stderr) {
107
+ if (encoding !== 'binary' && print_options.stderr)
108
+ process.stderr.write(chunk);
109
+ stderrs.push(chunk);
110
+ }
111
+ })()
127
112
  ]);
128
- const message = `process(${child.pid}) '${cmd}' exited ${code}${signal ? `, by signal ${signal}` : ''}. `;
113
+ const message = `process(${child.pid}) '${cmd}' exited ${code}${signal ? `, by signal ${signal}` : ''}.`;
129
114
  if (print_options.code || code || signal)
130
- console.log(message[!code && !signal ? 'green' : 'red'].pad(utils_1.output_width, { character: '─' }));
115
+ console.log(message[code || signal ? 'red' : 'blue']);
131
116
  const result = {
132
117
  pid: child.pid,
133
- stdout: encoding !== 'binary' ? stdout : (stdout.getContents() || Buffer.alloc(0)),
134
- stderr: encoding !== 'binary' ? stderr : (stderr.getContents() || Buffer.alloc(0)),
118
+ stdout: encoding === 'binary' ?
119
+ Buffer.concat(stdouts)
120
+ :
121
+ stdouts.join(''),
122
+ stderr: encoding === 'binary' ?
123
+ Buffer.concat(stderrs)
124
+ :
125
+ stderrs.join(''),
135
126
  code,
136
127
  signal,
137
128
  child,
package/process.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";;;;AAAA,iDAAqC;AAKrC,mDAAqD;AAErD,yEAA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O,IAAI,CAAC,gBAAQ,EAAE,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,CAAA;AACjD,CAAC;AAFD,8BAEC","sourcesContent":["import { spawn } from 'child_process'\nimport type { SpawnOptions, ChildProcess } from 'child_process'\nimport { Readable, Writable } from 'stream'\n\n\nimport { WritableStreamBuffer } from 'stream-buffers'\nimport type { WritableStreamBufferOptions } from 'stream-buffers'\nimport iconv from 'iconv-lite'\n\n\nimport './prototype'\nimport { Encoding } from './file'\nimport { inspect, output_width } from './utils'\n\nexport const fp_root = `${__dirname}/`.to_slash()\n\nexport const exe_node = process.execPath.to_slash()\n\n\n// ------------------------------------ start & call\ninterface StartOptions {\n /** `'d:/'` */\n cwd?: string\n \n /** `process.env` overwrite/add to process.env */\n env?: Record<string, string>\n \n /** `'utf-8'` child output encoding */\n encoding?: Encoding\n \n /** `true` print option (with details) */\n print?: boolean | {\n command?: boolean\n stdout?: boolean\n stderr?: boolean\n code?: boolean\n error?: boolean\n }\n \n /** `'pipe'` when 'ignore' then ignore stdio processing */\n stdio?: 'pipe' | 'ignore'\n \n /** `false` whether to break the connection with child (ignore stdio, unref) */\n detached?: boolean\n}\n\n/** start process \n - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `'D:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n*/\nexport function start (exe: string, args: string[] = [], {\n cwd = 'd:/',\n \n encoding = 'utf-8',\n \n print = true,\n \n stdio = 'pipe',\n \n detached = false,\n \n env,\n \n}: StartOptions = { }): ChildProcess {\n const options: SpawnOptions = {\n cwd,\n shell: false,\n windowsHide: !detached,\n stdio,\n ... env ? {\n env: { ...process.env, ...env }\n } : { },\n }\n \n if (print)\n console.log(`${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`.blue)\n \n if (detached) {\n let child = spawn(exe, args, {\n ...options,\n stdio: 'ignore',\n detached: true,\n })\n \n child.unref()\n return child\n }\n \n let child = spawn(exe, args, options)\n \n if (stdio === 'pipe')\n child.stdin.setDefaultEncoding('utf8')\n \n // prevent child spawn error crashing nodejs process\n child.on('error', error => {\n console.error(error)\n })\n \n if (stdio === 'ignore') return child\n \n if (encoding !== 'binary') {\n child.stdout = child.stdout.pipe(iconv.decodeStream(encoding)) as any as Readable\n child.stderr = child.stderr.pipe(iconv.decodeStream(encoding)) as any as Readable\n \n if (print) {\n child.stdout.on('data', (chunk) => {\n process.stdout.write(chunk)\n })\n child.stderr.on('data', (chunk) => {\n process.stderr.write(chunk)\n })\n }\n }\n \n return child\n}\n\n\nexport interface CallOptions extends StartOptions {\n /** `true` whether to throw Error when code is not 0 */\n throw_code?: boolean\n}\n\nexport interface CallResult<T = string> {\n pid: number\n stdout: T\n stderr: T\n code: number | null\n signal: NodeJS.Signals | null\n child: ChildProcess\n [inspect.custom] (): string\n}\n\n\n/** call process for result\n - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `'d:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call (exe: string, args?: string[]): Promise<CallResult<string>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding: 'binary', init_buffer_size?: number }): Promise<CallResult<Buffer>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding?: 'utf-8' | 'gb18030' }): Promise<CallResult<string>>\nexport async function call (exe: string, args: string[] = [], {\n encoding = 'utf-8', \n \n print = true,\n \n init_buffer_size,\n \n throw_code = true,\n}: CallOptions & { init_buffer_size?: number } = { }): Promise<CallResult<string | Buffer>> {\n const print_options = typeof print === 'boolean' ?\n {\n command: print,\n stdout: print,\n stderr: print,\n code: print,\n error: print\n }\n :\n print\n \n const cmd = `${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`\n \n if (print_options.command)\n console.log(cmd.blue)\n \n let child = start(exe, args, Object.assign(arguments[2] || { }, { print: false }))\n \n // --- collect output\n let stdout: string | WritableStreamBuffer\n let stderr: string | WritableStreamBuffer\n \n \n if (encoding !== 'binary') {\n stdout = ''\n stderr = ''\n \n child.stdout.on('data', chunk => {\n if (print_options.stdout)\n process.stdout.write(chunk)\n \n stdout += chunk\n })\n \n child.stderr.on('data', chunk => {\n if (print_options.stderr)\n process.stderr.write(chunk)\n \n stderr += chunk\n })\n } else {\n const stream_buffer_options: WritableStreamBufferOptions = init_buffer_size ? {\n initialSize: init_buffer_size,\n incrementAmount: init_buffer_size,\n } : { }\n \n stdout = new WritableStreamBuffer(stream_buffer_options)\n stderr = new WritableStreamBuffer(stream_buffer_options)\n \n if (print_options.stdout)\n child.stdout.pipe(stdout as WritableStreamBuffer)\n if (print_options.stderr)\n child.stderr.pipe(stderr as WritableStreamBuffer)\n }\n \n \n let code: number | null, \n signal: NodeJS.Signals\n \n await Promise.all([\n new Promise<void>( resolve => {\n child.stdout.on('end', () => { resolve() })\n }),\n new Promise<void>( resolve => {\n child.stderr.on('end', () => { resolve() })\n }),\n new Promise<void>( resolve => {\n child.on('exit', (_code, _signal: NodeJS.Signals) => {\n code = _code\n signal = _signal\n resolve()\n })\n })\n ])\n \n const message = `process(${ child.pid }) '${ cmd }' exited ${ code }${ signal ? `, by signal ${ signal }` : '' }. `\n \n if (print_options.code || code || signal)\n console.log(message[!code && !signal ? 'green' : 'red'].pad(output_width, { character: '─' }))\n \n const result = {\n pid: child.pid,\n stdout: encoding !== 'binary' ? (stdout as string) : ((stdout as WritableStreamBuffer).getContents() || Buffer.alloc(0)),\n stderr: encoding !== 'binary' ? (stderr as string) : ((stderr as WritableStreamBuffer).getContents() || Buffer.alloc(0)),\n code,\n signal,\n child,\n [inspect.custom] () {\n return inspect(this, { omit: ['child'] })\n }\n }\n \n if (code && throw_code)\n throw Object.assign(new Error(message), result)\n \n return result\n}\n\n\n/** call node <js> for result\n - js: .js path (relative path will resolve based on cwd)\n - args: `[]` arguments list\n - options\n - cwd?: `'d:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child process output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call_node (js: string, args: string[] = [], options?: CallOptions & { encoding?: 'utf-8' | 'gb18030' }) {\n return call(exe_node, [js, ...args], options)\n}\n\n"]}
1
+ {"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";;;;AAAA,iDAAqC;AAIrC,yEAA8B;AAE9B,uBAAoB;AAEpB,mCAAiC;AAEpB,QAAA,OAAO,GAAG,GAAG,SAAS,GAAG,CAAC,QAAQ,EAAE,CAAA;AAEpC,QAAA,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA;AA8BnD;;;;;;;;;;EAUE;AACF,SAAgB,KAAK,CAAE,GAAW,EAAE,OAAiB,EAAE,EAAE,EACrD,GAAG,GAAG,eAAO,EAEb,QAAQ,GAAG,OAAO,EAElB,KAAK,GAAG,IAAI,EAEZ,KAAK,GAAG,MAAM,EAEd,QAAQ,GAAG,KAAK,EAEhB,GAAG,MAEW,EAAG;IACjB,MAAM,OAAO,GAAiB;QAC1B,GAAG;QACH,KAAK,EAAE,KAAK;QACZ,WAAW,EAAE,CAAC,QAAQ;QACtB,KAAK;QACL,GAAI,GAAG,CAAC,CAAC,CAAC;YACN,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,EAAE;SAClC,CAAC,CAA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,GAAG,CAAA;IAElH,IAAI,aAAa,CAAC,IAAI,IAAI,IAAI,IAAI,MAAM;QACpC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAA;IAEzD,MAAM,MAAM,GAAG;QACX,GAAG,EAAE,KAAK,CAAC,GAAG;QAEd,MAAM,EAAE,QAAQ,KAAK,QAAQ,CAAC,CAAC;YAC3B,MAAM,CAAC,MAAM,CAAC,OAAmB,CAAC;YACtC,CAAC;gBACI,OAAoB,CAAC,IAAI,CAAC,EAAE,CAAC;QAElC,MAAM,EAAE,QAAQ,KAAK,QAAQ,CAAC,CAAC;YAC3B,MAAM,CAAC,MAAM,CAAC,OAAmB,CAAC;YACtC,CAAC;gBACI,OAAoB,CAAC,IAAI,CAAC,EAAE,CAAC;QAElC,IAAI;QAEJ,MAAM;QAEN,KAAK;QAEL,CAAC,eAAO,CAAC,MAAM,CAAC;YACZ,OAAO,IAAA,eAAO,EAAC,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QAC7C,CAAC;KACJ,CAAA;IAED,IAAI,IAAI,IAAI,UAAU;QAClB,MAAM,MAAM,CAAC,MAAM,CACf,IAAI,KAAK,CAAC,OAAO,CAAC,EAClB,MAAM,CACT,CAAA;IAEL,OAAO,MAAM,CAAA;AACjB,CAAC;AA/FD,oBA+FC;AAGD;;;;;;;;;;;EAWE;AACK,KAAK,UAAU,SAAS,CAAE,EAAU,EAAE,OAAiB,EAAE,EAAE,OAA0D;IACxH,OAAO,IAAI,CAAC,gBAAQ,EAAE,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,CAAA;AACjD,CAAC;AAFD,8BAEC","sourcesContent":["import { spawn } from 'child_process'\nimport type { SpawnOptions, ChildProcess } from 'child_process'\nimport { Readable, Writable } from 'stream'\n\nimport iconv from 'iconv-lite'\n\nimport './prototype'\nimport { Encoding } from './file'\nimport { inspect } from './utils'\n\nexport const fp_root = `${__dirname}/`.to_slash()\n\nexport const exe_node = process.execPath.to_slash()\n\n\n// ------------------------------------ start & call\ninterface StartOptions {\n /** `'d:/'` */\n cwd?: string\n \n /** `process.env` overwrite/add to process.env */\n env?: Record<string, string>\n \n /** `'utf-8'` child output encoding */\n encoding?: Encoding\n \n /** `true` print option (with details) */\n print?: boolean | {\n command?: boolean\n stdout?: boolean\n stderr?: boolean\n code?: boolean\n error?: boolean\n }\n \n /** `'pipe'` when 'ignore' then ignore stdio processing */\n stdio?: 'pipe' | 'ignore'\n \n /** `false` whether to break the connection with child (ignore stdio, unref) */\n detached?: boolean\n}\n\n/** start process \n - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `fp_root`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n*/\nexport function start (exe: string, args: string[] = [], {\n cwd = fp_root,\n \n encoding = 'utf-8',\n \n print = true,\n \n stdio = 'pipe',\n \n detached = false,\n \n env,\n \n}: StartOptions = { }): ChildProcess {\n const options: SpawnOptions = {\n cwd,\n shell: false,\n windowsHide: !detached,\n stdio,\n ... env ? {\n env: { ...process.env, ...env }\n } : { },\n }\n \n if (print)\n console.log(`${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`.blue)\n \n if (detached) {\n let child = spawn(exe, args, {\n ...options,\n stdio: 'ignore',\n detached: true,\n })\n \n child.unref()\n return child\n }\n \n let child = spawn(exe, args, options)\n \n if (stdio === 'pipe')\n child.stdin.setDefaultEncoding('utf8')\n \n // prevent child spawn error crashing nodejs process\n child.on('error', error => {\n console.error(error)\n })\n \n if (stdio === 'ignore') return child\n \n if (encoding !== 'binary') {\n if (encoding === 'utf-8') {\n child.stdout.setEncoding('utf-8')\n child.stderr.setEncoding('utf-8')\n } else {\n child.stdout = child.stdout.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n child.stderr = child.stderr.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n }\n \n if (print) {\n child.stdout.pipe(process.stdout, { end: false })\n child.stderr.pipe(process.stderr, { end: false })\n }\n }\n \n return child\n}\n\n\nexport interface CallOptions extends StartOptions {\n /** `true` whether to throw Error when code is not 0 */\n throw_code?: boolean\n}\n\nexport interface CallResult<T = string> {\n pid: number\n stdout: T\n stderr: T\n code: number | null\n signal: NodeJS.Signals | null\n child: ChildProcess\n [inspect.custom] (): string\n}\n\n\n/** call process for result\n - exe: .exe path or filename (full path is recommanded to skip path searching for better perf)\n - args: `[]` arguments list\n - options\n - cwd?: `'d:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call (exe: string, args?: string[]): Promise<CallResult<string>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding?: 'utf-8' | 'gb18030' }): Promise<CallResult<string>>\nexport async function call (exe: string, args?: string[], options?: CallOptions & { encoding: 'binary' }): Promise<CallResult<Buffer>>\nexport async function call (exe: string, args: string[] = [], options: CallOptions = { }): Promise<CallResult<string | Buffer>> {\n const {\n encoding = 'utf-8', \n print = true,\n throw_code = true,\n } = options\n \n const print_options = typeof print === 'boolean' ?\n {\n command: print,\n stdout: print,\n stderr: print,\n code: print,\n error: print\n }\n :\n print\n \n const cmd = `${exe} ${ args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' ') }`\n \n if (print_options.command)\n console.log(cmd.blue)\n \n options.print = false\n \n let child = start(exe, args, options)\n \n \n // --- collect output\n let stdouts: (string | Buffer)[] = [ ]\n let stderrs: (string | Buffer)[] = [ ]\n \n let code: number | null, \n signal: NodeJS.Signals\n \n await Promise.all([\n new Promise<void>(resolve => {\n child.on('exit', (_code, _signal) => {\n code = _code\n signal = _signal\n resolve()\n })\n }),\n (async () => {\n for await (const chunk of child.stdout as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print_options.stdout)\n process.stdout.write(chunk)\n stdouts.push(chunk)\n }\n })(),\n (async () => {\n for await (const chunk of child.stderr as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print_options.stderr)\n process.stderr.write(chunk)\n stderrs.push(chunk)\n }\n })()\n ])\n \n const message = `process(${ child.pid }) '${ cmd }' exited ${ code }${ signal ? `, by signal ${ signal }` : '' }.`\n \n if (print_options.code || code || signal)\n console.log(message[code || signal ? 'red' : 'blue'])\n \n const result = {\n pid: child.pid,\n \n stdout: encoding === 'binary' ?\n Buffer.concat(stdouts as Buffer[])\n :\n (stdouts as string[]).join(''),\n \n stderr: encoding === 'binary' ?\n Buffer.concat(stderrs as Buffer[])\n :\n (stderrs as string[]).join(''),\n \n code,\n \n signal,\n \n child,\n \n [inspect.custom] () {\n return inspect(this, { omit: ['child'] })\n }\n }\n \n if (code && throw_code)\n throw Object.assign(\n new Error(message), \n result\n )\n \n return result\n}\n\n\n/** call node <js> for result\n - js: .js path (relative path will resolve based on cwd)\n - args: `[]` arguments list\n - options\n - cwd?: `'d:/'`\n - env?: `process.env` overwrite/add to process.env\n - encoding?: `'utf-8'` child process output encoding\n - print?: `true` print option (with details)\n - stdio?: `'pipe'` when 'ignore' then ignore stdio processing\n - detached?: `false` whether to break the connection with child (ignore stdio, unref)\n - throw_code?: `true` whether to throw Error when code is not 0\n*/\nexport async function call_node (js: string, args: string[] = [], options?: CallOptions & { encoding?: 'utf-8' | 'gb18030' }) {\n return call(exe_node, [js, ...args], options)\n}\n\n"]}