-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobx.ts
165 lines (155 loc) · 5.15 KB
/
mobx.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Plugin } from '@merryjs/cli/lib/plugin'
import path from 'path'
import { FlutterOptions } from './action'
import * as quickType from 'quicktype'
import { generatePaths } from '@merryjs/swagger'
import changeCase from 'change-case'
export default async (api: Plugin, options: FlutterOptions) => {
const cacheName = `.merry-cache-${Date.now()}`
const cache = path.join(__dirname, cacheName)
api.fs.emptyDirSync(cache)
api.fs.ensureDirSync(cache)
if (!options.src) {
api.log('--src required')
return
}
// fix tpl path
if (options.tpl) {
options.tpl = path.join(process.cwd(), options.tpl)
} else {
options.tpl = './store.tpl'
}
if (options.clean_stores) {
api.fs.emptyDirSync(`${api.conf.dist}/${options.dist}`)
}
const result = await generatePaths(options.src, {
definitionName: '{path}',
schema: true,
})
for (const key in result) {
if (result.hasOwnProperty(key)) {
if (options.skip && new RegExp(options.skip, 'gi').test(key)) {
continue
}
const element = result[key]
const fileName = getFullPath(key)
const out = `${api.conf.dist}/${options.dist}/${fileName}.dart`
const parametersFile = `${cache}/${
options.dist
}/${fileName}-parameters.json`
const responsesFile = `${cache}/${
options.dist
}/${fileName}-responses.json`
const dartParametersFile = `${cache}/${
options.dist
}/${fileName}-parameters.dart`
const dartResponsesFile = `${cache}/${
options.dist
}/${fileName}-responses.dart`
for (const k in element) {
if (element.hasOwnProperty(k)) {
let el = element[k]
if (el.parameters) {
await api.fs.ensureFile(parametersFile)
try {
const json = JSON.parse(el.parameters)
let jsp = { ...json.properties }
if (json && jsp) {
// handle named parameters but in body
if (Object.keys(jsp).length === 1) {
Object.keys(jsp).forEach(op => {
if (jsp[op]['in'] === 'body') {
jsp = jsp[op].schema.properties
}
})
}
for (const j in jsp) {
if (jsp.hasOwnProperty(j)) {
const jn = jsp[j]
if (jn.type === 'number' && jn.format) {
let type = 'number'
switch (jn.format) {
case 'int32':
case 'int64':
type = 'integer'
break
// TODO: add more type if needed
default:
break
}
jsp[j].type = type
}
}
}
}
json.properties = jsp
element[k].parameters = JSON.stringify(json, null, 2)
} catch (error) {}
el = element[k]
await api.fs.writeFile(parametersFile, el.parameters, 'utf-8')
await quickType.main({
srcLang: 'schema',
src: [parametersFile],
lang: 'dart',
out: dartParametersFile,
topLevel: el.definitionParamsName,
})
}
if (el.responses) {
await api.fs.ensureFile(responsesFile)
await api.fs.writeFile(responsesFile, el.responses, 'utf-8')
await quickType.main({
srcLang: 'schema',
src: [responsesFile],
lang: 'dart',
out: dartResponsesFile,
topLevel: el.definitionEntityName,
})
}
}
}
// read ts to dart file.
const dartParameters = api.fs.existsSync(dartParametersFile)
? api.fs
.readFileSync(dartParametersFile, 'utf-8')
// replace import so we can import it in anywhere
.replace("import 'dart:convert';", '')
// Don not know why quick type add an Class after our parameters name
.replace(/ParamsClass\b/g, 'Params')
: ''
const dartResponses = api.fs.existsSync(dartResponsesFile)
? api.fs
.readFileSync(dartResponsesFile, 'utf-8')
// replace import so we can import it in anywhere
.replace("import 'dart:convert';", '')
: ''
await api.tmpl(options.tpl, out, {
definitions: element,
model: `${dartParameters}\n${dartResponses}`,
fileName: getFullPath(key, true),
})
}
}
api.fs.remove(cache)
}
/**
* get full path by key
* @param key
*/
function getFullPath(key: string, filename: boolean = false) {
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
}
if (filename) {
return changeCase.snakeCase(p)
}
const fullPath =
(folder ? changeCase.snakeCase(folder) + '/' : '') + changeCase.snakeCase(p)
return fullPath
}