index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. var path = require('path');
  3. var pkgUp = require('pkg-up');
  4. var multimatch = require('multimatch');
  5. var arrify = require('arrify');
  6. var resolvePkg = require('resolve-pkg');
  7. module.exports = function (grunt, opts) {
  8. opts = opts || {};
  9. var pattern = arrify(opts.pattern || ['grunt-*', '@*/grunt-*']);
  10. var config = opts.config || pkgUp.sync();
  11. var scope = arrify(opts.scope || ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']);
  12. if (typeof config === 'string') {
  13. config = require(path.resolve(config));
  14. }
  15. pattern.push('!grunt', '!grunt-cli');
  16. var names = scope.reduce(function (result, prop) {
  17. var deps = config[prop] || [];
  18. return result.concat(Array.isArray(deps) ? deps : Object.keys(deps));
  19. }, []);
  20. multimatch(names, pattern).forEach(function (pkgName) {
  21. if (opts.requireResolution === true) {
  22. try {
  23. grunt.loadTasks(resolvePkg(path.join(pkgName, 'tasks')));
  24. } catch (err) {
  25. grunt.log.error('npm package "' + pkgName + '" not found. Is it installed?');
  26. }
  27. } else {
  28. grunt.loadNpmTasks(pkgName);
  29. }
  30. });
  31. };