Skip to content

Commit

Permalink
fix(api spec): issues/17 local run openapi spec (#31)
Browse files Browse the repository at this point in the history
* remove local openapi/swagger spec in favor of remote rhsm resource
  • Loading branch information
cdcabrera authored Jul 3, 2019
1 parent 83ed9d5 commit 1aa0bcd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 222 deletions.
201 changes: 0 additions & 201 deletions docs/rhsm-subscriptions-api-spec.yaml

This file was deleted.

81 changes: 60 additions & 21 deletions scripts/openapi.docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@ const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const openApiSpecs = [
{
file: `${process.cwd()}/docs/rhsm-subscriptions-api-spec.yaml`,
file:
'https://raw.githubusercontent.com/RedHatInsights/rhsm-subscriptions/master/api/rhsm-subscriptions-api-spec.yaml',
outputDir: `${process.cwd()}/.openapi`,
outputFileName: 'rhsm.yaml',
port: 5050
}
];
const cache = {
tryAgainCount: 0
};

/**
* Set display colors
*
* @param {string} str
* @param {string} color
* @returns {string}
*/
const setColor = (str, color = 'reset') => {
const colors = {
blue: '\x1b[34m',
green: '\x1b[32m',
red: '\x1b[31m',
reset: '\x1b[0m',
yellow: '\x1b[33m'
};

return `${colors[color.toLowerCase()] || colors.reset}${str}${colors.reset}`;
};

/**
* Express serve local files
*
* @param {array} files
*/
const serveDocs = (files = []) => {
files.forEach(yamlFile => {
if (fs.existsSync(yamlFile.file)) {
Expand All @@ -23,51 +50,63 @@ const serveDocs = (files = []) => {

app.listen(yamlFile.port, () => {
console.log(
`\nYou can now view API docs for ${yamlFile.file
.split('/')
.pop()} in the browser.\n Open: http://localhost:${yamlFile.port}/docs/api\n`
`You can now view API docs for ${yamlFile.desc} in the browser.\n Open: http://localhost:${yamlFile.port}/docs/api\n`
);
});
} else if (cache.tryAgainCount < 10) {
setTimeout(() => {
console.info(`Locating ${yamlFile.file}...`);
console.info(`Locating ${yamlFile.desc}...`);
cache.tryAgainCount += 1;
serveDocs(yamlFile.file, yamlFile.port);
}, 1000);
} else {
console.info(`${yamlFile.file} doesn't exist`);
console.warn(setColor(`${yamlFile.desc} doesn't exist`, 'yellow'));
}
});
};

/**
* Load remote files and cache, or just confirm a local file.
*
* @param {Array} inputPaths
* @returns {Array}
*/
const getLocalApiSpec = (inputPaths = []) => {
const outputPaths = [];

inputPaths.forEach(inputPath => {
let outputPath = inputPath.file;
const inputPathFile = inputPath.file.split('/').pop();

if (/^http/i.test(inputPath.file)) {
const outputPath = path.join(inputPath.outputDir, inputPath.outputFileName);
const outputYaml = execSync(`curl ${inputPath.file}`);
const warning = `Unable to load ${inputPathFile} -> ${inputPath.outputFileName}, checking cache...`;
outputPath = path.join(inputPath.outputDir, inputPath.outputFileName);

if (!fs.existsSync(inputPath.outputDir)) {
fs.mkdirSync(inputPath.outputDir);
}
try {
const outputYaml = execSync(`curl --silent ${inputPath.file}`);

if (/openapi/i.test(outputYaml.toString())) {
fs.writeFileSync(outputPath, outputYaml);
} else {
console.warn(
`Unable to load ${inputPath.file.split('/').pop()} -> ${inputPath.outputFileName}, checking cache...`
);
if (!fs.existsSync(inputPath.outputDir)) {
fs.mkdirSync(inputPath.outputDir);
}

if (/openapi/i.test(outputYaml.toString())) {
fs.writeFileSync(outputPath, outputYaml);
} else {
console.warn(setColor(warning, 'yellow'));
}
} catch (e) {
console.warn(setColor(warning, 'yellow'));
}
}

outputPaths.push({ file: outputPath, port: inputPath.port });
if (fs.existsSync(outputPath)) {
console.log(setColor(`Success -> ${inputPathFile}`, 'green'));
outputPaths.push({ file: outputPath, port: inputPath.port, desc: inputPathFile });
} else {
outputPaths.push({ file: inputPath.file, port: inputPath.port });
console.warn(setColor(`Failed -> ${inputPathFile}`, 'red'));
}
});

console.log(outputPaths);

return outputPaths;
};

Expand Down

0 comments on commit 1aa0bcd

Please sign in to comment.