UNPKG

1.24 kBJavaScriptView Raw
1'use strict';
2const path = require('path');
3const pkgUp = require('pkg-up');
4const multimatch = require('multimatch');
5const arrify = require('arrify');
6const resolvePkg = require('resolve-pkg');
7
8module.exports = (grunt, options = {}) => {
9 const pattern = arrify(options.pattern || ['grunt-*', '@*/grunt-*']);
10 const scope = arrify(options.scope || ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']);
11
12 let cwd = process.cwd();
13 let config = options.config || pkgUp.sync();
14 if (typeof config === 'string') {
15 const configPath = path.resolve(config);
16 cwd = path.dirname(configPath);
17 config = require(configPath);
18 }
19
20 pattern.push('!grunt', '!grunt-cli');
21
22 const names = scope.reduce((result, property) => {
23 const dependencies = config[property] || [];
24 return result.concat(Array.isArray(dependencies) ? dependencies : Object.keys(dependencies));
25 }, []);
26
27 for (const packageName of multimatch(names, pattern)) {
28 if (options.requireResolution === true) {
29 try {
30 grunt.loadTasks(resolvePkg(path.join(packageName, 'tasks'), {cwd}));
31 } catch (_) {
32 grunt.log.error(`npm package \`${packageName}\` not found. Is it installed?`);
33 }
34 } else {
35 grunt.loadNpmTasks(packageName);
36 }
37 }
38};