Skip to content

Instantly share code, notes, and snippets.

@wjramos
Created March 10, 2017 21:15
Show Gist options
  • Save wjramos/6012f847368055d727d9efc7834f593c to your computer and use it in GitHub Desktop.
Save wjramos/6012f847368055d727d9efc7834f593c to your computer and use it in GitHub Desktop.
import colors from 'colors';
import { spawn } from 'child_process';
function asyncSpawn(command, stdout = true, stderr = true) {
const [cmd, ...args] = command.split(' ');
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, { env });
if (stdout) {
child.stdout.on('data', buffer => {
process.stdout.write(colors.white(buffer));
});
} else {
child.stdout.on('data', buffer => {
process.stdout.write(colors.white('.'));
});
}
if (stderr) {
child.stderr.on('data', buffer => {
process.stdout.write(colors.blue(buffer));
});
} else {
child.stdout.on('data', buffer => {
process.stdout.write(colors.white('.'));
});
}
child.on('close', code => {
if (code > 0) {
return reject();
}
return resolve();
});
child.on('error', err => {
error('Failed to start child process.' + err);
return reject();
});
return child;
});
}
function success(...messages) {
messages.forEach(msg => console.log('\n', colors.green.bold(msg), '\n\n'));
}
function error(...messages) {
messages.forEach(msg => console.error('\n', colors.red.bold(msg), '\n\n'));
}
function warn(...messages) {
messages.forEach(msg => console.warn('\n', colors.yellow.bold(msg), '\n\n'));
}
async function runBuild() {
console.log(colors.white.underline('Building project...'));
const command = 'cross-env babel-node ./node_modules/webpack/bin/webpack --progress';
try {
await asyncSpawn(command);
success(`Web assets have been successfully generated.`);
} catch (e) {
error('Could not successfully build.', e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment