feima-shortcuts 0.3.0-beta.4 → 0.3.0-beta.6

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/bin/feima.js CHANGED
@@ -3,7 +3,8 @@ const inquirer = require("inquirer");
3
3
  const { Command } = require('commander')
4
4
  const feima = require("../src/generate");
5
5
 
6
- const checkLocale = require('../src/scripts/check-locale')
6
+ const checkLocale = require('../src/scripts/check-locale')
7
+ const checkI18nDefault = require('../src/scripts/check-i18n-default')
7
8
  const { version } = require("../package.json"); // 读取 package.json 中的版本号
8
9
 
9
10
  const program = new Command()
@@ -19,6 +20,13 @@ program
19
20
  checkLocale.run();
20
21
  })
21
22
 
23
+ program
24
+ .command('check-i18n-default')
25
+ .description('检查未使用的 locale')
26
+ .action(() => {
27
+ checkI18nDefault.run();
28
+ })
29
+
22
30
 
23
31
  const run = () => {
24
32
  console.log(`🚀 当前版本:v${version}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feima-shortcuts",
3
- "version": "0.3.0-beta.4",
3
+ "version": "0.3.0-beta.6",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * i18n 默认值检查脚本
5
+ *
6
+ * 规则:
7
+ * - 扫描 src/components、src/views
8
+ * - 支持 t / $t
9
+ * - 识别是否存在默认值(第二个参数)
10
+ * - 汇总缺失默认值的 key
11
+ */
12
+
13
+ const fs = require('fs')
14
+ const path = require('path')
15
+ const glob = require('glob')
16
+
17
+ const ROOT = process.cwd()
18
+ const SCAN_DIRS = ['src/components', 'src/views']
19
+ const FILE_EXT = '{ts,js,tsx,jsx,vue}'
20
+
21
+ // 匹配:t('xxx') / t("xxx", "默认值")
22
+ const I18N_REG = /\b(\$?t)\s*\(\s*(['"])([^'"]+)\2\s*(?:,\s*(['"])([^'"]*)\4)?\s*\)/g
23
+
24
+ /** 结果结构 */
25
+ const resultMap = new Map()
26
+ const missingList = []
27
+
28
+ function scanFile(filePath) {
29
+ const code = fs.readFileSync(filePath, 'utf-8')
30
+ let match
31
+
32
+ while ((match = I18N_REG.exec(code))) {
33
+ const [, fn, , key, , defaultValue] = match
34
+
35
+ if (!resultMap.has(key)) {
36
+ resultMap.set(key, {
37
+ key,
38
+ defaultValue: defaultValue || '',
39
+ locations: [],
40
+ })
41
+ }
42
+
43
+ const record = resultMap.get(key)
44
+ record.locations.push({
45
+ file: path.relative(ROOT, filePath),
46
+ fn,
47
+ })
48
+
49
+ // 有一次缺失默认值就标记
50
+ if (!defaultValue) {
51
+ missingList.push({
52
+ key,
53
+ file: path.relative(ROOT, filePath),
54
+ })
55
+ }
56
+ }
57
+ }
58
+
59
+ function run() {
60
+ SCAN_DIRS.forEach((dir) => {
61
+ const absDir = path.join(ROOT, dir)
62
+ if (!fs.existsSync(absDir)) return
63
+
64
+ const files = glob.sync(`${absDir}/**/*.${FILE_EXT}`, {
65
+ nodir: true,
66
+ })
67
+
68
+ files.forEach(scanFile)
69
+ })
70
+
71
+ console.log('\n📦 i18n 使用统计\n')
72
+
73
+ console.log(`共发现 key 数量:${resultMap.size}`)
74
+ console.log(`缺失默认值使用次数:${missingList.length}\n`)
75
+
76
+ if (missingList.length) {
77
+ console.log('❌ 以下 key 缺失默认值:\n')
78
+
79
+ const uniq = new Map()
80
+ missingList.forEach((i) => {
81
+ if (!uniq.has(i.key)) uniq.set(i.key, [])
82
+ uniq.get(i.key).push(i.file)
83
+ })
84
+
85
+ uniq.forEach((files, key) => {
86
+ console.log(`- ${key}`)
87
+ files.forEach((f) => {
88
+ console.log(` ↳ ${f}`)
89
+ })
90
+ })
91
+
92
+ console.log('\n⚠️ 请为以上 key 补充默认值')
93
+ process.exitCode = 1
94
+ } else {
95
+ console.log('✅ 所有 i18n key 均设置了默认值')
96
+ }
97
+ }
98
+
99
+ exports.run = run
@@ -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*\)/g
49
+ const reg = /t\s*\(\s*['"`]([^'"`]+)['"`]\s*(?:,|\))/g
43
50
  const set = new Set()
44
51
  let m
45
- while ((m = reg.exec(code))) set.add(m[1])
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')
@@ -121,7 +131,8 @@ function checkPageLocale(pageDir) {
121
131
  return false
122
132
  }
123
133
 
124
- /* 组件 locale 检测 */
134
+ /* ================= 组件 locale 检测 ================= */
135
+
125
136
  function checkComponentLocale(componentDir) {
126
137
  const localePath = path.join(componentDir, 'locale.ts')
127
138
  if (!fs.existsSync(localePath)) return false
@@ -184,7 +195,7 @@ function checkComponentLocale(componentDir) {
184
195
  return hasError
185
196
  }
186
197
 
187
- /* 主入口 */
198
+ /* ================= 主入口 ================= */
188
199
 
189
200
  function run() {
190
201
  const root = path.resolve(process.cwd(), 'src/views')