diff --git a/src/lib/module/module.factory.test.ts b/src/lib/module/module.factory.test.ts index b4fa6100a..90d06ab93 100644 --- a/src/lib/module/module.factory.test.ts +++ b/src/lib/module/module.factory.test.ts @@ -17,10 +17,12 @@ describe('Module Factory', () => { name: 'foo', skipImport: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/foo/foo.module.ts'), + files.find((filename) => filename === '/foo/foo.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('/foo/foo.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -34,10 +36,12 @@ describe('Module Factory', () => { name: 'bar/foo', skipImport: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/bar/foo/foo.module.ts'), + files.find((filename) => filename === '/bar/foo/foo.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('/bar/foo/foo.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -52,10 +56,12 @@ describe('Module Factory', () => { path: 'bar', skipImport: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/bar/foo/foo.module.ts'), + files.find((filename) => filename === '/bar/foo/foo.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('/bar/foo/foo.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -64,15 +70,17 @@ describe('Module Factory', () => { 'export class FooModule {}\n', ); }); - it('should manage name to dasherize', async () => { + it('should manage name to normalize', async () => { const options: ModuleOptions = { name: 'fooBar', skipImport: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/foo-bar/foo-bar.module.ts'), + files.find((filename) => filename === '/foo-bar/foo-bar.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('/foo-bar/foo-bar.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -87,10 +95,12 @@ describe('Module Factory', () => { path: 'bar', skipImport: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/bar/foo/foo.module.ts'), + files.find((filename) => filename === '/bar/foo/foo.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('/bar/foo/foo.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -105,10 +115,12 @@ describe('Module Factory', () => { skipImport: true, flat: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/foo-bar.module.ts'), + files.find((filename) => filename === '/foo-bar.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('foo-bar.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -117,15 +129,17 @@ describe('Module Factory', () => { 'export class FooBarModule {}\n', ); }); - it('should manage path to dasherize', async () => { + it('should manage path to normalize', async () => { const options: ModuleOptions = { name: 'barBaz/foo', skipImport: true, }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/bar-baz/foo/foo.module.ts'), + files.find((filename) => filename === '/bar-baz/foo/foo.module.ts'), ).not.toBeUndefined(); expect(tree.readContent('/bar-baz/foo/foo.module.ts')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -134,16 +148,37 @@ describe('Module Factory', () => { 'export class FooModule {}\n', ); }); + it('should keep underscores in path and file name', async () => { + const options: ModuleOptions = { + name: '_bar/_foo', + skipImport: true, + }; + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); + const files: string[] = tree.files; + expect( + files.find((filename) => filename === '/_bar/_foo/_foo.module.ts'), + ).not.toBeUndefined(); + expect(tree.readContent('/_bar/_foo/_foo.module.ts')).toEqual( + "import { Module } from '@nestjs/common';\n" + + '\n' + + '@Module({})\n' + + 'export class FooModule {}\n', + ); + }); it('should manage javascript file', async () => { const options: ModuleOptions = { name: 'foo', skipImport: true, language: 'js', }; - const tree: UnitTestTree = await runner.runSchematicAsync('module', options).toPromise(); + const tree: UnitTestTree = await runner + .runSchematicAsync('module', options) + .toPromise(); const files: string[] = tree.files; expect( - files.find(filename => filename === '/foo/foo.module.js'), + files.find((filename) => filename === '/foo/foo.module.js'), ).not.toBeUndefined(); expect(tree.readContent('/foo/foo.module.js')).toEqual( "import { Module } from '@nestjs/common';\n" + @@ -156,11 +191,13 @@ describe('Module Factory', () => { const app: ApplicationOptions = { name: '', }; - let tree: UnitTestTree = await runner.runSchematicAsync('application', app).toPromise(); + let tree: UnitTestTree = await runner + .runSchematicAsync('application', app) + .toPromise(); const options: ModuleOptions = { name: 'foo', }; - tree = await runner.runSchematicAsync('module', options, tree).toPromise() + tree = await runner.runSchematicAsync('module', options, tree).toPromise(); expect(tree.readContent(normalize('/src/app.module.ts'))).toEqual( "import { Module } from '@nestjs/common';\n" + "import { AppController } from './app.controller';\n" + @@ -179,11 +216,13 @@ describe('Module Factory', () => { const app: ApplicationOptions = { name: '', }; - let tree: UnitTestTree = await runner.runSchematicAsync('application', app).toPromise(); + let tree: UnitTestTree = await runner + .runSchematicAsync('application', app) + .toPromise(); const module: ModuleOptions = { name: 'bar', }; - tree = await runner.runSchematicAsync('module', module, tree).toPromise();; + tree = await runner.runSchematicAsync('module', module, tree).toPromise(); const options: ModuleOptions = { name: 'bar/foo', }; diff --git a/src/lib/module/module.factory.ts b/src/lib/module/module.factory.ts index 05ac6ce50..d1d14735d 100644 --- a/src/lib/module/module.factory.ts +++ b/src/lib/module/module.factory.ts @@ -11,6 +11,7 @@ import { Tree, url, } from '@angular-devkit/schematics'; +import { normalizeToKebabOrSnakeCase } from '../../utils/formatting'; import { DeclarationOptions, ModuleDeclarator, @@ -39,9 +40,9 @@ function transform(source: ModuleOptions): ModuleOptions { target.type = 'module'; const location: Location = new NameParser().parse(target); - target.name = strings.dasherize(location.name); + target.name = normalizeToKebabOrSnakeCase(location.name); + target.path = normalizeToKebabOrSnakeCase(location.path); target.language = target.language !== undefined ? target.language : 'ts'; - target.path = strings.dasherize(location.path); target.path = target.flat ? target.path