10 examples of 'nodejs spawn' in JavaScript

Every line of 'nodejs spawn' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
151function runSpawn(cmdLine, args, callback) {
152 console.log("spawn cmdLine: " + cmdLine);
153 console.log("args: " + args);
154 var ls = child_process_1.spawn(cmdLine, args);
155 ls.stdout.on('data', function (data) {
156 console.log(data.toString());
157 });
158 ls.stderr.on('data', function (data) {
159 console.error(data.toString());
160 });
161 ls.on('close', function () {
162 callback();
163 });
164}
146function spawn (cmd, args, opts, cb) {
147 var c = proc.spawn(cmd, args, opts)
148 c.on('exit', function (code) {
149 if (code) return cb(new Error(cmd + ' exited with ' + code))
150 cb(null)
151 })
152}
36function spawn(filePath, args, stdout, stderr) {
37 return new Promise((resolve, reject) => {
38 const child = cp.spawn(
39 process.execPath,
40 [filePath].concat(args),
41 { stdio: "pipe" }
42 )
43 const out = new BufferStream()
44 const error = new BufferStream()
45
46 if (stdout != null) {
47 child.stdout.pipe(stdout)
48 }
49 if (stderr != null) {
50 child.stderr.pipe(stderr)
51 }
52 else {
53 child.stderr.pipe(error)
54 }
55 child.stdout.pipe(out)
56 child.on("close", (exitCode) => {
57 if (exitCode) {
58 reject(new Error(error.value || "Exited with non-zero code."))
59 }
60 else {
61 resolve()
62 }
63 })
64 child.on("error", reject)
65 })
66}
7function spawn(cmd, args, callback) {
8 console.log(">", cmd, args);
9
10 var spawn = child.spawn(cmd, args, { stdio: "inherit" });
11
12 spawn.on("exit", function (code) {
13 if (code === 0) {
14 callback();
15 } else {
16 console.log("Killing...");
17 process.exit(1);
18 }
19 });
20}
19export function simpleSpawn(execPath: string, args: string[] = [], printOutput: boolean = false): Promise {
20 return new Promise((resolve, reject) => {
21 const errorsBuffer: string[] = [];
22 const child = spawn(execPath, args, {
23 detached: true
24 });
25 child.stdout.on('data', data => {
26 if (printOutput) {
27 console.log(`${data}`);
28 }
29 });
30 child.stderr.on('data', data => {
31 if (printOutput) {
32 console.error(`${data}`);
33 } else {
34 errorsBuffer.push(`${data}`);
35 }
36 });
37 child.on('exit', (code, signal) => {
38 if (code !== 0) {
39 if (errorsBuffer.length > 0) {
40 console.error(errorsBuffer.join('\n'));
41 }
42 reject(new Error(`Child process completed with error code: ${code}, please use --verbose flag to see whole output`));
43 } else {
44 resolve();
45 }
46 });
47 child.on('error', error => {
48 console.error('Process finished with error', error);
49 reject(error);
50 });
51 });
52}
12spawn ($binary: string, $args: string[], $options: any) {
13 const {sandbox} = this;
14 const $events: { [key: string]: any[] } = {};
15
16 const addEvent = (type: string, handler: any) => {
17 ($events[type] = $events[type] || []).push(handler);
18 };
19
20 const runHandlers = (type: string, data: any) => {
21 $events.hasOwnProperty(type) && $events[type].forEach(handler => handler(data));
22 };
23
24 return this.childProcess = this.childProcesses[this.childProcesses.length] = {
25 $binary,
26 $args,
27 $options,
28
29 $closeWith(data = '', exitCode = 0) {
30 runHandlers('stdout', data);
31 runHandlers('exit', exitCode);
32 },
33
34 on: sandbox.spy((event, handler) => addEvent(event, handler)),
35 stdout: {on: sandbox.spy((type, handler) => addEvent('stdout', handler))},
36 stderr: {on: sandbox.spy((type, handler) => addEvent('stderr', handler))},
37 }
38}
51async function spawn(commandLine: string, options: Options = {}): Promise {
52 return new Promise((resolve, reject) => {
53 const args = commandLine.split(' ')
54 const spawned = require('cross-spawn')(head(args), tail(args), options)
55 const result = {
56 stdout: null,
57 status: null,
58 error: null,
59 }
60 if (spawned.stdout) {
61 spawned.stdout.on('data', data => {
62 if (isNil(result.stdout)) {
63 result.stdout = data
64 } else {
65 result.stdout += data
66 }
67 })
68 }
69 spawned.on('close', code => {
70 result.status = code
71 resolve(result)
72 })
73 spawned.on('error', err => {
74 result.error = err
75 resolve(result)
76 })
77 })
78}
475_spawn(exec, args, env, cwd) {
476 let worker;
477 let stdio;
478
479 stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
480 worker = childProcess.spawn(exec, args, {
481 cwd: cwd || process.cwd(),
482 env: env,
483 stdio: stdio
484 });
485
486 if (worker.stdout && worker.stderr) {
487 let appId = this.appId;
488 let pid = worker.pid;
489 // eslint-disable-next-line
490 let logStd = this.stdout;
491 // 非最简写法,保证写入函数尽可能少逻辑判断
492 if (!logStd) {
493 worker.stdout.on('data', (chunk) => {
494 // eslint-disable-next-line
495 console.log(`${log.getTime()} ${appId} #${pid} STDOUT`, chunk.toString().trimRight());
496 });
497 worker.stderr.on('data', (chunk) => {
498 // eslint-disable-next-line
499 console.error(`${log.getTime()} ${appId} #${pid} STDERR`, chunk.toString().trimRight());
500 });
501 } else {
502 worker.stdout.on('data', (chunk) => {
503 logStd.info(chunk.toString().trimRight());
504 });
505 worker.stderr.on('data', (chunk) => {
506 logStd.error(chunk.toString().trimRight());
507 });
508 }
509 }
510
511 return worker;
512}
5function spawn(cmd, args, options) {
6 if (process.platform === 'win32') {
7 if (cmd === 'node') {
8 cmd = args[0];
9 args = args.slice(1);
10 }
11 if (cmd.endsWith('js')) {
12 args = [cmd].concat(args);
13 cmd = process.execPath;
14 } else {
15 // The /C option is capitalized to make tests work. Specifically, this
16 // allows to bypass an issue in https://github.com/tapjs/spawn-wrap used
17 // by https://github.com/istanbuljs/nyc to track code coverage.
18 // The problem is that `mungeCmd()` (
19 // https://github.com/tapjs/spawn-wrap/blob/7931ab5c/index.js#L143)
20 // incorrectly replaces `spawn()` args.
21 args = ['/C', cmd].concat(args).map((arg) => {
22 return arg.replace(/\^/g, '^^^^');
23 });
24 cmd = 'cmd';
25 }
26 }
27 return child.spawn(cmd, args, options);
28}
28function spawn(exe, args, cwd) {
29 return new Promise((resolve, reject) => {
30 const child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
31 const buffer = [];
32 child.stderr.on('data', chunk => {
33 buffer.push(chunk.toString());
34 });
35 child.stdout.on('data', chunk => {
36 buffer.push(chunk.toString());
37 });
38 child.on('close', code => {
39 const output = buffer.join('');
40 if (code) {
41 const msg = output || 'Process failed: ' + code;
42 reject(new ProcessError(code, msg));
43 } else {
44 resolve(output);
45 }
46 });
47 });
48}

Related snippets