Skip to content

Commit

Permalink
refactor(module.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 5635906 commit 86e1981
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
83 changes: 61 additions & 22 deletions src/lib/module/module.factory.test.ts
Expand Up @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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',
};
Expand Down
5 changes: 3 additions & 2 deletions src/lib/module/module.factory.ts
Expand Up @@ -11,6 +11,7 @@ import {
Tree,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import {
DeclarationOptions,
ModuleDeclarator,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 86e1981

Please sign in to comment.