Created
November 23, 2014 18:59
-
-
Save pascalduez/80724f440bb2b6a9ebee to your computer and use it in GitHub Desktop.
Conditionnaly install npm deps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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