From ea1db974b44f7e0c8584fbd7674588b69d63b2ec Mon Sep 17 00:00:00 2001 From: Phil Parsons Date: Sat, 11 Jan 2014 10:01:51 +0000 Subject: [PATCH] Determine appname from bower/package.json if set --- lib/base.js | 27 +++++++++++++++++++++++++-- test/base.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/base.js b/lib/base.js index 69d0ea0f..1834ddbd 100644 --- a/lib/base.js +++ b/lib/base.js @@ -45,7 +45,7 @@ var fileLogger = { write: noop, warn: noop }; * @property {String} resolved - the path to the current generator * @property {String} generatorName * @property {String} description - Used in `--help` output - * @property {String} appname - Application name determine from the CWD folder + * @property {String} appname - The application name * @property {Storage} config - `.yo-rc` config file manager * @property {Object} src - File util instance scoped to `sourceRoot` * @property {Object} dest - File util instance scoped to `destinationRoot` @@ -121,7 +121,7 @@ var Base = module.exports = function Base(args, options) { this._options = []; this._hooks = []; this._conflicts = []; - this.appname = path.basename(process.cwd()).replace(/[^\w\s]+?/g, ' '); + this.appname = this.determineAppname(); this.option('help', { alias: 'h', @@ -728,6 +728,29 @@ Base.prototype.getCollisionFilter = function () { }; }; +/** + * Determines the name of the application. + * + * First checks for name in bower.json. + * Then checks for name in package.json. + * Finally defaults to the name of the current directory. + */ +Base.prototype.determineAppname = function () { + var appname; + + try { + appname = require(path.join(process.cwd(), 'bower.json')).name; + } catch (e) { + try { + appname = require(path.join(process.cwd(), 'package.json')).name; + } catch (e) { + appname = path.basename(process.cwd()); + } + } + + return appname.replace(/[^\w\s]+?/g, ' '); +}; + /** * Extend this Class to create a new one inherithing this one. * Also add a helper __super__ object poiting to the parent prototypes methods diff --git a/test/base.js b/test/base.js index 8248fcb4..3e0c8ac0 100644 --- a/test/base.js +++ b/test/base.js @@ -37,7 +37,7 @@ describe('yeoman.generators.Base', function () { // mandatory options, created by the env#create() helper resolved: 'ember:all', namespace: 'dummy', - env: env, + env: env }); this.dummy @@ -54,6 +54,37 @@ describe('yeoman.generators.Base', function () { }); }); + describe('#determineAppname', function () { + before(function () { + process.chdir(path.join(__dirname, 'temp.dev')); + }); + + afterEach(function () { + if (fs.existsSync('bower.json')) { + fs.unlinkSync('bower.json'); + delete require.cache[path.join(process.cwd(), 'bower.json')]; + } + if (fs.existsSync('package.json')) { + fs.unlinkSync('package.json'); + delete require.cache[path.join(process.cwd(), 'package.json')]; + } + }); + + it('returns appname from bower.json', function () { + fs.writeFileSync('bower.json', '{ "name": "app-name" }'); + assert.equal(this.dummy.determineAppname(), 'app name'); + }); + + it('returns appname from package.json', function () { + fs.writeFileSync('package.json', '{ "name": "package_app-name" }'); + assert.equal(this.dummy.determineAppname(), 'package_app name'); + }); + + it('returns appname from the current directory', function () { + assert.equal(this.dummy.determineAppname(), 'temp dev'); + }); + }); + describe('.extend()', function () { it('create a new object inheriting the Generator', function () { assert.ok(new (Base.extend())([], { resolved: 'path/', env: this.env }) instanceof Base);