Skip to content

Commit ba4d987

Browse files
Merge pull request #1586 from s5s5/s5s5/application-add-spec-option
feat(application): add spec option
2 parents 61cff89 + 1c43881 commit ba4d987

File tree

8 files changed

+138
-22
lines changed

8 files changed

+138
-22
lines changed

src/lib/application/application.factory.test.ts

Lines changed: 106 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('Application Factory', () => {
1919
.runSchematicAsync('application', options)
2020
.toPromise();
2121
const files: string[] = tree.files;
22-
expect(files).toEqual([
22+
expect(files.sort()).toEqual([
2323
'/project/.eslintrc.js',
2424
'/project/.gitignore',
2525
'/project/.prettierrc',
@@ -35,7 +35,7 @@ describe('Application Factory', () => {
3535
'/project/src/main.ts',
3636
'/project/test/app.e2e-spec.ts',
3737
'/project/test/jest-e2e.json',
38-
]);
38+
].sort());
3939

4040
expect(
4141
JSON.parse(tree.readContent('/project/package.json')),
@@ -51,7 +51,7 @@ describe('Application Factory', () => {
5151
.runSchematicAsync('application', options)
5252
.toPromise();
5353
const files: string[] = tree.files;
54-
expect(files).toEqual([
54+
expect(files.sort()).toEqual([
5555
`/project.foo.bar/.eslintrc.js`,
5656
`/project.foo.bar/.gitignore`,
5757
`/project.foo.bar/.prettierrc`,
@@ -67,7 +67,7 @@ describe('Application Factory', () => {
6767
`/project.foo.bar/src/main.ts`,
6868
`/project.foo.bar/test/app.e2e-spec.ts`,
6969
`/project.foo.bar/test/jest-e2e.json`,
70-
]);
70+
].sort());
7171

7272
expect(
7373
JSON.parse(tree.readContent('/project.foo.bar/package.json')),
@@ -83,7 +83,7 @@ describe('Application Factory', () => {
8383
.runSchematicAsync('application', options)
8484
.toPromise();
8585
const files: string[] = tree.files;
86-
expect(files).toEqual([
86+
expect(files.sort()).toEqual([
8787
'/awesome-project/.eslintrc.js',
8888
'/awesome-project/.gitignore',
8989
'/awesome-project/.prettierrc',
@@ -99,7 +99,7 @@ describe('Application Factory', () => {
9999
'/awesome-project/src/main.ts',
100100
'/awesome-project/test/app.e2e-spec.ts',
101101
'/awesome-project/test/jest-e2e.json',
102-
]);
102+
].sort());
103103

104104
expect(
105105
JSON.parse(tree.readContent('/awesome-project/package.json')),
@@ -115,7 +115,7 @@ describe('Application Factory', () => {
115115
.runSchematicAsync('application', options)
116116
.toPromise();
117117
const files: string[] = tree.files;
118-
expect(files).toEqual([
118+
expect(files.sort()).toEqual([
119119
'/_awesome-project/.eslintrc.js',
120120
'/_awesome-project/.gitignore',
121121
'/_awesome-project/.prettierrc',
@@ -131,7 +131,7 @@ describe('Application Factory', () => {
131131
'/_awesome-project/src/main.ts',
132132
'/_awesome-project/test/app.e2e-spec.ts',
133133
'/_awesome-project/test/jest-e2e.json',
134-
]);
134+
].sort());
135135

136136
expect(
137137
JSON.parse(tree.readContent('/_awesome-project/package.json')),
@@ -147,7 +147,7 @@ describe('Application Factory', () => {
147147
.runSchematicAsync('application', options)
148148
.toPromise();
149149
const files: string[] = tree.files;
150-
expect(files).toEqual([
150+
expect(files.sort()).toEqual([
151151
'/@/package/.eslintrc.js',
152152
'/@/package/.gitignore',
153153
'/@/package/.prettierrc',
@@ -163,7 +163,7 @@ describe('Application Factory', () => {
163163
'/@/package/src/main.ts',
164164
'/@/package/test/app.e2e-spec.ts',
165165
'/@/package/test/jest-e2e.json',
166-
]);
166+
].sort());
167167

168168
expect(
169169
JSON.parse(tree.readContent('/@/package/package.json')),
@@ -179,7 +179,7 @@ describe('Application Factory', () => {
179179
.runSchematicAsync('application', options)
180180
.toPromise();
181181
const files: string[] = tree.files;
182-
expect(files).toEqual([
182+
expect(files.sort()).toEqual([
183183
'/.eslintrc.js',
184184
'/.gitignore',
185185
'/.prettierrc',
@@ -195,7 +195,7 @@ describe('Application Factory', () => {
195195
'/src/main.ts',
196196
'/test/app.e2e-spec.ts',
197197
'/test/jest-e2e.json',
198-
]);
198+
].sort());
199199

200200
expect(JSON.parse(tree.readContent('/package.json'))).toMatchObject({
201201
name: path.basename(process.cwd()),
@@ -211,7 +211,7 @@ describe('Application Factory', () => {
211211
.runSchematicAsync('application', options)
212212
.toPromise();
213213
const files: string[] = tree.files;
214-
expect(files).toEqual([
214+
expect(files.sort()).toEqual([
215215
'/@scope/package/.eslintrc.js',
216216
'/@scope/package/.gitignore',
217217
'/@scope/package/.prettierrc',
@@ -227,7 +227,7 @@ describe('Application Factory', () => {
227227
'/@scope/package/src/main.ts',
228228
'/@scope/package/test/app.e2e-spec.ts',
229229
'/@scope/package/test/jest-e2e.json',
230-
]);
230+
].sort());
231231

232232
expect(
233233
JSON.parse(tree.readContent('/@scope/package/package.json')),
@@ -243,7 +243,7 @@ describe('Application Factory', () => {
243243
.runSchematicAsync('application', options)
244244
.toPromise();
245245
const files: string[] = tree.files;
246-
expect(files).toEqual([
246+
expect(files.sort()).toEqual([
247247
'/@-/package/.eslintrc.js',
248248
'/@-/package/.gitignore',
249249
'/@-/package/.prettierrc',
@@ -259,7 +259,7 @@ describe('Application Factory', () => {
259259
'/@-/package/src/main.ts',
260260
'/@-/package/test/app.e2e-spec.ts',
261261
'/@-/package/test/jest-e2e.json',
262-
]);
262+
].sort());
263263

264264
expect(
265265
JSON.parse(tree.readContent('/@-/package/package.json')),
@@ -278,7 +278,7 @@ describe('Application Factory', () => {
278278
.runSchematicAsync('application', options)
279279
.toPromise();
280280
const files: string[] = tree.files;
281-
expect(files).toEqual([
281+
expect(files.sort()).toEqual([
282282
'/123/.eslintrc.js',
283283
'/123/.gitignore',
284284
'/123/.prettierrc',
@@ -294,7 +294,7 @@ describe('Application Factory', () => {
294294
'/123/src/main.ts',
295295
'/123/test/app.e2e-spec.ts',
296296
'/123/test/jest-e2e.json',
297-
]);
297+
].sort());
298298

299299
expect(
300300
JSON.parse(tree.readContent('/123/package.json')),
@@ -311,7 +311,7 @@ describe('Application Factory', () => {
311311
.runSchematicAsync('application', options)
312312
.toPromise();
313313
const files: string[] = tree.files;
314-
expect(files).toEqual([
314+
expect(files.sort()).toEqual([
315315
'/project/.babelrc',
316316
'/project/.gitignore',
317317
'/project/.prettierrc',
@@ -328,7 +328,7 @@ describe('Application Factory', () => {
328328
'/project/src/main.js',
329329
'/project/test/app.e2e-spec.js',
330330
'/project/test/jest-e2e.json',
331-
]);
331+
].sort());
332332

333333
expect(JSON.parse(tree.readContent('/project/package.json'))).toMatchObject(
334334
{
@@ -345,7 +345,7 @@ describe('Application Factory', () => {
345345
.runSchematicAsync('application', options)
346346
.toPromise();
347347
const files: string[] = tree.files;
348-
expect(files).toEqual([
348+
expect(files.sort()).toEqual([
349349
'/app/.eslintrc.js',
350350
'/app/.gitignore',
351351
'/app/.prettierrc',
@@ -361,10 +361,94 @@ describe('Application Factory', () => {
361361
'/app/src/main.ts',
362362
'/app/test/app.e2e-spec.ts',
363363
'/app/test/jest-e2e.json',
364-
]);
364+
].sort());
365365

366366
expect(JSON.parse(tree.readContent('/app/package.json'))).toMatchObject({
367367
name: 'project',
368368
});
369369
});
370+
it('should not create a spec file', async () => {
371+
const options: ApplicationOptions = {
372+
name: 'project',
373+
spec: false,
374+
language: 'js',
375+
};
376+
const tree: UnitTestTree = await runner
377+
.runSchematicAsync('application', options)
378+
.toPromise();
379+
const files: string[] = tree.files;
380+
expect(files.sort()).toEqual([
381+
'/project/.babelrc',
382+
'/project/.gitignore',
383+
'/project/.prettierrc',
384+
'/project/README.md',
385+
'/project/index.js',
386+
'/project/jsconfig.json',
387+
'/project/nest-cli.json',
388+
'/project/nodemon.json',
389+
'/project/package.json',
390+
'/project/src/app.controller.js',
391+
'/project/src/app.module.js',
392+
'/project/src/app.service.js',
393+
'/project/src/main.js',
394+
'/project/test/jest-e2e.json',
395+
].sort());
396+
});
397+
it('should create a spec file', async () => {
398+
const options: ApplicationOptions = {
399+
name: 'project',
400+
spec: true,
401+
language: 'js',
402+
};
403+
const tree: UnitTestTree = await runner
404+
.runSchematicAsync('application', options)
405+
.toPromise();
406+
const files: string[] = tree.files;
407+
expect(files.sort()).toEqual([
408+
'/project/.babelrc',
409+
'/project/.gitignore',
410+
'/project/.prettierrc',
411+
'/project/README.md',
412+
'/project/index.js',
413+
'/project/jsconfig.json',
414+
'/project/nest-cli.json',
415+
'/project/nodemon.json',
416+
'/project/package.json',
417+
'/project/src/app.controller.js',
418+
'/project/src/app.controller.spec.js',
419+
'/project/src/app.module.js',
420+
'/project/src/app.service.js',
421+
'/project/src/main.js',
422+
'/project/test/app.e2e-spec.js',
423+
'/project/test/jest-e2e.json',
424+
].sort());
425+
});
426+
it('should create a spec file with custom file suffix', async () => {
427+
const options: ApplicationOptions = {
428+
name: 'project',
429+
spec: true,
430+
specFileSuffix: 'test',
431+
};
432+
const tree: UnitTestTree = await runner
433+
.runSchematicAsync('application', options)
434+
.toPromise();
435+
const files: string[] = tree.files;
436+
expect(files.sort()).toEqual([
437+
'/project/.eslintrc.js',
438+
'/project/.gitignore',
439+
'/project/.prettierrc',
440+
'/project/README.md',
441+
'/project/nest-cli.json',
442+
'/project/package.json',
443+
'/project/tsconfig.build.json',
444+
'/project/tsconfig.json',
445+
'/project/src/app.controller.test.ts',
446+
'/project/src/app.controller.ts',
447+
'/project/src/app.module.ts',
448+
'/project/src/app.service.ts',
449+
'/project/src/main.ts',
450+
'/project/test/app.e2e-test.ts',
451+
'/project/test/jest-e2e.json',
452+
].sort());
453+
});
370454
});

src/lib/application/application.factory.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { join, Path, strings } from '@angular-devkit/core';
22
import {
33
apply,
4+
filter,
45
mergeWith,
56
move,
7+
noop,
68
Rule,
79
Source,
810
template,
@@ -40,6 +42,9 @@ function transform(options: ApplicationOptions): ApplicationOptions {
4042
target.language = !!target.language ? target.language : DEFAULT_LANGUAGE;
4143
target.name = resolvePackageName(target.name.toString());
4244
target.version = !!target.version ? target.version : DEFAULT_VERSION;
45+
target.specFileSuffix = normalizeToKebabOrSnakeCase(
46+
options.specFileSuffix || 'spec',
47+
);
4348

4449
target.packageManager =
4550
!target.packageManager || target.packageManager === 'undefined'
@@ -76,6 +81,14 @@ function resolvePackageName(path: string) {
7681

7782
function generate(options: ApplicationOptions, path: string): Source {
7883
return apply(url(join('./files' as Path, options.language)), [
84+
options.spec ? noop() : filter((path) => !path.endsWith('__specFileSuffix__.ts')),
85+
options.spec
86+
? noop()
87+
: filter((path) => {
88+
const languageExtension = options.language || 'ts';
89+
const suffix = `__specFileSuffix__.${languageExtension}`;
90+
return !path.endsWith(suffix);
91+
}),
7992
template({
8093
...strings,
8194
...options,

src/lib/application/application.schema.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ export interface ApplicationOptions {
4040
* Nest included development dependencies (comma separated values).
4141
*/
4242
devDependencies?: string;
43+
/**
44+
* Specifies if a spec file is generated.
45+
*/
46+
spec?: boolean;
47+
/**
48+
* Specifies the file suffix of spec files.
49+
* @default "spec"
50+
*/
51+
specFileSuffix?: string;
4352
}

src/lib/application/schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
"devDependencies": {
5656
"type": "string",
5757
"description": "Nest application development dependencies."
58+
},
59+
"spec": {
60+
"type": "boolean",
61+
"default": true,
62+
"description": "Specifies if a spec file is generated."
63+
},
64+
"specFileSuffix": {
65+
"type": "string",
66+
"default": "spec",
67+
"description": "Specifies the file suffix of spec files."
5868
}
5969
},
6070
"required": ["name"]

0 commit comments

Comments
 (0)