Skip to content

Instantly share code, notes, and snippets.

@luvies
Last active August 25, 2018 23:01
Show Gist options
  • Save luvies/bea309144c1e415f7a115daba9950e6d to your computer and use it in GitHub Desktop.
Save luvies/bea309144c1e415f7a115daba9950e6d to your computer and use it in GitHub Desktop.
Some helpers to aid in Jakefiles
/**
* Provides various helpers for running a Jakefile
* Source: https://devspri.me/jake-helpers
* @module jake-helpers
*/
const { promisify } = require('util');
// create promise version of exec
jake.exec.promise = promisify(jake.exec);
/**
* The config for the helpers.
*/
const jakeOptions = {
/**
* Options for the cmd helper.
*/
cmd: {
/**
* Whether to echo the commands to the console as they are ran.
*/
echo: true,
/**
* The prefix to output before each command echo (if echoCmd is set to true).
*/
echoPrefix: '> ',
/**
* The suffix to output after each command echo (if echoCmd is set to true).
*/
echoSuffix: '',
/**
* Whether to default printStdout to true. If the option is passed in, this is ignored.
*/
printStdout: true,
/**
* Whether to default printStderr to true. If the option is passed in, this is ignored.
*/
printStderr: true
}
};
exports.jakeOptions = jakeOptions;
function cmdApplyDef(opts, opt) {
if (jakeOptions.cmd[opt]) {
opts[opt] = typeof opts[opt] === 'undefined' ? true : opts[opt];
}
}
/**
* Executes a command using jake.exec, applying the jakeOptions and return a promise
* instead of using a callback.
*/
function cmd(c, opts) {
opts = opts || {};
cmdApplyDef(opts, 'printStdout');
cmdApplyDef(opts, 'printStderr');
if (jakeOptions.cmd.echo) {
cp = Array.isArray(c) ? `[${c.join(',')}]` : c;
console.log(jakeOptions.cmd.echoPrefix, cp, jakeOptions.cmd.echoSuffix);
}
return jake.exec.promise(c, opts);
};
exports.cmd = cmd;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment