Skip to content

Commit

Permalink
Merge pull request #1586 from s5s5/s5s5/application-add-spec-option
Browse files Browse the repository at this point in the history
feat(application): add spec option
  • Loading branch information
kamilmysliwiec authored Oct 30, 2023
2 parents 61cff89 + 1c43881 commit ba4d987
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 22 deletions.
128 changes: 106 additions & 22 deletions src/lib/application/application.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/project/.eslintrc.js',
'/project/.gitignore',
'/project/.prettierrc',
Expand All @@ -35,7 +35,7 @@ describe('Application Factory', () => {
'/project/src/main.ts',
'/project/test/app.e2e-spec.ts',
'/project/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/project/package.json')),
Expand All @@ -51,7 +51,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
`/project.foo.bar/.eslintrc.js`,
`/project.foo.bar/.gitignore`,
`/project.foo.bar/.prettierrc`,
Expand All @@ -67,7 +67,7 @@ describe('Application Factory', () => {
`/project.foo.bar/src/main.ts`,
`/project.foo.bar/test/app.e2e-spec.ts`,
`/project.foo.bar/test/jest-e2e.json`,
]);
].sort());

expect(
JSON.parse(tree.readContent('/project.foo.bar/package.json')),
Expand All @@ -83,7 +83,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/awesome-project/.eslintrc.js',
'/awesome-project/.gitignore',
'/awesome-project/.prettierrc',
Expand All @@ -99,7 +99,7 @@ describe('Application Factory', () => {
'/awesome-project/src/main.ts',
'/awesome-project/test/app.e2e-spec.ts',
'/awesome-project/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/awesome-project/package.json')),
Expand All @@ -115,7 +115,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/_awesome-project/.eslintrc.js',
'/_awesome-project/.gitignore',
'/_awesome-project/.prettierrc',
Expand All @@ -131,7 +131,7 @@ describe('Application Factory', () => {
'/_awesome-project/src/main.ts',
'/_awesome-project/test/app.e2e-spec.ts',
'/_awesome-project/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/_awesome-project/package.json')),
Expand All @@ -147,7 +147,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/@/package/.eslintrc.js',
'/@/package/.gitignore',
'/@/package/.prettierrc',
Expand All @@ -163,7 +163,7 @@ describe('Application Factory', () => {
'/@/package/src/main.ts',
'/@/package/test/app.e2e-spec.ts',
'/@/package/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/@/package/package.json')),
Expand All @@ -179,7 +179,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/.eslintrc.js',
'/.gitignore',
'/.prettierrc',
Expand All @@ -195,7 +195,7 @@ describe('Application Factory', () => {
'/src/main.ts',
'/test/app.e2e-spec.ts',
'/test/jest-e2e.json',
]);
].sort());

expect(JSON.parse(tree.readContent('/package.json'))).toMatchObject({
name: path.basename(process.cwd()),
Expand All @@ -211,7 +211,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/@scope/package/.eslintrc.js',
'/@scope/package/.gitignore',
'/@scope/package/.prettierrc',
Expand All @@ -227,7 +227,7 @@ describe('Application Factory', () => {
'/@scope/package/src/main.ts',
'/@scope/package/test/app.e2e-spec.ts',
'/@scope/package/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/@scope/package/package.json')),
Expand All @@ -243,7 +243,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/@-/package/.eslintrc.js',
'/@-/package/.gitignore',
'/@-/package/.prettierrc',
Expand All @@ -259,7 +259,7 @@ describe('Application Factory', () => {
'/@-/package/src/main.ts',
'/@-/package/test/app.e2e-spec.ts',
'/@-/package/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/@-/package/package.json')),
Expand All @@ -278,7 +278,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/123/.eslintrc.js',
'/123/.gitignore',
'/123/.prettierrc',
Expand All @@ -294,7 +294,7 @@ describe('Application Factory', () => {
'/123/src/main.ts',
'/123/test/app.e2e-spec.ts',
'/123/test/jest-e2e.json',
]);
].sort());

expect(
JSON.parse(tree.readContent('/123/package.json')),
Expand All @@ -311,7 +311,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/project/.babelrc',
'/project/.gitignore',
'/project/.prettierrc',
Expand All @@ -328,7 +328,7 @@ describe('Application Factory', () => {
'/project/src/main.js',
'/project/test/app.e2e-spec.js',
'/project/test/jest-e2e.json',
]);
].sort());

expect(JSON.parse(tree.readContent('/project/package.json'))).toMatchObject(
{
Expand All @@ -345,7 +345,7 @@ describe('Application Factory', () => {
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
expect(files.sort()).toEqual([
'/app/.eslintrc.js',
'/app/.gitignore',
'/app/.prettierrc',
Expand All @@ -361,10 +361,94 @@ describe('Application Factory', () => {
'/app/src/main.ts',
'/app/test/app.e2e-spec.ts',
'/app/test/jest-e2e.json',
]);
].sort());

expect(JSON.parse(tree.readContent('/app/package.json'))).toMatchObject({
name: 'project',
});
});
it('should not create a spec file', async () => {
const options: ApplicationOptions = {
name: 'project',
spec: false,
language: 'js',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/project/.babelrc',
'/project/.gitignore',
'/project/.prettierrc',
'/project/README.md',
'/project/index.js',
'/project/jsconfig.json',
'/project/nest-cli.json',
'/project/nodemon.json',
'/project/package.json',
'/project/src/app.controller.js',
'/project/src/app.module.js',
'/project/src/app.service.js',
'/project/src/main.js',
'/project/test/jest-e2e.json',
].sort());
});
it('should create a spec file', async () => {
const options: ApplicationOptions = {
name: 'project',
spec: true,
language: 'js',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/project/.babelrc',
'/project/.gitignore',
'/project/.prettierrc',
'/project/README.md',
'/project/index.js',
'/project/jsconfig.json',
'/project/nest-cli.json',
'/project/nodemon.json',
'/project/package.json',
'/project/src/app.controller.js',
'/project/src/app.controller.spec.js',
'/project/src/app.module.js',
'/project/src/app.service.js',
'/project/src/main.js',
'/project/test/app.e2e-spec.js',
'/project/test/jest-e2e.json',
].sort());
});
it('should create a spec file with custom file suffix', async () => {
const options: ApplicationOptions = {
name: 'project',
spec: true,
specFileSuffix: 'test',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/project/.eslintrc.js',
'/project/.gitignore',
'/project/.prettierrc',
'/project/README.md',
'/project/nest-cli.json',
'/project/package.json',
'/project/tsconfig.build.json',
'/project/tsconfig.json',
'/project/src/app.controller.test.ts',
'/project/src/app.controller.ts',
'/project/src/app.module.ts',
'/project/src/app.service.ts',
'/project/src/main.ts',
'/project/test/app.e2e-test.ts',
'/project/test/jest-e2e.json',
].sort());
});
});
13 changes: 13 additions & 0 deletions src/lib/application/application.factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { join, Path, strings } from '@angular-devkit/core';
import {
apply,
filter,
mergeWith,
move,
noop,
Rule,
Source,
template,
Expand Down Expand Up @@ -40,6 +42,9 @@ function transform(options: ApplicationOptions): ApplicationOptions {
target.language = !!target.language ? target.language : DEFAULT_LANGUAGE;
target.name = resolvePackageName(target.name.toString());
target.version = !!target.version ? target.version : DEFAULT_VERSION;
target.specFileSuffix = normalizeToKebabOrSnakeCase(
options.specFileSuffix || 'spec',
);

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

function generate(options: ApplicationOptions, path: string): Source {
return apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('__specFileSuffix__.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix);
}),
template({
...strings,
...options,
Expand Down
9 changes: 9 additions & 0 deletions src/lib/application/application.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ export interface ApplicationOptions {
* Nest included development dependencies (comma separated values).
*/
devDependencies?: string;
/**
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
* @default "spec"
*/
specFileSuffix?: string;
}
10 changes: 10 additions & 0 deletions src/lib/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
"devDependencies": {
"type": "string",
"description": "Nest application development dependencies."
},
"spec": {
"type": "boolean",
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
}
},
"required": ["name"]
Expand Down

0 comments on commit ba4d987

Please sign in to comment.