Skip to content

Instantly share code, notes, and snippets.

@LouisRitchie
Created June 15, 2018 02:14
Show Gist options
  • Save LouisRitchie/a797882ce9b934b2f08a649bf7261776 to your computer and use it in GitHub Desktop.
Save LouisRitchie/a797882ce9b934b2f08a649bf7261776 to your computer and use it in GitHub Desktop.
Run shell from javascript. Want to call a bash script with many varied args? Put arg sets into JSON, `require('./my.json')`, form the command string and run it. Used with https://gist.github.com/LouisRitchie/9a90badb920bea29c65100a757b1c95d to get chapters from a book.
const { exec } = require('child_process')
const fs = require('fs')
const SCRIPT_FILENAME = 'split_chapter_into_filename.sh'
function executeChildProcess(command, shouldLog) {
const dir = exec(command, (err, stdout, stderr) => {
if (shouldLog) {
console.log(stdout)
}
})
if (shouldLog) {
dir.on('exit', code => {
console.log(`exit code: ${code}`)
})
}
}
if (!fs.existsSync(SCRIPT_FILENAME)) {
throw `${SCRIPT_FILENAME} does not exist; exiting.`
}
require('./pages.json').map((tuple, i) => executeChildProcess(`./${SCRIPT_FILENAME} textbook.djvu ${tuple[0] + 21} ${tuple[1] + 21} chapter_${i}.djvu`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment