Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 1, 2013 19:52
Show Gist options
  • Save cowboy/5067251 to your computer and use it in GitHub Desktop.
Save cowboy/5067251 to your computer and use it in GitHub Desktop.
Grunt: Test that package.json dependencies were installed correctly.
grunt.registerTask('deps', 'Test that package.json dependencies were installed correctly.', function() {
var pkg = grunt.file.readJSON('package.json');
var total = 0;
['dependencies', 'devDependencies'].forEach(function(key) {
var deps = pkg[key];
if (!deps) { return; }
grunt.verbose.subhead(key);
Object.keys(deps).forEach(function(name) {
total++;
var msg = 'Checking "' + name + '" dependency...';
grunt.verbose.write(msg);
try {
require(name);
grunt.verbose.ok();
} catch(err) {
grunt.verbose.or.write(msg);
grunt.log.error().error(err.message);
}
});
});
if (this.errorCount > 0) {
grunt.fail.fatal('Errors were encountered. Did you "npm install" first?');
} else {
grunt.verbose.writeln();
grunt.log.ok(total + ' dependencies installed correctly.');
}
});
@jzaefferer
Copy link

My grunt-check-modules task just delegates to npm ls and checks for an error exit code. Though this is an interesting idea, since npm ls has some bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment