@windrun-huaiin/dev-scripts 3.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 D8ger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,331 @@
1
+ # @packages/dev-scripts
2
+
3
+ 一个用于多语言项目的开发脚本工具集,支持翻译检查、翻译清理、博客索引生成等功能。
4
+
5
+ ## 功能特性
6
+
7
+ - ✅ **翻译检查**: 检查翻译文件的完整性和一致性
8
+ - 🧹 **翻译清理**: 自动清理未使用的翻译键
9
+ - 📝 **博客索引生成**: 自动生成博客索引和月度统计
10
+ - ⚙️ **配置驱动**: 支持多种配置方式,适配不同项目结构
11
+ - 🔧 **CLI工具**: 统一的命令行接口,易于集成到构建流程
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ # 在 monorepo 中使用
17
+ pnpm add -D @packages/dev-scripts
18
+
19
+ # 或在独立项目中使用
20
+ npm install -D @packages/dev-scripts
21
+ ```
22
+
23
+ ## 快速开始
24
+
25
+ ### 1. 配置 package.json
26
+
27
+ 在项目的 `package.json` 中添加配置:
28
+
29
+ ```json
30
+ {
31
+ "scripts": {
32
+ "check-translations": "dev-scripts check-translations",
33
+ "clean-translations": "dev-scripts clean-translations",
34
+ "generate-blog-index": "dev-scripts generate-blog-index"
35
+ },
36
+ "devScripts": {
37
+ "locales": ["en", "zh"],
38
+ "defaultLocale": "en",
39
+ "messageRoot": "messages",
40
+ "scanDirs": ["src/**/*.{tsx,ts,jsx,js}"],
41
+ "blogDir": "src/mdx/blog",
42
+ "logDir": "logs"
43
+ }
44
+ }
45
+ ```
46
+
47
+ ### 2. 运行命令
48
+
49
+ ```bash
50
+ # 检查翻译完整性
51
+ pnpm check-translations
52
+
53
+ # 清理未使用的翻译键(仅显示)
54
+ pnpm clean-translations
55
+
56
+ # 实际删除未使用的翻译键
57
+ pnpm clean-translations --remove
58
+
59
+ # 生成博客索引
60
+ pnpm generate-blog-index
61
+ ```
62
+
63
+ ## 配置选项
64
+
65
+ ### 通过 package.json 配置
66
+
67
+ 在 `package.json` 中添加 `devScripts` 字段:
68
+
69
+ ```json
70
+ {
71
+ "devScripts": {
72
+ "locales": ["en", "zh", "ja"], // 支持的语言列表
73
+ "defaultLocale": "en", // 默认语言
74
+ "messageRoot": "messages", // 翻译文件目录
75
+ "scanDirs": [ // 扫描的代码目录
76
+ "src/**/*.{tsx,ts,jsx,js}"
77
+ ],
78
+ "blogDir": "src/mdx/blog", // 博客MDX文件目录
79
+ "logDir": "logs" // 日志输出目录
80
+ }
81
+ }
82
+ ```
83
+
84
+ ### 通过配置文件
85
+
86
+ 创建 `dev-scripts.config.json`:
87
+
88
+ ```json
89
+ {
90
+ "i18n": {
91
+ "locales": ["en", "zh"],
92
+ "defaultLocale": "en",
93
+ "messageRoot": "messages"
94
+ },
95
+ "scan": {
96
+ "include": ["src/**/*.{tsx,ts,jsx,js}"],
97
+ "exclude": ["src/**/*.test.ts", "src/**/*.d.ts"]
98
+ },
99
+ "blog": {
100
+ "mdxDir": "src/mdx/blog",
101
+ "outputFile": "index.mdx",
102
+ "metaFile": "meta.json",
103
+ "iocSlug": "ioc",
104
+ "prefix": "blog"
105
+ },
106
+ "output": {
107
+ "logDir": "logs",
108
+ "verbose": false
109
+ }
110
+ }
111
+ ```
112
+
113
+ ## 命令详解
114
+
115
+ ### check-translations
116
+
117
+ 检查翻译文件的完整性和一致性。
118
+
119
+ ```bash
120
+ dev-scripts check-translations [options]
121
+
122
+ Options:
123
+ -v, --verbose 显示详细日志
124
+ --config <path> 指定配置文件路径
125
+ -h, --help 显示帮助信息
126
+ ```
127
+
128
+ **功能:**
129
+ - 扫描代码中使用的翻译键
130
+ - 检查翻译文件中是否存在对应的键
131
+ - 检查不同语言文件之间的一致性
132
+ - 生成详细的检查报告
133
+
134
+ **输出示例:**
135
+ ```
136
+ === 翻译检查报告 ===
137
+
138
+ ✅ en 翻译文件中包含所有使用的命名空间
139
+ ✅ zh 翻译文件中包含所有使用的命名空间
140
+
141
+ 🔴 en 翻译文件中缺失的键:
142
+ - common.newFeature
143
+ - dashboard.analytics
144
+
145
+ ✅ zh 翻译文件中包含所有使用的键
146
+ ```
147
+
148
+ ### clean-translations
149
+
150
+ 清理未使用的翻译键。
151
+
152
+ ```bash
153
+ dev-scripts clean-translations [options]
154
+
155
+ Options:
156
+ -v, --verbose 显示详细日志
157
+ --remove 实际删除未使用的键(默认只显示)
158
+ --config <path> 指定配置文件路径
159
+ -h, --help 显示帮助信息
160
+ ```
161
+
162
+ **功能:**
163
+ - 找出翻译文件中未在代码中使用的键
164
+ - 支持安全预览模式(默认)
165
+ - 支持实际删除模式(--remove)
166
+ - 自动清理空的命名空间对象
167
+
168
+ ### generate-blog-index
169
+
170
+ 生成博客索引文件和月度统计。
171
+
172
+ ```bash
173
+ dev-scripts generate-blog-index [options]
174
+
175
+ Options:
176
+ -v, --verbose 显示详细日志
177
+ --config <path> 指定配置文件路径
178
+ -h, --help 显示帮助信息
179
+ ```
180
+
181
+ **功能:**
182
+ - 扫描博客MDX文件
183
+ - 解析frontmatter元数据
184
+ - 生成主索引页面
185
+ - 生成月度统计页面
186
+ - 自动排序和分类
187
+
188
+ ## 支持的翻译模式
189
+
190
+ 脚本支持多种翻译使用模式:
191
+
192
+ ```typescript
193
+ // useTranslations hook
194
+ const t = useTranslations('common')
195
+ t('welcome')
196
+
197
+ // getTranslations (服务端)
198
+ const t = await getTranslations('common')
199
+ t('welcome')
200
+
201
+ // 带参数的getTranslations
202
+ const t = await getTranslations({ locale, namespace: 'common' })
203
+ t('welcome')
204
+
205
+ // FormattedMessage组件
206
+ <FormattedMessage id="welcome" />
207
+
208
+ // 模板字符串(动态键)
209
+ t(`tags.${tagName}`)
210
+
211
+ // 变量键
212
+ t(menuItem.key)
213
+ ```
214
+
215
+ ## 项目结构要求
216
+
217
+ ### 翻译文件结构
218
+
219
+ ```
220
+ messages/
221
+ ├── en.json # 英文翻译
222
+ ├── zh.json # 中文翻译
223
+ └── ja.json # 日文翻译(可选)
224
+ ```
225
+
226
+ 翻译文件格式:
227
+ ```json
228
+ {
229
+ "common": {
230
+ "welcome": "Welcome",
231
+ "goodbye": "Goodbye"
232
+ },
233
+ "dashboard": {
234
+ "title": "Dashboard",
235
+ "analytics": {
236
+ "title": "Analytics",
237
+ "views": "Views"
238
+ }
239
+ }
240
+ }
241
+ ```
242
+
243
+ ### 博客文件结构
244
+
245
+ ```
246
+ src/mdx/blog/
247
+ ├── index.mdx # 自动生成的索引文件
248
+ ├── ioc.mdx # 自动生成的月度统计
249
+ ├── meta.json # 特色文章配置
250
+ ├── 2024-01-01.mdx # 博客文章
251
+ ├── 2024-01-15.mdx
252
+ └── ...
253
+ ```
254
+
255
+ 博客文章frontmatter格式:
256
+ ```markdown
257
+ ---
258
+ title: "文章标题"
259
+ description: "文章描述"
260
+ icon: "BookOpen"
261
+ date: "2024-01-01"
262
+ ---
263
+
264
+ 文章内容...
265
+ ```
266
+
267
+ ## 集成示例
268
+
269
+ ### GitHub Actions
270
+
271
+ ```yaml
272
+ name: Check Translations
273
+ on: [push, pull_request]
274
+
275
+ jobs:
276
+ check:
277
+ runs-on: ubuntu-latest
278
+ steps:
279
+ - uses: actions/checkout@v3
280
+ - uses: actions/setup-node@v3
281
+ with:
282
+ node-version: '18'
283
+ - run: npm install
284
+ - run: npm run check-translations
285
+ ```
286
+
287
+ ### Pre-commit Hook
288
+
289
+ ```json
290
+ {
291
+ "husky": {
292
+ "hooks": {
293
+ "pre-commit": "dev-scripts check-translations"
294
+ }
295
+ }
296
+ }
297
+ ```
298
+
299
+ ## 故障排除
300
+
301
+ ### 常见问题
302
+
303
+ 1. **找不到翻译文件**
304
+ - 检查 `messageRoot` 配置是否正确
305
+ - 确保翻译文件存在且为有效JSON格式
306
+
307
+ 2. **扫描不到代码文件**
308
+ - 检查 `scanDirs` 配置是否包含正确的glob模式
309
+ - 确保文件路径相对于项目根目录
310
+
311
+ 3. **翻译键检测不准确**
312
+ - 当前基于正则表达式匹配,对于复杂的动态键可能需要手动处理
313
+ - 使用命名规范来帮助脚本识别(如 t1, t2 用于不同命名空间)
314
+
315
+ ### 调试模式
316
+
317
+ 使用 `--verbose` 选项获取详细日志:
318
+
319
+ ```bash
320
+ dev-scripts check-translations --verbose
321
+ ```
322
+
323
+ 这将显示:
324
+ - 扫描的文件列表
325
+ - 找到的翻译函数映射
326
+ - 提取的翻译键
327
+ - 详细的检查过程
328
+
329
+ ## 许可证
330
+
331
+ MIT License
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../dist/cli.js')