-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.ts
283 lines (242 loc) 路 7.28 KB
/
util.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import * as TJS from 'typescript-json-schema'
import * as Path from 'path'
import fs from 'fs'
import path from 'path'
import NestedFolder from 'nested-static'
/**
* @description
* Built-in Time Typescript Interface Parser
*/
export const parseInterfaces = async ( filePath: string) => {
const settings: TJS.PartialArgs = {
aliasRef: true,
required: true
}
const compilerOptions: TJS.CompilerOptions = { strictNullChecks: true }
const program = TJS.getProgramFromFiles([Path.resolve(filePath)], compilerOptions)
const schema = TJS.generateSchema(program, '*', settings)
if(schema == null || schema.definitions == null) throw new Error('Failed Generator Init')
return schema.definitions
}
/**
* @description
* Collect Typescript Interface Files Code
*/
export const collectInterfaceFiles = async (
folderPath: string,
callback: (argument: {
fileName: string
filePath: string
subPath: string
staticPath: string
fileData: string
interfaceName: string
typeName: string
}[]) => Promise<void>,
// No Collect Index File
noIndexFile: Boolean = true
) => {
// Search all of the child folders
return new Promise((resolve)=>{
NestedFolder(folderPath, async (folders)=>{
let data: any = []
for(let folder of folders){
let files = fs.readdirSync(folder.staticPath)
// Search all of the files
for(let file of files){
let filePath = folder.staticPath + '/' + file
filePath = path.resolve(filePath)
let stats = fs.statSync(filePath)
if(stats.isDirectory()) continue
if(noIndexFile && file == 'index.ts') continue
let fileData = String(fs.readFileSync(filePath))
let interfaceName = ''
let typeName = ``
try{
interfaceName =
fileData.split('export interface ')[1]
.split(' {')[0]
.split(' ').join('')
}catch(e){}
try{
typeName =
fileData.split('export type ')[1]
.split('\n')[0]
.split(' ').join('')
}catch(e){}
data.push({
fileName: file,
filePath,
subPath: folder.subPath,
staticPath: folder.staticPath,
fileData,
interfaceName,
typeName
})
}
}
await callback(data)
resolve()
})
})
}
/**
* @description
* The functions in the overwatch
* workshop are named in the pascal case.
* But the javascript are uses a camelcase,
* so we need to convert it.
*
* It'll be using in archived value function collector
* (`sequence/collectArchivedValue.ts`)
*/
export const camelCaseToPascalCase = (
name: string,
doSpace = true,
prefixRemove = false
)=>{
if(prefixRemove && name[0] == 'I'){
let temp = name.split('')
temp.shift()
name = temp.join('')
}
let pascalCase = ''
for(let alphabetIndex in name.split('')){
let alphabet = name[alphabetIndex]
if(Number(alphabetIndex) == 0){
pascalCase += alphabet.toUpperCase()
continue
}
let isUpperCase = alphabet == alphabet.toUpperCase()
if(isUpperCase){
if(doSpace) pascalCase += ' '
pascalCase += alphabet.toUpperCase()
}else{
pascalCase += alphabet.toLowerCase()
}
}
return pascalCase
}
/**
* @description
* The javascript interface name uses a pascal case.
* Conversion to camera case is required to
* infer variable names etc. through interface name.
*
* It'll be using when the collects resolver type names
* (`sequence/collectType.ts`)
*/
export const pascalCaseToCamelCase = (
name: string,
doSpace = true,
prefixRemove = false,
prefixInstall = false
)=>{
let camelCase = ''
if(prefixRemove && name[0] == 'I'){
let temp = name.split('')
temp.shift()
name = temp.join('')
}
for(let alphabetIndex in name.split('')){
let alphabet = name[alphabetIndex]
if(Number(alphabetIndex) == 0){
camelCase += alphabet.toLowerCase()
continue
}
let isLowerCase = alphabet == alphabet.toLowerCase()
if(isLowerCase){
camelCase += alphabet.toLowerCase()
}else{
if(doSpace) camelCase += ' '
camelCase += alphabet.toUpperCase()
}
}
if(prefixInstall && name[0] != 'I')
name = `I${name}`
return camelCase
}
/**
* @description
* Extracts the custom type name
* defined in the typescript file.
*/
export const typeExtractor = (code): string[] => {
let contexts = code
contexts = contexts.split('\t').join('')
contexts = contexts.split(' ').join('')
contexts = contexts.split('export type ')[1].split('\n')
contexts.shift()
let list: any = []
for(let context of contexts){
//console.log(context)
if(context[0] == '=' || context[0] == '|'){
if(context[1] == ' '){
context = context
.split('= ').join('')
.split('| ').join('')
.split('\r').join('')
list.push(context)
}
}
}
return list
}
/**
* @description
* In some overwatch interfaces,
* typedescript data type is sometimes used as it is.
* This pure data type designation
* must be extracted to form a generator.
*/
export const pureTypeNameExtractor = (name) => {
let typeNames: string[] = []
let word = ''
for(let char of name.split('')){
let isUpperCase = char.toUpperCase() == char
if(word.length != 0 && isUpperCase){
typeNames.push(word)
word = ''
}
word += char
}
if(word.length != 0)
typeNames.push(word)
typeNames.pop()
if(typeNames.length > 1){
if(typeNames[0] == 'Value')
typeNames.shift()
}
return typeNames.join('')
}
export const allUpperCaseToPascalCase = (
name,
doSpace = true,
prefixRemove = false,
prefixInstall = false
)=>{
let camelCase = ''
if(prefixRemove && name[0] == 'I'){
let temp = name.split('')
temp.shift()
name = temp.join('')
}
for(let alphabetIndex in name.split('')){
let alphabet = name[alphabetIndex]
if(Number(alphabetIndex) == 0){
camelCase += alphabet.toUpperCase()
continue
}
try{
let prevChar = name[Number(alphabetIndex)-1]
if(prevChar == ' '){
camelCase += alphabet.toUpperCase()
continue
}
}catch(e){}
camelCase += alphabet.toLowerCase()
}
if(prefixInstall && name[0] != 'I')
name = `I${name}`
return camelCase
}