-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
146 lines (140 loc) · 4.14 KB
/
index.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Plugin } from '@merryjs/cli/lib/plugin'
import { generatePaths, GenerateResult } from '@merryjs/swagger'
import changeCase from 'change-case'
import fs from 'fs-extra'
import path from 'path'
/**
* SwaggerAnswers
*/
export interface SwaggerAnswers {
name: string
}
export interface SwaggerOptions {
api: string
pattern?: string
dist: string
tpl: string
ext: string
file: string
/**
* clean dist folder
*/
clean_stores?: boolean
custom_api?: boolean
}
export default (api: Plugin) => {
api
.command('swagger [name]')
.option('-A, --api [value]', 'swagger api')
.option('-P, --pattern [value]', 'filter path by pattern')
.option('-D, --dist [value]', 'file writes to')
.option('-T, --tpl [value]', 'Provide your template if needed')
.option('-E, --ext [value]', 'file extension without [.] defaults to ts')
.option('-F, --file [value]', 'execute file if provided')
.option('-C, --clean_stores', 'clean stores')
.option('-U, --custom_api', 'custom api')
.action(async (name: string, options: SwaggerOptions) => {
if (!options) {
api.outputHelp()
return
}
if (!options.dist) {
api.log('The dist param are required so swagger can write to')
api.outputHelp()
return
}
if (!options.api) {
api.log('The api param are required so swagger can read from')
api.outputHelp()
return
}
if (options.clean_stores) {
api.fs.emptyDirSync(`${api.conf.dist}/${options.dist}`)
}
const resultOfDefinitions = await generatePaths(options.api, {
definitionName: '{path}',
})
let tpl = './api.tpl'
if (options.tpl) {
tpl = path.join(process.cwd(), options.tpl)
}
// filter by key
const resultWithPattern = Object.keys(resultOfDefinitions).filter(
key => !(options.pattern && new RegExp(options.pattern, 'gi').test(key))
)
const result: {
[key: string]: GenerateResult[]
} = {}
resultWithPattern.forEach(key => {
if (!(options.pattern && new RegExp(options.pattern, 'gi').test(key))) {
result[key] = resultOfDefinitions[key]
}
})
if (!options.custom_api) {
for (const key in result) {
if (result.hasOwnProperty(key)) {
if (
options.pattern &&
new RegExp(options.pattern, 'gi').test(key)
) {
continue
}
// export interface GenerateResult {
// responses: string
// parameters: string
// path: string
// summary?: string
// definitionEntityName?: string
// definitionParamsName?: string
// operation: string
// }
// const actions = {
// isGet,
// isPut,
// isPost,
// isDelete,
// isOptions,
// isHead,
// isPatch,
// }
const fullPath = getFullPath(key)
await api.tmplWithFormat(
tpl,
path.join(
api.conf.dist,
options.dist,
`${fullPath}.${options.ext || 'ts'}`
),
{ definitions: result[key] },
{ parser: 'typescript' }
)
}
}
}
if (options.file && fs.existsSync(options.file)) {
try {
const f = require(path.join(process.cwd(), options.file))
if (typeof f === 'function') {
f(result, { api, changeCase, options, getFullPath })
}
} catch (error) {
api.log('Can not call file from %s', options.file)
console.error(error)
}
}
})
}
function getFullPath(key: string) {
const paths = key.startsWith('/') ? key.substr(1).split('/') : key.split('/')
let folder = ''
let p = ''
if (paths.length > 1) {
folder = paths[0]
p = paths.filter((_, index) => index !== 0).join('/')
} else {
p = key
}
const fullPath =
(folder ? changeCase.snakeCase(folder) + '/' : '') + changeCase.snakeCase(p)
return fullPath
}