Skip to content

Commit

Permalink
fixup! fix lb4 openapi
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Apr 21, 2020
1 parent ffa7c01 commit 2f2c023
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
19 changes: 2 additions & 17 deletions packages/cli/generators/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,8 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
// Find openapi data sources
const openApiDataSources = dataSourceList.filter(ds => {
debug(`data source inspecting item: ${ds.className}`);
const datasourceJSONFile = path.join(
dataSourcesDir,
utils.dataSourceToJSONFileName(ds.className),
);
const dsObj = utils.getDataSourceConfig(dataSourcesDir, ds.className);

debug(`reading ${datasourceJSONFile}`);
const dsObj = this.fs.readJSON(datasourceJSONFile);
if (
dsObj.connector === 'openapi' ||
dsObj.connector === 'loopback-connector-openapi'
Expand Down Expand Up @@ -288,15 +283,6 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
}

async _generateDataSource() {
const jsonFile = this.dataSourceInfo.jsonFileName;
if (debug.enabled) {
debug(`Artifact output filename set to: ${jsonFile}`);
}
let dest = this.destinationPath(`src/datasources/${jsonFile}`);
if (debug.enabled) {
debug('Copying artifact to: %s', dest);
}

let specPath = this.url;
const parsed = parse(this.url);
if (parsed.protocol == null) {
Expand Down Expand Up @@ -324,7 +310,7 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
if (debug.enabled) {
debug(`Artifact output filename set to: ${dataSourceFile}`);
}
dest = this.destinationPath(`src/datasources/${dataSourceFile}`);
const dest = this.destinationPath(`src/datasources/${dataSourceFile}`);
if (debug.enabled) {
debug('Copying artifact to: %s', dest);
}
Expand Down Expand Up @@ -445,7 +431,6 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
this.dataSourceInfo.className =
utils.toClassName(this.dataSourceInfo.name) + 'DataSource';
this.dataSourceInfo.fileName = utils.toFileName(this.dataSourceInfo.name);
this.dataSourceInfo.jsonFileName = `${this.dataSourceInfo.fileName}.datasource.config.json`;
this.dataSourceInfo.outFile = `${this.dataSourceInfo.fileName}.datasource.ts`;
}

Expand Down
40 changes: 15 additions & 25 deletions packages/cli/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,27 +556,8 @@ exports.getDataSourceConnectorName = function (
if (!dataSourceClass) {
return false;
}
let result;
let jsonFileContent;

const datasourceJSONFile = path.join(
datasourcesDir,
exports.dataSourceToJSONFileName(dataSourceClass),
);

debug(`reading ${datasourceJSONFile}`);
try {
jsonFileContent = JSON.parse(fs.readFileSync(datasourceJSONFile, 'utf8'));
} catch (err) {
debug(`Error reading file ${datasourceJSONFile}: ${err.message}`);
err.message = `Cannot load ${datasourceJSONFile}: ${err.message}`;
throw err;
}

if (jsonFileContent.connector) {
result = jsonFileContent.connector;
}
return result;
const config = exports.getDataSourceConfig(datasourcesDir, dataSourceClass);
return config.connector;
};

/**
Expand All @@ -598,7 +579,7 @@ exports.isConnectorOfType = function (
return false;
}

const config = getDataSourceConfig(datasourcesDir, dataSourceClass);
const config = exports.getDataSourceConfig(datasourcesDir, dataSourceClass);

for (const connector of Object.values(connectors)) {
const matchedConnector =
Expand All @@ -612,7 +593,16 @@ exports.isConnectorOfType = function (
return null;
};

function getDataSourceConfig(datasourcesDir, dataSourceClass) {
/**
* Load the datasource configuration. Supports both the current TypeScript-based
* flavor and legacy JSON-based configuration.
* @param {string} datasourcesDir path for sources
* @param {string} dataSourceClass class name for the datasource
*/
exports.getDataSourceConfig = function getDataSourceConfig(
datasourcesDir,
dataSourceClass,
) {
const config =
readDataSourceConfigFromTypeScript(datasourcesDir, dataSourceClass) ||
// Support legacy JSON-based configuration.
Expand All @@ -622,7 +612,7 @@ function getDataSourceConfig(datasourcesDir, dataSourceClass) {

debug('datasource %s has config %o', dataSourceClass, config);
return config;
}
};

function readDataSourceConfigFromTypeScript(datasourcesDir, dataSourceClass) {
const srcFile = path.join(
Expand Down Expand Up @@ -664,7 +654,7 @@ exports.getDataSourceName = function (datasourcesDir, dataSourceClass) {
if (!dataSourceClass) {
return false;
}
const config = getDataSourceConfig(datasourcesDir, dataSourceClass);
const config = exports.getDataSourceConfig(datasourcesDir, dataSourceClass);
return config.name;
};

Expand Down

0 comments on commit 2f2c023

Please sign in to comment.