Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the set app name from bower.json #462

Merged
merged 1 commit into from Jan 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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