Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created October 1, 2016 18:57
Show Gist options
  • Save mhulse/b377800f25781ca5cd96ce557dcf419d to your computer and use it in GitHub Desktop.
Save mhulse/b377800f25781ca5cd96ce557dcf419d to your computer and use it in GitHub Desktop.
Asynchronous ImageMagick processing using Node.js.
let commands = {};
commands['make title text with drop shadow'] = `
convert \
-background none \
-font "Roboto Condensed/RobotoCondensed-Regular.ttf" \
-pointsize 500 \
-gravity Center \
-interline-spacing -30 \
-bordercolor none \
-strokewidth 16 \
\\( \
-stroke black \
label:"This is my super duper cool title that is long" \
-border 200x200 \
-blur 0x32 \
\\) \
\\( \
-stroke none \
-fill white \
label:"This is my super duper cool title that is long" \
-border 200x200 \
\\) \
-flatten \
+repage \
title.png
`;
// Add more commands here.
// Promisifying `child_process`:
function promiseFromChildProcess(child) {
return new Promise((resolve, reject) => {
child.addListener('error', (code, signal) => {
console.log('ChildProcess error', code, signal);
reject();
});
child.addListener('exit', (code, signal) => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
};
function childProcess(command) {
let child = exec(command);
child.stdout.on('data', function(data) {
console.log('stdout:', data);
});
child.stderr.on('data', function(data) {
console.log('stderr:', data);
});
child.on('close', function(code) {
//console.log('closing code:', code);
});
return this.promiseFromChildProcess(child);
};
Promise.resolve(true) // Dummy promise to make following chain more readable.
.then(() => {
console.log('Command 1 started …');
return childProcess(commands['make title text with drop shadow']);
}).then(() => {
console.log('Command 2 started …');
//return childProcess(…); // Rinse, wash and repeat.
return true;
}).then(() => {
console.log('Done!');
}).catch(() => {
console.log('SOME TESTS OR SOMETHING IN THE SAUCE STACK HAS FAILED!');
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment