Skip to content

Instantly share code, notes, and snippets.

@hw0k
Last active March 15, 2021 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hw0k/97e738ea668a4b7121c64e22a8b9b790 to your computer and use it in GitHub Desktop.
Save hw0k/97e738ea668a4b7121c64e22a8b9b790 to your computer and use it in GitHub Desktop.
OCE 2
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const fetch = require('node-fetch');
const OpenAPI = require('openapi-typescript-codegen');
const format = 'yaml'; // yaml or json
const specURL = 'https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml';
const outputPath = path.resolve(path.join(__dirname, '..', '__generated__'));
const docFilePath = path.join(outputPath, `spec.${format}`);
const accessAsync = promisify(fs.access);
const mkdirAsync = promisify(fs.mkdir);
const writeFileAsync = promisify(fs.writeFile);
async function makeDirectory(path) {
try {
await accessAsync(path);
} catch (err) {
if (err.code === 'ENOENT') {
await mkdirAsync(path);
}
else {
throw err;
}
}
}
/**
* 메인 로직
*/
async function run() {
try {
const response = await fetch(specURL);
const data = await response.text();
// 이 라이브러리는 input이 파일 경로여야 합니다.
// 그래서 문서를 파일에 저장한 후 그 경로를 사용합니다.
await makeDirectory(outputPath);
await writeFileAsync(docFilePath, data);
await OpenAPI.generate({
input: docFilePath,
output: outputPath,
});
} catch (err) {
console.error(err);
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment