_open (callback) { getOpenPort((err, port) => { if (err) return callback(err) // Stdout is reserved for TAP const stdio = ['ignore', 2, 2] const vars = { AIRTAP_SUPPORT_PORT: port } const cmd = Array.isArray(this.cmd) ? this.cmd.slice() : parseCmd(this.cmd, vars) const env = { ...process.env, ...vars } if (cmd[0].endsWith('.js')) { cmd.unshift(process.execPath) } else if (process.platform === 'win32' && isNpm(cmd[0])) { cmd[0] = cmd[0] + '.cmd' } this[kProcess] = spawn(cmd[0], cmd.slice(1), { cwd: this.cwd, stdio, env }) this[kProcess].on('exit', (code) => { // No opinion on whether this constitutes an error debug('exited with code %o', code) this[kProcess] = null }) this.port = port debug('active on port %o, pid %o', port, this[kProcess].pid) setTimeout(callback, this.wait) }) }
fs.writeFileSync(path.join(installPath, 'README.md'), RESP_URL); argv.unshift('install', name); cp.spawn(cmd, argv, { stdio: 'inherit', cwd: installPath
this._process = ChildProcess.spawn(this.command, args, options); this._stdin = this._process.stdin; this._stdout = this._process.stdout;
function run (script) { console.log(script.cyan) return new Promise(resolve => { const child = spawn('node', [path.join(__dirname, `${script}.js`)].concat(process.argv.slice(2)), { stdio: 'inherit' }) child.on('close', code => { resolve(code !== 0) }) }) }
vorpal .command('log grep <query>', 'Grep logs with <query>') .action(function(args, callback) { var self=this; var grep = spawn('grep', ['-e', args.query, appConfig.logFileName]); grep.stdout.on('data', function(data) { self.log(data.toString("UTF-8")); }); callback(); });
fs.readdirSync(bundles) .forEach(bundle => { const bundlePath = join(bundles, bundle); // ensure path has package.json if (!fs.existsSync(join(bundlePath, 'package.json'))) return; // npm binary based on OS const npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm'; // install folder cp.spawn(npmCmd, ['i'], { env: process.env, cwd: bundlePath, stdio: 'inherit' }); });
const runElmFormat = code => { const bin = '/src/elm/elm-format' return new Promise((resolve, reject) => { let output = [] const child = spawn(bin, ['--stdin']) child.stdout.on('data', c => output.push(c.toString())) child.stdin.write(code) child.stdin.end() child.on('close', exitCode => { return resolve(exitCode ? code : output.join('')) }) }) }
// Works on Mac. // TODO: Linux - Ubuntu et al. function get_arp_table(callback){ var arp = spawn('arp', ['-a']); var arp_str = ""; arp.stdout.setEncoding('utf8'); arp.stdout.on('data', function(data){ arp_str = data; }); arp.on('close', function(x){ callback(null, parse_arp_table(arp_str)); //arp.kill(); }); }
function deploy() { if (deployParams.length < 1) { return; } const deployParam = deployParams.shift(); console.log(`gcloud ${deployParam.join(' ')}`); spawn('gcloud', deployParam, { stdio: 'inherit', shell: true }).on('close', (code) => { console.log(`child process exited with code ${code}`); deploy(); }); }
vorpal .command('log start', 'Start output logs') .action(function(args, callback) { var self=this; if(tail){ self.log("Already listening to logs"); return callback(); } tail = spawn('tail', ['-f', appConfig.logFileName]); tail.stdout.on('data', function(data) { self.log(data.toString("UTF-8")); }); callback(); });
const action = async () => { const json = await ensurePackageJson(); if (!json) { return; } const scripts = Object.keys(json.scripts); const command = scripts.build; if (!command) { console.log(`${cross} build command not found in package.json`); console.log(''); process.exit(0); return; } const spawnOptions = { cwd: process.cwd(), detached: false, stdio: 'inherit', }; const parts = command.split(/\s+/); spawn(parts.shift(), parts, spawnOptions); }
function run(title) { console.log(`Run child process: ${title} ${scriptPath}`); const childProcess = spawn('node', [scriptPath]); console.log(`Child process ${title} pid: ${childProcess.pid}`); childProcess.stdout.on('data', (data) => process.stdout.write(`Process ${title}: ${data.toString()}`)); childProcess.stderr.on('data', (data) => process.stdout.write(`Process ${title}: ${data.toString()}`)); childProcess.on('error', ({message}) => console.log(`Process ${title} ERROR: ${message}`)); childProcess.on('close', (code) => console.log(`Process ${title} exited: ${code}`)); childProcesses.push(childProcess); }
const backupMongo = function (mongoUrl, path) { console.log('Backing up mongodb', mongoUrl, 'to', path); let uri = mongoUri.parse(mongoUrl); let parameters = dumpParameters(uri, path); const command = 'mongodump'; let future = new Future(); let dumpProcess = spawn(command, parameters); dumpProcess.on('error', console.log); dumpProcess.on('close', code => { console.log('mongodump ended with exit code', code); future.return(); }); return future.wait(); }
// 2. Word count using spawn(); pipe the input into this script: // // echo Hello World | node ec.js // // Running "node wc.js" doesn't work on Windows since there is // no equivalent to CTRL-D to terminate input function spawn_wc() { const { spawn } = require("child_process") const child = spawn("wc") process.stdin.pipe(child.stdin) child.stdout.on('data', (data) => { console.log(`child stdout:\t${data}`); }); }
function launch(id) { let prc = spawn('node', ['peer.js', id]); let stderr = ''; prc.stdout.setEncoding('utf8'); prc.stdout.on('data', function (data) { var str = data.toString() var lines = str.split(/(\r?\n)/g); console.log(lines.join("").trim()); }); prc.stderr.on('data', function (buf) { stderr += buf; console.log('Err: '+stderr); }); prc.on('close', function (code) { console.log('Peer ' + id + ' exited with code ' + code); console.log('stderr ' + stderr); }); }