-
-
Save Dmitry-N-Medvedev/6441b07f9524c0035a9a1daa55d11c88 to your computer and use it in GitHub Desktop.
Example use of spawnSync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options | |
const spawn = require('child_process').spawnSync | |
// object returned when process has completely exited | |
const child = spawn('which', ['node'] ) | |
// view status | |
console.log( child.status ) | |
// view output (stdout & stderr combined) | |
// must convert buffer to string | |
console.log (child.output.toString('utf8') ) | |
// e.g. function that uses spawnSync | |
// which: displays user path to specified binary | |
// @param command | |
// @return object | |
function which(cmd){ | |
return spawn('which', [cmd]) | |
} | |
console.log( which('npm').stdout.toString('utf8') ) | |
// piping | |
console.log( | |
"NODE PROCESSES: " + | |
spawn('grep', ['node'], { input: spawn('ps', ['aux'] ).stdout }) | |
.stdout.toString('utf8') | |
) | |
process.on('exit', function() { console.log('bye') } ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment