-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented rang:partial command + added tests
- Loading branch information
Showing
7 changed files
with
310 additions
and
10 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
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,172 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Dependencies | ||
*/ | ||
var yeoman = require('yeoman-generator'); | ||
var util = require('util'); | ||
var chalk = require('chalk'); | ||
|
||
/** | ||
* Configuration | ||
*/ | ||
var config = { | ||
appModules: './workspace/scripts/app/appModules.js', | ||
partialsPath: './workspace/scripts/app/partials/', | ||
controllerSubPath: 'controllers/', | ||
viewSubPath: 'templates/' | ||
}; | ||
|
||
/** | ||
* User answers | ||
*/ | ||
var userAnswers = { | ||
partialName: null, | ||
createController: null | ||
}; | ||
|
||
/** | ||
* RangPartialGenerator | ||
* | ||
* Always overwrites the file if a conflict occurred. | ||
* @constructor | ||
*/ | ||
function RangPartialGenerator() { | ||
yeoman.generators.Base.apply( this, arguments ); | ||
|
||
this.conflicter.force = true; | ||
|
||
/** | ||
* Show information on completion. | ||
*/ | ||
this.on('end', function () { | ||
this.log( | ||
'\n\n' + chalk.green('Success!') + ' Partial \'' + userAnswers.partialName + '\' created!' | ||
); | ||
}.bind( this )); | ||
} | ||
|
||
util.inherits( RangPartialGenerator, yeoman.generators.Base ); | ||
|
||
/** | ||
* Show greeting | ||
*/ | ||
RangPartialGenerator.prototype.greeting = function () { | ||
this.log( | ||
chalk.blue("\n _ _ _ \n" + | ||
" | | (_) | |\n" + | ||
" _ __ __ _ _ __ __ _ _ __ __ _ _ __| |_ _ __ _| |\n" + | ||
"| '__/ _` | '_ \\ / _` | | '_ \\ / _` | '__| __| |/ _` | |\n" + | ||
"| | | (_| | | | | (_| | _ | |_) | (_| | | | |_| | (_| | |\n" + | ||
"|_| \\__,_|_| |_|\\__, | (_) | .__/ \\__,_|_| \\__|_|\\__,_|_|\n" + | ||
" __/ | | | \n" + | ||
" |___/ |_| \n") | ||
); | ||
|
||
this.log( | ||
chalk.yellow('Welcome! I\'ll help you create a module. Answer the following questions.\n') | ||
); | ||
}; | ||
|
||
/** | ||
* Obtain the information which is needed to generate the module. | ||
*/ | ||
RangPartialGenerator.prototype.prompting = function () { | ||
var done = this.async(); | ||
|
||
this.prompt( | ||
[ | ||
{ | ||
type : 'input', | ||
name : 'partial', | ||
message : 'Partial name', | ||
validate: function( partial ) { | ||
return ( partial.length ) ? true : 'You must provide a partial name'; | ||
} | ||
}, | ||
{ | ||
type : 'confirm', | ||
name : 'controller', | ||
message : 'Need to create a controller?' | ||
} | ||
], | ||
function ( answers ) { | ||
userAnswers.partialName = answers.partial; | ||
userAnswers.createController = answers.controller; | ||
|
||
done(); | ||
} | ||
); | ||
}; | ||
|
||
/** | ||
* Create the module files | ||
*/ | ||
RangPartialGenerator.prototype.writing = function () { | ||
var partialPath = config.partialsPath + userAnswers.partialName + '/'; | ||
var controllerPath = partialPath + config.controllerSubPath + userAnswers.partialName + 'Controller.js'; | ||
var viewPath = partialPath + config.viewSubPath + userAnswers.partialName + '.tmpl.html'; | ||
|
||
// template | ||
this.template('./view.tmpl', viewPath, { partial: userAnswers.partialName }, {}); | ||
|
||
if ( userAnswers.createController ) { | ||
|
||
// main | ||
this.template('./main.tmpl', partialPath + '/main.js', { partial: userAnswers.partialName }, {}); | ||
|
||
// controller | ||
this.template('./controller.tmpl', controllerPath, { partial: userAnswers.partialName }, {}); | ||
|
||
} | ||
}; | ||
|
||
/** | ||
* Read an application dependencies | ||
*/ | ||
RangPartialGenerator.prototype._getDependencies = function () { | ||
return this.readFileAsString( config.appModules ); | ||
}; | ||
|
||
/** | ||
* Add module as the application dependency | ||
*/ | ||
RangPartialGenerator.prototype.updateDependencies = function () { | ||
|
||
// do not update dependencies, because the controller is not created | ||
if ( !userAnswers.createController ) { | ||
return; | ||
} | ||
|
||
var appModulesSrc = this._getDependencies(); | ||
var appModules = []; | ||
var lastModule; | ||
|
||
/** | ||
* CommonJS RegExp | ||
* Find all `require('module')` | ||
*/ | ||
var cjsRegExp = /require\s*\(\s*["']([^'"\s]+)["']\s*\)/g; | ||
|
||
appModulesSrc.replace( cjsRegExp, function ( module ) { | ||
appModules.push( module ); | ||
}); | ||
|
||
lastModule = appModules.pop(); | ||
|
||
/** | ||
* before: | ||
* require('oldmodule') | ||
* after: | ||
* require('oldmodule'), | ||
* require('module') | ||
*/ | ||
appModulesSrc = appModulesSrc.replace( | ||
lastModule, | ||
lastModule + ',\n' + ' require(\'partials/' + userAnswers.partialName + '/main\')' | ||
); | ||
|
||
this.write( config.appModules, appModulesSrc ); | ||
}; | ||
|
||
module.exports = RangPartialGenerator; |
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,9 @@ | ||
define(function () { | ||
'use strict'; | ||
|
||
return { | ||
<%= partial %>Controller: ['$scope', function ( $scope ) { | ||
$scope.partial = '<%= partial %>'; | ||
}] | ||
}; | ||
}); |
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,14 @@ | ||
define(function ( require ) { | ||
'use strict'; | ||
|
||
/** | ||
* Dependencies | ||
*/ | ||
var angular = require('angular'); | ||
var <%= partial %>Controller = require('./controllers/<%= partial %>Controller'); | ||
|
||
/** | ||
* Module: <%= partial %> partial | ||
*/ | ||
return angular.module('app.<%= partial %>', []).controller( <%= partial %>Controller ); | ||
}); |
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,4 @@ | ||
<div class="alert alert-success"> | ||
<span class="glyphicon glyphicon-ok-circle"></span> | ||
Partial successfully created | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Dependencies | ||
*/ | ||
var path = require('path'); | ||
var helpers = require('yeoman-generator').test; | ||
var assert = require('yeoman-generator').assert; | ||
var output = require('./mute'); | ||
|
||
describe('rang:partial generator', function () { | ||
var rangPartial; | ||
var tempFolder = 'tmp/tmpRangPartial'; | ||
var expected = { | ||
withController: { | ||
exist: [ | ||
'workspace/scripts/app/partials/test/main.js', | ||
'workspace/scripts/app/partials/test/templates/test.tmpl.html', | ||
'workspace/scripts/app/partials/test/controllers/testController.js' | ||
] | ||
}, | ||
withoutController: { | ||
exist: [ | ||
'workspace/scripts/app/partials/test/templates/test.tmpl.html' | ||
], | ||
notExist: [ | ||
'workspace/scripts/app/partials/test/main.js', | ||
'workspace/scripts/app/partials/test/controllers/testController.js' | ||
] | ||
} | ||
|
||
}; | ||
|
||
var expectedContent = [ | ||
[ | ||
'workspace/scripts/app/partials/test/main.js', | ||
/var testController = require\('\.\/controllers\/testController'\);/ | ||
], | ||
[ 'workspace/scripts/app/partials/test/main.js', /Module: test partial/ ], | ||
[ | ||
'workspace/scripts/app/partials/test/main.js', | ||
/return angular\.module\('app.test', \[]\)\.controller\( testController \);/ | ||
], | ||
[ 'workspace/scripts/app/partials/test/controllers/testController.js', /testController:/ ], | ||
[ 'workspace/scripts/app/appModules.js', /require\('partials\/test\/main'\)/ ] | ||
]; | ||
|
||
beforeEach(function ( done ) { | ||
helpers.testDirectory( path.join( __dirname, tempFolder ), function ( error ) { | ||
if ( error ) { | ||
done( error ); | ||
} | ||
|
||
rangPartial = helpers.createGenerator( 'rang:partial', ['../../../generators/partial'], [], {} ); | ||
rangPartial.on('run', output.mute); | ||
rangPartial.on('end', output.unmute); | ||
|
||
helpers.stub( rangPartial, '_getDependencies', function () { | ||
return "return [\n" + | ||
" require('partials/old/module')\n" + | ||
"];"; | ||
}); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should generate the partial without controller', function ( done ) { | ||
helpers.mockPrompt( rangPartial, { partial: 'test', controller: false } ); | ||
|
||
rangPartial.run( {}, function () { | ||
|
||
assert.file( expected.withoutController.exist ); | ||
assert.noFile( expected.withoutController.notExist ); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should generate the partial with controller and main files and their content', function ( done ) { | ||
helpers.mockPrompt( rangPartial, { partial: 'test', controller: true } ); | ||
|
||
rangPartial.run( {}, function () { | ||
|
||
assert.file( expected.withController.exist ); | ||
assert.fileContent( expectedContent ); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |