@pz4l/tinyimg-cli 0.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 +227 -0
- package/README.zh-CN.md +227 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1093 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
[English](README.md) | [简体中文](README.zh-CN.md)
|
|
2
|
+
|
|
3
|
+
# tinyimg-cli
|
|
4
|
+
|
|
5
|
+
Smart image compression CLI tool based on TinyPNG with multi-API key management, intelligent caching, and concurrent compression.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Batch Compression** - Supports files, directories, and glob patterns
|
|
10
|
+
- **Multi-API Key Management** - Smart rotation strategies to maximize free quota usage
|
|
11
|
+
- **Intelligent Caching** - MD5-based permanent cache to avoid redundant compression
|
|
12
|
+
- **Concurrency Control** - Configurable parallel compression tasks
|
|
13
|
+
- **Multiple Key Strategies** - Three modes: random, round-robin, priority
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
### Global Installation (Recommended)
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Using npm
|
|
21
|
+
npm install -g @pz4l/tinyimg-cli
|
|
22
|
+
|
|
23
|
+
# Using pnpm
|
|
24
|
+
pnpm add -g tinyimg-cli
|
|
25
|
+
|
|
26
|
+
# Using yarn
|
|
27
|
+
yarn global add tinyimg-cli
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Local Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Using npm
|
|
34
|
+
npm install -D @pz4l/tinyimg-cli
|
|
35
|
+
|
|
36
|
+
# Using pnpm
|
|
37
|
+
pnpm add -D tinyimg-cli
|
|
38
|
+
|
|
39
|
+
# Using yarn
|
|
40
|
+
yarn add -D tinyimg-cli
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Quick Start
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# Add API Key
|
|
47
|
+
tinyimg key add YOUR_TINYPNG_API_KEY
|
|
48
|
+
|
|
49
|
+
# Compress all images in current directory
|
|
50
|
+
tinyimg *.png *.jpg
|
|
51
|
+
|
|
52
|
+
# Compress specified directory
|
|
53
|
+
tinyimg ./images/
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
### Basic Compression Command
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
tinyimg [options] <input...>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Input parameters support:**
|
|
65
|
+
|
|
66
|
+
- Single file: `image.png`
|
|
67
|
+
- Multiple files: `image1.png image2.jpg`
|
|
68
|
+
- Directory: `./images/`
|
|
69
|
+
- Glob pattern: `./assets/**/*.png`
|
|
70
|
+
|
|
71
|
+
### Command Options
|
|
72
|
+
|
|
73
|
+
| Option | Description | Default |
|
|
74
|
+
| ------------------------- | ----------------------------------------------------------- | -------- |
|
|
75
|
+
| `-o, --output <dir>` | Specify output directory | In-place |
|
|
76
|
+
| `-k, --key <key>` | Specify API Key (higher priority than env variable) | - |
|
|
77
|
+
| `-m, --mode <mode>` | Key usage strategy: `random` \| `round-robin` \| `priority` | `random` |
|
|
78
|
+
| `-p, --parallel <number>` | Concurrency limit | `8` |
|
|
79
|
+
| `-c, --cache` | Enable cache | `true` |
|
|
80
|
+
| `--no-cache` | Disable cache | - |
|
|
81
|
+
| `-h, --help` | Show help information | - |
|
|
82
|
+
|
|
83
|
+
### Key Management Commands
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# Add API Key
|
|
87
|
+
tinyimg key add <key>
|
|
88
|
+
|
|
89
|
+
# Remove API Key (interactive selection)
|
|
90
|
+
tinyimg key remove
|
|
91
|
+
|
|
92
|
+
# Remove specified API Key
|
|
93
|
+
tinyimg key remove <key>
|
|
94
|
+
|
|
95
|
+
# List all API Keys and quota information
|
|
96
|
+
tinyimg key
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Key Display Format:** API keys are displayed in a masked format (first 4 chars + last 4 chars, e.g., `abcd****efgh`) for easy identification while protecting sensitive information.
|
|
100
|
+
|
|
101
|
+
## Usage Examples
|
|
102
|
+
|
|
103
|
+
### Compress Single File
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
tinyimg photo.png
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Compress Multiple Files
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
tinyimg image1.png image2.jpg image3.jpeg
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Compress Entire Directory
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
tinyimg ./assets/images/
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Use Glob Pattern
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# Compress all PNG files
|
|
125
|
+
tinyimg "./src/**/*.png"
|
|
126
|
+
|
|
127
|
+
# Compress multiple formats
|
|
128
|
+
tinyimg "./assets/**/*.{png,jpg,jpeg}"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Specify Output Directory
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# Compress to dist directory, preserve directory structure
|
|
135
|
+
tinyimg ./assets/ -o ./dist/
|
|
136
|
+
|
|
137
|
+
# Compress to specified directory
|
|
138
|
+
tinyimg photo.png -o ./compressed/
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Use Specific Key Strategy
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
# Round-robin mode (evenly distribute quota)
|
|
145
|
+
tinyimg ./images/ -m round-robin
|
|
146
|
+
|
|
147
|
+
# Priority mode (prefer specified key)
|
|
148
|
+
tinyimg ./images/ -m priority
|
|
149
|
+
|
|
150
|
+
# Random mode (default)
|
|
151
|
+
tinyimg ./images/ -m random
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Adjust Concurrency
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
# Lower concurrency (suitable for unstable network)
|
|
158
|
+
tinyimg ./images/ -p 4
|
|
159
|
+
|
|
160
|
+
# Higher concurrency (suitable for high-performance network)
|
|
161
|
+
tinyimg ./images/ -p 16
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Disable Cache
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
# Force re-compression of all images
|
|
168
|
+
tinyimg ./images/ --no-cache
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Specify API Key
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
# Use specific key for compression (highest priority)
|
|
175
|
+
tinyimg ./images/ -k YOUR_API_KEY
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Environment Variables
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# Set API Keys (multiple keys separated by comma)
|
|
182
|
+
export TINYPNG_KEYS="key1,key2,key3"
|
|
183
|
+
|
|
184
|
+
# Run compression command
|
|
185
|
+
tinyimg ./images/
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Cache System
|
|
189
|
+
|
|
190
|
+
TinyImg uses a two-level cache system:
|
|
191
|
+
|
|
192
|
+
1. **Project-level cache** - `node_modules/.tinyimg_cache/` (priority)
|
|
193
|
+
2. **Global cache** - `~/.tinyimg/cache/` (fallback)
|
|
194
|
+
|
|
195
|
+
Cache is based on MD5 hash of file content. Files with same content will hit cache regardless of location.
|
|
196
|
+
|
|
197
|
+
## Supported Formats
|
|
198
|
+
|
|
199
|
+
- **PNG** - Including transparent PNG
|
|
200
|
+
- **JPG/JPEG** - All JPEG variants
|
|
201
|
+
|
|
202
|
+
> Note: TinyPNG does not support WebP, AVIF, SVG, or other formats.
|
|
203
|
+
|
|
204
|
+
## Quota Management
|
|
205
|
+
|
|
206
|
+
TinyPNG free tier provides 500 compressions per month. TinyImg maximizes utilization through:
|
|
207
|
+
|
|
208
|
+
- **Multi-Key Management** - Configure multiple keys for automatic rotation
|
|
209
|
+
- **Intelligent Caching** - Avoid re-compressing identical content
|
|
210
|
+
- **Quota Monitoring** - Real-time display of remaining quota for each key
|
|
211
|
+
|
|
212
|
+
## Error Handling
|
|
213
|
+
|
|
214
|
+
| Error Type | Description | Solution |
|
|
215
|
+
| --------------------------- | ------------------------------ | --------------------------------------- |
|
|
216
|
+
| `NoValidKeysError` | No valid API Key configured | Run `tinyimg key add <key>` to add key |
|
|
217
|
+
| `AllKeysExhaustedError` | All keys quota exhausted | Add new key |
|
|
218
|
+
| `AllCompressionFailedError` | All compression methods failed | Check network connection and API status |
|
|
219
|
+
|
|
220
|
+
## Related Packages
|
|
221
|
+
|
|
222
|
+
- [@pz4l/tinyimg-core](https://github.com/pzehrel/tinyimg/tree/main/packages/tinyimg-core) - Core compression library
|
|
223
|
+
- [@pz4l/tinyimg-unplugin](https://github.com/pzehrel/tinyimg/tree/main/packages/tinyimg-unplugin) - Vite/Webpack/Rolldown plugin
|
|
224
|
+
|
|
225
|
+
## License
|
|
226
|
+
|
|
227
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
[English](README.md) | 简体中文
|
|
2
|
+
|
|
3
|
+
# tinyimg-cli
|
|
4
|
+
|
|
5
|
+
基于 TinyPNG 的智能图片压缩 CLI 工具,支持多 API Key 管理、智能缓存和并发压缩。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- **批量压缩** - 支持文件、目录和 glob 模式
|
|
10
|
+
- **多 API Key 管理** - 支持多个 API Key 管理
|
|
11
|
+
- **智能缓存** - 基于 MD5 的永久缓存,避免重复压缩
|
|
12
|
+
- **并发控制** - 可配置的并行压缩任务数
|
|
13
|
+
- **多种 Key 策略** - random、round-robin、priority 三种模式
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
### 全局安装(推荐)
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# 使用 npm
|
|
21
|
+
npm install -g @pz4l/tinyimg-cli
|
|
22
|
+
|
|
23
|
+
# 使用 pnpm
|
|
24
|
+
pnpm add -g tinyimg-cli
|
|
25
|
+
|
|
26
|
+
# 使用 yarn
|
|
27
|
+
yarn global add tinyimg-cli
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 项目本地安装
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# 使用 npm
|
|
34
|
+
npm install -D @pz4l/tinyimg-cli
|
|
35
|
+
|
|
36
|
+
# 使用 pnpm
|
|
37
|
+
pnpm add -D tinyimg-cli
|
|
38
|
+
|
|
39
|
+
# 使用 yarn
|
|
40
|
+
yarn add -D tinyimg-cli
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 快速开始
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# 添加 API Key
|
|
47
|
+
tinyimg key add YOUR_TINYPNG_API_KEY
|
|
48
|
+
|
|
49
|
+
# 压缩当前目录所有图片
|
|
50
|
+
tinyimg *.png *.jpg
|
|
51
|
+
|
|
52
|
+
# 压缩指定目录
|
|
53
|
+
tinyimg ./images/
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 使用方法
|
|
57
|
+
|
|
58
|
+
### 基本压缩命令
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
tinyimg [options] <input...>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**输入参数支持:**
|
|
65
|
+
|
|
66
|
+
- 单个文件:`image.png`
|
|
67
|
+
- 多个文件:`image1.png image2.jpg`
|
|
68
|
+
- 目录:`./images/`
|
|
69
|
+
- Glob 模式:`./assets/**/*.png`
|
|
70
|
+
|
|
71
|
+
### 命令选项
|
|
72
|
+
|
|
73
|
+
| 选项 | 说明 | 默认值 |
|
|
74
|
+
| ------------------------- | ----------------------------------------------------- | -------- |
|
|
75
|
+
| `-o, --output <dir>` | 指定输出目录 | 原地覆盖 |
|
|
76
|
+
| `-k, --key <key>` | 指定 API Key(优先级高于环境变量) | - |
|
|
77
|
+
| `-m, --mode <mode>` | Key 使用策略:`random` \| `round-robin` \| `priority` | `random` |
|
|
78
|
+
| `-p, --parallel <number>` | 并发数限制 | `8` |
|
|
79
|
+
| `-c, --cache` | 启用缓存 | `true` |
|
|
80
|
+
| `--no-cache` | 禁用缓存 | - |
|
|
81
|
+
| `-h, --help` | 显示帮助信息 | - |
|
|
82
|
+
|
|
83
|
+
### Key 管理命令
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# 添加 API Key
|
|
87
|
+
tinyimg key add <key>
|
|
88
|
+
|
|
89
|
+
# 移除 API Key(交互式选择)
|
|
90
|
+
tinyimg key remove
|
|
91
|
+
|
|
92
|
+
# 移除指定 API Key
|
|
93
|
+
tinyimg key remove <key>
|
|
94
|
+
|
|
95
|
+
# 列出所有 API Key 及额度信息
|
|
96
|
+
tinyimg key
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Key 显示格式:** API Key 会以脱敏方式显示(前 4 位 + 后 4 位,如 `abcd****efgh`),方便识别同时保护敏感信息。
|
|
100
|
+
|
|
101
|
+
## 使用示例
|
|
102
|
+
|
|
103
|
+
### 压缩单个文件
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
tinyimg photo.png
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 压缩多个文件
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
tinyimg image1.png image2.jpg image3.jpeg
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 压缩整个目录
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
tinyimg ./assets/images/
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 使用 Glob 模式
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# 压缩所有 PNG 文件
|
|
125
|
+
tinyimg "./src/**/*.png"
|
|
126
|
+
|
|
127
|
+
# 压缩多种格式
|
|
128
|
+
tinyimg "./assets/**/*.{png,jpg,jpeg}"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### 指定输出目录
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# 压缩到 dist 目录,保持目录结构
|
|
135
|
+
tinyimg ./assets/ -o ./dist/
|
|
136
|
+
|
|
137
|
+
# 压缩到指定目录
|
|
138
|
+
tinyimg photo.png -o ./compressed/
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 使用特定 Key 策略
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
# 轮询模式(均匀分配额度)
|
|
145
|
+
tinyimg ./images/ -m round-robin
|
|
146
|
+
|
|
147
|
+
# 优先级模式(优先使用指定 key)
|
|
148
|
+
tinyimg ./images/ -m priority
|
|
149
|
+
|
|
150
|
+
# 随机模式(默认)
|
|
151
|
+
tinyimg ./images/ -m random
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 调整并发数
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
# 降低并发数(适合网络不稳定环境)
|
|
158
|
+
tinyimg ./images/ -p 4
|
|
159
|
+
|
|
160
|
+
# 提高并发数(适合高性能网络)
|
|
161
|
+
tinyimg ./images/ -p 16
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### 禁用缓存
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
# 强制重新压缩所有图片
|
|
168
|
+
tinyimg ./images/ --no-cache
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 指定 API Key
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
# 使用特定 key 进行压缩(优先级最高)
|
|
175
|
+
tinyimg ./images/ -k YOUR_API_KEY
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## 环境变量
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# 设置 API Keys(多个 key 用逗号分隔)
|
|
182
|
+
export TINYPNG_KEYS="key1,key2,key3"
|
|
183
|
+
|
|
184
|
+
# 运行压缩命令
|
|
185
|
+
tinyimg ./images/
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## 缓存系统
|
|
189
|
+
|
|
190
|
+
TinyImg 使用两级缓存系统:
|
|
191
|
+
|
|
192
|
+
1. **项目级缓存** - `node_modules/.tinyimg_cache/`(优先)
|
|
193
|
+
2. **全局缓存** - `~/.tinyimg/cache/`(后备)
|
|
194
|
+
|
|
195
|
+
缓存基于文件内容的 MD5 哈希,相同内容的文件无论位置如何都会命中缓存。
|
|
196
|
+
|
|
197
|
+
## 支持格式
|
|
198
|
+
|
|
199
|
+
- **PNG** - 包括透明 PNG
|
|
200
|
+
- **JPG/JPEG** - 所有 JPEG 变体
|
|
201
|
+
|
|
202
|
+
> 注意:TinyPNG 不支持 WebP、AVIF、SVG 等格式。
|
|
203
|
+
|
|
204
|
+
## 额度管理
|
|
205
|
+
|
|
206
|
+
TinyImg 通过以下方式优化额度使用:
|
|
207
|
+
|
|
208
|
+
- **多 Key 管理** - 配置多个 key 自动轮换
|
|
209
|
+
- **智能缓存** - 避免重复压缩相同内容
|
|
210
|
+
- **额度监控** - 实时显示各 key 的剩余额度
|
|
211
|
+
|
|
212
|
+
## 错误处理
|
|
213
|
+
|
|
214
|
+
| 错误类型 | 说明 | 解决方案 |
|
|
215
|
+
| --------------------------- | ---------------------- | ------------------------------------- |
|
|
216
|
+
| `NoValidKeysError` | 没有配置有效的 API Key | 运行 `tinyimg key add <key>` 添加 key |
|
|
217
|
+
| `AllKeysExhaustedError` | 所有 key 额度已用完 | 添加新 key |
|
|
218
|
+
| `AllCompressionFailedError` | 所有压缩方式失败 | 检查网络连接和 API 状态 |
|
|
219
|
+
|
|
220
|
+
## 相关包
|
|
221
|
+
|
|
222
|
+
- [@pz4l/tinyimg-core](https://github.com/pzehrel/tinyimg/tree/main/packages/tinyimg-core) - 核心压缩库
|
|
223
|
+
- [@pz4l/tinyimg-unplugin](https://github.com/pzehrel/tinyimg/tree/main/packages/tinyimg-unplugin) - Vite/Webpack/Rolldown 插件
|
|
224
|
+
|
|
225
|
+
## License
|
|
226
|
+
|
|
227
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/cli.ts"],"mappings":";iBAwEsB,IAAA,CAAA,GAAI,OAAA"}
|