UNPKG

nltco-load-grunt-parent-tasks

Version:
79 lines (64 loc) 2.33 kB
'use strict'; var path = require('path'); /** * Returns the element as an array. * @method toArray * @param {*} el An array or any other data type. * @return {Array} The element as an array. */ function toArray(el) { return Array.isArray(el) ? el : [el]; } /** * Creates a resolution for loading a package.json file. * @method resolveConfig * @param {String|Object} config The location of a package.json or the contents of the package.json. * @return {Object} The contents of the package.json. */ function resolveConfig(config) { if (typeof config === 'string') { return require(path.resolve(config)); } return config; } function loadGruntParentTasks(grunt, options) { var log = function() { grunt.verbose.writeln(['[loadGruntParentTasks]'.magenta].concat(Array.prototype.slice.call(arguments)).join(' ')); }; options = options || {}; var _ = require('lodash'); var findup = require('findup-sync'); var globule = require('globule'); var config = resolveConfig(options.config || findup('package.json')); var pattern = toArray(options.pattern || ['grunt-*']); var scope = toArray(options.scope || ['dependencies', 'optionalDependencies']); var gruntCollection = path.resolve('./node_modules/', (options.module || 'grunt-collection')); var gruntCollectionJson = path.join(gruntCollection, 'package.json'); var newPkg = {}; var names = []; var deps = {}; pattern.push('!grunt', '!grunt-cli'); names = scope.reduce(function (result, prop) { deps[prop] = {}; return result.concat(Object.keys(config[prop] || {})); }, []); // Create a new depenencies object of grunt tasks to load based on the // globbing pattern and scope. globule.match(pattern, names).map(function(name) { scope.forEach(function (prop) { if (config[prop] && config[prop][name]) { deps[prop][name] = config[prop][name]; } }); }); newPkg = _.assign({}, config, { keywords: [ 'gruntcollection' ], scripts: {} }, deps); log('Writing: ' + ('' + gruntCollectionJson).cyan); grunt.file.mkdir(gruntCollection); grunt.file.write(gruntCollectionJson, JSON.stringify(newPkg, null, 2)); log('loadNpmTasks: ' + 'grunt-collection'.cyan); grunt.loadNpmTasks('grunt-collection'); } module.exports = loadGruntParentTasks;