I'm not setup here to run jitsu deploy
. However, here is some code that illustrates how you can deal with the prompts:
var command = require('child_process').spawn('./command.js');
require('colors');
var stdout = "";
var prompt_re = /Is it okay \(yes\)\?.*?$/m;
command.stdout.on('data', function (data) {
console.log("stdout data: ".green + data);
stdout += data;
if (prompt_re.test(stdout)) {
command.stdin.write("yes\n");
// Flush the current buffer.
stdout = "";
}
});
command.stdout.on('error', function (err) {
console.log('stdout error: '.red, err);
});
var exit_msg = 'Exited with code... ';
command.on('exit', function (code) {
if (code != 0) {
console.log(exit_msg.red, code);
process.exit(1); // Or whatever you want to handle errors.
}
console.log(exit_msg.green, code);
// The code you want to execute once your command is done goes here.
});
Some explanations:
The code above buffers the data it gets from
command.stdout
into as string stored in thestdout
variable and tests against that because if there's a lot of output there's no guarantee that the prompt will arrive in a singledata
event. (It could, for instance, come in one event that contains a bunch of data + the stringIs it
and then the nextdata
event could contain the rest of the prompt.)The regular expression
prompt_re
contains '.*?$' because the command I use to simulate getting a prompt uses escape codes for colors and I could not be bothered to match against the exact codes that are output.This code assumes that the command will stop right away after outputting a prompt and waits there. This seems to be a safe assumption.
It also assumes that the prompt text cannot appear as something which is not a prompt. Whether this is safe depends on your specific scenario.
The ./command.js
file I used to simulate the command being run is:
#!/usr/bin/env node
var prompt = require("prompt");
function ask(cb) {
prompt.get(["Is it okay (yes)?"], function (err, result) {
console.log("asked");
cb();
});
}
setTimeout(function () {
ask(function () {
setTimeout(function () {
ask(function () { process.exit(0); });
}, 1000);
});
}, 5000);
It waits 5s, prompts once, waits 1s, prompts a second time and exits.