Skip to content

Commit

Permalink
fix a number of small errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante committed Mar 31, 2016
1 parent 8402639 commit 84b9b97
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 117 deletions.
74 changes: 37 additions & 37 deletions bin/sprout
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

var CLI = require('./../lib')
, pkg = require('./../package.json')
, ArgParse = require('argparse').ArgumentParser;
var pkg = require('./../package.json')
var ArgParse = require('argparse').ArgumentParser

/*
* Create argument parser.
Expand All @@ -12,7 +12,7 @@ var parser = new ArgParse({
version: pkg.version,
description: pkg.description,
addHelp: true
});
})

/*
* Create argument subparser.
Expand All @@ -21,59 +21,59 @@ var parser = new ArgParse({
var subParser = parser.addSubparsers({
title: 'Actions',
dest: 'action'
});
})

/*
* $ sprout add
*/

var add = subParser.addParser('add', { aliases: ['install'], addHelp: true });
add.addArgument(['name'], { help: 'name of template' });
add.addArgument(['src'], { help: 'source path or remote git' });
add.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' });
add.setDefaults({ action: 'add' });
var add = subParser.addParser('add', { aliases: ['install'], addHelp: true })
add.addArgument(['name'], { help: 'name of template' })
add.addArgument(['src'], { help: 'source path or remote git' })
add.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' })
add.setDefaults({ action: 'add' })

/*
* $ sprout remove
*/

var remove = subParser.addParser('remove', { aliases: ['delete', 'rm'], addHelp: true });
remove.addArgument(['name'], { help: 'name of template' });
remove.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' });
remove.setDefaults({ action: 'remove' });
var remove = subParser.addParser('remove', { aliases: ['delete', 'rm'], addHelp: true })
remove.addArgument(['name'], { help: 'name of template' })
remove.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' })
remove.setDefaults({ action: 'remove' })

/*
* $ sprout list
*/

var list = subParser.addParser('list', { aliases: ['ls', 'all'], addHelp: false });
list.setDefaults({ action: 'list' });
var list = subParser.addParser('list', { aliases: ['ls', 'all'], addHelp: false })
list.setDefaults({ action: 'list' })

/*
* $ sprout init
*/

var init = subParser.addParser('init', { aliases: ['new', 'create'], addHelp: true });
init.addArgument(['name'], { help: 'name of template' });
init.addArgument(['target'], { help: 'destination path' });
init.addArgument(['-l', '--locals'], { nargs: '*', help: 'locals' });
init.addArgument(['-t', '--tag'], { help: 'git tag' });
init.addArgument(['-b', '--branch'], { help: 'git branch' });
init.addArgument(['-c', '--config'], { help: 'a config file with pre-defined values (either json or yml/yaml)' });
init.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' });
init.setDefaults({ action: 'init' });
var init = subParser.addParser('init', { aliases: ['new', 'create'], addHelp: true })
init.addArgument(['name'], { help: 'name of template' })
init.addArgument(['target'], { help: 'destination path' })
init.addArgument(['-l', '--locals'], { nargs: '*', help: 'locals' })
init.addArgument(['-t', '--tag'], { help: 'git tag' })
init.addArgument(['-b', '--branch'], { help: 'git branch' })
init.addArgument(['-c', '--config'], { help: 'a config file with pre-defined values (either json or yml/yaml)' })
init.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' })
init.setDefaults({ action: 'init' })

/*
* $ sprout run
*/

var run = subParser.addParser('run', { aliases: ['generate'], addHelp: true });
run.addArgument(['name'], { help: 'name of template' });
run.addArgument(['generator'], { help: 'name of generator to run' });
run.addArgument([], { nargs: '*', dest: 'args', help: 'arguments' });
run.addArgument(['-t', '--target'], { help: 'destination path' });
run.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' });
run.setDefaults({ action: 'run' });
var run = subParser.addParser('run', { aliases: ['generate'], addHelp: true })
run.addArgument(['name'], { help: 'name of template' })
run.addArgument(['generator'], { help: 'name of generator to run' })
run.addArgument([], { nargs: '*', dest: 'args', help: 'arguments' })
run.addArgument(['-t', '--target'], { help: 'destination path' })
run.addArgument(['-v', '--verbose'], { action: 'storeTrue', help: 'verbose mode' })
run.setDefaults({ action: 'run' })

/*
* Parse arguments.
Expand All @@ -87,18 +87,18 @@ var args = parser.parseArgs()
* add to an `options` object.
*/

var options = {};
var options = {}

options.verbose = args.verbose;
delete args.verbose;
options.verbose = args.verbose
delete args.verbose

/*
* Run CLI with arguments.
*/

var cli = new CLI(process.cwd(), options);
var cli = new CLI(process.cwd(), options)
cli.run(args).catch(
function () {
process.exit(1);
process.exit(1)
}
);
)
26 changes: 12 additions & 14 deletions lib/commands/add.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
var helpers = require('./../helpers')
var path = require('path')

module.exports = (function () {
/*
* Add a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/
return function (cli, args) {
var name = args.name
var src = helpers.isGitUrl(args.src) ? args.src : path.resolve(cli.cwd, args.src)
return cli.sprout.add(name, src).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` from ' + src + ' added!')
})
}
}.call(this))
/*
* Add a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/
module.exports = function (cli, args) {
var name = args.name
var src = helpers.isGitUrl(args.src) ? args.src : path.resolve(cli.cwd, args.src)
return cli.sprout.add(name, src).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` from ' + src + ' added!')
})
}
39 changes: 18 additions & 21 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ var helpers = require('./../helpers')
var path = require('path')
var _ = require('lodash')

module.exports = (function () {
/*
* Initialize a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/

return function (cli, args) {
var name = args.name
var target = path.resolve(cli.cwd, args.target)
if (_.isArray(args.locals)) {
args.locals = helpers.parseKeyValuesArray(args.locals)
}
if (args.configPath) {
args.configPath = path.resolve(cli.cwd, args.configPath)
}
args.questionnaire = helpers.questionnaire
return cli.sprout.init(name, target, _.omit(args, 'name', 'target')).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` initialized at ' + target + '!')
})
/*
* Initialize a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/
module.exports = function (cli, args) {
var name = args.name
var target = path.resolve(cli.cwd, args.target)
if (_.isArray(args.locals)) {
args.locals = helpers.parseKeyValuesArray(args.locals)
}
if (args.configPath) {
args.configPath = path.resolve(cli.cwd, args.configPath)
}
}.call(this))
args.questionnaire = helpers.questionnaire
return cli.sprout.init(name, target, helpers.stripExtras(_.omit(args, 'name', 'target'))).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` initialized at ' + target + '!')
})
}
25 changes: 11 additions & 14 deletions lib/commands/list.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
var W = require('when')
var _ = require('lodash')

module.exports = (function () {
/*
* List all templates.
* @param {Function} cli - CLI instance.
*/

return function (cli) {
if (_.isEmpty(cli.sprout.templates)) {
cli.emitter.emit('error', new Error('no templates exist!'))
} else {
cli.emitter.emit('list', _.keys(cli.sprout.templates))
}
return W.resolve()
/*
* List all templates.
* @param {Function} cli - CLI instance.
*/
module.exports = function (cli) {
if (_.isEmpty(cli.sprout.templates)) {
cli.emitter.emit('error', new Error('no templates exist!'))
} else {
cli.emitter.emit('list', _.keys(cli.sprout.templates))
}
}.call(this))
return W.resolve()
}
25 changes: 11 additions & 14 deletions lib/commands/remove.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
module.exports = (function () {
/*
* Remove a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/

return function (cli, args) {
var name = args.name
return cli.sprout.remove(name).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` removed!')
})
}
}.call(this))
/*
* Remove a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/
module.exports = function (cli, args) {
var name = args.name
return cli.sprout.remove(name).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` removed!')
})
}
29 changes: 13 additions & 16 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
var path = require('path')

module.exports = (function () {
/*
* Run a generator on a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/

return function (cli, args) {
var name = args.name
var generator = args.generator
var target = args.target ? path.resolve(cli.cwd, args.target) : cli.cwd
return cli.sprout.run(name, target, generator, args.args).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` ran generator `' + generator + '` at ' + target + '!')
})
}
}.call(this))
/*
* Run a generator on a template.
* @param {Function} cli - CLI instance.
* @param {Object} args - CLI arguments.
*/
module.exports = function (cli, args) {
var name = args.name
var generator = args.generator
var target = args.target ? path.resolve(cli.cwd, args.target) : cli.cwd
return cli.sprout.run(name, target, generator, args.args).then(function () {
return cli.emitter.emit('success', 'template `' + name + '` ran generator `' + generator + '` at ' + target + '!')
})
}
14 changes: 13 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exports.questionnaire = function (questions, skip) {
var question
for (var i = 0; i < questions.length; i++) {
question = questions[i]
if (!_.contains(skip, question.name)) {
if (!_.includes(skip, question.name)) {
qs.push(question)
}
}
Expand All @@ -89,6 +89,18 @@ exports.questionnaire = function (questions, skip) {
})
}

/**
* Strips any null values or setters that argparse adds to the args object
* @param {Object} obj args from argparse
* @return {Object} sanitized args object
*/
exports.stripExtras = function (obj) {
return _.reduce(obj, function (m, v, k) {
if (v && ['isset', 'set', 'get', 'unset'].indexOf(k) < 0) { m[k] = v }
return m
}, {})
}

/* A private helper function for determining a
* path in the current user folder for Sprout.
* @returns {String} - a user path for Sprout.
Expand Down

0 comments on commit 84b9b97

Please sign in to comment.