diff --git a/packages/cli/generators/repository/index.js b/packages/cli/generators/repository/index.js index 2d8e241f576f..577385d5eadc 100644 --- a/packages/cli/generators/repository/index.js +++ b/packages/cli/generators/repository/index.js @@ -11,9 +11,6 @@ const inspect = require('util').inspect; const path = require('path'); const chalk = require('chalk'); const utils = require('../../lib/utils'); -const util = require('util'); -const fs = require('fs'); -const exists = util.promisify(fs.exists); const SERVICE_VALUE_CONNECTOR = 'soap,rest'; const KEY_VALUE_CONNECTOR = 'kv-'; @@ -39,7 +36,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { super(args, opts); /** instance helper method isolated from the execution loop - * @connectorType: can be a single or a comma separated list + * @connectorType: can be a single or a comma separated string list */ this.isConnectorType = async function(connectorType, dataSourceClassName) { debug(`callling isConnectorType ${connectorType}`); @@ -47,20 +44,16 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { let result = false; let datasourceJSONFile = path.join( - 'src', - 'datasources', + this.artifactInfo.datasourcesDir, dataSourceClassName .replace('Datasource', '.datasource.json') .toLowerCase(), ); try { - const jsonFileExists = await exists(datasourceJSONFile); - if (jsonFileExists) { - jsonFileContent = this.fs.readJSON(datasourceJSONFile, {}); - } + jsonFileContent = this.fs.readJSON(datasourceJSONFile, {}); } catch (err) { - debug(`${ERROR_READING_FILE} ${datasourceJSONFile}: ${err}`); + debug(`${ERROR_READING_FILE} ${datasourceJSONFile}: ${err.message}`); return this.exit(err); } @@ -72,7 +65,6 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { break; } } - return result; }; } @@ -96,6 +88,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { this.artifactInfo.rootDir, 'models', ); + this.artifactInfo.defaultTemplate = REPOSITORY_CRUD_TEMPLATE; } setOptions() { @@ -170,8 +163,10 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { if (result) { this.artifactInfo.repositoryTypeClass = KEY_VALUE_REPOSITORY; + this.artifactInfo.defaultTemplate = REPOSITORY_KV_TEMPLATE; } else { this.artifactInfo.repositoryTypeClass = DEFAULT_CRUD_REPOSITORY; + this.artifactInfo.defaultTemplate = REPOSITORY_CRUD_TEMPLATE; } // assign the data source name to the information artifact @@ -179,9 +174,9 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { .replace('Datasource', '') .toLowerCase(); - Object.assign(this.artifactInfo, {dataSourceName: dataSourceName}); - // parent async end() checks for name property, albeit we don't use it here - Object.assign(this.artifactInfo, {name: dataSourceName}); + Object.assign(this.artifactInfo, { + dataSourceName: dataSourceName, + }); } async promptModels() { @@ -239,28 +234,21 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { // all of the templates! if (this.shouldExit()) return false; - this.artifactInfo.className = utils.toClassName(this.artifactInfo.name); + this.artifactInfo.className = utils.toClassName( + this.artifactInfo.modelName, + ); this.artifactInfo.outFile = utils.kebabCase(this.artifactInfo.modelName) + '.repository.ts'; + if (debug.enabled) { debug(`Artifact output filename set to: ${this.artifactInfo.outFile}`); } - let template = ''; - - /* place a switch statement for future repository types */ - switch (this.artifactInfo.repositoryTypeClass) { - case KEY_VALUE_REPOSITORY: - template = REPOSITORY_KV_TEMPLATE; - break; - default: - template = REPOSITORY_CRUD_TEMPLATE; - } - const source = this.templatePath( - path.join('src', 'repositories', template), + path.join('src', 'repositories', this.artifactInfo.defaultTemplate), ); + if (debug.enabled) { debug(`Using template at: ${source}`); } diff --git a/packages/cli/test/integration/cli/cli.integration.js b/packages/cli/test/integration/cli/cli.integration.js index 1f9347bd578d..ee6fa01ea1ab 100644 --- a/packages/cli/test/integration/cli/cli.integration.js +++ b/packages/cli/test/integration/cli/cli.integration.js @@ -24,7 +24,7 @@ describe('cli', () => { expect(entries).to.eql([ 'Available commands: ', ' lb4 app\n lb4 extension\n lb4 controller\n lb4 datasource\n ' + - 'lb4 model\n lb4 example\n lb4 openapi', + 'lb4 model\n lb4 repository\n lb4 example\n lb4 openapi', ]); }); @@ -42,7 +42,7 @@ describe('cli', () => { expect(entries).to.containEql('Available commands: '); expect(entries).to.containEql( ' lb4 app\n lb4 extension\n lb4 controller\n lb4 datasource\n ' + - 'lb4 model\n lb4 example\n lb4 openapi', + 'lb4 model\n lb4 repository\n lb4 example\n lb4 openapi', ); });