feima-shortcuts 0.3.0-beta.1 → 0.3.0-beta.3

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feima-shortcuts",
3
- "version": "0.3.0-beta.1",
3
+ "version": "0.3.0-beta.3",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -109,6 +109,16 @@ function checkPageLocale(pageDir) {
109
109
 
110
110
  const localeKeys = loadLocaleKeys(localePath)
111
111
  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
112
122
  const unused = localeKeys.filter((k) => !usedKeys.has(k))
113
123
 
114
124
  if (unused.length) {
@@ -137,6 +147,8 @@ function checkComponentLocale(componentDir) {
137
147
  const invalidFiles = []
138
148
  const skippedFiles = []
139
149
 
150
+ const undefinedKeyMap = new Map() // 文件 -> [key]
151
+
140
152
  vueFiles.forEach((file) => {
141
153
  const code = fs.readFileSync(file, 'utf-8')
142
154
  const localeImport = detectLocaleImport(code)
@@ -152,7 +164,14 @@ function checkComponentLocale(componentDir) {
152
164
  return
153
165
  }
154
166
 
155
- extractUsedKeys(code).forEach((k) => usedKeys.add(k))
167
+ const keys = extractUsedKeys(code)
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
+ }
156
175
  })
157
176
 
158
177
  let hasError = false
@@ -181,6 +200,15 @@ function checkComponentLocale(componentDir) {
181
200
  )
182
201
  }
183
202
 
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
+
184
212
  return hasError
185
213
  }
186
214
 
@@ -211,3 +239,7 @@ function run() {
211
239
  }
212
240
 
213
241
  exports.run = run
242
+
243
+ if (require.main === module) {
244
+ run()
245
+ }