Skip to content

Commit f9f239f

Browse files
feat(cli): add util shared functions and constants
1 parent e9651e1 commit f9f239f

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

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.toModelFileName(this.artifactInfo.name);
195194

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

packages/cli/generators/repository/index.js

Lines changed: 24 additions & 12 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.toModelFileName(modelName),
5151
);
5252
try {
5353
fileContent = this.fs.read(modelFile, {});
@@ -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
}
@@ -310,7 +314,11 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
310314

311315
this.options.model = utils.toClassName(this.options.model);
312316
// assign the model name from the command line only if it is valid
313-
if (modelList.length > 0 && modelList.includes(this.options.model)) {
317+
if (
318+
modelList &&
319+
modelList.length > 0 &&
320+
modelList.includes(this.options.model)
321+
) {
314322
Object.assign(this.artifactInfo, {modelNameList: [this.options.model]});
315323
} else {
316324
modelList = [];
@@ -399,25 +407,29 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
399407

400408
if (this.options.name) {
401409
this.artifactInfo.className = utils.toClassName(this.options.name);
402-
this.artifactInfo.outFile =
403-
utils.kebabCase(this.options.name) + '.repository.ts';
410+
this.artifactInfo.outFile = utils.toRepositoryFileName(this.options.name);
404411

405412
// make sure the name supplied from cmd line is only used once
406413
delete this.options.name;
407414
} else {
408415
this.artifactInfo.className = utils.toClassName(
409416
this.artifactInfo.modelName,
410417
);
411-
this.artifactInfo.outFile =
412-
utils.kebabCase(this.artifactInfo.modelName) + '.repository.ts';
418+
this.artifactInfo.outFile = utils.toRepositoryFileName(
419+
this.artifactInfo.modelName,
420+
);
413421
}
414422

415423
if (debug.enabled) {
416424
debug(`Artifact output filename set to: ${this.artifactInfo.outFile}`);
417425
}
418426

419427
const source = this.templatePath(
420-
path.join('src', 'repositories', this.artifactInfo.defaultTemplate),
428+
path.join(
429+
utils.sourceRootDir,
430+
utils.repositoriesDir,
431+
this.artifactInfo.defaultTemplate,
432+
),
421433
);
422434

423435
if (debug.enabled) {

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.toModelFileName = 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.toRepositoryFileName = 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';

0 commit comments

Comments
 (0)