feima-shortcuts 0.3.0-beta.3 → 0.3.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -4,7 +4,8 @@ const path = require('path')
|
|
|
4
4
|
const glob = require('glob')
|
|
5
5
|
const ts = require('typescript')
|
|
6
6
|
|
|
7
|
-
/*
|
|
7
|
+
/* ================= 工具函数 ================= */
|
|
8
|
+
|
|
8
9
|
function flattenLocale(obj, prefix = '') {
|
|
9
10
|
let res = []
|
|
10
11
|
for (const k in obj) {
|
|
@@ -38,11 +39,19 @@ function loadLocaleKeys(localePath) {
|
|
|
38
39
|
return obj ? flattenLocale(obj) : []
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
/**
|
|
43
|
+
* 提取使用到的 locale key
|
|
44
|
+
* 兼容:
|
|
45
|
+
* t('a.b.c')
|
|
46
|
+
* t('a.b.c', 'fallback')
|
|
47
|
+
*/
|
|
41
48
|
function extractUsedKeys(code) {
|
|
42
|
-
const reg = /t\s*\(\s*['"`]([^'"`]+)['"`]\s
|
|
49
|
+
const reg = /t\s*\(\s*['"`]([^'"`]+)['"`]\s*(?:,|\))/g
|
|
43
50
|
const set = new Set()
|
|
44
51
|
let m
|
|
45
|
-
while ((m = reg.exec(code)))
|
|
52
|
+
while ((m = reg.exec(code))) {
|
|
53
|
+
set.add(m[1])
|
|
54
|
+
}
|
|
46
55
|
return set
|
|
47
56
|
}
|
|
48
57
|
|
|
@@ -77,7 +86,8 @@ function detectUseLocale(code) {
|
|
|
77
86
|
}
|
|
78
87
|
}
|
|
79
88
|
|
|
80
|
-
/* 页面 locale 检测 */
|
|
89
|
+
/* ================= 页面 locale 检测 ================= */
|
|
90
|
+
|
|
81
91
|
function checkPageLocale(pageDir) {
|
|
82
92
|
const localePath = path.join(pageDir, 'locale.ts')
|
|
83
93
|
const indexVue = path.join(pageDir, 'index.vue')
|
|
@@ -109,16 +119,6 @@ function checkPageLocale(pageDir) {
|
|
|
109
119
|
|
|
110
120
|
const localeKeys = loadLocaleKeys(localePath)
|
|
111
121
|
const usedKeys = extractUsedKeys(code)
|
|
112
|
-
|
|
113
|
-
// 检测代码用到但 locale.ts 未定义的 key
|
|
114
|
-
const undefinedKeys = Array.from(usedKeys).filter((k) => !localeKeys.includes(k))
|
|
115
|
-
if (undefinedKeys.length) {
|
|
116
|
-
console.log(`\n❗ 页面中使用了 locale.ts 中不存在的 key:`)
|
|
117
|
-
console.log(` ${indexVue}`)
|
|
118
|
-
undefinedKeys.forEach((k) => console.log(` - ${k}`))
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 检测 locale.ts 里未使用的 key
|
|
122
122
|
const unused = localeKeys.filter((k) => !usedKeys.has(k))
|
|
123
123
|
|
|
124
124
|
if (unused.length) {
|
|
@@ -131,7 +131,8 @@ function checkPageLocale(pageDir) {
|
|
|
131
131
|
return false
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
/* 组件 locale 检测 */
|
|
134
|
+
/* ================= 组件 locale 检测 ================= */
|
|
135
|
+
|
|
135
136
|
function checkComponentLocale(componentDir) {
|
|
136
137
|
const localePath = path.join(componentDir, 'locale.ts')
|
|
137
138
|
if (!fs.existsSync(localePath)) return false
|
|
@@ -147,8 +148,6 @@ function checkComponentLocale(componentDir) {
|
|
|
147
148
|
const invalidFiles = []
|
|
148
149
|
const skippedFiles = []
|
|
149
150
|
|
|
150
|
-
const undefinedKeyMap = new Map() // 文件 -> [key]
|
|
151
|
-
|
|
152
151
|
vueFiles.forEach((file) => {
|
|
153
152
|
const code = fs.readFileSync(file, 'utf-8')
|
|
154
153
|
const localeImport = detectLocaleImport(code)
|
|
@@ -164,14 +163,7 @@ function checkComponentLocale(componentDir) {
|
|
|
164
163
|
return
|
|
165
164
|
}
|
|
166
165
|
|
|
167
|
-
|
|
168
|
-
keys.forEach((k) => usedKeys.add(k))
|
|
169
|
-
|
|
170
|
-
// 检测未定义 key
|
|
171
|
-
const undefinedKeys = Array.from(keys).filter((k) => !localeKeys.includes(k))
|
|
172
|
-
if (undefinedKeys.length) {
|
|
173
|
-
undefinedKeyMap.set(file, undefinedKeys)
|
|
174
|
-
}
|
|
166
|
+
extractUsedKeys(code).forEach((k) => usedKeys.add(k))
|
|
175
167
|
})
|
|
176
168
|
|
|
177
169
|
let hasError = false
|
|
@@ -200,19 +192,10 @@ function checkComponentLocale(componentDir) {
|
|
|
200
192
|
)
|
|
201
193
|
}
|
|
202
194
|
|
|
203
|
-
if (undefinedKeyMap.size) {
|
|
204
|
-
hasError = true
|
|
205
|
-
console.log(`\n❗ 组件中存在未定义的 locale key:`)
|
|
206
|
-
for (const [file, keys] of undefinedKeyMap.entries()) {
|
|
207
|
-
console.log(` ${path.relative(process.cwd(), file)}`)
|
|
208
|
-
keys.forEach((k) => console.log(` - ${k}`))
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
195
|
return hasError
|
|
213
196
|
}
|
|
214
197
|
|
|
215
|
-
/* 主入口 */
|
|
198
|
+
/* ================= 主入口 ================= */
|
|
216
199
|
|
|
217
200
|
function run() {
|
|
218
201
|
const root = path.resolve(process.cwd(), 'src/views')
|
|
@@ -239,7 +222,3 @@ function run() {
|
|
|
239
222
|
}
|
|
240
223
|
|
|
241
224
|
exports.run = run
|
|
242
|
-
|
|
243
|
-
if (require.main === module) {
|
|
244
|
-
run()
|
|
245
|
-
}
|