Skip to content

Commit

Permalink
refactor(middleware.factory): keep underscores 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 591a850 commit 5635906
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
68 changes: 53 additions & 15 deletions src/lib/middleware/middleware.factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { MiddlewareOptions } from './middleware.schema';

Expand All @@ -12,10 +15,12 @@ describe('Middleware Factory', () => {
name: 'foo',
flat: false,
};
const tree: UnitTestTree = await runner.runSchematicAsync('middleware', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo/foo.middleware.ts'),
files.find((filename) => filename === '/foo/foo.middleware.ts'),
).not.toBeUndefined();
expect(tree.readContent('/foo/foo.middleware.ts')).toEqual(
"import { Injectable, NestMiddleware } from '@nestjs/common';\n" +
Expand All @@ -33,10 +38,12 @@ describe('Middleware Factory', () => {
name: 'bar/foo',
flat: false,
};
const tree: UnitTestTree = await runner.runSchematicAsync('middleware', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar/foo/foo.middleware.ts'),
files.find((filename) => filename === '/bar/foo/foo.middleware.ts'),
).not.toBeUndefined();
expect(tree.readContent('/bar/foo/foo.middleware.ts')).toEqual(
"import { Injectable, NestMiddleware } from '@nestjs/common';\n" +
Expand All @@ -55,10 +62,12 @@ describe('Middleware Factory', () => {
path: 'baz',
flat: false,
};
const tree: UnitTestTree = await runner.runSchematicAsync('middleware', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/baz/foo/foo.middleware.ts'),
files.find((filename) => filename === '/baz/foo/foo.middleware.ts'),
).not.toBeUndefined();
expect(tree.readContent('/baz/foo/foo.middleware.ts')).toEqual(
"import { Injectable, NestMiddleware } from '@nestjs/common';\n" +
Expand All @@ -71,15 +80,17 @@ describe('Middleware Factory', () => {
'}\n',
);
});
it('should manage name to dasherize', async () => {
it('should manage name to normalize', async () => {
const options: MiddlewareOptions = {
name: 'fooBar',
flat: false,
};
const tree: UnitTestTree = await runner.runSchematicAsync('middleware', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo-bar/foo-bar.middleware.ts'),
files.find((filename) => filename === '/foo-bar/foo-bar.middleware.ts'),
).not.toBeUndefined();
expect(tree.readContent('/foo-bar/foo-bar.middleware.ts')).toEqual(
"import { Injectable, NestMiddleware } from '@nestjs/common';\n" +
Expand All @@ -92,15 +103,17 @@ describe('Middleware Factory', () => {
'}\n',
);
});
it('should manage path to dasherize', async () => {
it('should manage path to normalize', async () => {
const options: MiddlewareOptions = {
name: 'barBaz/foo',
flat: false,
};
const tree: UnitTestTree = await runner.runSchematicAsync('middleware', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar-baz/foo/foo.middleware.ts'),
files.find((filename) => filename === '/bar-baz/foo/foo.middleware.ts'),
).not.toBeUndefined();
expect(tree.readContent('/bar-baz/foo/foo.middleware.ts')).toEqual(
"import { Injectable, NestMiddleware } from '@nestjs/common';\n" +
Expand All @@ -113,16 +126,41 @@ describe('Middleware Factory', () => {
'}\n',
);
});
it('should keep underscores in path and file name', async () => {
const options: MiddlewareOptions = {
name: '_bar/_foo',
flat: false,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find((filename) => filename === '/_bar/_foo/_foo.middleware.ts'),
).not.toBeUndefined();
expect(tree.readContent('/_bar/_foo/_foo.middleware.ts')).toEqual(
"import { Injectable, NestMiddleware } from '@nestjs/common';\n" +
'\n' +
'@Injectable()\n' +
'export class FooMiddleware implements NestMiddleware {\n' +
' use(req: any, res: any, next: () => void) {\n' +
' next();\n' +
' }\n' +
'}\n',
);
});
it('should manage javascript file', async () => {
const options: MiddlewareOptions = {
name: 'foo',
language: 'js',
flat: false,
};
const tree: UnitTestTree = await runner.runSchematicAsync('middleware', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('middleware', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo/foo.middleware.js'),
files.find((filename) => filename === '/foo/foo.middleware.js'),
).not.toBeUndefined();
expect(tree.readContent('/foo/foo.middleware.js')).toEqual(
"import { Injectable } from '@nestjs/common';\n" +
Expand Down
7 changes: 4 additions & 3 deletions src/lib/middleware/middleware.factory.ts
Original file line number Diff line number Diff line change
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 { MiddlewareOptions } from './middleware.schema';
Expand All @@ -28,8 +29,8 @@ function transform(options: MiddlewareOptions): MiddlewareOptions {
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: MiddlewareOptions): MiddlewareOptions {
function generate(options: MiddlewareOptions): 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 5635906

Please sign in to comment.