Last active
March 15, 2021 13:33
-
-
Save hw0k/97e738ea668a4b7121c64e22a8b9b790 to your computer and use it in GitHub Desktop.
OCE 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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