Skip to content

Commit

Permalink
Renamed actions => commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Oehlman committed May 3, 2012
1 parent 9eb128d commit 71d4715
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
53 changes: 27 additions & 26 deletions lib/scaffolder.js
Expand Up @@ -29,10 +29,10 @@ function Scaffolder(opts) {
opts = opts || {};

// initialise the default action path
this.actionPath = opts.actionPath || path.join('lib', 'actions');
this.commandPath = opts.commandPath || path.join('lib', 'commands');

// initialise the empty actions array
this.actions = {};
// initialise the empty commands array
this.commands = {};

this.targetModule = opts.module;
this.ready = false;
Expand Down Expand Up @@ -123,35 +123,35 @@ Scaffolder.prototype.getPath = function(callback) {

Scaffolder.prototype.loadActions = function(callback) {
// determine the action path
var actionPath = path.resolve(this.srcPath, this.actionPath),
var commandPath = path.resolve(this.srcPath, this.commandPath),
scaffolder = this;

// read the files in the action path
debug('attempting to load action definitions from: ' + actionPath);
fs.readdir(actionPath, function(err, files) {
var action, reqErr, actionModule;
debug('attempting to load action definitions from: ' + commandPath);
fs.readdir(commandPath, function(err, files) {
var command, reqErr, commandModule;

(files || []).filter(filters.jsOnly).forEach(function(file) {
// initialise the action module path
actionModule = path.join(actionPath, file);
debug('attempting to load action module: ' + actionModule);
commandModule = path.join(commandPath, file);
debug('attempting to load action module: ' + commandModule);

// reset the action
action = null;
command = null;

// load the action module
try {
action = require(actionModule);
command = require(commandModule);
}
catch (e) {
debug('failed loading action module: ' + actionModule);
reqErr = reqErr || new Error('Unable to load action module: ' + actionModule);
debug('failed loading action module: ' + commandModule);
reqErr = reqErr || new Error('Unable to load action module: ' + commandModule);
}

// if we loaded the action succesfully, then add a few extra details and load
if (action) {
action.name = action.name || path.basename(file, '.js');
scaffolder.actions[action.name] = action;
if (command) {
command.name = command.name || path.basename(file, '.js');
scaffolder.commands[command.name] = command;
}
});

Expand Down Expand Up @@ -213,7 +213,7 @@ Scaffolder.prototype.main = function(opts, handler) {
};

Scaffolder.prototype.run = function(name, opts, callback) {
var action = this.actions[name];
var command = this.commands[name];

// remap args if required
if (typeof opts == 'function') {
Expand All @@ -222,22 +222,22 @@ Scaffolder.prototype.run = function(name, opts, callback) {
}

// if we don't have an action, trigger the callback
if (! action) {
callback(new Error('Unable to find action: ' + name));
if (! command) {
callback(new Error('Unable to find handler for command: ' + name));
return;
}

// if the action does not have a run function, then return an error
if (typeof action.run != 'function') {
if (typeof command.run != 'function') {
callback(new Error('Action "' + name + '" does not have a run function'));
return;
}

// parse the command line with reference to the specified command
opts = _.extend({}, opts, nopt(action.args, action.shorthand || {}, opts.argv, opts.startArg));
opts = _.extend({}, opts, nopt(command.args || {}, command.shorthand || {}, opts.argv, opts.startArg));

// run the action handler
action.run.call(this, opts, callback);
command.run.call(this, opts, callback);
};


Expand All @@ -252,11 +252,12 @@ exports = module.exports = function(opts, initFn) {
// ensure we have options
opts = opts || {};

// initialise the cmdline to process argv
opts.argv = opts.argv || process.argv;

// initialise the start arg to 0 if custom args have been provided, or 2 if we are using process.argv
opts.startArg = opts.argv ? 0 : 2;

// initialise the cmdline to process argv
opts.argv = opts.argv || process.argv;
debug('processing args: ', opts.argv.slice(opts.startArg));

// extend the opts by parsing with nopt
opts = _.extend({}, opts, nopt(coreCommands.known, coreCommands.shorthand, opts.argv, opts.startArg));
Expand All @@ -269,7 +270,7 @@ exports = module.exports = function(opts, initFn) {

// run the main function
// if an init function has been passed, that will run
// otherwise actions will be run if they are specified on the cli
// otherwise commands will be run if they are specified on the cli
scaffolder.main(opts, initFn);

return scaffolder;
Expand Down
6 changes: 3 additions & 3 deletions test/actions.js → test/commands.js
Expand Up @@ -6,14 +6,14 @@ var scaffolder = require('../'),
scaffolderOpts = {
argv: [],
silent: true,
actionPath: path.resolve(__dirname, 'test-actions')
commandPath: path.resolve(__dirname, 'test-commands')
};

describe('scaffolder action loader tests', function() {
it('should be able to load actions', function(done) {
scaffolder(scaffolderOpts).on('ready', function() {
expect(this.actions).to.be.ok();
expect(this.actions.test).to.be.ok();
expect(this.commands).to.be.ok();
expect(this.commands.test).to.be.ok();
done();
});
});
Expand Down
File renamed without changes.

0 comments on commit 71d4715

Please sign in to comment.