Skip to content

Commit

Permalink
refactor(interceptor): factory keeps underscore in path and file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlatin Stanimirov committed Mar 6, 2022
1 parent c4e1bc3 commit 1381305
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
64 changes: 50 additions & 14 deletions src/lib/interceptor/interceptor.factory.test.ts
Expand Up @@ -15,10 +15,12 @@ describe('Interceptor Factory', () => {
const options: InterceptorOptions = {
name: 'foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interceptor', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo.interceptor.ts'),
files.find((filename) => filename === '/foo.interceptor.ts'),
).toBeDefined();
expect(tree.readContent('/foo.interceptor.ts')).toEqual(
"import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';\n" +
Expand All @@ -37,10 +39,12 @@ describe('Interceptor Factory', () => {
const options: InterceptorOptions = {
name: 'bar/foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interceptor', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar/foo.interceptor.ts'),
files.find((filename) => filename === '/bar/foo.interceptor.ts'),
).toBeDefined();
expect(tree.readContent('/bar/foo.interceptor.ts')).toEqual(
"import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';\n" +
Expand All @@ -60,10 +64,12 @@ describe('Interceptor Factory', () => {
name: 'foo',
path: 'baz',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interceptor', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/baz/foo.interceptor.ts'),
files.find((filename) => filename === '/baz/foo.interceptor.ts'),
).toBeDefined();
expect(tree.readContent('/baz/foo.interceptor.ts')).toEqual(
"import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';\n" +
Expand All @@ -78,14 +84,16 @@ describe('Interceptor Factory', () => {
);
});

it('should manage name to dasherize', async () => {
it('should manage name to normalize', async () => {
const options: InterceptorOptions = {
name: 'fooBar',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interceptor', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo-bar.interceptor.ts'),
files.find((filename) => filename === '/foo-bar.interceptor.ts'),
).toBeDefined();
expect(tree.readContent('/foo-bar.interceptor.ts')).toEqual(
"import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';\n" +
Expand All @@ -100,14 +108,40 @@ describe('Interceptor Factory', () => {
);
});

it('should manage path to dasherize', async () => {
it('should keep underscores in path and file name', async () => {
const options: InterceptorOptions = {
name: '_bar/_foo',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find((filename) => filename === '/_bar/_foo.interceptor.ts'),
).toBeDefined();
expect(tree.readContent('/_bar/_foo.interceptor.ts')).toEqual(
"import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';\n" +
"import { Observable } from 'rxjs';\n" +
'\n' +
'@Injectable()\n' +
'export class FooInterceptor implements NestInterceptor {\n' +
' intercept(context: ExecutionContext, next: CallHandler): Observable<any> {\n' +
' return next.handle();\n' +
' }\n' +
'}\n',
);
});

it('should manage path to normalize', async () => {
const options: InterceptorOptions = {
name: 'barBaz/foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interceptor', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar-baz/foo.interceptor.ts'),
files.find((filename) => filename === '/bar-baz/foo.interceptor.ts'),
).toBeDefined();
expect(tree.readContent('/bar-baz/foo.interceptor.ts')).toEqual(
"import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';\n" +
Expand All @@ -127,10 +161,12 @@ describe('Interceptor Factory', () => {
name: 'foo',
language: 'js',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interceptor', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interceptor', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo.interceptor.js'),
files.find((filename) => filename === '/foo.interceptor.js'),
).toBeDefined();
expect(tree.readContent('/foo.interceptor.js')).toEqual(
"import { Injectable } from '@nestjs/common';\n" +
Expand Down
7 changes: 4 additions & 3 deletions src/lib/interceptor/interceptor.factory.ts
Expand Up @@ -13,6 +13,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { InterceptorOptions } from './interceptor.schema';
Expand All @@ -28,8 +29,8 @@ function transform(options: InterceptorOptions): InterceptorOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = strings.dasherize(location.name);
target.path = strings.dasherize(location.path);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.language = target.language !== undefined ? target.language : 'ts';

target.path = target.flat
Expand All @@ -41,7 +42,7 @@ function transform(options: InterceptorOptions): InterceptorOptions {
function generate(options: InterceptorOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter(path => !path.endsWith('.spec.ts')),
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
template({
...strings,
...options,
Expand Down

0 comments on commit 1381305

Please sign in to comment.