-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (34 loc) · 1.2 KB
/
index.js
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
41
42
43
44
45
46
import { readYAML } from './utils/yaml.js'
import { TEMPLATE, parseTemplate, parseEndpoints, parseFileName } from './utils/parse.js'
import fs from 'fs'
const cwd = process.cwd()
function generate() {
const [api] = readYAML('./api.yaml')
const { base_url, endpoints } = api
const axiosClientTemplate = parseTemplate(TEMPLATE('axios-client'), {
base_url,
})
for (let endpoint of endpoints) {
const currentTemplate = [axiosClientTemplate]
const parsedEndpoints = parseEndpoints(endpoint)
parsedEndpoints.forEach(({ method, action, path }) => {
const output = parseTemplate(TEMPLATE(method), {
action,
method,
path,
})
currentTemplate.push(output)
})
const fileName = parseFileName(endpoint)
if (fs.existsSync(`${cwd}/api/${fileName}`)) {
fs.unlinkSync(`${cwd}/api/${fileName}`)
}
currentTemplate.forEach((template) => {
if (!fs.existsSync(`${cwd}/api`)) {
fs.mkdirSync(`${cwd}/api`)
}
fs.appendFileSync(`${cwd}/api/${fileName}`, template + '\n')
})
}
}
generate()