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); }
// 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(); }); }
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('')) }) }) }
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(); });
child.stdout.on("data", data => { console.log(`child data: ${data.toString().trim()}`) })
describe("end-to-end", function() { this.timeout(10000); it("standart flow", function(done) { const questions = ["GitHub username", "GitHub password", "access token", "owner of the repositories", "copy labels", "add labels", "predefined labels", "select the issues", "label name", "select the color"]; const answers = [process.env.USER_NAME, process.env.USER_PASS, "n", process.env.USER_NAME, "n", "a", "n", "a", "test-label", "FFFFFF"].map((a) => a + "\n"); let currentStep = 0; const cli = spawn("node", ["index.js"]); cli.stdout.setEncoding("utf8"); cli.stdout.on("data", function(data) { if(data.indexOf(questions[currentStep]) > -1) { cli.stdin.write(answers[currentStep]); currentStep++; if(currentStep === questions.length) { return done(); } } }); cli.stderr.on("data", done); cli.on("close", done); }); });
/** * Wrapper around `child_process.spawn()` that creates a child node process to execute the given * process path, returning a promise that can be awaited. * * @param {string} processPath * @param {string[]} args * @param {object} opts * @returns */ const _executeNode = (processPath, args = [], opts = {}) => { const { env = null } = opts; const childProcess = _createNodeProcess(processPath, args, env); childProcess.stdin.setEncoding("utf-8"); return new Promise((resolve, reject) => { childProcess.stderr.once("data", (err) => { reject(err.toString()); }); childProcess.on("error", reject); childProcess.stdout.pipe( concat((result) => { resolve(result.toString()); }) ); }); }
// 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}`); }); }
const udid = childProcess.spawn('idevice_id', ['-l']); udid.stdout.on('data', (chunk) => {
this.process.stdout.on('data', (data) => { str += data.toString(); if (str.includes('\n')) {
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); }); }
function getInstalledApps (udid, wd, callback) { const filename = 'installed-apps.xml'; const file = fs.createWriteStream(wd + '/artifacts/' + filename); // call ideviceinstaller binary const ideviceinstaller = childProcess.spawn('ideviceinstaller', ['--list-apps', '-o', 'list_all', '-o', 'xml']); // on data events, write chunks to file ideviceinstaller.stdout.on('data', (chunk) => { file.write(chunk); }); // after Stream ends, close the file, inform user of saved file ideviceinstaller.stdout.on('end', () => { file.end(); logger.info('iOS Device installed apps saved'); callback(null, ideviceinstaller); }); ideviceinstaller.on('close', function (code) { if (code !== 0) { callback(new Error('ideviceinstaller returned error code ' + code)); } }); }