Skip to content

Instantly share code, notes, and snippets.

@msikma
Last active October 26, 2018 00:47
Show Gist options
  • Save msikma/43894340eed3d28f435e0c3ac273aa92 to your computer and use it in GitHub Desktop.
Save msikma/43894340eed3d28f435e0c3ac273aa92 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const { execSync, spawn, exec } = require('child_process')
// Pass '0', '1' or '2' as only argument.
const type = Math.max(Math.min(process.argv[2] ? parseInt(process.argv[2], 10) : 0, 2), 0)
console.log('Type', type)
// Note: executing 'command' which outputs more than 8192 characters of data.
// This is caused by a process.exit(0) (in the 'command' script) that appears
// immediately after a large write operation, e.g. a console.log() over 8192 bytes.
// If this is the case, the process may exit before the write is complete.
//
// To fix, replace the process.exit(0) with process.exitCode = 0
const cmd = 'insert command here'
if (type === 0) {
let stdout = execSync(cmd)
console.log(stdout.buffer)
console.log('Length', stdout.toString('utf8').length) // 8191
}
if (type === 1) {
const child = spawn(cmd)
let out = ''
child.stdout.on('data', (data) => {
out += data.toString()
console.log('Length', out.length) // 8191
});
child.stdout.on('end', (data) => {
console.log('Done')
console.log('Final length', out.length) // 8191
});
}
if (type === 2) {
const child = exec(cmd, (err, stdout, stderr) => {
console.log(stdout.length) // 8191
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment