Skip to content

Commit 961c355

Browse files
BroccoDanny Blue
authored and
Danny Blue
committed
feature(blueprints): add spec/no-spec flags to all blueprints (angular#2382)
Fixes angular#2108
1 parent e1d9930 commit 961c355

File tree

8 files changed

+136
-19
lines changed

8 files changed

+136
-19
lines changed

packages/angular-cli/blueprints/class/index.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
const stringUtils = require('ember-cli-string-utils');
2-
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
2+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
3+
const Blueprint = require('ember-cli/lib/models/blueprint');
4+
const getFiles = Blueprint.prototype.files;
35

46
module.exports = {
57
description: '',
6-
8+
79
anonymousOptions: [
810
'<class-type>'
911
],
10-
12+
13+
availableOptions: [
14+
{ name: 'spec', type: Boolean, default: true }
15+
],
16+
1117
normalizeEntityName: function (entityName) {
1218
var parsedPath = dynamicPathParser(this.project, entityName);
1319

@@ -19,15 +25,25 @@ module.exports = {
1925
var classType = options.args [2]
2026
this.fileName = stringUtils.dasherize(options.entity.name);
2127
if (classType) {
22-
this.fileName += '.' + classType;
28+
this.fileName += '.' + classType;
2329
}
24-
return {
30+
return {
2531
dynamicPath: this.dynamicPath.dir,
2632
flat: options.flat,
2733
fileName: this.fileName
2834
};
2935
},
3036

37+
files: function() {
38+
var fileList = getFiles.call(this);
39+
40+
if (this.options && !this.options.spec) {
41+
fileList = fileList.filter(p => p.indexOf('__name__.spec.ts') < 0);
42+
}
43+
44+
return fileList;
45+
},
46+
3147
fileMapTokens: function () {
3248
// Return custom template variables here.
3349
return {

packages/angular-cli/blueprints/directive/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
var path = require('path');
2-
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
1+
const path = require('path');
2+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
33
const stringUtils = require('ember-cli-string-utils');
44
const astUtils = require('../../utilities/ast-utils');
55
const findParentModule = require('../../utilities/find-parent-module').default;
66
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
7+
const Blueprint = require('ember-cli/lib/models/blueprint');
8+
const getFiles = Blueprint.prototype.files;
79

810
module.exports = {
911
description: '',
1012

1113
availableOptions: [
1214
{ name: 'flat', type: Boolean, default: true },
13-
{ name: 'prefix', type: Boolean, default: true }
15+
{ name: 'prefix', type: Boolean, default: true },
16+
{ name: 'spec', type: Boolean, default: true }
1417
],
1518

1619
beforeInstall: function() {
@@ -46,6 +49,16 @@ module.exports = {
4649
};
4750
},
4851

52+
files: function() {
53+
var fileList = getFiles.call(this);
54+
55+
if (this.options && !this.options.spec) {
56+
fileList = fileList.filter(p => p.indexOf('__name__.directive.spec.ts') < 0);
57+
}
58+
59+
return fileList;
60+
},
61+
4962
fileMapTokens: function (options) {
5063
// Return custom template variables here.
5164
return {

packages/angular-cli/blueprints/pipe/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
var path = require('path');
2-
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
1+
const path = require('path');
2+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
33
const stringUtils = require('ember-cli-string-utils');
44
const astUtils = require('../../utilities/ast-utils');
55
const findParentModule = require('../../utilities/find-parent-module').default;
66
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
7+
const Blueprint = require('ember-cli/lib/models/blueprint');
8+
const getFiles = Blueprint.prototype.files;
79

810
module.exports = {
911
description: '',
1012

1113
availableOptions: [
12-
{ name: 'flat', type: Boolean, default: true }
14+
{ name: 'flat', type: Boolean, default: true },
15+
{ name: 'spec', type: Boolean, default: true }
1316
],
1417

1518
beforeInstall: function() {
@@ -34,6 +37,16 @@ module.exports = {
3437
};
3538
},
3639

40+
files: function() {
41+
var fileList = getFiles.call(this);
42+
43+
if (this.options && !this.options.spec) {
44+
fileList = fileList.filter(p => p.indexOf('__name__.pipe.spec.ts') < 0);
45+
}
46+
47+
return fileList;
48+
},
49+
3750
fileMapTokens: function (options) {
3851
// Return custom template variables here.
3952
return {

packages/angular-cli/blueprints/service/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
var path = require('path');
1+
const path = require('path');
22
const chalk = require('chalk');
3-
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
3+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
4+
const Blueprint = require('ember-cli/lib/models/blueprint');
5+
const getFiles = Blueprint.prototype.files;
46

57
module.exports = {
68
description: '',
79

810
availableOptions: [
9-
{ name: 'flat', type: Boolean, default: true }
11+
{ name: 'flat', type: Boolean, default: true },
12+
{ name: 'spec', type: Boolean, default: true }
1013
],
1114

1215
normalizeEntityName: function (entityName) {
@@ -23,6 +26,16 @@ module.exports = {
2326
};
2427
},
2528

29+
files: function() {
30+
var fileList = getFiles.call(this);
31+
32+
if (this.options && !this.options.spec) {
33+
fileList = fileList.filter(p => p.indexOf('__name__.service.spec.ts') < 0);
34+
}
35+
36+
return fileList;
37+
},
38+
2639
fileMapTokens: function (options) {
2740
// Return custom template variables here.
2841
return {

tests/acceptance/generate-class.spec.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ describe('Acceptance: ng generate class', function () {
3232
it('ng generate class my-class', function () {
3333
return ng(['generate', 'class', 'my-class']).then(() => {
3434
expect(existsSync(path.join(testPath, 'my-class.ts'))).to.equal(true);
35+
expect(existsSync(path.join(testPath, 'my-class.spec.ts'))).to.equal(true);
3536
});
3637
});
37-
38+
39+
it('ng generate class my-class --no-spec', function () {
40+
return ng(['generate', 'class', 'my-class', '--no-spec']).then(() => {
41+
expect(existsSync(path.join(testPath, 'my-class.ts'))).to.equal(true);
42+
expect(existsSync(path.join(testPath, 'my-class.spec.ts'))).to.equal(false);
43+
});
44+
});
45+
3846
it('ng generate class my-class model', function () {
3947
return ng(['generate', 'class', 'my-class', 'model']).then(() => {
4048
expect(existsSync(path.join(testPath, 'my-class.model.ts'))).to.equal(true);
@@ -46,7 +54,7 @@ describe('Acceptance: ng generate class', function () {
4654
expect(existsSync(path.join(testPath, 'shared', 'my-class.ts'))).to.equal(true);
4755
});
4856
});
49-
57+
5058
it(`ng generate class shared${path.sep}my-class model`, function () {
5159
return ng(['generate', 'class', 'shared/my-class', 'model']).then(() => {
5260
expect(existsSync(path.join(testPath, 'shared', 'my-class.model.ts'))).to.equal(true);

tests/acceptance/generate-directive.spec.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,33 @@ describe('Acceptance: ng generate directive', function () {
4444
it('my-dir --flat false', function () {
4545
const appRoot = path.join(root, 'tmp/foo');
4646
const testPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.ts');
47+
const testSpecPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.spec.ts');
4748
const appModulePath = path.join(appRoot, 'src/app/app.module.ts');
4849

4950
return ng(['generate', 'directive', 'my-dir', '--flat', 'false'])
50-
.then(() => expect(existsSync(testPath)).to.equal(true))
51+
.then(() => {
52+
expect(existsSync(testPath)).to.equal(true);
53+
expect(existsSync(testSpecPath)).to.equal(true);
54+
})
5155
.then(() => readFile(appModulePath, 'utf-8'))
5256
.then(content => {
5357
expect(content).matches(/import.*\bMyDirDirective\b.*from '.\/my-dir\/my-dir.directive';/);
5458
expect(content).matches(/declarations:\s*\[[^\]]+?,\r?\n\s+MyDirDirective\r?\n/m);
5559
});
5660
});
5761

62+
it('my-dir --flat false --no-spec', function () {
63+
const appRoot = path.join(root, 'tmp/foo');
64+
const testPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.ts');
65+
const testSpecPath = path.join(appRoot, 'src/app/my-dir/my-dir.directive.spec.ts');
66+
67+
return ng(['generate', 'directive', 'my-dir', '--flat', 'false', '--no-spec'])
68+
.then(() => {
69+
expect(existsSync(testPath)).to.equal(true);
70+
expect(existsSync(testSpecPath)).to.equal(false);
71+
});
72+
});
73+
5874
it('test' + path.sep + 'my-dir', function () {
5975
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test'));
6076
return ng(['generate', 'directive', 'test' + path.sep + 'my-dir', '--flat', 'false']).then(() => {

tests/acceptance/generate-pipe.spec.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,32 @@ describe('Acceptance: ng generate pipe', function () {
3838
it('ng generate pipe my-pipe', function () {
3939
const appRoot = path.join(root, 'tmp/foo');
4040
const testPath = path.join(appRoot, 'src/app/my-pipe.pipe.ts');
41+
const testSpecPath = path.join(appRoot, 'src/app/my-pipe.pipe.spec.ts');
4142
const appModulePath = path.join(appRoot, 'src/app/app.module.ts');
4243
return ng(['generate', 'pipe', 'my-pipe'])
43-
.then(() => expect(existsSync(testPath)).to.equal(true))
44+
.then(() => {
45+
expect(existsSync(testPath)).to.equal(true);
46+
expect(existsSync(testSpecPath)).to.equal(true);
47+
})
4448
.then(() => readFile(appModulePath, 'utf-8'))
4549
.then(content => {
4650
expect(content).matches(/import.*\bMyPipePipe\b.*from '.\/my-pipe.pipe';/);
4751
expect(content).matches(/declarations:\s*\[[^\]]+?,\r?\n\s+MyPipePipe\r?\n/m);
4852
});
4953
});
5054

55+
it('ng generate pipe my-pipe --no-spec', function () {
56+
const appRoot = path.join(root, 'tmp/foo');
57+
const testPath = path.join(appRoot, 'src/app/my-pipe.pipe.ts');
58+
const testSpecPath = path.join(appRoot, 'src/app/my-pipe.pipe.spec.ts');
59+
60+
return ng(['generate', 'pipe', 'my-pipe', '--no-spec'])
61+
.then(() => {
62+
expect(existsSync(testPath)).to.equal(true);
63+
expect(existsSync(testSpecPath)).to.equal(false);
64+
});
65+
});
66+
5167
it('ng generate pipe test' + path.sep + 'my-pipe', function () {
5268
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test'));
5369
return ng(['generate', 'pipe', 'test' + path.sep + 'my-pipe']).then(() => {

tests/acceptance/generate-service.spec.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,32 @@ describe('Acceptance: ng generate service', function () {
3737
it('ng generate service my-svc', function () {
3838
const appRoot = path.join(root, 'tmp/foo');
3939
const testPath = path.join(appRoot, 'src/app/my-svc.service.ts');
40+
const testSpecPath = path.join(appRoot, 'src/app/my-svc.service.spec.ts');
4041
const appModulePath = path.join(appRoot, 'src/app/app.module.ts');
4142

4243
return ng(['generate', 'service', 'my-svc'])
43-
.then(() => expect(existsSync(testPath)).to.equal(true))
44+
.then(() => {
45+
expect(existsSync(testPath)).to.equal(true);
46+
expect(existsSync(testSpecPath)).to.equal(true);
47+
})
48+
.then(() => readFile(appModulePath, 'utf-8'))
49+
.then(content => {
50+
expect(content).not.to.matches(/import.*\MySvcService\b.*from '.\/my-svc.service';/);
51+
expect(content).not.to.matches(/providers:\s*\[MySvcService\]/m);
52+
});
53+
});
54+
55+
it('ng generate service my-svc --no-spec', function () {
56+
const appRoot = path.join(root, 'tmp/foo');
57+
const testPath = path.join(appRoot, 'src/app/my-svc.service.ts');
58+
const testSpecPath = path.join(appRoot, 'src/app/my-svc.service.spec.ts');
59+
const appModulePath = path.join(appRoot, 'src/app/app.module.ts');
60+
61+
return ng(['generate', 'service', 'my-svc', '--no-spec'])
62+
.then(() => {
63+
expect(existsSync(testPath)).to.equal(true);
64+
expect(existsSync(testSpecPath)).to.equal(false);
65+
})
4466
.then(() => readFile(appModulePath, 'utf-8'))
4567
.then(content => {
4668
expect(content).not.to.matches(/import.*\MySvcService\b.*from '.\/my-svc.service';/);

0 commit comments

Comments
 (0)