@sakitam-gis/sprite-cli 1.0.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/README.md ADDED
@@ -0,0 +1,395 @@
1
+ # @sakitam-gis/sprite-cli
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@sakitam-gis/sprite-cli.svg)](https://www.npmjs.com/package/@sakitam-gis/sprite-cli)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@sakitam-gis/sprite-cli.svg)](https://www.npmjs.com/package/@sakitam-gis/sprite-cli)
5
+ [![license](https://img.shields.io/npm/l/@sakitam-gis/sprite-cli.svg)](https://github.com/sakitam-gis/sprite-cli/blob/main/LICENSE.md)
6
+ [![Node.js CI](https://github.com/sakitam-gis/sprite-cli/workflows/Node.js%20CI/badge.svg)](https://github.com/sakitam-gis/sprite-cli/actions)
7
+
8
+ 一个现代化的、功能强大的雪碧图生成工具,用于从 SVG 和 PNG 文件生成优化的雪碧图(sprite sheets)。使用 TypeScript 编写,具有完善的工程化配置和高级优化功能。
9
+
10
+ > 本项目基于 [@mapbox/spritezero](https://github.com/mapbox/spritezero) 核心功能,完全使用 TypeScript 重写,无需外部依赖,并添加了更多高级特性。
11
+
12
+ [English](./README.md) | [快速开始](./QUICK_START.md)
13
+
14
+ ## ✨ 特性
15
+
16
+ ### 🎨 图像支持
17
+ - ✅ **SVG 支持** - 原生支持矢量图形,自动优化
18
+ - ✅ **PNG 支持** - 支持光栅图像输入
19
+ - ✅ **多种像素比率** - 支持 1x、2x、3x 等 Retina 显示屏
20
+ - ✅ **智能去重** - unique 模式,自动识别相同图像
21
+
22
+ ### ⚡️ 性能优化
23
+ - 🚀 **高效布局算法** - Shelf Best Height Fit,空间利用率 80-95%
24
+ - 🔥 **并发处理** - 多图像并行处理,提升性能
25
+ - 💾 **智能缓存** - SVG 解析缓存,避免重复计算
26
+ - 🗜️ **PNG 优化** - 使用 Sharp 进行颜色量化和压缩优化
27
+
28
+ ### 📋 元数据支持
29
+ - 📐 **9-patch 元数据** - 支持 stretchX/Y 区域定义
30
+ - 📄 **内容区域** - 支持 content 区域定义
31
+ - 📝 **文本占位符** - 支持 placeholder 区域定义
32
+ - 🏷️ **自动提取** - 基于 Mapbox 规范自动提取元数据
33
+
34
+ ### 🛠️ 开发体验
35
+ - 💪 **TypeScript 编写** - 完整的类型支持和 IDE 智能提示
36
+ - ⚙️ **现代化工具链** - ESLint、Prettier、Husky、Vitest
37
+ - 📦 **零配置使用** - 开箱即用的 CLI 工具
38
+ - ✅ **完善测试** - 50+ 测试用例,覆盖核心功能
39
+
40
+ ## 📦 安装
41
+
42
+ ### 全局安装
43
+
44
+ ```bash
45
+ npm install -g @sakitam-gis/sprite-cli
46
+ # 或
47
+ pnpm add -g @sakitam-gis/sprite-cli
48
+ # 或
49
+ yarn global add @sakitam-gis/sprite-cli
50
+ ```
51
+
52
+ ### 本地安装
53
+
54
+ ```bash
55
+ npm install --save-dev @sakitam-gis/sprite-cli
56
+ # 或
57
+ pnpm add -D @sakitam-gis/sprite-cli
58
+ # 或
59
+ yarn add -D @sakitam-gis/sprite-cli
60
+ ```
61
+
62
+ ## 🚀 快速开始
63
+
64
+ ### 安装
65
+
66
+ ```bash
67
+ # npm
68
+ npm install -g @sakitam-gis/sprite-cli
69
+
70
+ # pnpm (推荐)
71
+ pnpm add -g @sakitam-gis/sprite-cli
72
+
73
+ # yarn
74
+ yarn global add @sakitam-gis/sprite-cli
75
+ ```
76
+
77
+ ### 基本用法
78
+
79
+ ```bash
80
+ sprite-maker [output] [input] [options]
81
+ ```
82
+
83
+ ### 示例
84
+
85
+ #### 1️⃣ 生成标准雪碧图(1x)
86
+
87
+ ```bash
88
+ sprite-maker output/sprite icons/
89
+ ```
90
+
91
+ 生成:
92
+ - `output/sprite.png` - 雪碧图图片
93
+ - `output/sprite.json` - 布局信息(包含位置和元数据)
94
+
95
+ #### 2️⃣ 生成 Retina 雪碧图(2x)
96
+
97
+ ```bash
98
+ sprite-maker output/sprite@2x icons/ --retina
99
+ # 或
100
+ sprite-maker output/sprite@2x icons/ --ratio=2
101
+ ```
102
+
103
+ #### 3️⃣ 生成多种分辨率
104
+
105
+ ```bash
106
+ sprite-maker output/sprite icons/ # 1x
107
+ sprite-maker output/sprite@2x icons/ --ratio=2 # 2x
108
+ sprite-maker output/sprite@3x icons/ --ratio=3 # 3x
109
+ ```
110
+
111
+ #### 4️⃣ 使用 unique 模式(去重)
112
+
113
+ ```bash
114
+ sprite-maker output/sprite icons/ --unique
115
+ ```
116
+
117
+ #### 5️⃣ PNG 优化选项
118
+
119
+ ```bash
120
+ # 调色板模式(减小文件大小)
121
+ sprite-maker output/sprite icons/ --palette --colors=128
122
+
123
+ # 自定义质量
124
+ sprite-maker output/sprite icons/ --quality=90
125
+ ```
126
+
127
+ ## 📖 详细文档
128
+
129
+ 查看 [快速开始指南](./QUICK_START.md) 获取更多详细示例和高级用法。
130
+
131
+ ## 🔧 CLI 选项
132
+
133
+ | 选项 | 说明 | 默认值 |
134
+ |------|------|--------|
135
+ | `--retina` | 生成 2x 像素比率(等同于 --ratio=2) | `false` |
136
+ | `--ratio=<n>` | 指定像素比率(1x, 2x, 3x 等) | `1` |
137
+ | `--unique` | 去重模式,相同图像只存储一次 | `false` |
138
+ | `--colors=<n>` | PNG 颜色数量(2-256) | `256` |
139
+ | `--quality=<n>` | PNG 质量(0-100) | `80` |
140
+ | `--palette` | 使用调色板模式压缩 PNG | `false` |
141
+ | `--help, -h` | 显示帮助信息 | - |
142
+
143
+ ## 🛠️ 开发
144
+
145
+ ### 环境要求
146
+
147
+ - Node.js >= 18.0.0
148
+ - pnpm(推荐)或 npm / yarn
149
+
150
+ ### 克隆项目
151
+
152
+ ```bash
153
+ git clone <repository-url>
154
+ cd icon-cli
155
+ ```
156
+
157
+ ### 安装依赖
158
+
159
+ ```bash
160
+ pnpm install
161
+ # 或
162
+ npm install
163
+ ```
164
+
165
+ ### 开发命令
166
+
167
+ ```bash
168
+ # 开发模式(监听文件变化)
169
+ pnpm dev
170
+
171
+ # 构建
172
+ pnpm build
173
+
174
+ # 类型检查
175
+ pnpm type-check
176
+
177
+ # 代码检查
178
+ pnpm lint
179
+
180
+ # 修复代码问题
181
+ pnpm lint:fix
182
+
183
+ # 格式化代码
184
+ pnpm format
185
+
186
+ # 检查代码格式
187
+ pnpm format:check
188
+
189
+ # 运行测试
190
+ pnpm test
191
+
192
+ # 监听模式运行测试
193
+ pnpm test:watch
194
+
195
+ # 测试覆盖率
196
+ pnpm test:coverage
197
+ ```
198
+
199
+ ## 🏗️ 项目结构
200
+
201
+ ```
202
+ .
203
+ ├── src/
204
+ │ ├── cli.ts # CLI 入口文件
205
+ │ ├── types.ts # TypeScript 类型定义
206
+ │ ├── utils.ts # 工具函数
207
+ │ └── core/
208
+ │ ├── index.ts # 核心模块导出
209
+ │ ├── layout.ts # 布局算法(Shelf Best Height Fit)
210
+ │ ├── image.ts # 图像渲染和 PNG 优化
211
+ │ ├── metadata.ts # SVG 元数据提取
212
+ │ └── shelf-pack.ts # ShelfPack 打包算法
213
+ ├── test/
214
+ │ ├── cli.test.ts # CLI 端到端测试
215
+ │ ├── layout-efficiency.test.ts # 布局效率测试
216
+ │ ├── sorting.test.ts # 排序稳定性测试
217
+ │ ├── metadata.test.ts # 元数据提取测试
218
+ │ ├── performance.test.ts # 性能优化测试
219
+ │ ├── shelf-pack.test.ts # ShelfPack 算法测试
220
+ │ ├── error-handling.test.ts # 错误处理测试
221
+ │ └── fixture/ # 测试 fixtures
222
+ ├── dist/ # 编译输出目录
223
+ ├── tsconfig.json # TypeScript 配置
224
+ ├── tsup.config.ts # 构建配置(tsup)
225
+ ├── vitest.config.ts # 测试配置(vitest)
226
+ ├── .eslintrc.cjs # ESLint 配置
227
+ ├── .prettierrc.json # Prettier 配置
228
+ └── commitlint.config.cjs # Commitlint 配置
229
+ ```
230
+
231
+ ## 🔧 工程化配置
232
+
233
+ 本项目采用了现代化的工程化工具链:
234
+
235
+ ### 代码质量
236
+
237
+ - **TypeScript** - 类型安全
238
+ - **ESLint** - 代码检查
239
+ - **Prettier** - 代码格式化
240
+ - **lint-staged** - Git 暂存区文件检查
241
+
242
+ ### Git 规范
243
+
244
+ - **Husky** - Git hooks 管理
245
+ - **commitlint** - Commit 信息规范检查
246
+
247
+ Commit 信息格式:
248
+
249
+ ```
250
+ <type>(<scope>): <subject>
251
+
252
+ type: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
253
+ ```
254
+
255
+ 示例:
256
+ ```bash
257
+ git commit -m "feat: 添加新的命令行选项"
258
+ git commit -m "fix: 修复文件路径解析问题"
259
+ git commit -m "docs: 更新 README 文档"
260
+ ```
261
+
262
+ ### 构建和测试
263
+
264
+ - **tsup** - 快速的 TypeScript 构建工具
265
+ - **Vitest** - 现代化的单元测试框架
266
+
267
+ ## 📝 输出格式
268
+
269
+ ### JSON 布局文件
270
+
271
+ 生成的 JSON 文件包含每个图标的位置、尺寸和元数据信息:
272
+
273
+ ```json
274
+ {
275
+ "icon-name": {
276
+ "width": 24,
277
+ "height": 24,
278
+ "x": 0,
279
+ "y": 0,
280
+ "pixelRatio": 1,
281
+ "metadata": {
282
+ "stretchX": [[8, 16]],
283
+ "stretchY": [[8, 16]],
284
+ "content": [4, 4, 20, 20],
285
+ "placeholder": [6, 6, 18, 18]
286
+ }
287
+ }
288
+ }
289
+ ```
290
+
291
+ ### 元数据说明
292
+
293
+ - `stretchX`: 水平拉伸区域(9-patch)
294
+ - `stretchY`: 垂直拉伸区域(9-patch)
295
+ - `content`: 内容区域边界 `[left, top, right, bottom]`
296
+ - `placeholder`: 文本占位符区域 `[left, top, right, bottom]`
297
+
298
+ ### SVG 元数据标记
299
+
300
+ 在 SVG 文件中使用特殊的 ID 来定义元数据:
301
+
302
+ ```xml
303
+ <svg width="24" height="24">
304
+ <!-- 定义水平拉伸区域 -->
305
+ <path id="mapbox-stretch-x" d="M 8 0 L 16 0 L 16 24 L 8 24 Z"/>
306
+
307
+ <!-- 定义垂直拉伸区域 -->
308
+ <path id="mapbox-stretch-y" d="M 0 8 L 24 8 L 24 16 L 0 16 Z"/>
309
+
310
+ <!-- 定义内容区域 -->
311
+ <path id="mapbox-content" d="M 4 4 L 20 4 L 20 20 L 4 20 Z"/>
312
+
313
+ <!-- 定义文本占位符 -->
314
+ <path id="mapbox-text-placeholder" d="M 6 6 L 18 6 L 18 18 L 6 18 Z"/>
315
+ </svg>
316
+ ```
317
+
318
+ ### PNG 雪碧图
319
+
320
+ 生成的 PNG 文件是一个包含所有图标的合成图片,经过高级优化:
321
+
322
+ - ✅ 自动颜色量化(可配置)
323
+ - ✅ 最大压缩级别(level 9)
324
+ - ✅ 自适应滤波
325
+ - ✅ 可选调色板模式
326
+
327
+ ## 🤝 贡献
328
+
329
+ 欢迎贡献代码!请确保:
330
+
331
+ 1. Fork 本仓库
332
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
333
+ 3. 提交更改 (`git commit -m 'feat: 添加某个特性'`)
334
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
335
+ 5. 开启 Pull Request
336
+
337
+ 提交代码前,请确保:
338
+ - 通过所有测试 (`pnpm test`)
339
+ - 代码符合规范 (`pnpm lint`)
340
+ - 代码格式正确 (`pnpm format:check`)
341
+
342
+ ## 📄 许可证
343
+
344
+ ISC License
345
+
346
+ ## 🔍 核心算法
347
+
348
+ ### Shelf Best Height Fit
349
+
350
+ 本项目使用 Shelf Best Height Fit 算法进行 2D 矩形打包:
351
+
352
+ 1. **排序优化** - 按高度降序 + ID 字母序排序
353
+ 2. **智能打包** - 选择最合适的货架(shelf)放置图像
354
+ 3. **空间复用** - 自动复用释放的空闲区域
355
+ 4. **自动扩展** - 根据需要动态调整画布大小
356
+ 5. **空间利用率** - 达到 80-95%(比简单算法提升 10-20%)
357
+
358
+ ### 性能优化
359
+
360
+ - **SVG 缓存** - 缓存解析结果,避免重复计算
361
+ - **并发处理** - 使用 `Promise.all` 并行处理多个图像
362
+ - **惰性计算** - 按需提取元数据和优化
363
+
364
+ ## 📊 性能测试
365
+
366
+ 基准测试结果(361 个 SVG 图标):
367
+
368
+ ```
369
+ ✓ 布局算法效率 > 80%
370
+ ✓ SVG 缓存加速 > 2x
371
+ ✓ 并发处理加速 > 3x
372
+ ✓ 大批量处理 (361 icons) < 5s
373
+ ```
374
+
375
+ ## 🙏 致谢
376
+
377
+ - 原项目:[spritezero-cli](https://github.com/mapbox/spritezero-cli)
378
+ - 核心库:[@mapbox/spritezero](https://github.com/mapbox/spritezero)
379
+ - 打包算法:[ShelfPack](https://github.com/mapbox/shelf-pack)
380
+ - 图像处理:[Sharp](https://github.com/lovell/sharp)
381
+ - SVG 优化:[SVGO](https://github.com/svg/svgo)
382
+
383
+ ## 📮 联系
384
+
385
+ - Issues: [GitHub Issues](https://github.com/sakitam-gis/sprite-cli/issues)
386
+ - Pull Requests: [GitHub PRs](https://github.com/sakitam-gis/sprite-cli/pulls)
387
+ - NPM: [@sakitam-gis/sprite-cli](https://www.npmjs.com/package/@sakitam-gis/sprite-cli)
388
+
389
+ ## 📜 更新日志
390
+
391
+ 查看 [CHANGELOG.md](./CHANGELOG.md) 了解版本更新历史。
392
+
393
+ ## 🌟 Star History
394
+
395
+ [![Star History Chart](https://api.star-history.com/svg?repos=sakitam-gis/sprite-cli&type=Date)](https://star-history.com/#sakitam-gis/sprite-cli&Date)
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node