Skip to content

Commit

Permalink
Merge pull request #183 from UndeadBaneGitHub/angular-route-updated
Browse files Browse the repository at this point in the history
Angular route subgenerator and test added
  • Loading branch information
codydaig committed Jan 14, 2016
2 parents b651438 + d1d0f5c commit c01190b
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 3 deletions.
119 changes: 119 additions & 0 deletions angular-route/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';

var fs = require('fs'),
s = require('underscore.string'),
yeoman = require('yeoman-generator'),
engine = require('ejs').render,
htmlWiring = require("html-wiring");

var ViewGenerator = yeoman.generators.Base.extend({
askForModuleName: function () {
var modulesFolder = process.cwd() + '/modules/';
var done = this.async();

var prompts = [{
type: 'list',
name: 'moduleName',
default: 'core',
message: 'Which module does this route belongs to?',
choices: []
},{
type: 'input',
name: 'name',
default: '',
message: 'What is the name of the route (leave it blank to inherit it from module)?'
}];

// Add module choices
if (fs.existsSync(modulesFolder)) {

fs.readdirSync(modulesFolder).forEach(function (folder) {
var stat = fs.statSync(modulesFolder + '/' + folder);

if (stat.isDirectory()) {
prompts[0].choices.push({
value: folder,
name: folder
});
}
});
}

this.prompt(prompts, function (props) {
this.moduleName = props.moduleName;
this.name = props.name || this.moduleName;

this.controllerName = props.controllerName;

this.slugifiedModuleName = s(this.moduleName).slugify().value();
this.humanizedModuleName = s(this.moduleName).humanize().value();

this.slugifiedName = s(this.name).humanize().slugify().value();
this.classifiedName = s(this.slugifiedName).classify().value();
this.humanizedName = s(this.slugifiedName).humanize().value();

done();
}.bind(this));
},

askForRouteDetails: function () {
var done = this.async();

var prompts = [{
name: 'routePath',
message: 'What do you want your route path to be?',
default: this.slugifiedName
}, {
name: 'viewName',
message: 'What do you want to call your view?',
default: this.slugifiedName
}, {
name: 'controllerName',
message: 'What do you want to call your controller?',
default: this.classifiedName
}];

this.prompt(prompts, function (props) {
this.routePath = props.routePath;
this.viewName = props.viewName;
this.controllerName = props.controllerName;

this.slugifiedRoutePath = s(this.routePath).slugify().value();

this.slugifiedViewName = s(this.viewName).slugify().value();
this.humanizedViewName = s(this.viewName).humanize().value();

this.slugifiedControllerName = s(this.controllerName).humanize().slugify().value();
this.classifiedControllerName = s(this.slugifiedControllerName).classify().value();
this.humanizedControllerName = s(this.slugifiedControllerName).humanize().value();

done();
}.bind(this));
},

renderRoute: function () {
var routesFilePath = process.cwd() + '/modules/' + this.slugifiedModuleName + '/client/config/' + this.slugifiedModuleName + '.client.routes.js';

// If routes file exists we add a new state otherwise we render a new one
if (fs.existsSync(routesFilePath)) {
// Read the source routes file content
var routesFileContent = htmlWiring.readFileAsString(routesFilePath);

// Append the new state
routesFileContent = routesFileContent.replace(/\$stateProvider(\s+)?\n/, engine(this.read('_.client.route.js'), this));

// Save route file
htmlWiring.writeFileFromString(routesFileContent, routesFilePath);
} else {
this.template('_.client.routes.js', 'modules/' + this.slugifiedModuleName + '/client/config/' + this.slugifiedModuleName + '.client.routes.js')
}
},

renderRouteViewController: function () {
this.template('_.client.controller.js', 'modules/' + this.slugifiedModuleName + '/client/controllers/' + this.slugifiedControllerName + '.client.controller.js');
this.template('_.client.view.html', 'modules/' + this.slugifiedModuleName + '/client/views/' + this.slugifiedViewName + '.client.view.html');
this.template('_.client.controller.tests.js', 'modules/' + this.slugifiedModuleName + '/tests/client/' + this.slugifiedControllerName + '.client.controller.tests.js');
}
});

module.exports = ViewGenerator;
8 changes: 8 additions & 0 deletions angular-route/templates/_.client.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

angular.module('<%= slugifiedModuleName %>').controller('<%= classifiedControllerName %>Controller', ['$scope',
function ($scope) {
// Controller Logic
// ...
}
]);
58 changes: 58 additions & 0 deletions angular-route/templates/_.client.controller.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

(function() {
// <%= humanizedControllerName %> Controller Spec
describe('<%= humanizedControllerName %> Controller Tests', function() {
// Initialize global variables
var <%= classifiedControllerName %>Controller,
$scope,
$httpBackend,
$stateParams,
$location;

// The $resource service augments the response object with methods for updating and deleting the resource.
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
// the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher.
// When the toEqualData matcher compares two objects, it takes only object properties into
// account and ignores methods.
beforeEach(function () {
jasmine.addMatchers({
toEqualData: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
return {
pass: angular.equals(actual, expected)
};
}
};
}
});
});

// Then we can start by loading the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));

// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) {
// Set a new global scope
$scope = $rootScope.$new();

// Point global variables to injected services
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;

// Initialize the <%= humanizedControllerName %> controller.
<%= classifiedControllerName %>Controller = $controller('<%= classifiedControllerName %>Controller', {
$scope: $scope
});
}));

it('Should do some controller test', inject(function () {
// The test logic
// ...
}));
});
}());
5 changes: 5 additions & 0 deletions angular-route/templates/_.client.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$stateProvider
.state('<%= slugifiedName %>', {
url: '/<%= slugifiedRoutePath %>',
templateUrl: 'modules/<%= slugifiedModuleName %>/client/views/<%= slugifiedViewName %>.client.view.html'
})
13 changes: 13 additions & 0 deletions angular-route/templates/_.client.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

//Setting up route
angular.module('<%= slugifiedModuleName %>').config(['$stateProvider',
function($stateProvider) {
// <%= humanizedModuleName %> state routing
$stateProvider
.state('<%= slugifiedName %>', {
url: '/<%= slugifiedRoutePath %>',
templateUrl: 'modules/<%= slugifiedModuleName %>/client/views/<%= slugifiedViewName %>.client.view.html'
});
}
]);
3 changes: 3 additions & 0 deletions angular-route/templates/_.client.view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<section data-ng-controller="<%= classifiedControllerName %>Controller">
This is the <%= humanizedName %> view
</section>
6 changes: 3 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ var gulp = require('gulp'),
// ESLint JS linting task
gulp.task('eslint', function () {
return gulp
.src('./app/**/*', './test/**/*')
.src(['./app/**/*', './test/**/*', '!./test/temp'])
.pipe(eslint())
.pipe(eslint.format());
});

// JS linting task
gulp.task('jshint', function () {
return gulp
.src('./app/**/*', './test/**/*')
.src(['./app/**/*', './test/**/*', '!./test/temp'])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
Expand All @@ -25,7 +25,7 @@ gulp.task('lint', gulp.parallel('eslint', 'jshint'));
// Mocha test task
gulp.task('mocha', function () {
return gulp
.src('./test/**/*')
.src(['./test/**/*', '!./test/temp'])
.pipe(mocha({
reporter: 'spec'
}));
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"dependencies": {
"bluebird": "~3.0.6",
"chalk": "~1.1.1",
"ejs": "~2.3.4",
"html-wiring": "~1.2.0",
"mkdirp": "~0.5.1",
"underscore.inflections": "~0.2.1",
"underscore.string": "~3.2.2",
"yeoman-generator": "~0.21.1",
Expand Down
Loading

0 comments on commit c01190b

Please sign in to comment.