Skip to content

Commit

Permalink
Merge pull request #64 from carrot/generators
Browse files Browse the repository at this point in the history
Generators!
  • Loading branch information
nporteschaikin committed Mar 26, 2015
2 parents bdd9f08 + 42dc552 commit f49b31f
Show file tree
Hide file tree
Showing 50 changed files with 748 additions and 251 deletions.
12 changes: 12 additions & 0 deletions bin/sprout
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ init.addArgument(['-c', '--config'], { help: 'a config file with pre-defined val
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' });


/* A helper function for determining a Sprout path.
* @returns {String} - a path for Sprout.
Expand Down
2 changes: 1 addition & 1 deletion lib/api/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (function () {
* @param {Function} sprout - Sprout instance.
* @param {String} name - name to save template as.
* @param {String} src - path or URL to template source.
* @return {Promise} - Promise for Sprout instance.
* @return {Promise} - Promise for Template instance.
*/

return function (sprout, name, src) {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = (function () {
* @param {Function} sprout - Sprout instance.
* @param {String} name - name of template to initialize.
* @param {String} target - The path to save the template to.
* @return {Promise} - Promise for Sprout instance.
* @return {Promise} - Promise for Template instance.
*/

return function (sprout, name, target, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (function () {
* Remove a template.
* @param {Function} sprout - Sprout instance.
* @param {String} name - name of template to remove.
* @return {Promise} - Promise for Sprout instance.
* @return {Promise} - Promise for Template instance.
*/

return function (sprout, name) {
Expand Down
23 changes: 23 additions & 0 deletions lib/api/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var Template = require('./../template')
, Promise = require('bluebird');

module.exports = (function () {

/*
* Run a generator in an existing template
* given the target path and generator name.
* @param {Function} sprout - Sprout instance.
* @param {String} name - name of template to run generator from.
* @param {String} target - The path of the existing instance.
* @param {String} generator - The generator to use.
* @param {Array} args - An array of arguments to pass to the generator.
* @return {Promise} - Promise for Template instance.
*/

return function (sprout, name, target, generator, args) {
var template = sprout.templates[name];
if (template) return template.run(target, generator, args);
return Promise.reject(new Error('template ' + name + ' does not exist'));
}

}.call(this));
18 changes: 17 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Sprout = require('./')
, Emitter = require('events').EventEmitter
, helpers = require('./helpers')
, inquirer = require('inquirer')
, Promise = require('bluebird')
Expand Down Expand Up @@ -87,6 +86,23 @@ module.exports = (function () {
return self.emitter.emit('success', 'template `' + name + '` initialized at ' + target + '!');
}
);
},

/*
* Run a generator on a template.
* @param {Object} options - CLI arguments.
*/

run: function (options) {
var self = this
, name = options.name
, generator = options.generator
, target = options.target ? path.resolve(process.cwd(), options.target) : process.cwd();
return this.sprout.run(name, target, generator, options.args).then(
function () {
return self.emitter.emit('success', 'template `' + name + '` ran generator `' + generator + '` at ' + target + '!');
}
)
}

}
Expand Down
77 changes: 0 additions & 77 deletions lib/configFile.js

This file was deleted.

16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,28 @@ module.exports = (function () {
* given target path.
* @param {String} name - name of template to initialize.
* @param {String} target - The path to save the template to.
* @param {Object} options - Initialization options.
* @return {Promise} - Promise for Sprout instance.
*/

init: function (name, target, options) {
return require('./api/init')(this, name, target, options)
.return(this);
},

/*
* Run a generator in an existing template
* given the target path and generator name.
* @param {String} name - name of template to run generator from.
* @param {String} target - The path of the existing instance.
* @param {String} generator - The generator to use.
* @param {Array} args - An array of arguments to pass to the generator.
* @return {Promise} - Promise for Sprout instance.
*/

run: function (name, target, generator, args) {
return require('./api/run')(this, name, target, generator, args)
.return(this);
}

}
Expand Down
120 changes: 102 additions & 18 deletions lib/template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Utils = require('./utils')
, ConfigFile = require('./configFile')
, helpers = require('./helpers')
, Promise = require('bluebird')
, which = require('which')
Expand Down Expand Up @@ -38,6 +37,7 @@ module.exports = (function () {
this.name = name;
this.path = path.join(sprout.path, name);
this.root = path.join(this.path, 'root');
this.generators = path.join(this.path, 'generators');

/*
* If `src` is set, use `isGitURL` helper to
Expand Down Expand Up @@ -335,9 +335,9 @@ module.exports = (function () {
function (resolve, reject) {

/*
* Check for init.coffee. If init.coffee doesn't
* exist, confirm that init.js exists. Require
* init once this is determined.
* Check for init.js. If init.js doesn't
* exist, confirm that init.coffee exists.
* Require init once this is determined.
*/

var initCoffee = path.join(self.path, 'init.coffee')
Expand Down Expand Up @@ -450,20 +450,6 @@ module.exports = (function () {
self.emitter.emit('msg', 'copying files in root to target');
return ncp(self.root, target);

}
).then(
function () {

var configFile = new ConfigFile(target, {name: self.name});

/*
* Write configuration file with
* the template's name.
*/

self.emitter.emit('msg', 'creating sprout config file at target');
return configFile.write();

}
).then(
function () {
Expand Down Expand Up @@ -629,6 +615,104 @@ module.exports = (function () {
).return(this);
},

/*
* Run a template generator in the specified target.
* @param {String} target - the target path.
* @param {String} name - the name of the generator to use.
* @param
* @return {Promise} - Promise for Template instance.
*/

run: function (target, generator, args) {
var self = this;
return new Promise(
function (resolve, reject) {

/*
* If target not passed,
* throw an error.
*/

if (!target) {
return reject(new Error('target path required'));
}

/*
* If target directory doesn't exist,
* throw an error.
*/

if (!fs.existsSync(target)) {
return reject(new Error(target + ' does not exist'));
}

/*
* If generator name isn't
* passed, throw an error.
*/

if (!generator) {
return reject(new Error('generator name required'));
}

/*
* Check for {generator}.js. If {generator}.js
* doesn't exist, confirm that {name}.coffee exists.
* Require {generator}.js or {generator}.coffee once
* this is determined.
*/

var generatorCoffee = path.join(self.generators, generator + '.coffee')
, generatorJs = path.join(self.generators, generator + '.js')
, generatorPath;

if (fs.existsSync(generatorJs)) {
generatorPath = generatorJs;
} else if (fs.existsSync(generatorCoffee)) {
generatorPath = generatorCoffee;
} else {
return reject(new Error('`' + generator + '` is not a generator in this template'));
}

try {
self.emitter.emit('msg', 'requiring `' + generator + '` generator');
return resolve(require(generatorPath));
} catch (error) {
return reject(error);
}

}
).then(
function (generator) {

/*
* Create a Utils instance where
* the `src` and `target` are both
* the target directory.
*/

var utils = new Utils(self.path, target);

/*
* Add `utils` as the first object
* in our `args` array.
*/

(args = _.isArray(args) ? args : []).unshift(utils);

/*
* Call the generator; pass
* the Utils instance and the
* arguments.
*/

self.emitter.emit('msg', 'running `' + generator + '` generator');
return generator.apply(null, args);

}
).return(this);
},

/*
* Delete the template.
* @return {Promise} - Promise for Template instance.
Expand Down
Loading

0 comments on commit f49b31f

Please sign in to comment.