-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.ts
207 lines (188 loc) · 5.62 KB
/
common.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
import path from 'path'
import { languageFilePath } from './index'
import fs from 'fs-extra'
import { platform } from 'os'
import { crc32 } from 'crc'
export interface ReturnValue {
index: number
output: string
lang: { [key: string]: any }
}
export interface JsonMap {
[key: string]: string
}
export interface ObjectMap {
[key: string]: any
}
export function getTextCodeId(t: string) {
return 'T_' + crc32(t).toString(32)
}
export function hasChinese(str = '') {
if (str.includes('$t(')) return false
return /[\u4E00-\u9FA5]+/g.test(str)
}
export function hasVueText(text = '') {
if (text.includes('{{') && text.includes('}}')) return true
return false
}
// 递归解析所有 Vue 表达式并生成数组
// ABC昵称{{ userInfo.A }}你好啊!{{ userInfo.B }}卧槽
// TO
// ["ABC昵称","{{ userInfo.A }}","你好啊!","{{ userInfo.B }}",卧槽]
export function htmlTextVueExpressParse(text = '') {
const result: string[] = []
function inner(point: number): number {
if (point > text.length - 1) return 0
const left = text.indexOf('{{', point)
const right = text.indexOf('}}', left)
if (right != -1 && left != -1) {
result.push(text.slice(point, left))
const vueExpress = text.slice(left, right + 2)
if (hasChinese(vueExpress)) throw new Error('The expression cannot contain Chinese characters')
result.push(vueExpress)
return inner(right + 2)
}
result.push(text.slice(point, text.length))
return 0
}
inner(0)
return result
}
export function hasVueAttr(attrName = '') {
const keywords = ['v-', '@', ':']
for (const iterator of keywords) {
if (attrName.indexOf(iterator) === 0) {
return true
}
}
return false
}
export function fillNumber(index = 0) {
if (index < 10) return `00${index}`
if (index < 100) return `0${index}`
if (index < 1000) return `${index}`
return String(index)
}
export function setObjectAttr(root: any, attrPath: string[], key: string, value: string) {
let current = root
for (let index = 0; index < attrPath.length; index++) {
const element = attrPath[index]
if (!current[element]) {
current[element] = {}
}
current = current[element]
}
current[key] = value
}
export function getObjectAttr(root: ObjectMap, attrPath: string[]): JsonMap | null {
let current = root
for (let index = 0; index < attrPath.length; index++) {
const element = attrPath[index]
if (!current[element]) return null
current = current[element]
}
return current
}
// 公共文本处理
export function includeCommonLang(commonLang: JsonMap, text: string) {
// 重复内容直接返回
for (const key in commonLang) {
if (commonLang[key] === text) return key
}
// 生成随机ID
const mapKey = getTextCodeId(text)
return mapKey
}
export function isExistsTextForLang(prefix: string, map: JsonMap, text: string): string {
const langJsonMap = JSON.parse(fs.readFileSync(languageFilePath, 'utf-8'))
for (const key in langJsonMap) {
const element = langJsonMap[key]
if (element === text) {
return key
}
}
return ''
}
export function includeLangMap(filePrefix: string, index: number, text: string, commonLang: JsonMap, outputLanguageConfig: JsonMap) {
let isBreak = false
let tFnKey = getTextCodeId(text)
if (isExistsTextForLang(filePrefix, outputLanguageConfig, text)) {
tFnKey = isExistsTextForLang(filePrefix, outputLanguageConfig, text)
isBreak = true
} else {
outputLanguageConfig[tFnKey] = text
}
return {
tFnKey,
isBreak,
}
}
// 寻找文件中最大的编码序号,并同时忽略 CommonText 内容
// $t 不能换行
export function findMax$tNumber(code = '') {
const lines = code.split('\n')
let max = 0
lines.forEach((line) => {
function inner(start = 0) {
const sp = line.indexOf('$t(', start)
const commonTextP = line.indexOf('CommonText', start)
const ep = line.indexOf(')', sp)
if (sp > 0 && ep > 0 && (commonTextP == -1 || commonTextP > ep)) {
const keyExpress = line.slice(sp + 3, ep)
const keyExpressArr = keyExpress.replace(/'/gim, '').split('')
let n = ''
let find = false
for (const ch of keyExpressArr) {
if (!isNaN(Number(ch))) {
find = true
n += String(ch)
} else if (find) {
break
}
}
if (Number(n) > max) max = Number(n)
inner(ep + 1)
}
}
inner()
})
return max
}
// 寻找文件中最大的编码序号,并同时忽略 CommonText 内容
// $t 不能换行
export function findMax$tNumberByLangFile(language: ObjectMap, prefix: string) {
let max = 0
const keyList = getObjectAttr(language, prefix.split('.'))
for (const key in keyList) {
if (!isNaN(Number(key)) && Number(key) > max) {
max = Number(key)
}
}
return max
}
// 取路径最后两项作为语言后缀
// /Users/wangkun/Documents/SupportProject/weplay-admin-huafu-project/src/view/Censor/components/guardList.vue
export function buildLangPrefix(targetPath: string) {
let prefix1 = ''
let prefix2 = ''
const LR = platform() === 'win32' ? '\\' : '/'
const prefix3 = targetPath.split(LR).slice(-2)
let tmpFlag = false
for (const iterator of targetPath.split(LR)) {
if (iterator === 'view') continue
if (iterator === 'app') continue
if (iterator === 'src') {
tmpFlag = true
continue
}
if (tmpFlag) {
prefix1 = iterator
break
}
}
if (prefix3[0] === prefix1) prefix3.shift()
prefix2 = prefix3.join('_').replace(path.extname(prefix3.join('')), '')
// 语言文件最终前缀
let filePrefix = [prefix1, prefix2].join('.')
return filePrefix
}