-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from UndeadBaneGitHub/angular-route-updated
Angular route subgenerator and test added
- Loading branch information
Showing
9 changed files
with
554 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// ... | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// ... | ||
})); | ||
}); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}); | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.