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('')) }) }) }
// 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}`); }); }
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()); }) ); }); }