Skip to content

Commit

Permalink
No globals?
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Mar 12, 2012
1 parent a0dfb40 commit 6beb430
Show file tree
Hide file tree
Showing 44 changed files with 2,204 additions and 1,884 deletions.
97 changes: 54 additions & 43 deletions grunt.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
/*global config:true, task:true*/
config.init({
pkg: '<json:package.json>',
test: {
all: ['test/**/*.js']
},
lint: {
all: ['grunt.js', 'lib/**/*.js', 'tasks/*.js', 'tasks/*/*.js', 'test/**/*.js']
},
watch: {
files: '<config:lint.all>',
tasks: 'default'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true
/*
* grunt
* https://github.com/cowboy/grunt
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/

module.exports = function(grunt) {
// Grunt utilities.
var task = grunt.task;
var file = grunt.file;
var utils = grunt.utils;
var log = grunt.log;
var verbose = grunt.verbose;
var fail = grunt.fail;
var option = grunt.option;
var config = grunt.config;
var template = grunt.template;

// Initialize config.
grunt.initConfig({
pkg: '<json:package.json>',
test: {
all: ['test/**/*.js']
},
lint: {
all: ['grunt.js', 'lib/**/*.js', 'tasks/*.js', 'tasks/*/*.js', 'test/**/*.js']
},
globals: {
exports: true,
grunt: true,
utils: true,
task: true,
file: true,
fail: true,
config: true,
option: true,
template: true,
log: true,
verbose: true
watch: {
files: '<config:lint.all>',
tasks: 'default'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true
},
globals: {}
}
}
});
});

// Default task.
grunt.registerTask('default', 'lint test');

// Default task.
task.registerTask('default', 'lint test');
};
61 changes: 33 additions & 28 deletions lib/grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,35 @@

var path = require('path');

// Perhaps, someday, this will be the only global.
global.grunt = {};

// Used to require grunt libs. Sweet, sugary goodness. Mmmmn.
grunt.require = function(libname) { return require(path.join(__dirname, 'grunt', libname)); };

// Internal grunt libs, exposed globally for convenience.
global.utils = grunt.require('utils');
global.task = grunt.require('task');
global.file = grunt.require('file');
global.fail = grunt.require('fail');
global.config = grunt.require('config');
global.option = grunt.require('option');
global.template = grunt.require('template');
global.log = grunt.require('log');
global.verbose = log.verbose;

grunt.version = file.readJson(path.join(__dirname, '../package.json')).version;
exports.version = grunt.version;
var grunt = module.exports = {};

// Internal grunt libs, exposed for convenience.
var utils = grunt.utils = require('./grunt/utils');
var template = grunt.template = require('./grunt/template');
var log = grunt.log = require('./grunt/log');
var verbose = grunt.verbose = log.verbose;
var fail = grunt.fail = require('./grunt/fail');
var file = grunt.file = require('./grunt/file');
var option = grunt.option = require('./grunt/option');
var config = grunt.config = require('./grunt/config');
var task = grunt.task = require('./grunt/task');
var version = grunt.version = file.readJson(path.join(__dirname, '../package.json')).version;

// More convenience stuff.
grunt.registerTask = task.registerTask;
grunt.registerMultiTask = task.registerMultiTask;
grunt.registerInitTask = task.registerInitTask;
grunt.registerHelper = task.registerHelper;
grunt.helper = task.helper.bind(task);
grunt.loadTasks = task.loadTasks;
grunt.loadNpmTasks = task.loadNpmTasks;

grunt.initConfig = config.init;

// Handle exceptions.
process.on('uncaughtException', function (e) {
fail.warn(e, 3);
});
// process.on('uncaughtException', function (e) {
// fail.warn(e, 3);
// });

// Disable colors if --no-colors was passed.
function initColors() {
Expand Down Expand Up @@ -67,7 +72,7 @@ function initColors() {

// Expose the task interface. I've never called this manually, and have no idea
// how it will work. But it might.
exports.tasks = function(tasks, options, done) {
grunt.tasks = function(tasks, options, done) {
// Update options with passed-in options.
option.init(options);

Expand All @@ -76,10 +81,10 @@ exports.tasks = function(tasks, options, done) {

if (option('help')) {
// Load and display help if the user did --help.
grunt.require('help');
require('./grunt/help');
} else if (option('version')) {
// Display the current grunt version if the user did --version.
log.writeln('grunt v' + grunt.version);
log.writeln('grunt v' + version);
return;
}

Expand Down Expand Up @@ -125,9 +130,9 @@ exports.tasks = function(tasks, options, done) {
};

// This is only executed when run via command line.
exports.cli = function(options, done) {
grunt.cli = function(options, done) {
// Parse task list and options from the command line.
var cli = grunt.require('cli');
var cli = require('./grunt/cli');

// CLI-parsed options override any passed-in "default" options.
if (options) {
Expand All @@ -145,5 +150,5 @@ exports.cli = function(options, done) {
}

// Run tasks.
exports.tasks(cli.tasks, cli.options, done);
grunt.tasks(cli.tasks, cli.options, done);
};
76 changes: 39 additions & 37 deletions lib/grunt/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,115 +7,117 @@
* http://benalman.com/about/license/
*/

var grunt = require('../grunt');

// The actual config data.
var data;

// If prop is an array, convert it to a props string.
function getPropString(prop) {
if (utils.kindOf(prop) === 'array') {
return prop.map(exports.escape).join('.');
}
return prop;
}

// Get/set config data. If data hasn't been set, return null. If value was
// passed, set value. If props string wasn't passed, return all data. Otherwise,
// return the prop's value.
exports = module.exports = function(prop, value) {
var config = module.exports = function(prop, value) {
if (arguments.length === 2) {
// Two arguments were passed, set the property's value.
return exports.set(prop, value);
return config.set(prop, value);
} else {
// Get the property's value (or the entire data object).
return exports.get(prop);
return config.get(prop);
}
};

// If prop is an array, convert it to a props string.
function getPropString(prop) {
if (grunt.utils.kindOf(prop) === 'array') {
return prop.map(config.escape).join('.');
}
return prop;
}

// Get config data.
exports.get = function(prop) {
config.get = function(prop) {
// Abort if no config data exists.
if (!data) { return null; }
// If prop is an array, convert it to a prop string.
prop = getPropString(prop);

if (prop) {
// A property string/array was passed, get that property's value.
return utils.namespace.get(data, prop);
return grunt.utils.namespace.get(data, prop);
} else {
// Nothing was passed. Return a shalow clone of the actual config data.
return utils._.clone(data);
return grunt.utils._.clone(data);
}
};

// Set config data.
exports.set = function(prop, value) {
config.set = function(prop, value) {
// Abort if no config data exists.
if (!data) { return null; }
// If prop is an array, convert it to a prop string.
prop = getPropString(prop);
// Set the property's value.
return utils.namespace.set(data, prop, value);
return grunt.utils.namespace.set(data, prop, value);
};

// Get config data, processing it as a template if necessary.
exports.process = function(prop) {
return utils.recurse(exports.get(prop), function(value) {
config.process = function(prop) {
return grunt.utils.recurse(config.get(prop), function(value) {
if (typeof value !== 'string') { return value; }
return template.process(value, data);
return grunt.template.process(value, data);
});
};

// Initialize config data.
exports.init = function(obj) {
verbose.write('Initializing config...').ok();
config.init = function(obj) {
grunt.verbose.write('Initializing config...').ok();
// Initialize data.
data = obj || {};
// Expand data.
exports.expand();
config.expand();
// Return data.
return data;
};

// Recursively expand config directives.
exports.expand = function() {
config.expand = function() {
// Expand expandable directives.
var expandable = ['config', 'json'];
data = utils.recurse(data, function(value) {
data = grunt.utils.recurse(data, function(value) {
if (typeof value !== 'string') { return value; }
// If value is an expandable directive, expand it.
var parts = task.getDirectiveParts(value) || [];
return expandable.indexOf(parts[0]) !== -1 ? task.directive(value) : value;
var parts = grunt.task.getDirectiveParts(value) || [];
return expandable.indexOf(parts[0]) !== -1 ? grunt.task.directive(value) : value;
});
};

// Escape any . in name with \. so dot-based namespacing works properly.
exports.escape = function(str) {
config.escape = function(str) {
return str.replace(/\./g, '\\.');
};

// Test to see if required config params have been defined. If not, throw an
// exception (use this inside of a task).
exports.requires = function() {
var props = utils.toArray(arguments).map(getPropString);
config.requires = function() {
var props = grunt.utils.toArray(arguments).map(getPropString);
var msg = 'Verifying option' + (props.length === 1 ? '' : 's') +
' ' + log.wordlist(props) + ' exist' + (props.length === 1 ? 's' : '') +
' ' + grunt.log.wordlist(props) + ' exist' + (props.length === 1 ? 's' : '') +
' in config...';
verbose.write(msg);
grunt.verbose.write(msg);
var failProps = data && props.filter(function(prop) {
return exports(prop) === undefined;
return config.get(prop) === undefined;
}).map(function(prop) {
return '"' + prop + '"';
});
if (data && failProps.length === 0) {
verbose.ok();
grunt.verbose.ok();
return true;
} else {
verbose.or.write(msg);
log.error().error('Unable to process task.');
grunt.verbose.or.write(msg);
grunt.log.error().error('Unable to process task.');
if (!data) {
throw task.taskError('Unable to load config.');
throw grunt.task.taskError('Unable to load config.');
} else {
throw task.taskError('Required config propert' +
throw grunt.task.taskError('Required config propert' +
(failProps.length === 1 ? 'y' : 'ies') + ' ' + failProps.join(', ') +
' missing.');
}
Expand Down
Loading

0 comments on commit 6beb430

Please sign in to comment.