From 71d4715e5ff163c4c423df8e3cc39d9b04612d02 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Thu, 3 May 2012 21:49:38 +1000 Subject: [PATCH] Renamed actions => commands --- lib/scaffolder.js | 53 ++++++++++---------- test/{actions.js => commands.js} | 6 +-- test/{test-actions => test-commands}/test.js | 0 3 files changed, 30 insertions(+), 29 deletions(-) rename test/{actions.js => commands.js} (81%) rename test/{test-actions => test-commands}/test.js (100%) diff --git a/lib/scaffolder.js b/lib/scaffolder.js index 46a44b0..fbe5d6c 100644 --- a/lib/scaffolder.js +++ b/lib/scaffolder.js @@ -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; @@ -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; } }); @@ -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') { @@ -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); }; @@ -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)); @@ -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; diff --git a/test/actions.js b/test/commands.js similarity index 81% rename from test/actions.js rename to test/commands.js index 92bc042..faa00d2 100644 --- a/test/actions.js +++ b/test/commands.js @@ -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(); }); }); diff --git a/test/test-actions/test.js b/test/test-commands/test.js similarity index 100% rename from test/test-actions/test.js rename to test/test-commands/test.js