Skip to content

Commit be81131

Browse files
marioestradarosavirkt25
authored andcommitted
feat(cli): add repositorymixin and imports by a new switch
add the switch --enableRepository to CLI to automatically add the juggler imports and include RepositoryMixin in the extended class fix #1594
1 parent 53653b6 commit be81131

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

packages/cli/generators/app/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = class AppGenerator extends ProjectGenerator {
1111
// Note: arguments and options should be defined in the constructor.
1212
constructor(args, opts) {
1313
super(args, opts);
14+
this.buildOptions.push('enableRepository');
1415
}
1516

1617
_setupGenerator() {
@@ -21,6 +22,11 @@ module.exports = class AppGenerator extends ProjectGenerator {
2122
description: 'Application class name',
2223
});
2324

25+
this.option('enableRepository', {
26+
type: Boolean,
27+
description: 'Include repository imports and RepositoryMixin',
28+
});
29+
2430
return super._setupGenerator();
2531
}
2632

packages/cli/generators/app/templates/src/application.ts.ejs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@ import {MySequence} from './sequence';
55
/* tslint:disable:no-unused-variable */
66
// Binding and Booter imports are required to infer types for BootMixin!
77
import {BootMixin, Booter, Binding} from '@loopback/boot';
8+
<% if (project.enableRepository) { -%>
9+
10+
// juggler imports are required to infer types for RepositoryMixin!
11+
import {
12+
Class,
13+
Repository,
14+
RepositoryMixin,
15+
juggler,
16+
} from '@loopback/repository';
17+
<% } -%>
818
/* tslint:enable:no-unused-variable */
919

10-
export class <%= project.applicationName %> extends BootMixin(RestApplication) {
20+
export class <%= project.applicationName %> <% if (!project.enableRepository) {-%>extends BootMixin(RestApplication) {<% } else { -%>extends BootMixin(
21+
RepositoryMixin(RestApplication),
22+
) {
23+
<% } -%>
1124
constructor(options?: ApplicationConfig) {
1225
super(options);
1326

packages/cli/test/integration/generators/app.integration.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ describe('app-generator specific files', () => {
3131
assert.file('src/application.ts');
3232
assert.fileContent(
3333
'src/application.ts',
34-
/class MyAppApplication extends BootMixin\(RestApplication/,
34+
/class MyAppApplication extends BootMixin\(/,
35+
);
36+
assert.fileContent(
37+
'src/application.ts',
38+
/RepositoryMixin\(RestApplication\)/,
3539
);
3640
assert.fileContent('src/application.ts', /constructor\(/);
3741
assert.fileContent('src/application.ts', /this.projectRoot = __dirname/);
@@ -66,10 +70,14 @@ describe('app-generator with --applicationName', () => {
6670
.withPrompts(props);
6771
});
6872
it('generates all the proper files', () => {
73+
assert.file('src/application.ts');
74+
assert.fileContent('src/application.ts', /class MyApp extends BootMixin\(/);
75+
});
76+
it('generates the application with RepositoryMixin', () => {
6977
assert.file('src/application.ts');
7078
assert.fileContent(
7179
'src/application.ts',
72-
/class MyApp extends BootMixin\(RestApplication/,
80+
/RepositoryMixin\(RestApplication\)/,
7381
);
7482
});
7583
});
@@ -95,10 +103,14 @@ function testFormat() {
95103
.withPrompts(props);
96104
});
97105
it('generates all the proper files', () => {
106+
assert.file('src/application.ts');
107+
assert.fileContent('src/application.ts', /class MyApp extends BootMixin\(/);
108+
});
109+
it('generates the application with RepositoryMixin', () => {
98110
assert.file('src/application.ts');
99111
assert.fileContent(
100112
'src/application.ts',
101-
/class MyApp extends BootMixin\(RestApplication/,
113+
/RepositoryMixin\(RestApplication\)/,
102114
);
103115
});
104116
}

0 commit comments

Comments
 (0)