I'm working on a small cli tool that can automatically deploy Google Home actions based on the projects that are setup in a directory.
Basically my script checks the directories and then asks which project to deploy. The actual mand that should run is ing from Google's cli gactions
To run it with arguments I setup a spawned process in my node-script:
const { spawn } = require('child_process')
const child = spawn('./gactions', [
'update',
'--action-package',
'<PATH-TO-PACKAGE>',
'--project',
'<PROJECT-NAME>'
])
child.stdout.on('data', data => {
console.log(data)
}
However, the first time a project is deployed, the gactions cli will prompt for an authorization code. Running the code above, I can actually see the prompt, but the script won't proceed when actually entering that code.
I guess there must be some way in the child process to capture that input? Or isn't this possible at all?
I'm working on a small cli tool that can automatically deploy Google Home actions based on the projects that are setup in a directory.
Basically my script checks the directories and then asks which project to deploy. The actual mand that should run is ing from Google's cli gactions
To run it with arguments I setup a spawned process in my node-script:
const { spawn } = require('child_process')
const child = spawn('./gactions', [
'update',
'--action-package',
'<PATH-TO-PACKAGE>',
'--project',
'<PROJECT-NAME>'
])
child.stdout.on('data', data => {
console.log(data)
}
However, the first time a project is deployed, the gactions cli will prompt for an authorization code. Running the code above, I can actually see the prompt, but the script won't proceed when actually entering that code.
I guess there must be some way in the child process to capture that input? Or isn't this possible at all?
Share Improve this question edited Nov 26, 2018 at 16:35 mihai 38.6k11 gold badges62 silver badges89 bronze badges asked Nov 19, 2018 at 14:37 MaartenMaarten 6452 gold badges8 silver badges22 bronze badges 2-
The child process's
stdin
,stdout
, andstderr
are streams, so you can manipulate them as you like. The node documentation gives you a very simple example as a starting point. – lependu Commented Nov 19, 2018 at 15:00 - Ok. But how do I capture that input when prompted for? – Maarten Commented Nov 19, 2018 at 16:03
2 Answers
Reset to default 8Simply pipe all standard input from the parent process to the child and all output from the child to the parent.
The code below is a full wrapper around any shell mand, with input/output/error redirection:
const { spawn } = require('child_process');
var child = spawn(mand, args);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
process.stdin.pipe(child.stdin);
child.on('exit', () => process.exit())
Note that if you pipe stdout
you don't need handle the data
event anymore.
require( "child_process" ).spawnSync( "sh", [ "-c", "npm adduser" ], { stdio: "inherit", stdin: "inherit" } );
this will execute the mand given as we normally do in terminal.