Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api spec): issues/17 local run openapi spec #31

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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