Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Created November 23, 2014 18:59
Show Gist options
  • Save pascalduez/80724f440bb2b6a9ebee to your computer and use it in GitHub Desktop.
Save pascalduez/80724f440bb2b6a9ebee to your computer and use it in GitHub Desktop.
Conditionnaly install npm deps
// Programmatically install npm packages.
function npmInstall(pkgs, cb) {
var npm = require('npm');
npm.load({}, function (err) {
if (err) {
throw err;
}
console.log(chalk.red(
'>> Installing npm packages `' + pkgs.join(', ') + '`'
));
npm.commands.install(pkgs, function (err) {
if (err) {
throw err;
}
cb();
});
npm.on('log', console.log);
});
}
// Example
// Check whether `atom-screenshot` is installed,
// and install if need be.
grunt.registerTask('install-screenshot', 'Check and install screenshot deps', function () {
var done = this.async();
var atomScreenshot;
try {
atomScreenshot = require('atom-screenshot');
}
catch (err) {
npmInstall(['atom-screenshot'], done);
}
finally {
atomScreenshot && done();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment