From 11f861158d43e404e9b1b6112912a9fd07b8e2a5 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Thu, 11 Aug 2016 13:25:03 -0400 Subject: [PATCH] feat(module): add generation of modules --- .../files/__path__/__name__.module.spec.ts | 15 +++++ .../module/files/__path__/__name__.module.ts | 8 +++ addon/ng2/blueprints/module/index.js | 45 ++++++++++++++ addon/ng2/commands/generate.ts | 1 + tests/acceptance/generate-module.spec.js | 60 +++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 addon/ng2/blueprints/module/files/__path__/__name__.module.spec.ts create mode 100644 addon/ng2/blueprints/module/files/__path__/__name__.module.ts create mode 100644 addon/ng2/blueprints/module/index.js create mode 100644 tests/acceptance/generate-module.spec.js diff --git a/addon/ng2/blueprints/module/files/__path__/__name__.module.spec.ts b/addon/ng2/blueprints/module/files/__path__/__name__.module.spec.ts new file mode 100644 index 000000000000..d33c39399fed --- /dev/null +++ b/addon/ng2/blueprints/module/files/__path__/__name__.module.spec.ts @@ -0,0 +1,15 @@ +/* tslint:disable:no-unused-variable */ + +import <%= classifiedModuleName %>Module from './<%= dasherizedModuleName %>.module'; + +describe('<%= classifiedModuleName %>Module', () => { + let <%= camelizedModuleName %>Module; + + beforeEach(() => { + <%= camelizedModuleName %>Module = new <%= classifiedModuleName %>Module(); + }); + + it('should create an instance', () => { + expect(<%= camelizedModuleName %>Module).toBeTruthy(); + }) +}); \ No newline at end of file diff --git a/addon/ng2/blueprints/module/files/__path__/__name__.module.ts b/addon/ng2/blueprints/module/files/__path__/__name__.module.ts new file mode 100644 index 000000000000..0f5b8b702ab0 --- /dev/null +++ b/addon/ng2/blueprints/module/files/__path__/__name__.module.ts @@ -0,0 +1,8 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +@NgModule({ + imports: [ CommonModule ], + declarations: [] +}) +export default class <%= classifiedModuleName %>Module { } diff --git a/addon/ng2/blueprints/module/index.js b/addon/ng2/blueprints/module/index.js new file mode 100644 index 000000000000..0d55885caebb --- /dev/null +++ b/addon/ng2/blueprints/module/index.js @@ -0,0 +1,45 @@ +var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +var Blueprint = require('ember-cli/lib/models/blueprint'); +var getFiles = Blueprint.prototype.files; + +module.exports = { + description: '', + + availableOptions: [ + { name: 'spec', type: Boolean, default: false } + ], + + normalizeEntityName: function (entityName) { + var parsedPath = dynamicPathParser(this.project, entityName); + + this.dynamicPath = parsedPath; + return parsedPath.name; + }, + + locals: function (options) { + return { + dynamicPath: this.dynamicPath.dir, + spec: options.spec + }; + }, + + files: function() { + var fileList = getFiles.call(this); + + if (!this.options || !this.options.spec) { + fileList = fileList.filter(p => p.indexOf('__name__.module.spec.ts') < 0); + } + + return fileList; + }, + + fileMapTokens: function () { + // Return custom template variables here. + return { + __path__: () => { + this.generatePath = this.dynamicPath.dir; + return this.generatePath; + } + }; + } +}; diff --git a/addon/ng2/commands/generate.ts b/addon/ng2/commands/generate.ts index 80b34ceca9b7..a38f298b38f5 100644 --- a/addon/ng2/commands/generate.ts +++ b/addon/ng2/commands/generate.ts @@ -53,6 +53,7 @@ const aliasMap = { 'c': 'component', 'd': 'directive', 'e': 'enum', + 'm': 'module', 'p': 'pipe', 'r': 'route', 's': 'service' diff --git a/tests/acceptance/generate-module.spec.js b/tests/acceptance/generate-module.spec.js new file mode 100644 index 000000000000..99aae352798e --- /dev/null +++ b/tests/acceptance/generate-module.spec.js @@ -0,0 +1,60 @@ +'use strict'; + +const ng = require('../helpers/ng'); +const tmp = require('../helpers/tmp'); + +const conf = require('ember-cli/tests/helpers/conf'); +const existsSync = require('exists-sync'); +const expect = require('chai').expect; +const path = require('path'); +const root = process.cwd(); + +const testPath = path.join(root, 'tmp', 'foo', 'src', 'app'); + +describe('Acceptance: ng generate module', function () { + before(conf.setup); + + after(conf.restore); + + beforeEach(function () { + return tmp.setup('./tmp').then(function () { + process.chdir('./tmp'); + }).then(function () { + return ng(['new', 'foo', '--skip-npm', '--skip-bower']); + }); + }); + + afterEach(function () { + this.timeout(10000); + + return tmp.teardown('./tmp'); + }); + + it('ng generate module my-module', function () { + return ng(['generate', 'module', 'my-module']).then(() => { + expect(existsSync(path.join(testPath, 'my-module.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'my-module.module.spec.ts'))).to.equal(false); + }); + }); + + it('ng generate module my-module --spec', function () { + return ng(['generate', 'module', 'my-module', '--spec']).then(() => { + expect(existsSync(path.join(testPath, 'my-module.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'my-module.module.spec.ts'))).to.equal(true); + }); + }); + + it(`ng generate module shared${path.sep}my-module`, function () { + return ng(['generate', 'module', 'shared/my-module']).then(() => { + expect(existsSync(path.join(testPath, 'shared', 'my-module.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'shared', 'my-module.module.spec.ts'))).to.equal(false); + }); + }); + + it(`ng generate module shared${path.sep}my-module --spec`, function () { + return ng(['generate', 'module', 'shared/my-module', '--spec']).then(() => { + expect(existsSync(path.join(testPath, 'shared', 'my-module.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'shared', 'my-module.module.spec.ts'))).to.equal(true); + }); + }); +});