Skip to content

Commit

Permalink
fix(gen): fix more appPath, other changes
Browse files Browse the repository at this point in the history
Views and decorators weren't using appPath correctly. The code is ugly
and not DRY at all but will be cleaned up later. gitIgnore also needed
some appPath love.

Moved the gitignore creation to the package section of app/index.js

Also did more work with tests, cleaning them up and testing more things.
They are going to have to have a full rewrite so it makes me sad doing
these small changes.
  • Loading branch information
eddiemonge committed May 16, 2014
1 parent 53035aa commit 3631740
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 467 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
node_modules
test/temp
test/tmp
2 changes: 2 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var Generator = module.exports = function Generator(args, options) {
} catch (e) {}
}
this.env.options.appPath = this.env.options.appPath || 'app';
this.options.appPath = this.env.options.appPath;
}

this.appPath = this.env.options.appPath;
Expand Down Expand Up @@ -268,6 +269,7 @@ Generator.prototype.packageFiles = function packageFiles() {
this.coffee = this.env.options.coffee;
this.template('root/_bower.json', 'bower.json');
this.template('root/_bowerrc', '.bowerrc');
this.template('root/gitignore', '.gitignore');
this.template('root/_package.json', 'package.json');
this.template('root/_Gruntfile.js', 'Gruntfile.js');
};
Expand Down
1 change: 0 additions & 1 deletion common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Generator.prototype.setupEnv = function setupEnv() {
this.copy('.editorconfig');
this.copy('.gitattributes');
this.copy('.jshintrc');
this.copy('gitignore', '.gitignore');
this.directory('test');

this.sourceRoot(join(__dirname, '../templates/common'));
Expand Down
26 changes: 22 additions & 4 deletions decorator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@
var util = require('util');
var ScriptBase = require('../script-base.js');
var fs = require('fs');
var path = require('path');

function buildRelativePath(fileName){
return 'decorators/' + fileName + "Decorator";
return path.join('decorators', fileName + "Decorator");
}

var Generator = module.exports = function Generator(args, options) {
ScriptBase.apply(this, arguments);
this.fileName = this.name;

if (typeof this.env.options.appPath === 'undefined') {
this.env.options.appPath = this.options.appPath;

if (!this.env.options.appPath) {
try {
this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath;
} catch (e) {}
}
this.env.options.appPath = this.env.options.appPath || 'app';
this.options.appPath = this.env.options.appPath;
}
};

util.inherits(Generator, ScriptBase);
Expand All @@ -18,8 +31,10 @@ Generator.prototype.askForOverwrite = function askForOverwrite() {
var cb = this.async();

// TODO: Any yeoman.util function to handle this?
var fileExists = fs.existsSync(this.env.cwd + '/app/scripts/' + buildRelativePath(this.fileName) + ".js");
if (fileExists) {
if (fs.existsSync(path.join(
this.env.cwd, this.env.options.appPath,
'scripts', buildRelativePath(this.fileName) + ".js"
))) {
var prompts = [{
type: 'confirm',
name: 'overwriteDecorator',
Expand Down Expand Up @@ -62,6 +77,9 @@ Generator.prototype.askForNewName = function askForNewName() {
};

Generator.prototype.createDecoratorFiles = function createDecoratorFiles() {
this.appTemplate('decorator', 'scripts/' + buildRelativePath(this.fileName));
this.appTemplate(
'decorator',
path.join('scripts', buildRelativePath(this.fileName))
);
this.addScriptToIndex(buildRelativePath(this.fileName));
};
1 change: 1 addition & 0 deletions script-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var Generator = module.exports = function Generator() {
} catch (e) {}
}
this.env.options.appPath = this.env.options.appPath || 'app';
this.options.appPath = this.env.options.appPath;
}

if (typeof this.env.options.testPath === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion templates/common/root/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
dist
.tmp
.sass-cache
app/bower_components
<%= appPath %>/bower_components
70 changes: 32 additions & 38 deletions test/test-appname-substitution.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/*global describe, before, it, beforeEach */
'use strict';

var fs = require('fs');
var assert = require('assert');
var path = require('path');
var util = require('util');
var generators = require('yeoman-generator');
var helpers = require('yeoman-generator').test;

describe('Angular generator template mechanism', function () {
Expand All @@ -22,56 +18,54 @@ describe('Angular generator template mechanism', function () {
'karma:app'
]
];
helpers.testDirectory(path.join(__dirname, 'temp', appName), function (err) {
helpers.testDirectory(path.join(__dirname, 'tmp', appName), function (err) {
if (err) {
done(err);
}

angular = helpers.createGenerator('angular:app', deps, [appName], {
'skip-welcome-message': true,
'skip-install': true,
'skip-message': true
});

helpers.mockPrompt(angular, {
compass: true,
bootstrap: true,
compassBootstrap: true,
modules: []
});

done();
});
});

it('should generate the same appName in every file', function (done) {
var expected = [
'app/scripts/app.js',
'app/scripts/controllers/main.js',
'app/index.html',
'test/spec/controllers/main.js'
];

helpers.mockPrompt(angular, {
compass: true,
bootstrap: true,
compassBootstrap: true,
modules: []
});

angular.run({}, function () {
// Check if all files are created for the test
helpers.assertFile(expected);

// read JS Files
var app_js = fs.readFileSync('app/scripts/app.js', 'utf8');
var main_js = fs.readFileSync('app/scripts/controllers/main.js', 'utf8');
var main_test_js = fs.readFileSync('test/spec/controllers/main.js', 'utf8');

// Test JS Files
var regex_js = new RegExp('module\\(\'' + appName + 'App\'');

assert.ok(regex_js.test(app_js), 'app.js template using a wrong appName');
assert.ok(regex_js.test(main_js), 'main.js template using a wrong appName');
assert.ok(regex_js.test(main_test_js), 'controller spec template using a wrong appName');
helpers.assertFile([
'app/scripts/app.js',
'app/scripts/controllers/main.js',
'app/index.html',
'test/spec/controllers/main.js'
]);

// read HTML file
var index_html = fs.readFileSync('app/index.html', 'utf8');
helpers.assertFileContent(
'app/scripts/app.js',
new RegExp('module\\(\'' + appName + 'App\'')
);
helpers.assertFileContent(
'app/scripts/controllers/main.js',
new RegExp('module\\(\'' + appName + 'App\'')
);
helpers.assertFileContent(
'test/spec/controllers/main.js',
new RegExp('module\\(\'' + appName + 'App\'')
);

// Test HTML File
var regex_html = new RegExp('ng-app=\"' + appName + 'App\"');
assert.ok(regex_html.test(index_html), 'index.html template using a wrong appName');
helpers.assertFileContent(
'app/index.html',
new RegExp('ng-app=\"' + appName + 'App\"')
);
done();
});
});
Expand Down
Loading

0 comments on commit 3631740

Please sign in to comment.