@chaoswise/intl 3.1.1 → 3.1.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/bin/chaoswise-intl.js +17 -4
- package/bin/scripts/conf/default.js +16 -2
- package/bin/scripts/nozhcn.js +125 -17
- package/bin/scripts/nozhcnGuard.js +444 -0
- package/bin/scripts/util/findZhCnInFile.js +127 -6
- package/bin/scripts/util/fixI18nDefaultInFile.js +102 -4
- package/bin/scripts/util/fixZhCnInFile.js +290 -0
- package/bin/scripts/util/makeVisitorDowngrade.js +82 -0
- package/bin/scripts/util/transformAst.js +11 -2
- package/bin/scripts/verify.js +115 -34
- package/package.json +1 -1
package/bin/scripts/verify.js
CHANGED
|
@@ -8,41 +8,14 @@ const transformAst = require('./util/transformAst');
|
|
|
8
8
|
const log = require('./util/log');
|
|
9
9
|
const file = require('./util/file');
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* - pending : 临时 key,存在于 relationKey.json(尚未执行 update 替换)
|
|
14
|
-
* - ok : 已在本地 locale 文件中找到
|
|
15
|
-
* - missing : 两处均找不到,属于"孤立 id",运行时将无法解析
|
|
16
|
-
*
|
|
17
|
-
* 用法:
|
|
18
|
-
* chaoswise-intl verify → 发现 missing 时 exit(1)(CI 门禁)
|
|
19
|
-
* chaoswise-intl verify --warn → 仅警告,不 exit(1)
|
|
20
|
-
*/
|
|
21
|
-
async function verify({ exitOnMissing = true } = {}) {
|
|
22
|
-
log.info('词条 id 校验中...');
|
|
23
|
-
|
|
24
|
-
const conf = getConf();
|
|
25
|
-
const files = targetOutputFiles(conf);
|
|
26
|
-
|
|
27
|
-
if (!files.length) {
|
|
28
|
-
log.warn('未找到需要扫描的文件,请检查 output 配置');
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// ── Step 1: 收集代码中所有原始 id(传入空对象,不触发替换也不写文件)──────
|
|
11
|
+
// ── 公共:扫描并分类所有 id ────────────────────────────────────────────────────
|
|
12
|
+
function scanIds(conf, files) {
|
|
33
13
|
const info = transformAst('update', files, conf, {});
|
|
34
14
|
const allIds = [...new Set(info.downloadIds.filter(Boolean))];
|
|
35
15
|
|
|
36
|
-
if (!allIds.length) {
|
|
37
|
-
log.success('未发现任何国际化 id,无需校验');
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// ── Step 2: 从 relationKey.json 获取待替换的临时 key 集合 ───────────────
|
|
42
16
|
const relationKey = file.readJson('relationKey.json') || {};
|
|
43
17
|
const pendingKeySet = new Set(Object.keys(relationKey));
|
|
44
18
|
|
|
45
|
-
// ── Step 3: 从本地 locale 文件获取已下载的合法 id 集合 ────────────────────
|
|
46
19
|
const rootPath = process.cwd();
|
|
47
20
|
const localeDir = path.resolve(rootPath, conf.localeOutput, 'locales');
|
|
48
21
|
const localeFiles = glob.sync(`${localeDir}/*.json`);
|
|
@@ -56,7 +29,6 @@ async function verify({ exitOnMissing = true } = {}) {
|
|
|
56
29
|
}
|
|
57
30
|
});
|
|
58
31
|
|
|
59
|
-
// ── Step 4: 分类统计 ──────────────────────────────────────────────────────
|
|
60
32
|
const pending = [];
|
|
61
33
|
const missing = [];
|
|
62
34
|
const ok = [];
|
|
@@ -71,27 +43,62 @@ async function verify({ exitOnMissing = true } = {}) {
|
|
|
71
43
|
}
|
|
72
44
|
}
|
|
73
45
|
|
|
74
|
-
|
|
46
|
+
return { allIds, pending, missing, ok };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ── 公共:打印分类结果 ─────────────────────────────────────────────────────────
|
|
50
|
+
function printResult({ allIds, pending, missing, ok }, files) {
|
|
75
51
|
console.log('');
|
|
76
52
|
log.info(`扫描到 ${allIds.length} 个唯一 id(文件数:${files.length})`);
|
|
77
53
|
|
|
78
54
|
if (ok.length) {
|
|
79
55
|
log.success(`✓ ${ok.length} 个 id 已在本地 locale 文件中覆盖`);
|
|
80
56
|
}
|
|
81
|
-
|
|
82
57
|
if (pending.length) {
|
|
83
58
|
log.warnToLog(`⚠ ${pending.length} 个临时 key 尚未执行 update 替换:`);
|
|
84
59
|
pending.forEach(({ tempKey, realId }) => {
|
|
85
60
|
log.warnToLog(` ${tempKey} → ${realId}`);
|
|
86
61
|
});
|
|
87
62
|
}
|
|
88
|
-
|
|
89
63
|
if (missing.length) {
|
|
90
|
-
log.error(`✗ ${missing.length}
|
|
64
|
+
log.error(`✗ ${missing.length} 个孤立 id(relationKey.json 和本地 locale 文件中均不存在):`);
|
|
91
65
|
missing.forEach((id) => log.error(` ${id}`));
|
|
92
66
|
console.log('');
|
|
93
67
|
log.error('提示:孤立 id 可能是未上传到国际化平台、未执行 update 拉取,或已在平台删除的词条');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* verify: 检测当前工程输出文件中所有 intl.get('id') 的 id
|
|
73
|
+
* - pending : 临时 key,存在于 relationKey.json(尚未执行 update 替换)
|
|
74
|
+
* - ok : 已在本地 locale 文件中找到
|
|
75
|
+
* - missing : 两处均找不到,属于"孤立 id",运行时将无法解析
|
|
76
|
+
*
|
|
77
|
+
* 用法:
|
|
78
|
+
* chaoswise-intl verify → 发现 missing 时 exit(1)(CI 门禁)
|
|
79
|
+
* chaoswise-intl verify --warn → 仅警告,不 exit(1)
|
|
80
|
+
*/
|
|
81
|
+
async function verify({ exitOnMissing = true } = {}) {
|
|
82
|
+
log.info('词条 id 校验中...');
|
|
83
|
+
|
|
84
|
+
const conf = getConf();
|
|
85
|
+
const files = targetOutputFiles(conf);
|
|
94
86
|
|
|
87
|
+
if (!files.length) {
|
|
88
|
+
log.warnToLog('未找到需要扫描的文件,请检查 output 配置');
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const result = scanIds(conf, files);
|
|
93
|
+
|
|
94
|
+
if (!result.allIds.length) {
|
|
95
|
+
log.success('未发现任何国际化 id,无需校验');
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
printResult(result, files);
|
|
100
|
+
|
|
101
|
+
if (result.missing.length) {
|
|
95
102
|
if (exitOnMissing) {
|
|
96
103
|
process.exit(1);
|
|
97
104
|
}
|
|
@@ -101,4 +108,78 @@ async function verify({ exitOnMissing = true } = {}) {
|
|
|
101
108
|
}
|
|
102
109
|
}
|
|
103
110
|
|
|
111
|
+
/**
|
|
112
|
+
* verifyFix: 将孤立 id 对应的 intl 调用降级回字符串字面量(downgrade 策略)
|
|
113
|
+
*
|
|
114
|
+
* 形态 A intl.get('orphan-id').d('默认文案') → '默认文案'
|
|
115
|
+
* 形态 B intl.get('orphan-id') → 无法自动恢复,仅打印警告 + 行号
|
|
116
|
+
*
|
|
117
|
+
* 修复完成后重新扫描,确认无残留孤立 id。
|
|
118
|
+
*
|
|
119
|
+
* 用法:
|
|
120
|
+
* chaoswise-intl verify fix
|
|
121
|
+
*/
|
|
122
|
+
async function verifyFix() {
|
|
123
|
+
log.info('孤立词条降级修复中...');
|
|
124
|
+
|
|
125
|
+
const conf = getConf();
|
|
126
|
+
const files = targetOutputFiles(conf);
|
|
127
|
+
|
|
128
|
+
if (!files.length) {
|
|
129
|
+
log.warnToLog('未找到需要扫描的文件,请检查 output 配置');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ── Step 1: 扫描,找出孤立 id ────────────────────────────────────────────
|
|
134
|
+
const { missing, allIds } = scanIds(conf, files);
|
|
135
|
+
|
|
136
|
+
if (!allIds.length) {
|
|
137
|
+
log.success('未发现任何国际化 id,无需处理');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!missing.length) {
|
|
142
|
+
log.success('没有孤立 id,无需修复');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
log.info(`共 ${missing.length} 个孤立 id,开始降级处理...`);
|
|
147
|
+
missing.forEach((id) => log.warnToLog(` - ${id}`));
|
|
148
|
+
console.log('');
|
|
149
|
+
|
|
150
|
+
// ── Step 2: 执行 downgrade AST 变换(写回文件)────────────────────────────
|
|
151
|
+
const missingIdSet = new Set(missing);
|
|
152
|
+
const noDefaultIds = []; // 形态B:无法自动恢复的裸 id
|
|
153
|
+
|
|
154
|
+
// transformAst 的 replaceWords 参数传入非空对象才触发写文件,
|
|
155
|
+
// downgrade visitor 通过 returns.missingIdSet / noDefaultIds 通信
|
|
156
|
+
transformAst('downgrade', files, conf, missingIdSet, noDefaultIds);
|
|
157
|
+
|
|
158
|
+
// ── Step 3: 打印形态 B 警告 ──────────────────────────────────────────────
|
|
159
|
+
if (noDefaultIds.length) {
|
|
160
|
+
console.log('');
|
|
161
|
+
log.warnToLog(`⚠ ${noDefaultIds.length} 处孤立 id 没有 .d() 默认文案,无法自动恢复,需手动处理:`);
|
|
162
|
+
noDefaultIds.forEach(({ id, loc }) => {
|
|
163
|
+
const locStr = loc ? ` (行 ${loc.start.line},列 ${loc.start.column + 1})` : '';
|
|
164
|
+
log.warnToLog(` ${id}${locStr}`);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ── Step 4: 重新扫描,确认修复结果 ──────────────────────────────────────
|
|
169
|
+
console.log('');
|
|
170
|
+
log.info('修复完成,重新扫描验证...');
|
|
171
|
+
const after = scanIds(conf, files);
|
|
172
|
+
printResult(after, files);
|
|
173
|
+
|
|
174
|
+
if (!after.missing.length) {
|
|
175
|
+
console.log('');
|
|
176
|
+
log.success('★★★ 孤立 id 已全部降级,请执行 chaoswise-intl collect 重新国际化 ★★★');
|
|
177
|
+
} else {
|
|
178
|
+
console.log('');
|
|
179
|
+
log.error(`仍有 ${after.missing.length} 个孤立 id 未能自动修复(均为形态 B),请手动处理后重试`);
|
|
180
|
+
process.exit(1);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
104
184
|
module.exports = verify;
|
|
185
|
+
module.exports.fix = verifyFix;
|