base-generators
Advanced tools
Comparing version 0.1.12 to 0.1.14
124
index.js
@@ -14,2 +14,3 @@ /*! | ||
var debug = require('debug')('base:generators'); | ||
var search = require('./lib/search'); | ||
var tasks = require('./lib/tasks'); | ||
@@ -33,7 +34,12 @@ var cache = require('./lib/cache'); | ||
var resolveCache = {}; | ||
var globalCache = {}; | ||
var findCache = {}; | ||
var getCache = {}; | ||
/** | ||
* Search caches | ||
*/ | ||
var memory = {}; | ||
memory.getGenerator = search(); | ||
memory.resolve = search(); | ||
memory.global = search(); | ||
memory.find = search(); | ||
/** | ||
@@ -80,5 +86,4 @@ * Add plugins necessary for running generators. | ||
var opts = util.extend({ configfile: this.configfile }, options); | ||
if (resolveCache[name]) { | ||
return resolveCache[name]; | ||
if (memory.resolve.has(name)) { | ||
return memory.resolve.get(name); | ||
} | ||
@@ -94,3 +99,3 @@ | ||
if (filepath) { | ||
resolveCache[name] = filepath; | ||
memory.resolve.set(name, filepath); | ||
return filepath; | ||
@@ -218,14 +223,13 @@ } | ||
this.define('getGenerator', function(name, fn) { | ||
this.define('getGenerator', function(name, aliasFn) { | ||
debug('getting generator "%s"', name); | ||
if (typeof fn !== 'function') { | ||
fn = this.toAlias.bind(this); | ||
if (typeof aliasFn !== 'function') { | ||
aliasFn = this.toAlias.bind(this); | ||
} | ||
if (getCache[name]) { | ||
return getCache[name]; | ||
if (memory.getGenerator.has(name)) { | ||
return memory.getGenerator.get(name); | ||
} | ||
var findGlobal = true; | ||
var props = name.split('.'); | ||
@@ -238,11 +242,10 @@ var len = props.length; | ||
var key = props[idx]; | ||
app = app.findGenerator(key, fn, findGlobal); | ||
app = app.findGenerator(key, aliasFn); | ||
if (!app) { | ||
break; | ||
} | ||
findGlobal = false; | ||
} | ||
if (app) { | ||
getCache[name] = app; | ||
memory.getGenerator.set(name, app); | ||
return app; | ||
@@ -253,27 +256,2 @@ } | ||
/** | ||
* Search for globally installed generator `name`. If found, then generator | ||
* is registered and returned, otherwise `undefined` is returned. | ||
* | ||
* @name .globalGenerator | ||
* @param {String} `name` | ||
* @return {Object|undefined} | ||
* @api public | ||
*/ | ||
this.define('globalGenerator', function(name) { | ||
debug('getting global generator "%s"', name); | ||
if (globalCache[name]) return globalCache[name]; | ||
var filepath = this.resolve(name); | ||
if (filepath) { | ||
this.register(name, filepath); | ||
var generator = this.generators.get(name); | ||
if (generator) { | ||
globalCache[name] = generator; | ||
return generator; | ||
} | ||
} | ||
}); | ||
/** | ||
* Find generator `name`, by first searching the cache, | ||
@@ -285,3 +263,3 @@ * then searching the cache of the `base` generator, | ||
* @param {String} `name` | ||
* @param {Function} `fn` Optionally supply a rename function. | ||
* @param {Function} `aliasFn` Optionally supply a rename function. | ||
* @return {Object|undefined} Returns the generator instance if found, or undefined. | ||
@@ -291,11 +269,7 @@ * @api public | ||
this.define('findGenerator', function(name, aliasFn, findGlobal) { | ||
if (typeof aliasFn === 'boolean') { | ||
findGlobal = aliasFn; | ||
aliasFn = null; | ||
} | ||
this.define('findGenerator', function(name, aliasFn) { | ||
debug('finding generator "%s"', name); | ||
if (findCache[name]) { | ||
return findCache[name]; | ||
var gen = memory.find.get(name); | ||
if (util.isObject(gen) || typeof gen === 'function') { | ||
return gen; | ||
} | ||
@@ -309,2 +283,3 @@ | ||
if (sub) { | ||
memory.find.set(name, sub); | ||
return sub; | ||
@@ -319,11 +294,16 @@ } | ||
// if global lookup is enabled, search global `node_modules` | ||
if (!generator && findGlobal === true) { | ||
generator = this.globalGenerator(name); | ||
// try searching in local and global node_modules | ||
if (!generator) { | ||
var fp = this.resolve(name); | ||
if (fp) generator = this.register(name, fp); | ||
} | ||
if (!generator && name.indexOf(this.prefix) === -1) { | ||
generator = this.findGenerator(this.toFullname(name), aliasFn); | ||
} | ||
// if resolved, cache it | ||
if (generator) { | ||
if (!this.firstGen) this.firstGen = generator; | ||
findCache[name] = generator; | ||
memory.find.set(name, generator); | ||
return generator; | ||
@@ -334,2 +314,32 @@ } | ||
/** | ||
* Search for globally installed generator `name`. If found, then generator | ||
* is registered and returned, otherwise `undefined` is returned. | ||
* | ||
* @name .globalGenerator | ||
* @param {String} `name` | ||
* @return {Object|undefined} | ||
* @api public | ||
*/ | ||
this.define('globalGenerator', function(name) { | ||
debug('getting global generator "%s"', name); | ||
if (memory.global.has(name)) { | ||
return memory.global.get(name); | ||
} | ||
var filepath = this.resolve(name, {cwd: util.gm}); | ||
if (memory.global.has(filepath)) { | ||
return memory.global.get(filepath); | ||
} | ||
if (utils.isGenerator(filepath, this.prefix)) { | ||
var generator = this.getGenerator(name); | ||
if (generator) { | ||
memory.global.set(name, generator); | ||
return generator; | ||
} | ||
} | ||
}); | ||
/** | ||
* Invoke the given generator in the context of (`this`) the current | ||
@@ -521,3 +531,3 @@ * instance. | ||
app.define('toAlias', function toAlias(name, options) { | ||
this.define('toAlias', function toAlias(name, options) { | ||
var opts = util.extend({}, config, this.options, options); | ||
@@ -542,3 +552,3 @@ if (!opts.prefix && !opts.modulename) { | ||
app.define('toFullname', function toFullname(alias, options) { | ||
this.define('toFullname', function toFullname(alias, options) { | ||
var opts = util.extend({prefix: this.prefix}, config, this.options, options); | ||
@@ -545,0 +555,0 @@ var fullname = util.toFullname(alias, opts); |
@@ -148,3 +148,3 @@ 'use strict'; | ||
var names = [name, alias, fullname, basename, aliasPath, namePath]; | ||
var names = [fullname, name, alias, basename, aliasPath, namePath]; | ||
names = utils.unique(names.filter(Boolean)); | ||
@@ -151,0 +151,0 @@ |
@@ -43,34 +43,11 @@ 'use strict'; | ||
env.configfile = this.configfile; | ||
env.alias = utils.toAlias(name, opts); | ||
env.name = utils.toFullname(env.alias, opts); | ||
this.name = env.alias; | ||
debug('createEnv · name: "%s", alias: "%s"', env.name, env.alias); | ||
if (typeof fn === 'string') { | ||
env.path = utils.tryResolve(fn, {configfile: this.configfile}); | ||
if (typeof env.path === 'undefined') { | ||
throw new Error('cannot find generator: ' + fn); | ||
} | ||
envPath(this, name, env, opts, fn); | ||
define(env, 'cwd', function() { | ||
return path.dirname(env.path); | ||
}); | ||
} else if (typeof fn === 'function') { | ||
envFn(this, name, env, opts, fn); | ||
define(env, 'templates', function() { | ||
return path.resolve(env.cwd, 'templates'); | ||
}); | ||
} else if (utils.isObject(fn) && fn.isGenerator) { | ||
envFn(this, name, env, opts, fn); | ||
define(env, 'fn', function() { | ||
if (paths[env.path]) return paths[env.path]; | ||
var fn = utils.tryRequire(env.path) || utils.tryRequire(env.cwd); | ||
if (fn) { | ||
paths[env.path] = fn; | ||
return fn; | ||
} | ||
}); | ||
} else if (typeof fn === 'function') { | ||
env.path = undefined; | ||
env.fn = fn; | ||
} | ||
@@ -83,2 +60,52 @@ | ||
}; | ||
function envPath(app, name, env, opts, fn) { | ||
env.path = utils.tryResolve(fn, {configfile: app.configfile}); | ||
if (typeof env.path === 'undefined') { | ||
throw new Error('cannot find generator: ' + fn); | ||
} | ||
env.alias = utils.toAlias(name, opts); | ||
env.name = utils.toFullname(env.alias, opts); | ||
createPaths(app, name, env, opts); | ||
define(env, 'cwd', function() { | ||
return path.dirname(env.path); | ||
}); | ||
define(env, 'templates', function() { | ||
return path.resolve(env.cwd, 'templates'); | ||
}); | ||
define(env, 'fn', function() { | ||
if (paths[env.path]) return paths[env.path]; | ||
if (utils.isObject(fn)) { | ||
paths[env.path] = fn; | ||
return fn; | ||
} | ||
var fn = utils.tryRequire(env.path) || utils.tryRequire(env.cwd); | ||
if (fn) { | ||
paths[env.path] = fn; | ||
return fn; | ||
} | ||
}); | ||
} | ||
function envFn(app, name, env, opts, fn) { | ||
env.alias = name; | ||
env.name = name; | ||
env.path = undefined; | ||
env.fn = fn; | ||
createPaths(app, name, env, opts); | ||
} | ||
function createPaths(app, name, env, opts) { | ||
if (typeof opts.alias === 'function') { | ||
env.alias = opts.alias(name, opts); | ||
} | ||
app.name = env.alias; | ||
debug('createEnv · name: "%s", alias: "%s"', env.name, env.alias); | ||
} | ||
}; | ||
@@ -85,0 +112,0 @@ |
@@ -51,2 +51,14 @@ 'use strict'; | ||
utils.isGenerator = function(filepath, prefix) { | ||
if (typeof filepath !== 'string') { | ||
return false; | ||
} | ||
var basename = path.basename(path.dirname(filepath)); | ||
return basename.indexOf(prefix) !== -1; | ||
}; | ||
/** | ||
* Return the given value as-is | ||
*/ | ||
utils.identity = function(val) { | ||
@@ -53,0 +65,0 @@ return val; |
{ | ||
"name": "base-generators", | ||
"description": "Adds project-generator support to your `base` application.", | ||
"version": "0.1.12", | ||
"version": "0.1.14", | ||
"homepage": "https://github.com/jonschlinkert/base-generators", | ||
@@ -32,3 +32,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"extend-shallow": "^2.0.1", | ||
"generator-util": "^0.2.6", | ||
"generator-util": "^0.2.7", | ||
"get-value": "^2.0.3", | ||
@@ -35,0 +35,0 @@ "lazy-cache": "^1.0.3" |
@@ -248,3 +248,3 @@ # base-generators [](https://www.npmjs.com/package/base-generators) [](https://travis-ci.org/jonschlinkert/base-generators) | ||
### [.resolve](index.js#L76) | ||
### [.resolve](index.js#L82) | ||
@@ -281,3 +281,3 @@ Attempts to find a generator with the given `name` and `options`. | ||
### [.register](index.js#L134) | ||
### [.register](index.js#L139) | ||
@@ -301,3 +301,3 @@ Register generator `name` with the given `fn`. | ||
### [.generator](index.js#L158) | ||
### [.generator](index.js#L163) | ||
@@ -321,3 +321,3 @@ Register generator `name` with the given `fn`, or get generator `name` if only one argument is passed. This method calls the `.getGenerator` method but goes one step further: if `name` is not already registered, it will try to resolve and register the generator before returning it (or `undefined` if unsuccessful). | ||
### [.hasGenerator](index.js#L190) | ||
### [.hasGenerator](index.js#L195) | ||
@@ -346,3 +346,3 @@ Return true if the given `name` exists on the `generators` object. Dot-notation may be used to check for sub-generators. | ||
### [.getGenerator](index.js#L214) | ||
### [.getGenerator](index.js#L219) | ||
@@ -365,6 +365,7 @@ Get generator `name` from `app.generators`. Dot-notation may be used to get a sub-generator. | ||
### [.globalGenerator](index.js#L256) | ||
### [.findGenerator](index.js#L261) | ||
Search for globally installed generator `name`. If found, then generator | ||
is registered and returned, otherwise `undefined` is returned. | ||
Find generator `name`, by first searching the cache, | ||
then searching the cache of the `base` generator, | ||
and last searching for a globally installed generator. | ||
@@ -374,9 +375,9 @@ **Params** | ||
* `name` **{String}** | ||
* `returns` **{Object|undefined}** | ||
* `aliasFn` **{Function}**: Optionally supply a rename function. | ||
* `returns` **{Object|undefined}**: Returns the generator instance if found, or undefined. | ||
### [.findGenerator](index.js#L283) | ||
### [.globalGenerator](index.js#L312) | ||
Find generator `name`, by first searching the cache, | ||
then searching the cache of the `base` generator, | ||
and last searching for a globally installed generator. | ||
Search for globally installed generator `name`. If found, then generator | ||
is registered and returned, otherwise `undefined` is returned. | ||
@@ -386,6 +387,5 @@ **Params** | ||
* `name` **{String}** | ||
* `fn` **{Function}**: Optionally supply a rename function. | ||
* `returns` **{Object|undefined}**: Returns the generator instance if found, or undefined. | ||
* `returns` **{Object|undefined}** | ||
### [.invoke](index.js#L335) | ||
### [.invoke](index.js#L345) | ||
@@ -405,3 +405,3 @@ Invoke the given generator in the context of (`this`) the current instance. | ||
### [.extendWith](index.js#L372) | ||
### [.extendWith](index.js#L382) | ||
@@ -426,3 +426,3 @@ Alias for `.invoke`, Extend the current generator instance with the settings of other generators. | ||
### [.generate](index.js#L410) | ||
### [.generate](index.js#L420) | ||
@@ -465,3 +465,3 @@ Run a `generator` and `tasks`, calling the given `callback` function upon completion. | ||
### [.generateEach](index.js#L485) | ||
### [.generateEach](index.js#L495) | ||
@@ -485,3 +485,3 @@ Iterate over an array of generators and tasks, calling [generate](#generate) on each. | ||
### [.alias](index.js#L509) | ||
### [.alias](index.js#L519) | ||
@@ -496,3 +496,3 @@ Create a generator alias from the given `name`. | ||
### [.fullname](index.js#L529) | ||
### [.fullname](index.js#L539) | ||
@@ -507,3 +507,3 @@ Create a generator's full name from the given `alias`. | ||
### [.configname](index.js#L544) | ||
### [.configname](index.js#L554) | ||
@@ -513,3 +513,3 @@ Getter/setter for defining the `configname` name to use for lookups. | ||
### [.configfile](index.js#L566) | ||
### [.configfile](index.js#L576) | ||
@@ -519,3 +519,3 @@ Getter/setter for defining the `configfile` name to use for lookups. | ||
### [.configpath](index.js#L584) | ||
### [.configpath](index.js#L594) | ||
@@ -525,3 +525,3 @@ Getter/setter for defining the `configpath` name to use for lookups. | ||
### [.modulename](index.js#L602) | ||
### [.modulename](index.js#L612) | ||
@@ -531,3 +531,3 @@ Getter/setter for defining the `modulename` name to use for lookups. | ||
### [.base](index.js#L629) | ||
### [.base](index.js#L639) | ||
@@ -558,3 +558,3 @@ Getter/setter for the `base` (or shared) instance of `generate`. | ||
* [bach](https://www.npmjs.com/package/bach): Compose your async functions with elegance | [homepage](https://github.com/gulpjs/bach#readme) | ||
* [bach](https://www.npmjs.com/package/bach): Compose your async functions with elegance | [homepage](https://github.com/gulpjs/bach) | ||
* [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base) | [homepage](https://github.com/node-base/base) | ||
@@ -608,2 +608,2 @@ * [base-fs](https://www.npmjs.com/package/base-fs): base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file… [more](https://www.npmjs.com/package/base-fs) | [homepage](https://github.com/jonschlinkert/base-fs) | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on February 12, 2016._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on February 15, 2016._ |
52661
22637
9
1068
Updatedgenerator-util@^0.2.7