Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.