xshell 0.0.36 → 0.0.37
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/package.json +1 -1
- package/process.js +3 -2
- package/process.js.map +1 -1
package/package.json
CHANGED
package/process.js
CHANGED
|
@@ -92,8 +92,9 @@ async function call(exe, args = [], options = {}) {
|
|
|
92
92
|
if (typeof stdio === 'string')
|
|
93
93
|
stdio = [stdio, stdio, stdio];
|
|
94
94
|
const cmd = (exe.includes(' ') ? exe.quote() : exe) +
|
|
95
|
-
args.length ? (' ' +
|
|
96
|
-
|
|
95
|
+
(args.length ? (' ' + args.map(arg => arg.includes(' ') ? arg.quote() : arg).join(' '))
|
|
96
|
+
:
|
|
97
|
+
'');
|
|
97
98
|
if (print.command)
|
|
98
99
|
console.log(cmd.blue);
|
|
99
100
|
let child = start(exe, args, {
|
package/process.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";;;;AAAA,iDAIsB;AAGtB,oEAA8B;AAE9B,0BAAuB;AAEvB,yCAAoC;AAEvB,QAAA,OAAO,GAAG,GAAG,SAAS,GAAG,CAAC,QAAQ,EAAE,CAAA;AAEpC,QAAA,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA;AA6BnD;;;;;;;;;;EAUE;AACF,SAAgB,KAAK,CAAE,GAAW,EAAE,OAAiB,EAAG,EAAE,EACtD,GAAG,GAAG,eAAO,EAEb,QAAQ,GAAG,OAAO,EAElB,KAAK,GAAG,IAAI,EAEZ,KAAK,GAAG,MAAM,EAEd,QAAQ,GAAG,KAAK,EAEhB,GAAG,MACW,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,EAA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M,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,kBAAO,CAAC,MAAM,CAAC;YACZ,OAAO,IAAA,kBAAO,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;AAhHD,oBAgHC;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 {\n spawn,\n type SpawnOptions,\n type ChildProcess\n} from 'child_process'\nimport { Readable } from 'stream'\n\nimport iconv from 'iconv-lite'\n\nimport './prototype.js'\nimport { Encoding } from './file.js'\nimport { inspect } from './utils.js'\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 stdout: boolean\n stderr: boolean\n command: boolean\n code: boolean\n }\n \n /** `'pipe'` when 'ignore' then ignore stdio processing */\n stdio?: 'pipe' | 'ignore' | ['pipe' | 'ignore' | 'inherit', 'pipe' | 'ignore' | 'inherit', 'pipe' | 'ignore' | 'inherit']\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}: 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 (typeof print === 'boolean')\n print = {\n stdout: print,\n stderr: print,\n command: print,\n code: print,\n }\n \n if (typeof stdio === 'string')\n stdio = [stdio, stdio, stdio]\n \n if (print.command)\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 // 防止 child spawn 失败时 crash nodejs 进程\n child.on('error', error => {\n console.error(error)\n })\n \n if (stdio[0] === 'pipe')\n child.stdin.setDefaultEncoding('utf8')\n \n if (\n stdio.every(s => \n s === 'ignore')\n )\n return child\n \n if (encoding !== 'binary') {\n if (stdio[1] === 'pipe') {\n if (encoding === 'utf-8')\n child.stdout.setEncoding('utf-8')\n else\n child.stdout = child.stdout.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n \n if (print.stdout)\n child.stdout.pipe(process.stdout, { end: false })\n }\n \n if (stdio[2] === 'pipe') {\n if (encoding === 'utf-8')\n child.stderr.setEncoding('utf-8')\n else\n child.stderr = child.stderr.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n \n if (print.stderr)\n child.stderr.pipe(process.stderr, { end: false })\n }\n }\n \n return child\n}\n\n\nexport interface CallOptions extends StartOptions {\n throw_code?: boolean\n input?: string\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 - input?: string\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 throw_code = true,\n input,\n } = options\n \n let {\n stdio = 'pipe',\n print = true\n } = options\n \n if (typeof print === 'boolean')\n print = {\n command: print,\n stdout: print,\n stderr: print,\n code: print,\n }\n \n if (typeof stdio === 'string')\n stdio = [stdio, stdio, stdio]\n \n const cmd = \n (exe.includes(' ') ? exe.quote() : exe) + \n args.length ? (\n ' ' + \n args.map(arg => \n arg.includes(' ') ? arg.quote() : arg\n ).join(' ')\n ) : ''\n \n if (print.command)\n console.log(cmd.blue)\n \n let child = start(exe, args, {\n ...options,\n print: false,\n })\n \n if (input)\n child.stdin.write(input)\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.once('exit', (_code, _signal) => {\n code = _code\n signal = _signal\n resolve()\n })\n }),\n (async () => {\n if (stdio[1] === 'pipe')\n for await (const chunk of child.stdout as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print.stdout)\n process.stdout.write(chunk)\n stdouts.push(chunk)\n }\n })(),\n (async () => {\n if (stdio[2] === 'pipe')\n for await (const chunk of child.stderr as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print.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.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"]}
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";;;;AAAA,iDAIsB;AAGtB,oEAA8B;AAE9B,0BAAuB;AAEvB,yCAAoC;AAEvB,QAAA,OAAO,GAAG,GAAG,SAAS,GAAG,CAAC,QAAQ,EAAE,CAAA;AAEpC,QAAA,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA;AA6BnD;;;;;;;;;;EAUE;AACF,SAAgB,KAAK,CAAE,GAAW,EAAE,OAAiB,EAAG,EAAE,EACtD,GAAG,GAAG,eAAO,EAEb,QAAQ,GAAG,OAAO,EAElB,KAAK,GAAG,IAAI,EAEZ,KAAK,GAAG,MAAM,EAEd,QAAQ,GAAG,KAAK,EAEhB,GAAG,MACW,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,CAAC,CAAC,EAAG;KACV,CAAA;IAED,IAAI,OAAO,KAAK,KAAK,SAAS;QAC1B,KAAK,GAAG;YACJ,MAAM,EAAE,KAAK;YACb,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,KAAK;SACd,CAAA;IAEL,IAAI,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;IAEjC,IAAI,KAAK,CAAC,OAAO;QACb,OAA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AElC,IAAI;QAEJ,MAAM;QAEN,KAAK;QAEL,CAAC,kBAAO,CAAC,MAAM,CAAC;YACZ,OAAO,IAAA,kBAAO,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;AAhHD,oBAgHC;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 {\n spawn,\n type SpawnOptions,\n type ChildProcess\n} from 'child_process'\nimport { Readable } from 'stream'\n\nimport iconv from 'iconv-lite'\n\nimport './prototype.js'\nimport { Encoding } from './file.js'\nimport { inspect } from './utils.js'\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 stdout: boolean\n stderr: boolean\n command: boolean\n code: boolean\n }\n \n /** `'pipe'` when 'ignore' then ignore stdio processing */\n stdio?: 'pipe' | 'ignore' | ['pipe' | 'ignore' | 'inherit', 'pipe' | 'ignore' | 'inherit', 'pipe' | 'ignore' | 'inherit']\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}: 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 (typeof print === 'boolean')\n print = {\n stdout: print,\n stderr: print,\n command: print,\n code: print,\n }\n \n if (typeof stdio === 'string')\n stdio = [stdio, stdio, stdio]\n \n if (print.command)\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 // 防止 child spawn 失败时 crash nodejs 进程\n child.on('error', error => {\n console.error(error)\n })\n \n if (stdio[0] === 'pipe')\n child.stdin.setDefaultEncoding('utf8')\n \n if (\n stdio.every(s => \n s === 'ignore')\n )\n return child\n \n if (encoding !== 'binary') {\n if (stdio[1] === 'pipe') {\n if (encoding === 'utf-8')\n child.stdout.setEncoding('utf-8')\n else\n child.stdout = child.stdout.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n \n if (print.stdout)\n child.stdout.pipe(process.stdout, { end: false })\n }\n \n if (stdio[2] === 'pipe') {\n if (encoding === 'utf-8')\n child.stderr.setEncoding('utf-8')\n else\n child.stderr = child.stderr.pipe(\n iconv.decodeStream(encoding)\n ) as any as Readable\n \n if (print.stderr)\n child.stderr.pipe(process.stderr, { end: false })\n }\n }\n \n return child\n}\n\n\nexport interface CallOptions extends StartOptions {\n throw_code?: boolean\n input?: string\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 - input?: string\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 throw_code = true,\n input,\n } = options\n \n let {\n stdio = 'pipe',\n print = true\n } = options\n \n if (typeof print === 'boolean')\n print = {\n command: print,\n stdout: print,\n stderr: print,\n code: print,\n }\n \n if (typeof stdio === 'string')\n stdio = [stdio, stdio, stdio]\n \n const cmd = \n (exe.includes(' ') ? exe.quote() : exe) + \n (args.length ? (\n ' ' + args.map(arg => \n arg.includes(' ') ? arg.quote() : arg\n ).join(' '))\n :\n '')\n \n if (print.command)\n console.log(cmd.blue)\n \n let child = start(exe, args, {\n ...options,\n print: false,\n })\n \n if (input)\n child.stdin.write(input)\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.once('exit', (_code, _signal) => {\n code = _code\n signal = _signal\n resolve()\n })\n }),\n (async () => {\n if (stdio[1] === 'pipe')\n for await (const chunk of child.stdout as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print.stdout)\n process.stdout.write(chunk)\n stdouts.push(chunk)\n }\n })(),\n (async () => {\n if (stdio[2] === 'pipe')\n for await (const chunk of child.stderr as AsyncIterable<string | Buffer>) {\n if (encoding !== 'binary' && print.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.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"]}
|