Skip to content

Commit 74f87be

Browse files
authored
Merge pull request #295 from monicalopezgris/fixconfigModule
Fixconfig module
2 parents c1c9dbd + 6032728 commit 74f87be

23 files changed

Lines changed: 1649 additions & 82 deletions

packages/schematics/src/lib/allinone/all-in-one.factory_spec.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/schematics/src/lib/auth-jwt/auth.factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function authJWT(options: IAuthJWTOptions): Rule {
9999
if (!options.path) {
100100
options.path = '.';
101101
}
102+
options.path = strings.dasherize(options.path);
102103
const config: boolean = existsConfigModule(tree, options.path);
103104
return chain([
104105
mergeWith(
Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1-
// import { Tree } from '@angular-devkit/schematics';
2-
// import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
3-
// import * as path from 'path';
1+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
2+
import * as path from 'path';
43

5-
// const collectionPath = path.join(__dirname, '../collection.json');
6-
7-
// describe('schematics', () => {
8-
// it('works', () => {
9-
// const runner = new SchematicTestRunner('schematics', collectionPath);
10-
// const tree = runner.runSchematic('schematics', {}, Tree.empty());
11-
12-
// expect(tree.files).toEqual([]);
13-
// });
14-
// });
15-
// //
4+
describe('Auth Factory', () => {
5+
const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json'));
6+
it('should set on module app', async () => {
7+
const optionsApp: object = {
8+
name: 'path',
9+
};
10+
const optionsModule: object = {
11+
path: 'path',
12+
};
13+
let app;
14+
app = await runner.runSchematicAsync('application', optionsApp);
15+
app.subscribe(async tree => {
16+
app = await runner.runSchematicAsync('auth-jwt', optionsModule, tree);
17+
app.subscribe(tree => {
18+
const files: string[] = tree.files;
19+
expect(files.find(filename => filename === '/path/src/app/core/core.module.ts')).toBeDefined();
20+
expect(tree.readContent('/path/src/app/core/core.module.ts')).toEqual(
21+
"import { Global, Module } from '@nestjs/common';\n" +
22+
"import { ClassSerializerInterceptor } from '@devon4node/common/serializer';\n" +
23+
"import { APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core';\n" +
24+
"import { WinstonLogger } from '../shared/logger/winston.logger';\n" +
25+
"import { BusinessLogicFilter } from '../shared/filters/business-logic.filter';\n" +
26+
"import { AuthModule } from './auth/auth.module';\n" +
27+
"import { UserModule } from './user/user.module';\n" +
28+
'\n' +
29+
'@Global()\n' +
30+
'@Module({\n' +
31+
' imports: [UserModule, AuthModule],\n' +
32+
' controllers: [],\n' +
33+
' providers: [\n' +
34+
' { provide: APP_FILTER, useClass: BusinessLogicFilter },\n' +
35+
' { provide: APP_INTERCEPTOR, useClass: ClassSerializerInterceptor },\n' +
36+
' WinstonLogger,\n' +
37+
' ],\n' +
38+
' exports: [UserModule, AuthModule, WinstonLogger],\n' +
39+
'})\n' +
40+
'export class CoreModule {}\n',
41+
);
42+
});
43+
});
44+
});
45+
});

packages/schematics/src/lib/config-module/config-module.factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export function configModule(options: IConfigOptions): Rule {
6868
if (!options.path) {
6969
options.path = '.';
7070
}
71+
options.path = strings.dasherize(options.path);
7172

7273
return (host: Tree): Rule => {
7374
options.name = JSON.parse(host.read((options.path || '.') + '/package.json')!.toString('utf-8')).name;
Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,87 @@
1-
import { Tree } from '@angular-devkit/schematics';
21
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
32
import * as path from 'path';
43

5-
const collectionPath = path.join(__dirname, '../collection.json');
6-
7-
describe('schematics', () => {
8-
it('works', done => {
9-
const runner = new SchematicTestRunner('schematics', collectionPath);
10-
const obsTree = runner.runSchematicAsync('schematics', {}, Tree.empty());
11-
12-
obsTree.subscribe(tree => {
13-
expect(tree.files).toEqual([]);
14-
done();
4+
describe('Config-module', () => {
5+
const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json'));
6+
it('should manage path', async () => {
7+
const optionsApp: object = {
8+
name: 'foo',
9+
};
10+
const optionsModule: object = {
11+
path: 'foo',
12+
};
13+
let app;
14+
app = await runner.runSchematicAsync('application', optionsApp);
15+
app.subscribe(tree => {
16+
app = runner.runSchematicAsync('config-module', optionsModule, tree);
17+
app.subscribe(tree => {
18+
const files: string[] = tree.files;
19+
expect(files.find(filename => filename === '/foo/src/main.ts')).toBeDefined();
20+
expect(tree.readContent('/foo/src/main.ts')).toEqual(
21+
"import { NestFactory } from '@nestjs/core';\n" +
22+
"import { AppModule } from './app/app.module';\n" +
23+
"import { WinstonLogger } from './app/shared/logger/winston.logger';\n" +
24+
"import { ValidationPipe } from '@nestjs/common';\n" +
25+
"import { ConfigService } from '@devon4node/config';\n" +
26+
'\n' +
27+
'async function bootstrap(): Promise<void> {\n' +
28+
' const app = await NestFactory.create(AppModule, { logger: new WinstonLogger() });\n' +
29+
' const configModule = app.get(ConfigService);\n' +
30+
' app.useGlobalPipes(\n' +
31+
' new ValidationPipe({\n' +
32+
' transform: true,\n' +
33+
' }),\n' +
34+
' );\n' +
35+
' app.setGlobalPrefix(configModule.values.globalPrefix);\n' +
36+
' await app.listen(configModule.values.port);\n' +
37+
'}\n' +
38+
'bootstrap();\n',
39+
);
40+
});
41+
});
42+
});
43+
it('should manage declaration in core module', async () => {
44+
const optionsApp: object = {
45+
name: '',
46+
};
47+
const optionsModule: object = {
48+
path: '',
49+
};
50+
let app;
51+
app = await runner.runSchematicAsync('application', optionsApp);
52+
app.subscribe(tree => {
53+
app = runner.runSchematicAsync('config-module', optionsModule, tree);
54+
app.subscribe(tree => {
55+
const files: string[] = tree.files;
56+
expect(files.find(filename => filename === '/src/app/core/core.module.ts')).toBeDefined();
57+
expect(tree.readContent('/src/app/core/core.module.ts')).toEqual(
58+
"import { Global, Module } from '@nestjs/common';\n" +
59+
"import { ClassSerializerInterceptor } from '@devon4node/common/serializer';\n" +
60+
"import { APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core';\n" +
61+
"import { WinstonLogger } from '../shared/logger/winston.logger';\n" +
62+
"import { BusinessLogicFilter } from '../shared/filters/business-logic.filter';\n" +
63+
"import { ConfigModule } from '@devon4node/config';\n" +
64+
"import { Config } from '../shared/model/config/config.model';\n" +
65+
'\n' +
66+
'@Global()\n' +
67+
'@Module({\n' +
68+
' imports: [\n' +
69+
' ConfigModule.forRoot({\n' +
70+
" configPrefix: 'devon4node',\n" +
71+
' configType: Config,\n' +
72+
' }),\n' +
73+
' ],\n' +
74+
' controllers: [],\n' +
75+
' providers: [\n' +
76+
' { provide: APP_FILTER, useClass: BusinessLogicFilter },\n' +
77+
' { provide: APP_INTERCEPTOR, useClass: ClassSerializerInterceptor },\n' +
78+
' WinstonLogger,\n' +
79+
' ],\n' +
80+
' exports: [ConfigModule, WinstonLogger],\n' +
81+
'})\n' +
82+
'export class CoreModule {}\n',
83+
);
84+
});
1585
});
1686
});
1787
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { normalize } from '@angular-devkit/core';
2+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
3+
import * as path from 'path';
4+
5+
describe('Controller Factory', () => {
6+
const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json'));
7+
it('should manage name only', () => {
8+
const optionsApp: object = {
9+
name: 'project',
10+
spec: false,
11+
};
12+
runner.runSchematicAsync('controller', optionsApp).subscribe(tree => {
13+
const files: string[] = tree.files;
14+
expect(files.find(filename => filename === '/src/app/controllers/project.controller.ts')).toBeDefined();
15+
expect(files.find(filename => filename === '/src/app/controllers/project.controller.spec.ts')).not.toBeDefined();
16+
expect(tree.readContent('/src/app/controllers/project.controller.ts')).toEqual(
17+
"import { Controller } from '@nestjs/common';\n" +
18+
'\n' +
19+
"@Controller('projects')\n" +
20+
'export class ProjectController {}\n',
21+
);
22+
});
23+
});
24+
it('should manage name and path', () => {
25+
const optionsApp: object = {
26+
name: 'foo',
27+
path: 'bar',
28+
skipImport: true,
29+
};
30+
runner.runSchematicAsync('controller', optionsApp).subscribe(tree => {
31+
const files: string[] = tree.files;
32+
expect(files.find(filename => filename === '/bar/src/app/controllers/foo.controller.ts')).toBeDefined();
33+
expect(files.find(filename => filename === '/bar/src/app/controllers/foo.controller.spec.ts')).toBeDefined();
34+
expect(tree.readContent('/bar/src/app/controllers/foo.controller.ts')).toEqual(
35+
"import { Controller } from '@nestjs/common';\n" +
36+
'\n' +
37+
"@Controller('foos')\n" +
38+
'export class FooController {}\n',
39+
);
40+
});
41+
});
42+
it('should manage name to dasherize', () => {
43+
const optionsApp: object = {
44+
name: 'fooBar',
45+
skipImport: true,
46+
};
47+
runner.runSchematicAsync('controller', optionsApp).subscribe(tree => {
48+
const files: string[] = tree.files;
49+
expect(files.find(filename => filename === '/src/app/controllers/foo-bar.controller.ts')).toBeDefined();
50+
expect(files.find(filename => filename === '/src/app/controllers/foo-bar.controller.spec.ts')).toBeDefined();
51+
expect(tree.readContent('/src/app/controllers/foo-bar.controller.ts')).toEqual(
52+
"import { Controller } from '@nestjs/common';\n" +
53+
'\n' +
54+
"@Controller('foo-bars')\n" +
55+
'export class FooBarController {}\n',
56+
);
57+
});
58+
});
59+
it('should manage declaration in app module', () => {
60+
const optionsApp: object = {
61+
name: '',
62+
};
63+
const optionsModule: object = {
64+
name: 'foo',
65+
};
66+
runner.runSchematicAsync('application', optionsApp).subscribe(tree => {
67+
runner.runSchematicAsync('controller', optionsModule, tree).subscribe(tree => {
68+
expect(tree.readContent(normalize('/src/app/app.module.ts'))).toEqual(
69+
"import { Module } from '@nestjs/common';\n" +
70+
"import { AppController } from './app.controller';\n" +
71+
"import { AppService } from './app.service';\n" +
72+
"import { CoreModule } from './core/core.module';\n" +
73+
"import { FooController } from './controllers/foo.controller';\n" +
74+
'\n' +
75+
'@Module({\n' +
76+
' imports: [CoreModule],\n' +
77+
' controllers: [FooController, AppController],\n' +
78+
' providers: [AppService],\n' +
79+
'})\n' +
80+
'export class AppModule {}\n',
81+
);
82+
});
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)