Skip to content

Commit

Permalink
Determine appname from bower/package.json if set
Browse files Browse the repository at this point in the history
  • Loading branch information
p-m-p committed Jan 12, 2014
1 parent 3642640 commit ea1db97
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
27 changes: 25 additions & 2 deletions lib/base.js
Expand Up @@ -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`
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion test/base.js
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit ea1db97

Please sign in to comment.