Skip to content

Commit 26915e5

Browse files
feat(cli): add util shared functions and constants
1 parent 1a6e548 commit 26915e5

File tree

7 files changed

+83
-52
lines changed

7 files changed

+83
-52
lines changed

docs/site/Repository-generator.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ file.
4545

4646
### Notes
4747

48-
Service oriented datasources such as REST or SOAP are not considered valid and
49-
thus not presented to you in the selection list.
48+
Service oriented datasources such as REST or SOAP are not considered valid in
49+
this context and will not be presented to you in the selection list.
5050

5151
There should be at least one valid _(KeyValue or Persisted)_ data source and one
5252
model already created in their respective directories.
@@ -65,12 +65,12 @@ The tool will prompt you for:
6565

6666
- **Please select the datasource.** _(name)_ If the name of the datasource had
6767
been supplied from the command line, the prompt is skipped, otherwise it will
68-
present you, the list of available datasources to select one. It will use this
68+
present you the list of available datasources to select one. It will use this
6969
datasource to check what kind of repository it will generate.
7070

7171
- **Select the model(s) you want to generate a repository.** _(model)_ If the
7272
name of the model had been supplied from the command line with `--model`
73-
option and it is a valid model, the the prompt is skipped, otherwise it will
73+
option and it is a valid model, then the prompt is skipped, otherwise it will
7474
present the error `Error: No models found` in the console.
7575

7676
If no `--model` is supplied, then the it will present you with a valid list of

packages/cli/generators/model/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
3030
_setupGenerator() {
3131
this.artifactInfo = {
3232
type: 'model',
33-
rootDir: 'src',
33+
rootDir: utils.sourceRootDir,
3434
};
3535

3636
this.artifactInfo.outDir = path.resolve(
3737
this.artifactInfo.rootDir,
38-
'models',
38+
utils.modelsDir,
3939
);
4040

4141
// Model Property Types
@@ -190,8 +190,7 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
190190
debug('scaffolding');
191191

192192
// Data for templates
193-
this.artifactInfo.fileName = utils.kebabCase(this.artifactInfo.name);
194-
this.artifactInfo.outFile = `${this.artifactInfo.fileName}.model.ts`;
193+
this.artifactInfo.outFile = utils.getModelFileName(this.artifactInfo.name);
195194

196195
// Resolved Output Path
197196
const tsPath = this.destinationPath(

packages/cli/generators/repository/index.js

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
4747
let fileContent = '';
4848
let modelFile = path.join(
4949
this.artifactInfo.modelDir,
50-
`${utils.kebabCase(modelName)}.model.ts`,
50+
utils.getModelFileName(modelName),
5151
);
5252
try {
5353
fileContent = this.fs.read(modelFile, {});
@@ -63,12 +63,12 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
6363
* helper method to inspect and validate a repository type
6464
*/
6565
async _inferRepositoryType() {
66-
if (!this.artifactInfo.dataSourceClassName) {
66+
if (!this.artifactInfo.dataSourceClass) {
6767
return;
6868
}
6969
let result = this._isConnectorOfType(
7070
KEY_VALUE_CONNECTOR,
71-
this.artifactInfo.dataSourceClassName,
71+
this.artifactInfo.dataSourceClass,
7272
);
7373
debug(`KeyValue Connector: ${result}`);
7474

@@ -81,17 +81,19 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
8181
}
8282

8383
// assign the data source name to the information artifact
84-
let dataSourceName = this.artifactInfo.dataSourceClassName
84+
let dataSourceName = this.artifactInfo.dataSourceClass
8585
.replace('Datasource', '')
8686
.toLowerCase();
87-
let dataSourceImportName = this.artifactInfo.dataSourceClassName.replace(
87+
88+
let dataSourceClassName = this.artifactInfo.dataSourceClass.replace(
8889
'Datasource',
8990
'DataSource',
9091
);
9192

9293
Object.assign(this.artifactInfo, {
93-
dataSourceImportName: dataSourceImportName,
94+
dataSourceClassName: dataSourceClassName,
9495
});
96+
9597
Object.assign(this.artifactInfo, {
9698
dataSourceName: dataSourceName,
9799
});
@@ -102,19 +104,17 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
102104
* connectorType supplied for the given connector name
103105
* @param {string} connectorType single or a comma separated string array
104106
*/
105-
_isConnectorOfType(connectorType, dataSourceClassName) {
107+
_isConnectorOfType(connectorType, dataSourceClass) {
106108
debug(`calling isConnectorType ${connectorType}`);
107109
let jsonFileContent = '';
108110
let result = false;
109111

110-
if (!dataSourceClassName) {
112+
if (!dataSourceClass) {
111113
return false;
112114
}
113115
let datasourceJSONFile = path.join(
114116
this.artifactInfo.datasourcesDir,
115-
dataSourceClassName
116-
.replace('Datasource', '.datasource.json')
117-
.toLowerCase(),
117+
dataSourceClass.replace('Datasource', '.datasource.json').toLowerCase(),
118118
);
119119

120120
try {
@@ -141,20 +141,20 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
141141
_setupGenerator() {
142142
this.artifactInfo = {
143143
type: 'repository ',
144-
rootDir: 'src',
144+
rootDir: utils.sourceRootDir,
145145
};
146146

147147
this.artifactInfo.outDir = path.resolve(
148148
this.artifactInfo.rootDir,
149-
'repositories',
149+
utils.repositoriesDir,
150150
);
151151
this.artifactInfo.datasourcesDir = path.resolve(
152152
this.artifactInfo.rootDir,
153-
'datasources',
153+
utils.datasourcesDir,
154154
);
155155
this.artifactInfo.modelDir = path.resolve(
156156
this.artifactInfo.rootDir,
157-
'models',
157+
utils.modelsDir,
158158
);
159159

160160
this.artifactInfo.defaultTemplate = REPOSITORY_CRUD_TEMPLATE;
@@ -230,7 +230,11 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
230230
'datasource',
231231
true,
232232
);
233-
debug(`datasourcesList from src/datasources : ${datasourcesList}`);
233+
debug(
234+
`datasourcesList from ${utils.sourceRootDir}/${
235+
utils.datasourcesDir
236+
} : ${datasourcesList}`,
237+
);
234238
} catch (err) {
235239
return this.exit(err);
236240
}
@@ -244,17 +248,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
244248
return result;
245249
});
246250

247-
if (availableDatasources.includes(cmdDatasourceName)) {
248-
Object.assign(this.artifactInfo, {
249-
dataSourceClassName: cmdDatasourceName,
250-
});
251-
}
252-
253-
debug(
254-
`artifactInfo.dataSourceClassName ${
255-
this.artifactInfo.dataSourceClassName
256-
}`,
257-
);
251+
debug(`artifactInfo.dataSourceClass ${this.artifactInfo.dataSourceClass}`);
258252

259253
if (availableDatasources.length === 0) {
260254
return this.exit(
@@ -267,13 +261,19 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
267261
);
268262
}
269263

264+
if (availableDatasources.includes(cmdDatasourceName)) {
265+
Object.assign(this.artifactInfo, {
266+
dataSourceClass: cmdDatasourceName,
267+
});
268+
}
269+
270270
return this.prompt([
271271
{
272272
type: 'list',
273-
name: 'dataSourceClassName',
273+
name: 'dataSourceClass',
274274
message: PROMPT_MESSAGE_DATA_SOURCE,
275275
choices: availableDatasources,
276-
when: !this.artifactInfo.dataSourceClassName,
276+
when: !this.artifactInfo.dataSourceClass,
277277
default: availableDatasources[0],
278278
validate: utils.validateClassName,
279279
},
@@ -310,7 +310,11 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
310310

311311
this.options.model = utils.toClassName(this.options.model);
312312
// assign the model name from the command line only if it is valid
313-
if (modelList.length > 0 && modelList.includes(this.options.model)) {
313+
if (
314+
modelList &&
315+
modelList.length > 0 &&
316+
modelList.includes(this.options.model)
317+
) {
314318
Object.assign(this.artifactInfo, {modelNameList: [this.options.model]});
315319
} else {
316320
modelList = [];
@@ -399,25 +403,31 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
399403

400404
if (this.options.name) {
401405
this.artifactInfo.className = utils.toClassName(this.options.name);
402-
this.artifactInfo.outFile =
403-
utils.kebabCase(this.options.name) + '.repository.ts';
406+
this.artifactInfo.outFile = utils.getRepositoryFileName(
407+
this.options.name,
408+
);
404409

405410
// make sure the name supplied from cmd line is only used once
406411
delete this.options.name;
407412
} else {
408413
this.artifactInfo.className = utils.toClassName(
409414
this.artifactInfo.modelName,
410415
);
411-
this.artifactInfo.outFile =
412-
utils.kebabCase(this.artifactInfo.modelName) + '.repository.ts';
416+
this.artifactInfo.outFile = utils.getRepositoryFileName(
417+
this.artifactInfo.modelName,
418+
);
413419
}
414420

415421
if (debug.enabled) {
416422
debug(`Artifact output filename set to: ${this.artifactInfo.outFile}`);
417423
}
418424

419425
const source = this.templatePath(
420-
path.join('src', 'repositories', this.artifactInfo.defaultTemplate),
426+
path.join(
427+
utils.sourceRootDir,
428+
utils.repositoriesDir,
429+
this.artifactInfo.defaultTemplate,
430+
),
421431
);
422432

423433
if (debug.enabled) {

packages/cli/generators/repository/templates/src/repositories/repository-crud-default-template.ts.ejs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {<%= repositoryTypeClass %>, juggler} from '@loopback/repository';
22
import {<%= modelName %>} from '../models';
3+
import {<%= dataSourceClassName %>} from '../datasources';
34
import {inject} from '@loopback/core';
45

56
export class <%= className %>Repository extends <%= repositoryTypeClass %><
67
<%= modelName %>,
78
typeof <%= modelName %>.prototype.<%= idType %>
89
> {
910
constructor(
10-
@inject('datasources.<%= dataSourceName %>') protected datasource: juggler.DataSource,
11+
@inject('datasources.<%= dataSourceName %>') protected datasource: <%= dataSourceClassName %>,
1112
) {
1213
super(<%= modelName %>, datasource);
1314
}

packages/cli/generators/repository/templates/src/repositories/repository-kv-template.ts.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {<%= repositoryTypeClass %>} from '@loopback/repository';
22
import {<%= modelName %>} from '../models';
3-
import {<%= dataSourceImportName %>} from '../datasources';
3+
import {<%= dataSourceClassName %>} from '../datasources';
44
import {inject} from '@loopback/core';
55

66
export class <%= className %>Repository extends <%= repositoryTypeClass %><
77
<%= modelName %>
88
> {
99
constructor(
10-
@inject('datasources.<%= dataSourceName %>') datasource: <%= dataSourceImportName %>,
10+
@inject('datasources.<%= dataSourceName %>') datasource: <%= dataSourceClassName %>,
1111
) {
1212
super(<%= modelName %>,datasource);
1313
}

packages/cli/lib/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,24 @@ function validateValue(name, unallowedCharacters) {
370370
}
371371
return true;
372372
}
373+
/**
374+
* Returns the modelName in the directory file format for the model
375+
* @param {string} modelName
376+
*/
377+
exports.getModelFileName = function(modelName) {
378+
return `${_.kebabCase(modelName)}.model.ts`;
379+
};
380+
381+
/**
382+
* Returns the repositoryName in the directory file format for the repository
383+
* @param {string} repositoryName
384+
*/
385+
exports.getRepositoryFileName = function(repositoryName) {
386+
return `${_.kebabCase(repositoryName)}.repository.ts`;
387+
};
388+
389+
// literal strings with artifacts directory locations
390+
exports.repositoriesDir = 'repositories';
391+
exports.datasourcesDir = 'datasources';
392+
exports.modelsDir = 'models';
393+
exports.sourceRootDir = 'src';

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('lb4 repository', () => {
3030
describe('generate repositories on special conditions', () => {
3131
it('generates a multi-word crud repository', async () => {
3232
const multiItemPrompt = {
33-
dataSourceClassName: 'DbmemDatasource',
33+
dataSourceClass: 'DbmemDatasource',
3434
modelNameList: ['MultiWord'],
3535
};
3636

@@ -116,7 +116,7 @@ describe('lb4 repository', () => {
116116

117117
it('generates a repository asking for the ID name', async () => {
118118
const multiItemPrompt = {
119-
dataSourceClassName: 'DbmemDatasource',
119+
dataSourceClass: 'DbmemDatasource',
120120
modelNameList: ['InvalidId'],
121121
propertyName: 'myid',
122122
};
@@ -152,7 +152,7 @@ describe('lb4 repository', () => {
152152
describe('all invalid parameters and usage', () => {
153153
it('does not run with an invalid model name', async () => {
154154
const basicPrompt = {
155-
dataSourceClassName: 'DbmemDatasource',
155+
dataSourceClass: 'DbmemDatasource',
156156
};
157157
return expect(
158158
testUtils
@@ -168,7 +168,7 @@ describe('lb4 repository', () => {
168168

169169
it("does not run when user doesn't select a model", async () => {
170170
const basicPrompt = {
171-
dataSourceClassName: 'DbmemDatasource',
171+
dataSourceClass: 'DbmemDatasource',
172172
};
173173
return expect(
174174
testUtils
@@ -197,7 +197,7 @@ describe('lb4 repository', () => {
197197
describe('valid generation of crud repositories', () => {
198198
it('generates a crud repository from default model', async () => {
199199
const basicPrompt = {
200-
dataSourceClassName: 'DbmemDatasource',
200+
dataSourceClass: 'DbmemDatasource',
201201
};
202202
await testUtils
203203
.executeGenerator(generator)
@@ -283,7 +283,7 @@ describe('lb4 repository', () => {
283283

284284
it('generates a kv repository from decorator defined model', async () => {
285285
const basicPrompt = {
286-
dataSourceClassName: 'DbkvDatasource',
286+
dataSourceClass: 'DbkvDatasource',
287287
};
288288
await testUtils
289289
.executeGenerator(generator)

0 commit comments

Comments
 (0)