Skip to content

Commit

Permalink
fix(cli): fixed ds names that were hyphened
Browse files Browse the repository at this point in the history
Now uses the functions created utils to refer properly to the src/datasources file name

close #1791
  • Loading branch information
marioestradarosa committed Oct 2, 2018
1 parent 353b202 commit 568307c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
48 changes: 5 additions & 43 deletions packages/cli/generators/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const ERROR_READING_FILE = 'Error reading file';
const ERROR_NO_DATA_SOURCES_FOUND = 'No datasources found at';
const ERROR_NO_MODELS_FOUND = 'No models found at';
const ERROR_NO_MODEL_SELECTED = 'You did not select a valid model';
const ERROR_NO_DIRECTORY = 'The directory was not found';

module.exports = class RepositoryGenerator extends ArtifactGenerator {
// Note: arguments and options should be defined in the constructor.
Expand Down Expand Up @@ -66,8 +65,9 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
if (!this.artifactInfo.dataSourceClass) {
return;
}
let result = this._isConnectorOfType(
let result = utils.isConnectorOfType(
KEY_VALUE_CONNECTOR,
this.artifactInfo.datasourcesDir,
this.artifactInfo.dataSourceClass,
);
debug(`KeyValue Connector: ${result}`);
Expand All @@ -89,45 +89,6 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
utils.toClassName(this.artifactInfo.dataSourceName) + 'DataSource';
}

/**
* load the connectors available and check if the basedModel matches any
* connectorType supplied for the given connector name
* @param {string} connectorType single or a comma separated string array
*/
_isConnectorOfType(connectorType, dataSourceClass) {
debug(`calling isConnectorType ${connectorType}`);
let jsonFileContent = '';
let result = false;

if (!dataSourceClass) {
return false;
}
let datasourceJSONFile = path.join(
this.artifactInfo.datasourcesDir,
dataSourceClass.replace('Datasource', '.datasource.json').toLowerCase(),
);

try {
jsonFileContent = this.fs.readJSON(datasourceJSONFile, {});
} catch (err) {
debug(`${ERROR_READING_FILE} ${datasourceJSONFile}: ${err.message}`);
return this.exit(err);
}

for (let connector of Object.values(connectors)) {
const matchedConnector =
jsonFileContent.connector === connector.name ||
jsonFileContent.connector === `loopback-connector-${connector.name}`;

if (matchedConnector && connectorType.includes(connector.baseModel)) {
result = true;
break;
}
}

return result;
}

_setupGenerator() {
this.artifactInfo = {
type: 'repository ',
Expand Down Expand Up @@ -238,9 +199,10 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
}

const availableDatasources = datasourcesList.filter(item => {
debug(`data source unfiltered list: ${item}`);
const result = this._isConnectorOfType(
debug(`data source inspecting item: ${item}`);
const result = utils.isConnectorOfType(
VALID_CONNECTORS_FOR_REPOSITORY,
this.artifactInfo.datasourcesDir,
item,
);
return result;
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/test/fixtures/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ exports.SANDBOX_FILES = [
connector: 'memory',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'my-ds.datasource.json',
content: JSON.stringify({
name: 'MyDS',
connector: 'memory',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'dbmem.datasource.ts',
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/test/integration/generators/repository.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,45 @@ describe('lb4 repository', function() {
);
});

it('generates a crud repository from hyphened model file name', async () => {
const basicPrompt = {
dataSourceClass: 'MyDsDatasource',
};
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(SANDBOX_PATH, {
additionalFiles: SANDBOX_FILES,
}),
)
.withPrompts(basicPrompt)
.withArguments(' --model Defaultmodel');
const expectedFile = path.join(
SANDBOX_PATH,
REPOSITORY_APP_PATH,
'defaultmodel.repository.ts',
);
assert.file(expectedFile);
assert.fileContent(
expectedFile,
/import {MyDSDataSource} from '..\/datasources';/,
);
assert.fileContent(
expectedFile,
/\@inject\('datasources.MyDS'\) protected datasource: MyDSDataSource,/,
);
assert.fileContent(
expectedFile,
/export class DefaultmodelRepository extends DefaultCrudRepository\</,
);
assert.fileContent(expectedFile, /typeof Defaultmodel.prototype.id/);
assert.file(INDEX_FILE);
assert.fileContent(
INDEX_FILE,
/export \* from '.\/defaultmodel.repository';/,
);
});

it('generates a crud repository from decorator defined model', async () => {
await testUtils
.executeGenerator(generator)
Expand Down

0 comments on commit 568307c

Please sign in to comment.