-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
40 lines (33 loc) · 1.1 KB
/
build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { promises } from 'node:fs'
import YAML from 'yamljs'
import prettier from 'prettier'
import converter from 'swagger2openapi'
import SwaggerParser from '@apidevtools/swagger-parser'
const swaggerYML = await promises.readFile('yml/swagger.yml', { encoding: 'utf8' }) // 2.0 YML
const openapiJSON = await formatJSON((await converter.convertStr(swaggerYML, {})).openapi)
{ // 3.0 JSON
await validate(openapiJSON)
await write('json/openapi.json', openapiJSON)
}
{ // 2.0 JSON
const swaggerJson = await formatJSON(YAML.parse(swaggerYML))
await validate(swaggerJson)
await write('json/swagger.json', swaggerJson)
}
{ // 3.0 YML
const openapiYML = await prettier.format(YAML.stringify(JSON.parse(openapiJSON)), {
parser: "yaml"
})
await write('yml/openapi.yml', openapiYML)
}
async function formatJSON(jsonStr) {
return await prettier.format(JSON.stringify(jsonStr), {
parser: "json"
});
}
async function write(path, content) {
return await promises.writeFile(path, content, { encoding: "utf8" });
}
async function validate(json) {
return await SwaggerParser.validate(JSON.parse(json));
}