Skip to content

Instantly share code, notes, and snippets.

@indieisaconcept
Created November 5, 2012 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indieisaconcept/4016862 to your computer and use it in GitHub Desktop.
Save indieisaconcept/4016862 to your computer and use it in GitHub Desktop.
Grunt helpers for when using grunt-hub
var JSON5 = require('json5'),
path = require('path'),
fs = require('fs'),
existsSync = fs.existsSync || path.existsSync;
exports.init = function (grunt) {
'use strict';
grunt.util = grunt.util || grunt.utils;
var util = {},
exports = {},
_ = grunt.util._;
// ===========================
// UTILITIES
// ===========================
util = {
// shared:
// Load tasks which may have been installed in a parent directory. Typical usecase,
// would be if you are using something like grunt-hub to manage multiple projects
//
// local:
// Load tasks are local to this current Gruntfile only. This allows
// additional tasks to be installed on a project by project basis, either by
// npm or with a tasks directory
tasks: function (/* String */ mode, /* String */ base, /* Object */ options) {
var cwd = process.cwd(),
dir = base && existsSync(base) ? base : cwd,
loadAll = (options === true),
loaders = {
loadNpmTasks: loadAll ? loadAll : options.npm,
loadTasks: loadAll ? loadAll : options.tasks
},
changeDir = (mode === 'shared' && (dir !== cwd));
if (dir) {
Object.keys(loaders).forEach(function (/* String */ key) {
var loader = exports[key],
opts = loaders[key] !== true ? loaders[key] : undefined,
updateDir = changeDir ? process.cwd() !== dir : false;
if (loader) {
if (updateDir) {
process.chdir(dir);
}
loader(opts);
}
});
// restore the directory
if (changeDir && process.cwd() !== cwd) {
process.chdir(cwd);
}
} else {
//grunt.log.writeln('Loading shared tasks ...');
}
}
};
// ===========================
// TASK
// ===========================
// loadTasks
// Small wrapper around grunt.loadTasks to change the
exports.loadTasks = function (/* Object */ options) {
options = options || '**!(node_modules)/tasks/**!(lib)/*.js';
var tasks = _.isString(options) && grunt.file.expandFiles(options) || _.isArray(options) && options;
if (_.isArray(tasks)) {
grunt.verbose.writeln('Loading tasks ...');
_.chain(tasks).map(function (task) {
return path.dirname(task);
}).uniq().value().forEach(grunt.loadTasks);
}
};
// loadNpmTasks
// Wrapper around grunt.loadNpmTasks to allow an array of tasks
// to be loaded or based on dirs matching a pattern inside node_modules
exports.loadNpmTasks = function (/* Object */ options) {
options = options || {};
_.defaults(options, {
glob: 'grunt-*'
});
var prefixLen = options.glob.length,
tasks = options.tasks;
tasks = tasks || grunt.file.expandDirs('node_modules/' + options.glob);
// filter out tasks which do not meet the import
// criteria based on prefix defined
if (_.isArray(tasks)) {
grunt.verbose.writeln('Loading npm tasks ...');
tasks = tasks.map(function (/* String */ task) {
return path.basename(task);
});
console.log(tasks);
tasks.forEach(function (/* String */ task) {
grunt.loadNpmTasks(task);
});
}
},
// load
// Wrapper for loading various tasks for use by grunt for the current
// context.
//
// load({
// base: 'some/path',
// tasks: {
// shared: {
// npm: true | false,
// local: true | false
// }.
// local: {
// npm: true | false,
// local: true | false
// }
// }
// })
//
exports.load = function (/* Object */ options) {
var tasks = options.tasks ? Object.keys(options.tasks) : [],
base = options.base;
tasks.forEach(function (/* String */ key) {
util.tasks(key, base, options.tasks[key]);
});
};
// ===========================
// FILE
// ===========================
exports.file = {};
// readJSON
// Wrapper around grunt.file.readJSON to support JSON5
// https://github.com/gruntjs/grunt/pull/244
exports.file.readJSON = function (/* String */ filePath, /* String */ ext) {
var src = existsSync(filePath) ? grunt.file.read(filePath) : undefined,
result;
if (src) {
ext = (ext || path.extname(filePath)).replace('.', '').toUpperCase(),
grunt.verbose.write('Parsing ' + filePath + '...');
if (ext === 'JSON5') {
try {
result = JSON5.parse(src);
grunt.verbose.ok();
} catch (e) {
grunt.verbose.error();
throw grunt.util.error('Unable to parse "' + filePath + '" file (' + e.message + ').', e);
}
} else {
result = grunt.file.readJSON(filePath);
}
} else {
grunt.verbose.write('Unable to find ' + filePath);
}
return result;
};
return exports;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment