Skip to content

Commit

Permalink
feat(app): Add generator for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thaiat committed Sep 14, 2016
1 parent 0829134 commit b07a870
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ yo mcfly-ng2 [project-name]

You then have access to the following sub generators:
* **yo mcfly-ng2:target** (Creates a new target - you can think of it as another application)
* **yo mcfly-ng2:module** (Creates a module)
* **yo mcfly-ng2:service** (Creates a service)
* **yo mcfly-ng2:pipe** (Creates a pipe)
* **yo mcfly-ng2:component** (Creates a component)
Expand Down
13 changes: 9 additions & 4 deletions generators/componentGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = generators.Base.extend({
if (!this.basetype) {
throw 'basetype is undefined';
}
if (!this.basefolder) {
throw 'basefolder is undefined';
}
// if (!this.basefolder) {
// throw 'basefolder is undefined';
// }

// applying mixins
mixinInspector.extend(this);
Expand Down Expand Up @@ -91,7 +91,12 @@ module.exports = generators.Base.extend({
},

writing: function() {
var destinationPath = path.join(this.configOptions.clientFolder, 'scripts', this.modulename, this.basefolder);
var destinationPath;
if (this.basefolder.length > 0) {
destinationPath = path.join(this.configOptions.clientFolder, 'scripts', this.modulename, this.basefolder);
} else {
destinationPath = path.join(this.configOptions.clientFolder, 'scripts', this.modulename);
}
if (this.hasOwnFolder) {
destinationPath = path.join(destinationPath, this[this.basetype + 'nameFile']);
}
Expand Down
41 changes: 41 additions & 0 deletions generators/module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
var path = require('path');
var ComponentGenerator = require('../componentGenerator');
var Generator = module.exports = ComponentGenerator.extend({

constructor: function() {
this.basetype = 'ngmodule'; // this will create a property this.ngmodulename
this.basefolder = ''; // this is the folder for the components
this.hasOwnFolder = false; // to specify if the component files should be in a subfolder
this.isDasherize = true; // to specify that the file name should be dasherized
ComponentGenerator.apply(this, arguments);
},

initializing: function() {
Generator.__super__.initializing.apply(this, arguments);
},

prompting: function() {
var done = this.async();
Generator.__super__.prompting.call(this, done);

},

configuring: function() {
Generator.__super__.configuring.apply(this, arguments);
},

writing: function() {
var destinationPath = Generator.__super__.writing.apply(this, arguments);
this.fs.copyTpl(
this.templatePath('_module.ts'),
this.destinationPath(path.join(destinationPath, 'index.ts')), {
ngmodulenameFile: this.ngmodulenameFile,
ngmodulename: this.ngmodulename,
ngmodulenameClass: this.ngmodulenameClass
}
);

}

});
10 changes: 10 additions & 0 deletions generators/module/templates/_module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';

@NgModule({
declarations: [],
imports: [],
providers: [],
bootstrap: [],
entryComponents: []
})
export class <%=ngmodulenameClass%>Module { }
55 changes: 55 additions & 0 deletions test/mocha/module.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
var assert = require('yeoman-assert');
//var helpers = require('yeoman-test');
var testHelper = require('./testHelper');
//var generatorFullname = testHelper.mixins.getGeneratorFullname(); // generator-mcfly-ng2
var generatorShortname = testHelper.mixins.getGeneratorShortname(); // mcfly-ng2

describe(generatorShortname + ':module', function() {
var targetname = 'dashboard';
var clientFolder = 'client';
var componentname = 'my dummy';

var config = testHelper.getYoRc({
clientFolder: clientFolder
});

before(function(done) {
var self = this;
testHelper.runGenerator('module')
.withArguments([targetname, componentname])
.inTmpDir(function(dir) {
// setting up expected files
testHelper.createFolderStructure(config, dir, clientFolder, targetname);
})
.on('ready', function(generator) {
self.generator = generator;
})
.on('end', done);
});

it('creates expected files', function() {
var pathdir = clientFolder + '/scripts/dashboard/';

var expectedFiles = [
pathdir + 'index.ts'
];

assert.file(expectedFiles);

var expectedContents = [
[pathdir + 'index.ts', /export class MyDummyModule/]
];

assert.fileContent(expectedContents);

});

it('exposes valid client targets and client modules', function() {
var configOptions = this.generator.configOptions;
var clientModules = configOptions.clientModules;
var clientTargets = configOptions.clientTargets;
assert.objectContent(clientModules, ['app', 'common', 'dashboard', 'dummy', 'tata', 'toto']);
assert.objectContent(clientTargets, ['app', 'tata', 'toto']);
});
});

0 comments on commit b07a870

Please # to comment.