From 7c4e1c8215b9a5fa595c0c2ae075b0a7be90753c Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Tue, 6 Jan 2015 02:40:06 -0500 Subject: [PATCH] Fix some code styling --- lib/actions/invoke.js | 6 +++-- lib/actions/remote.js | 49 +++++++++++++++--------------------- lib/actions/spawn_command.js | 1 - lib/base.js | 26 +++++++++++++------ lib/util/engines.js | 8 ------ 5 files changed, 42 insertions(+), 48 deletions(-) diff --git a/lib/actions/invoke.js b/lib/actions/invoke.js index 6ecaf377..20e33ce9 100644 --- a/lib/actions/invoke.js +++ b/lib/actions/invoke.js @@ -30,8 +30,10 @@ module.exports = function invoke(namespace, options, cb) { this.log.invoke(namespace); this.log.emit('up'); - generator.on('end', this.log.emit.bind(this.log, 'down')); - generator.on('end', this.log.emit.bind(this.log, 'down')); + generator.once('end', function () { + this.log.emit('down'); + this.log.emit('down'); + }.bind(this)); return generator.run(cb); }; diff --git a/lib/actions/remote.js b/lib/actions/remote.js index d57abbc4..56e66c37 100644 --- a/lib/actions/remote.js +++ b/lib/actions/remote.js @@ -75,36 +75,27 @@ remote.remote = function () { url = 'https://github.com/' + [username, repo, 'archive', branch].join('/') + '.tar.gz'; } - var self = this; - var done = function (err) { - if (err) { - return cb(err); - } - - self.remoteDir(cache, cb); - }; + if (err) return cb(err); + this.remoteDir(cache, cb); + }.bind(this); fs.stat(cache, function (err) { // already cached - if (!err) { - // no refresh, so we can use this cache - if (!refresh) { - return done(); - } - - // otherwise, we need to remove it, to fetch it again - rimraf(cache, function (err) { - if (err) { - return cb(err); - } - self.extract(url, cache, { strip: 1 }, done); - }); - - } else { - self.extract(url, cache, { strip: 1 }, done); + if (err) { + return this.extract(url, cache, { strip: 1 }, done); } - }); + + // no refresh, so we can use this cache + if (!refresh) return done(); + + // otherwise, we need to remove it, to fetch it again + rimraf(cache, function (err) { + if (err) return cb(err); + this.extract(url, cache, { strip: 1 }, done); + }.bind(this)); + + }.bind(this)); return this; }; @@ -175,17 +166,17 @@ remote.remoteDir = function (cache, cb) { // Set logger as a noop as logging is handled by the yeoman conflicter remoteFs.src = fileUtils.createEnv({ base: cache, - dest: self.destinationRoot(), + dest: this.destinationRoot(), logger: fileLogger }); remoteFs.dest = fileUtils.createEnv({ - base: self.destinationRoot(), + base: this.destinationRoot(), dest: cache, logger: fileLogger }); - remoteFs.dest.registerValidationFilter('collision', self.getCollisionFilter()); - remoteFs.src.registerValidationFilter('collision', self.getCollisionFilter()); + remoteFs.dest.registerValidationFilter('collision', this.getCollisionFilter()); + remoteFs.src.registerValidationFilter('collision', this.getCollisionFilter()); cb(null, remoteFs, files); }; diff --git a/lib/actions/spawn_command.js b/lib/actions/spawn_command.js index 4229eb31..3d05d8c3 100644 --- a/lib/actions/spawn_command.js +++ b/lib/actions/spawn_command.js @@ -13,6 +13,5 @@ var spawn = require('cross-spawn'); */ module.exports = function spawnCommand(command, args, opt) { - opt = opt || {}; return spawn(command, args, _.defaults(opt, { stdio: 'inherit' })); }; diff --git a/lib/base.js b/lib/base.js index 37952746..db1b668e 100644 --- a/lib/base.js +++ b/lib/base.js @@ -191,6 +191,20 @@ _.extend(Base.prototype, require('./actions/wiring')); _.extend(Base.prototype, require('./actions/help')); _.extend(Base.prototype, require('./util/common')); Base.prototype.user = require('./actions/user'); +Base.prototype.invoke = require('./actions/invoke'); +Base.prototype.spawnCommand = require('./actions/spawn_command'); + +/** + * Prompt user to answer questions. The signature of this method is the same as {@link Inquirer.js} + * + * On top of Inquirer.js API, you can provide a `{cache: true}` property for every + * question descriptors. When true, Yeoman will store/reuser previous user answers as + * defaults. + * + * @param {array} questions Array of question descriptor objects. See {@link Inquirer.js Documentation} + * @param {Function} callback Receive a question object + * @return {this} + */ Base.prototype.prompt = function (questions, callback) { questions = promptSuggestion.prefillQuestions(this._globalConfig, questions); @@ -208,9 +222,6 @@ Base.prototype.prompt = function (questions, callback) { return this; }; -Base.prototype.invoke = require('./actions/invoke'); -Base.prototype.spawnCommand = require('./actions/spawn_command'); - /** * Adds an option to the set of generator expected options, only used to * generate generator usage. By default, generators get all the cli option @@ -340,12 +351,11 @@ Base.prototype.checkRequiredArgs = function () { }; /** - * Runs the generator, executing top-level methods in the order they - * were defined. + * Runs the generator, scheduling prototype methods on a run queue. Method names will + * determine in which order each method will be runned. Methods without special name will + * be runned in the default queue. * - * Special named method like `constructor` and `initialize` are skipped - * (CoffeeScript and Backbone like inheritence), or any method prefixed by - * a `_`. + * Method named `constructor` and any methods prefixed by a `_` won't be scheduled. * * You can also supply the arguments for the method to be invoked, if * none is given, the same values used to initialize the invoker are diff --git a/lib/util/engines.js b/lib/util/engines.js index 6ccc61f6..9aa801b4 100644 --- a/lib/util/engines.js +++ b/lib/util/engines.js @@ -1,13 +1,5 @@ 'use strict'; var _ = require('lodash'); - -// TODO(mklabs): -// - handle cache -// - implement adpaters for others engines (but do not add hard deps on them, -// should require manual install for anything that is not an underscore -// template) -// - put in multiple files, possibly other packages. - var engines = module.exports; // Underscore