diff --git a/README.md b/README.md index d51daed..98f73ff 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/generators/componentGenerator.js b/generators/componentGenerator.js index a4aa99f..b5bcecd 100644 --- a/generators/componentGenerator.js +++ b/generators/componentGenerator.js @@ -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); @@ -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']); } diff --git a/generators/module/index.js b/generators/module/index.js new file mode 100644 index 0000000..877e004 --- /dev/null +++ b/generators/module/index.js @@ -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 + } + ); + + } + +}); diff --git a/generators/module/templates/_module.ts b/generators/module/templates/_module.ts new file mode 100644 index 0000000..7c8495c --- /dev/null +++ b/generators/module/templates/_module.ts @@ -0,0 +1,10 @@ +import { NgModule } from '@angular/core'; + +@NgModule({ + declarations: [], + imports: [], + providers: [], + bootstrap: [], + entryComponents: [] +}) +export class <%=ngmodulenameClass%>Module { } diff --git a/test/mocha/module.spec.js b/test/mocha/module.spec.js new file mode 100644 index 0000000..2b9e88e --- /dev/null +++ b/test/mocha/module.spec.js @@ -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']); + }); +});