minify-pic-cli 1.0.2 → 1.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.
Files changed (4) hide show
  1. package/README.md +110 -85
  2. package/README.zh-CN.md +174 -0
  3. package/index.js +45 -13
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -1,149 +1,174 @@
1
- # Minify Pic CLI - 图片批量压缩工具
1
+ # Minify Pic CLI - Batch Image Compression Tool
2
2
 
3
- 一个简单易用的图片批量压缩命令行工具,支持 PNG、JPEG、GIF 格式,适合前端和设计师快速优化图片体积。
3
+ English | [简体中文](./README.zh-CN.md)
4
4
 
5
- ## 功能特性
5
+ A simple and easy-to-use command-line tool for batch image compression, supporting PNG, JPEG, and GIF formats. Perfect for frontend developers and designers to quickly optimize image file sizes.
6
6
 
7
- - 🖼️ 支持多种图片格式:PNG、JPEG、GIF
8
- - 📁 批量处理目录及子目录中的所有图片
9
- - ⚙️ 可自定义压缩质量和参数
10
- - 🚫 支持排除指定目录
11
- - 📊 显示压缩前后文件大小对比
12
- - 🎯 保持原有目录结构
7
+ ## Features
13
8
 
14
- ## 安装
9
+ - 🖼️ Support multiple image formats: PNG, JPEG, GIF
10
+ - 📁 Batch process all images in directories and subdirectories
11
+ - ⚙️ Customizable compression quality and parameters
12
+ - 🚫 Support for excluding specified directories
13
+ - 📊 Display file size comparison before and after compression
14
+ - 🎯 Maintain original directory structure
15
+ - ⚡ Support skip confirmation for automation
16
+ - 🔄 Support in-place compression to replace original files
15
17
 
16
- ### 全局安装
18
+ ## Installation
19
+
20
+ ### Global Installation
17
21
 
18
22
  ```bash
19
- # 使用 pnpm 安装
23
+ # Install using pnpm
20
24
  pnpm install -g minify-pic-cli
21
25
 
22
- # 或使用 npm
26
+ # Or using npm
23
27
  npm install -g minify-pic-cli
24
28
  ```
25
29
 
26
- ## 使用方法
30
+ ## Usage
27
31
 
28
- ### 基本用法
32
+ ### Basic Usage
29
33
 
30
34
  ```bash
31
- # 压缩当前目录的所有图片
35
+ # Compress all images in current directory
32
36
  mpic
33
37
 
34
- # 或直接运行
38
+ # Or run directly
35
39
  node index.js
36
40
  ```
37
41
 
38
- ### 命令行选项
42
+ ### Command Line Options
39
43
 
40
44
  ```bash
41
- mpic [选项]
42
-
43
- 选项:
44
- -d, --dir <dir> 需要压缩的目录 (默认: 当前目录)
45
- -o, --output <output> 输出目录 (默认: ./output)
46
- -q, --quality <quality> 压缩质量 0-100 (默认: 80)
47
- -g, --gif-colours <colours> GIF调色板最大数量 2-256 (默认: 128)
48
- -b, --black-dirs <dirs> 排除的子文件夹名称,逗号分隔 (默认: "no")
49
- -v, --version 显示版本号
50
- -h, --help 显示帮助信息
45
+ mpic [options]
46
+
47
+ Options:
48
+ -d, --dir <dir> Directory to compress (default: current directory)
49
+ -o, --output <output> Output directory (default: ./output)
50
+ -q, --quality <quality> Compression quality 0-100 (default: 80)
51
+ -g, --gif-colours <colours> GIF palette maximum colors 2-256 (default: 128)
52
+ -b, --black-dirs <dirs> Exclude subdirectories, comma-separated (default: "no")
53
+ -y, --yes Skip confirmation and start compression directly
54
+ -r, --replace In-place compression, replace original files
55
+ -v, --version Display version number
56
+ -h, --help Display help information
51
57
  ```
52
58
 
53
- ### 使用示例
59
+ ### Usage Examples
54
60
 
55
61
  ```bash
56
- # 压缩指定目录的图片,输出到 compressed 文件夹
62
+ # Compress images in specified directory, output to compressed folder
57
63
  mpic -d ./images -o ./compressed
58
64
 
59
- # 设置压缩质量为 90
65
+ # Set compression quality to 90
60
66
  mpic -q 90
61
67
 
62
- # 排除 node_modules .git 目录
68
+ # Exclude node_modules and .git directories
63
69
  mpic -b "node_modules,.git"
64
70
 
65
- # 设置 GIF 调色板为 64
71
+ # Set GIF palette to 64 colors
66
72
  mpic -g 64
67
73
 
68
- # 组合使用多个选项
74
+ # Skip confirmation prompt, start compression automatically
75
+ mpic -y
76
+
77
+ # In-place compression to replace original files (no new directory)
78
+ mpic -r
79
+
80
+ # Skip confirmation + in-place replace (recommended for automation)
81
+ mpic -y -r
82
+
83
+ # In-place replace + custom compression quality
84
+ mpic -y -r -q 90
85
+
86
+ # Combine multiple options
69
87
  mpic -d ./src/assets -o ./dist/assets -q 85 -b "node_modules,.git"
70
88
  ```
71
89
 
72
- ## 支持的图片格式
90
+ ## Supported Image Formats
73
91
 
74
- - **PNG**: 支持透明度,适合图标和简单图形
75
- - **JPEG**: 适合照片和复杂图像
76
- - **GIF**: 支持动画,可调整调色板颜色数量
92
+ - **PNG**: Supports transparency, suitable for icons and simple graphics
93
+ - **JPEG**: Suitable for photos and complex images
94
+ - **GIF**: Supports animation, adjustable palette colors
77
95
 
78
- ## 配置说明
96
+ ## Configuration
79
97
 
80
- ### 压缩质量 (quality)
81
- - 范围:0-100
82
- - 默认:80
83
- - 数值越高,质量越好,文件越大
84
- - 建议:80-90 为最佳平衡点
98
+ ### Compression Quality (quality)
99
+ - Range: 0-100
100
+ - Default: 80
101
+ - Higher value = better quality, larger file size
102
+ - Recommended: 80-90 for best balance
85
103
 
86
- ### GIF 调色板颜色数 (gif-colours)
87
- - 范围:2-256
88
- - 默认:128
89
- - 颜色数越少,文件越小,但可能影响视觉效果
90
- - 建议:64-128 为常用范围
104
+ ### GIF Palette Colors (gif-colours)
105
+ - Range: 2-256
106
+ - Default: 128
107
+ - Fewer colors = smaller file size, may affect visual quality
108
+ - Recommended: 64-128 for common use
91
109
 
92
- ### 排除目录 (black-dirs)
93
- - 支持多个目录,用逗号分隔
94
- - 默认排除 "no" 目录
95
- - 常用排除目录:`node_modules,.git,dist,build`
110
+ ### Exclude Directories (black-dirs)
111
+ - Support multiple directories, comma-separated
112
+ - Default exclude: "no" directory
113
+ - Common exclude directories: `node_modules,.git,dist,build`
96
114
 
97
- ## 输出说明
115
+ ## Output Information
98
116
 
99
- 工具会在控制台显示:
100
- - 当前工作目录
101
- - 压缩进度和结果
102
- - 文件大小变化对比
103
- - 最终输出目录
117
+ The tool displays in console:
118
+ - Current working directory
119
+ - Compression progress and results
120
+ - File size comparison
121
+ - Final output directory
104
122
 
105
- 示例输出:
123
+ Example output:
106
124
  ```
107
- 当前目录路径为: /path/to/your/project
108
- 是否需要压缩当前目录的所有图片?Y/Ny
109
- 压缩完成 [大小变化: 2.5MB ---->>>> 1.2MB] /path/to/output/image1.jpg
110
- 压缩完成 [大小变化: 800KB ---->>>> 400KB] /path/to/output/image2.png
111
- 压缩任务全部完成,已输出至 /path/to/output
125
+ Current directory path: /path/to/your/project
126
+ Do you need to compress all images in the current directory? Y/N: y
127
+ Compression complete [Size change: 2.5MB ---->>>> 1.2MB] /path/to/output/image1.jpg
128
+ Compression complete [Size change: 800KB ---->>>> 400KB] /path/to/output/image2.png
129
+ All compression tasks completed, output to /path/to/output
112
130
  ```
113
131
 
114
- ## 注意事项
132
+ ## Important Notes
115
133
 
116
- 1. **备份原文件**:工具会创建新的压缩文件,不会覆盖原文件
117
- 2. **输出目录**:默认输出到 `./output` 目录,会自动创建
118
- 3. **目录结构**:压缩后会保持原有的目录结构
119
- 4. **大文件处理**:对于大文件,压缩可能需要一些时间
120
- 5. **权限问题**:确保对目标目录有写入权限
134
+ 1. **Backup Original Files**: Default mode creates new compressed files in output directory without overwriting originals
135
+ 2. **In-Place Replace Mode**: Using `-r` parameter will directly replace original files. Use with caution and backup important files first
136
+ 3. **Output Directory**: Default output to `./output` directory, automatically created (not created when using `-r`)
137
+ 4. **Directory Structure**: Maintains original directory structure after compression
138
+ 5. **Large File Processing**: Compression of large files may take some time
139
+ 6. **Permission Issues**: Ensure write permissions for target directories
140
+ 7. **Automation Scripts**: For CI/CD or automation scripts, use `-y` parameter to skip interactive confirmation
121
141
 
122
- ## 技术栈
142
+ ## Tech Stack
123
143
 
124
- - **Node.js**: 运行环境
125
- - **Sharp**: 高性能图片处理库
126
- - **Commander.js**: 命令行参数解析
127
- - **Readline**: 用户交互
144
+ - **Node.js**: Runtime environment
145
+ - **Sharp**: High-performance image processing library
146
+ - **Commander.js**: Command-line argument parsing
147
+ - **Readline**: User interaction
128
148
 
129
- ## 开发
149
+ ## Development
130
150
 
131
151
  ```bash
132
- # 安装依赖
152
+ # Install dependencies
133
153
  pnpm install
134
154
 
135
- # 运行开发版本
155
+ # Run development version
136
156
  node index.js
137
157
  ```
138
158
 
139
- ## 许可证
159
+ ## License
140
160
 
141
161
  ISC
142
162
 
143
- ## 更新日志
163
+ ## Changelog
164
+
165
+ ### v1.0.3
166
+ - Added `-y, --yes` parameter: Skip confirmation prompt for automation support
167
+ - Added `-r, --replace` parameter: In-place compression to replace original files
168
+ - Improved user experience with more flexible usage options
144
169
 
145
170
  ### v1.0.2
146
- - 初始版本发布
147
- - 支持 PNGJPEGGIF 格式压缩
148
- - 命令行参数支持
149
- - 批量处理功能
171
+ - Initial release
172
+ - Support PNG, JPEG, GIF format compression
173
+ - Command-line parameter support
174
+ - Batch processing functionality
@@ -0,0 +1,174 @@
1
+ # Minify Pic CLI - 图片批量压缩工具
2
+
3
+ 简体中文 | [English](./README.md)
4
+
5
+ 一个简单易用的图片批量压缩命令行工具,支持 PNG、JPEG、GIF 格式,适合前端和设计师快速优化图片体积。
6
+
7
+ ## 功能特性
8
+
9
+ - 🖼️ 支持多种图片格式:PNG、JPEG、GIF
10
+ - 📁 批量处理目录及子目录中的所有图片
11
+ - ⚙️ 可自定义压缩质量和参数
12
+ - 🚫 支持排除指定目录
13
+ - 📊 显示压缩前后文件大小对比
14
+ - 🎯 保持原有目录结构
15
+ - ⚡ 支持跳过确认,自动化执行
16
+ - 🔄 支持原地压缩替换原文件
17
+
18
+ ## 安装
19
+
20
+ ### 全局安装
21
+
22
+ ```bash
23
+ # 使用 pnpm 安装
24
+ pnpm install -g minify-pic-cli
25
+
26
+ # 或使用 npm
27
+ npm install -g minify-pic-cli
28
+ ```
29
+
30
+ ## 使用方法
31
+
32
+ ### 基本用法
33
+
34
+ ```bash
35
+ # 压缩当前目录的所有图片
36
+ mpic
37
+
38
+ # 或直接运行
39
+ node index.js
40
+ ```
41
+
42
+ ### 命令行选项
43
+
44
+ ```bash
45
+ mpic [选项]
46
+
47
+ 选项:
48
+ -d, --dir <dir> 需要压缩的目录 (默认: 当前目录)
49
+ -o, --output <output> 输出目录 (默认: ./output)
50
+ -q, --quality <quality> 压缩质量 0-100 (默认: 80)
51
+ -g, --gif-colours <colours> GIF调色板最大数量 2-256 (默认: 128)
52
+ -b, --black-dirs <dirs> 排除的子文件夹名称,逗号分隔 (默认: "no")
53
+ -y, --yes 跳过确认,直接开始压缩
54
+ -r, --replace 原地压缩替换原文件,不输出到新目录
55
+ -v, --version 显示版本号
56
+ -h, --help 显示帮助信息
57
+ ```
58
+
59
+ ### 使用示例
60
+
61
+ ```bash
62
+ # 压缩指定目录的图片,输出到 compressed 文件夹
63
+ mpic -d ./images -o ./compressed
64
+
65
+ # 设置压缩质量为 90
66
+ mpic -q 90
67
+
68
+ # 排除 node_modules 和 .git 目录
69
+ mpic -b "node_modules,.git"
70
+
71
+ # 设置 GIF 调色板为 64 色
72
+ mpic -g 64
73
+
74
+ # 跳过确认提示,自动开始压缩
75
+ mpic -y
76
+
77
+ # 原地压缩替换原文件(不创建新目录)
78
+ mpic -r
79
+
80
+ # 跳过确认 + 原地替换(自动化场景推荐)
81
+ mpic -y -r
82
+
83
+ # 原地替换 + 自定义压缩质量
84
+ mpic -y -r -q 90
85
+
86
+ # 组合使用多个选项
87
+ mpic -d ./src/assets -o ./dist/assets -q 85 -b "node_modules,.git"
88
+ ```
89
+
90
+ ## 支持的图片格式
91
+
92
+ - **PNG**: 支持透明度,适合图标和简单图形
93
+ - **JPEG**: 适合照片和复杂图像
94
+ - **GIF**: 支持动画,可调整调色板颜色数量
95
+
96
+ ## 配置说明
97
+
98
+ ### 压缩质量 (quality)
99
+ - 范围:0-100
100
+ - 默认:80
101
+ - 数值越高,质量越好,文件越大
102
+ - 建议:80-90 为最佳平衡点
103
+
104
+ ### GIF 调色板颜色数 (gif-colours)
105
+ - 范围:2-256
106
+ - 默认:128
107
+ - 颜色数越少,文件越小,但可能影响视觉效果
108
+ - 建议:64-128 为常用范围
109
+
110
+ ### 排除目录 (black-dirs)
111
+ - 支持多个目录,用逗号分隔
112
+ - 默认排除 "no" 目录
113
+ - 常用排除目录:`node_modules,.git,dist,build`
114
+
115
+ ## 输出说明
116
+
117
+ 工具会在控制台显示:
118
+ - 当前工作目录
119
+ - 压缩进度和结果
120
+ - 文件大小变化对比
121
+ - 最终输出目录
122
+
123
+ 示例输出:
124
+ ```
125
+ 当前目录路径为: /path/to/your/project
126
+ 是否需要压缩当前目录的所有图片?Y/N:y
127
+ 压缩完成 [大小变化: 2.5MB ---->>>> 1.2MB] /path/to/output/image1.jpg
128
+ 压缩完成 [大小变化: 800KB ---->>>> 400KB] /path/to/output/image2.png
129
+ 压缩任务全部完成,已输出至 /path/to/output
130
+ ```
131
+
132
+ ## 注意事项
133
+
134
+ 1. **备份原文件**:默认模式会创建新的压缩文件到输出目录,不会覆盖原文件
135
+ 2. **原地替换模式**:使用 `-r` 参数会直接替换原文件,请谨慎使用,建议先备份重要文件
136
+ 3. **输出目录**:默认输出到 `./output` 目录,会自动创建(使用 `-r` 时不会创建)
137
+ 4. **目录结构**:压缩后会保持原有的目录结构
138
+ 5. **大文件处理**:对于大文件,压缩可能需要一些时间
139
+ 6. **权限问题**:确保对目标目录有写入权限
140
+ 7. **自动化脚本**:在 CI/CD 或自动化脚本中,建议使用 `-y` 参数跳过交互确认
141
+
142
+ ## 技术栈
143
+
144
+ - **Node.js**: 运行环境
145
+ - **Sharp**: 高性能图片处理库
146
+ - **Commander.js**: 命令行参数解析
147
+ - **Readline**: 用户交互
148
+
149
+ ## 开发
150
+
151
+ ```bash
152
+ # 安装依赖
153
+ pnpm install
154
+
155
+ # 运行开发版本
156
+ node index.js
157
+ ```
158
+
159
+ ## 许可证
160
+
161
+ ISC
162
+
163
+ ## 更新日志
164
+
165
+ ### v1.0.3
166
+ - 新增 `-y, --yes` 参数:跳过确认提示,支持自动化执行
167
+ - 新增 `-r, --replace` 参数:原地压缩替换原文件
168
+ - 优化用户体验,提供更多灵活的使用方式
169
+
170
+ ### v1.0.2
171
+ - 初始版本发布
172
+ - 支持 PNG、JPEG、GIF 格式压缩
173
+ - 命令行参数支持
174
+ - 批量处理功能
package/index.js CHANGED
@@ -71,10 +71,20 @@ async function compressImage(filePath, config, baseDir = config.targetDir) {
71
71
  throw new Error(`不支持的文件类型: ${ext}`);
72
72
  }
73
73
 
74
- // 保持目录结构
75
- const relativePath = path.relative(baseDir, filePath);
76
- const outputFilePath = path.join(config.outputDir, relativePath);
77
- const outputFileDir = path.dirname(outputFilePath);
74
+ // 原地替换模式或输出到新目录
75
+ let outputFilePath;
76
+ let outputFileDir;
77
+
78
+ if (config.replace) {
79
+ // 原地替换:使用临时文件
80
+ outputFilePath = filePath + '.tmp';
81
+ outputFileDir = path.dirname(outputFilePath);
82
+ } else {
83
+ // 保持目录结构输出到新目录
84
+ const relativePath = path.relative(baseDir, filePath);
85
+ outputFilePath = path.join(config.outputDir, relativePath);
86
+ outputFileDir = path.dirname(outputFilePath);
87
+ }
78
88
 
79
89
  try {
80
90
  if (!fs.existsSync(outputFileDir)) {
@@ -83,12 +93,27 @@ async function compressImage(filePath, config, baseDir = config.targetDir) {
83
93
  const buffer = await sharpInstance.toBuffer();
84
94
  fs.writeFileSync(outputFilePath, buffer);
85
95
  fs.chmodSync(outputFilePath, 0o646);
86
- const afterSize = getFileSizeWithUnit(outputFilePath);
87
- console.log(
88
- `压缩完成 [大小变化: ${beforeSize} ---->>>> ${afterSize}] ${outputFilePath}`
89
- );
96
+
97
+ // 如果是原地替换模式,将临时文件重命名为原文件
98
+ if (config.replace) {
99
+ fs.unlinkSync(filePath);
100
+ fs.renameSync(outputFilePath, filePath);
101
+ const afterSize = getFileSizeWithUnit(filePath);
102
+ console.log(
103
+ `压缩完成 [大小变化: ${beforeSize} ---->>>> ${afterSize}] ${filePath}`
104
+ );
105
+ } else {
106
+ const afterSize = getFileSizeWithUnit(outputFilePath);
107
+ console.log(
108
+ `压缩完成 [大小变化: ${beforeSize} ---->>>> ${afterSize}] ${outputFilePath}`
109
+ );
110
+ }
90
111
  } catch (error) {
91
- console.error(`压缩出错: ${outputFilePath}`, error);
112
+ console.error(`压缩出错: ${config.replace ? filePath : outputFilePath}`, error);
113
+ // 如果原地替换失败,清理临时文件
114
+ if (config.replace && fs.existsSync(outputFilePath)) {
115
+ fs.unlinkSync(outputFilePath);
116
+ }
92
117
  }
93
118
  }
94
119
 
@@ -100,8 +125,8 @@ async function compressFiles(dir, config, baseDir = config.targetDir) {
100
125
  const filePath = path.join(dir, file);
101
126
  const stat = fs.statSync(filePath);
102
127
 
103
- // 跳过 output 目录
104
- if (filePath === config.outputDir) {
128
+ // 跳过 output 目录(非原地替换模式)
129
+ if (!config.replace && filePath === config.outputDir) {
105
130
  console.log(`跳过的目录: ${filePath}(为输出目录)`);
106
131
  continue;
107
132
  }
@@ -129,15 +154,18 @@ program
129
154
  .option("-q, --quality <quality>", "压缩质量(0-100)", String(DEFAULT_CONFIG.quality))
130
155
  .option("-g, --gif-colours <colours>", "GIF调色板最大数量(2-256)", String(DEFAULT_CONFIG.gifColours))
131
156
  .option("-b, --black-dirs <dirs>", "排除的子文件夹名称(逗号分隔)", val => val.split(","), DEFAULT_CONFIG.blackDirs)
157
+ .option("-y, --yes", "跳过确认,直接开始压缩")
158
+ .option("-r, --replace", "原地压缩替换原文件,不输出到新目录")
132
159
  .version(require('./package.json').version, '-v, --version', '显示版本号')
133
160
  .action(async (options) => {
134
161
  // 合并配置
135
162
  const config = {
136
163
  targetDir: path.resolve(process.cwd(), options.dir),
137
- outputDir: path.resolve(process.cwd(), options.output),
164
+ outputDir: options.replace ? null : path.resolve(process.cwd(), options.output),
138
165
  quality: parseInt(options.quality, 10),
139
166
  gifColours: parseInt(options.gifColours, 10),
140
167
  blackDirs: Array.isArray(options.blackDirs) ? options.blackDirs : [options.blackDirs],
168
+ replace: options.replace || false,
141
169
  };
142
170
 
143
171
  let shouldContinue = options.yes;
@@ -152,7 +180,11 @@ program
152
180
  const inputDir = config.targetDir;
153
181
  compressFiles(inputDir, config, inputDir)
154
182
  .then(() => {
155
- console.log("压缩任务全部完成,已输出至", config.outputDir);
183
+ if (config.replace) {
184
+ console.log("压缩任务全部完成,已原地替换所有图片");
185
+ } else {
186
+ console.log("压缩任务全部完成,已输出至", config.outputDir);
187
+ }
156
188
  })
157
189
  .catch((err) => {
158
190
  console.error("压缩文件时出错:", err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minify-pic-cli",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "一个简单易用的图片批量压缩命令行工具,支持 PNG、JPEG、GIF 格式,适合前端和设计师快速优化图片体积。",
5
5
  "main": "index.js",
6
6
  "bin": {