From 9a2c44b0da806606fd2b28420bbfb7479742ad79 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Sun, 2 Oct 2016 22:14:17 -0400 Subject: [PATCH] feature(blueprints): add spec/no-spec flags to all blueprints (#2382) Fixes #2108 --- .../angular-cli/blueprints/class/index.js | 26 +++++++++++++++---- .../angular-cli/blueprints/directive/index.js | 19 +++++++++++--- packages/angular-cli/blueprints/pipe/index.js | 19 +++++++++++--- .../angular-cli/blueprints/service/index.js | 19 +++++++++++--- tests/acceptance/generate-class.spec.js | 12 +++++++-- tests/acceptance/generate-directive.spec.js | 18 ++++++++++++- tests/acceptance/generate-pipe.spec.js | 18 ++++++++++++- tests/acceptance/generate-service.spec.js | 24 ++++++++++++++++- 8 files changed, 136 insertions(+), 19 deletions(-) diff --git a/packages/angular-cli/blueprints/class/index.js b/packages/angular-cli/blueprints/class/index.js index 6e92316d493f..a280be2b22dc 100644 --- a/packages/angular-cli/blueprints/class/index.js +++ b/packages/angular-cli/blueprints/class/index.js @@ -1,13 +1,19 @@ const stringUtils = require('ember-cli-string-utils'); -var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const Blueprint = require('ember-cli/lib/models/blueprint'); +const getFiles = Blueprint.prototype.files; module.exports = { description: '', - + anonymousOptions: [ '' ], - + + availableOptions: [ + { name: 'spec', type: Boolean, default: true } + ], + normalizeEntityName: function (entityName) { var parsedPath = dynamicPathParser(this.project, entityName); @@ -19,15 +25,25 @@ module.exports = { var classType = options.args [2] this.fileName = stringUtils.dasherize(options.entity.name); if (classType) { - this.fileName += '.' + classType; + this.fileName += '.' + classType; } - return { + return { dynamicPath: this.dynamicPath.dir, flat: options.flat, fileName: this.fileName }; }, + files: function() { + var fileList = getFiles.call(this); + + if (this.options && !this.options.spec) { + fileList = fileList.filter(p => p.indexOf('__name__.spec.ts') < 0); + } + + return fileList; + }, + fileMapTokens: function () { // Return custom template variables here. return { diff --git a/packages/angular-cli/blueprints/directive/index.js b/packages/angular-cli/blueprints/directive/index.js index 60d029aabbab..5523ae7eeffb 100644 --- a/packages/angular-cli/blueprints/directive/index.js +++ b/packages/angular-cli/blueprints/directive/index.js @@ -1,16 +1,19 @@ -var path = require('path'); -var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const path = require('path'); +const dynamicPathParser = require('../../utilities/dynamic-path-parser'); const stringUtils = require('ember-cli-string-utils'); const astUtils = require('../../utilities/ast-utils'); const findParentModule = require('../../utilities/find-parent-module').default; const NodeHost = require('@angular-cli/ast-tools').NodeHost; +const Blueprint = require('ember-cli/lib/models/blueprint'); +const getFiles = Blueprint.prototype.files; module.exports = { description: '', availableOptions: [ { name: 'flat', type: Boolean, default: true }, - { name: 'prefix', type: Boolean, default: true } + { name: 'prefix', type: Boolean, default: true }, + { name: 'spec', type: Boolean, default: true } ], beforeInstall: function() { @@ -46,6 +49,16 @@ module.exports = { }; }, + files: function() { + var fileList = getFiles.call(this); + + if (this.options && !this.options.spec) { + fileList = fileList.filter(p => p.indexOf('__name__.directive.spec.ts') < 0); + } + + return fileList; + }, + fileMapTokens: function (options) { // Return custom template variables here. return { diff --git a/packages/angular-cli/blueprints/pipe/index.js b/packages/angular-cli/blueprints/pipe/index.js index bc4b0710ca60..6300ff4b78c4 100644 --- a/packages/angular-cli/blueprints/pipe/index.js +++ b/packages/angular-cli/blueprints/pipe/index.js @@ -1,15 +1,18 @@ -var path = require('path'); -var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const path = require('path'); +const dynamicPathParser = require('../../utilities/dynamic-path-parser'); const stringUtils = require('ember-cli-string-utils'); const astUtils = require('../../utilities/ast-utils'); const findParentModule = require('../../utilities/find-parent-module').default; const NodeHost = require('@angular-cli/ast-tools').NodeHost; +const Blueprint = require('ember-cli/lib/models/blueprint'); +const getFiles = Blueprint.prototype.files; module.exports = { description: '', availableOptions: [ - { name: 'flat', type: Boolean, default: true } + { name: 'flat', type: Boolean, default: true }, + { name: 'spec', type: Boolean, default: true } ], beforeInstall: function() { @@ -34,6 +37,16 @@ module.exports = { }; }, + files: function() { + var fileList = getFiles.call(this); + + if (this.options && !this.options.spec) { + fileList = fileList.filter(p => p.indexOf('__name__.pipe.spec.ts') < 0); + } + + return fileList; + }, + fileMapTokens: function (options) { // Return custom template variables here. return { diff --git a/packages/angular-cli/blueprints/service/index.js b/packages/angular-cli/blueprints/service/index.js index ea535fd44d2b..b2912cdabd4a 100644 --- a/packages/angular-cli/blueprints/service/index.js +++ b/packages/angular-cli/blueprints/service/index.js @@ -1,12 +1,15 @@ -var path = require('path'); +const path = require('path'); const chalk = require('chalk'); -var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const Blueprint = require('ember-cli/lib/models/blueprint'); +const getFiles = Blueprint.prototype.files; module.exports = { description: '', availableOptions: [ - { name: 'flat', type: Boolean, default: true } + { name: 'flat', type: Boolean, default: true }, + { name: 'spec', type: Boolean, default: true } ], normalizeEntityName: function (entityName) { @@ -23,6 +26,16 @@ module.exports = { }; }, + files: function() { + var fileList = getFiles.call(this); + + if (this.options && !this.options.spec) { + fileList = fileList.filter(p => p.indexOf('__name__.service.spec.ts') < 0); + } + + return fileList; + }, + fileMapTokens: function (options) { // Return custom template variables here. return { diff --git a/tests/acceptance/generate-class.spec.js b/tests/acceptance/generate-class.spec.js index d0c38f964135..ee67f611f734 100644 --- a/tests/acceptance/generate-class.spec.js +++ b/tests/acceptance/generate-class.spec.js @@ -32,9 +32,17 @@ describe('Acceptance: ng generate class', function () { it('ng generate class my-class', function () { return ng(['generate', 'class', 'my-class']).then(() => { expect(existsSync(path.join(testPath, 'my-class.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'my-class.spec.ts'))).to.equal(true); }); }); - + + it('ng generate class my-class --no-spec', function () { + return ng(['generate', 'class', 'my-class', '--no-spec']).then(() => { + expect(existsSync(path.join(testPath, 'my-class.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'my-class.spec.ts'))).to.equal(false); + }); + }); + it('ng generate class my-class model', function () { return ng(['generate', 'class', 'my-class', 'model']).then(() => { expect(existsSync(path.join(testPath, 'my-class.model.ts'))).to.equal(true); @@ -46,7 +54,7 @@ describe('Acceptance: ng generate class', function () { expect(existsSync(path.join(testPath, 'shared', 'my-class.ts'))).to.equal(true); }); }); - + it(`ng generate class shared${path.sep}my-class model`, function () { return ng(['generate', 'class', 'shared/my-class', 'model']).then(() => { expect(existsSync(path.join(testPath, 'shared', 'my-class.model.ts'))).to.equal(true); diff --git a/tests/acceptance/generate-directive.spec.js b/tests/acceptance/generate-directive.spec.js index 3620720b2018..9f013a69ee52 100644 --- a/tests/acceptance/generate-directive.spec.js +++ b/tests/acceptance/generate-directive.spec.js @@ -44,10 +44,14 @@ describe('Acceptance: ng generate directive', function () { it('my-dir --flat false', function () { const appRoot = path.join(root, 'tmp/foo'); const testPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.ts'); + const testSpecPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.spec.ts'); const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); return ng(['generate', 'directive', 'my-dir', '--flat', 'false']) - .then(() => expect(existsSync(testPath)).to.equal(true)) + .then(() => { + expect(existsSync(testPath)).to.equal(true); + expect(existsSync(testSpecPath)).to.equal(true); + }) .then(() => readFile(appModulePath, 'utf-8')) .then(content => { expect(content).matches(/import.*\bMyDirDirective\b.*from '.\/my-dir\/my-dir.directive';/); @@ -55,6 +59,18 @@ describe('Acceptance: ng generate directive', function () { }); }); + it('my-dir --flat false --no-spec', function () { + const appRoot = path.join(root, 'tmp/foo'); + const testPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.ts'); + const testSpecPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.spec.ts'); + + return ng(['generate', 'directive', 'my-dir', '--flat', 'false', '--no-spec']) + .then(() => { + expect(existsSync(testPath)).to.equal(true); + expect(existsSync(testSpecPath)).to.equal(false); + }); + }); + it('test' + path.sep + 'my-dir', function () { fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test')); return ng(['generate', 'directive', 'test' + path.sep + 'my-dir', '--flat', 'false']).then(() => { diff --git a/tests/acceptance/generate-pipe.spec.js b/tests/acceptance/generate-pipe.spec.js index 2d98f8605895..eedb299ac84d 100644 --- a/tests/acceptance/generate-pipe.spec.js +++ b/tests/acceptance/generate-pipe.spec.js @@ -38,9 +38,13 @@ describe('Acceptance: ng generate pipe', function () { it('ng generate pipe my-pipe', function () { const appRoot = path.join(root, 'tmp/foo'); const testPath = path.join(appRoot, 'src/app/my-pipe.pipe.ts'); + const testSpecPath = path.join(appRoot, 'src/app/my-pipe.pipe.spec.ts'); const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); return ng(['generate', 'pipe', 'my-pipe']) - .then(() => expect(existsSync(testPath)).to.equal(true)) + .then(() => { + expect(existsSync(testPath)).to.equal(true); + expect(existsSync(testSpecPath)).to.equal(true); + }) .then(() => readFile(appModulePath, 'utf-8')) .then(content => { expect(content).matches(/import.*\bMyPipePipe\b.*from '.\/my-pipe.pipe';/); @@ -48,6 +52,18 @@ describe('Acceptance: ng generate pipe', function () { }); }); + it('ng generate pipe my-pipe --no-spec', function () { + const appRoot = path.join(root, 'tmp/foo'); + const testPath = path.join(appRoot, 'src/app/my-pipe.pipe.ts'); + const testSpecPath = path.join(appRoot, 'src/app/my-pipe.pipe.spec.ts'); + + return ng(['generate', 'pipe', 'my-pipe', '--no-spec']) + .then(() => { + expect(existsSync(testPath)).to.equal(true); + expect(existsSync(testSpecPath)).to.equal(false); + }); + }); + it('ng generate pipe test' + path.sep + 'my-pipe', function () { fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test')); return ng(['generate', 'pipe', 'test' + path.sep + 'my-pipe']).then(() => { diff --git a/tests/acceptance/generate-service.spec.js b/tests/acceptance/generate-service.spec.js index 233d95720e06..f91584cba73f 100644 --- a/tests/acceptance/generate-service.spec.js +++ b/tests/acceptance/generate-service.spec.js @@ -37,10 +37,32 @@ describe('Acceptance: ng generate service', function () { it('ng generate service my-svc', function () { const appRoot = path.join(root, 'tmp/foo'); const testPath = path.join(appRoot, 'src/app/my-svc.service.ts'); + const testSpecPath = path.join(appRoot, 'src/app/my-svc.service.spec.ts'); const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); return ng(['generate', 'service', 'my-svc']) - .then(() => expect(existsSync(testPath)).to.equal(true)) + .then(() => { + expect(existsSync(testPath)).to.equal(true); + expect(existsSync(testSpecPath)).to.equal(true); + }) + .then(() => readFile(appModulePath, 'utf-8')) + .then(content => { + expect(content).not.to.matches(/import.*\MySvcService\b.*from '.\/my-svc.service';/); + expect(content).not.to.matches(/providers:\s*\[MySvcService\]/m); + }); + }); + + it('ng generate service my-svc --no-spec', function () { + const appRoot = path.join(root, 'tmp/foo'); + const testPath = path.join(appRoot, 'src/app/my-svc.service.ts'); + const testSpecPath = path.join(appRoot, 'src/app/my-svc.service.spec.ts'); + const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); + + return ng(['generate', 'service', 'my-svc', '--no-spec']) + .then(() => { + expect(existsSync(testPath)).to.equal(true); + expect(existsSync(testSpecPath)).to.equal(false); + }) .then(() => readFile(appModulePath, 'utf-8')) .then(content => { expect(content).not.to.matches(/import.*\MySvcService\b.*from '.\/my-svc.service';/);