Skip to content

Commit

Permalink
feat(cli): add entries in cli integration test
Browse files Browse the repository at this point in the history
add lb4 repository to the cli integration test

implements loopbackio#1588
  • Loading branch information
marioestradarosa committed Sep 18, 2018
1 parent c914753 commit c215bf0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
44 changes: 16 additions & 28 deletions packages/cli/generators/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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-';
Expand All @@ -39,28 +36,24 @@ 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}`);
let jsonFileContent = '';
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);
}

Expand All @@ -72,7 +65,6 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
break;
}
}

return result;
};
}
Expand All @@ -96,6 +88,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
this.artifactInfo.rootDir,
'models',
);
this.artifactInfo.defaultTemplate = REPOSITORY_CRUD_TEMPLATE;
}

setOptions() {
Expand Down Expand Up @@ -170,18 +163,20 @@ 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
let dataSourceName = this.artifactInfo.dataSourceClassName
.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() {
Expand Down Expand Up @@ -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}`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/integration/cli/cli.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
});

Expand All @@ -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',
);
});

Expand Down

0 comments on commit c215bf0

Please sign in to comment.