@zhaoshijun/compress 1.4.1 → 1.4.2

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 (2) hide show
  1. package/bin/compress.js +144 -34
  2. package/package.json +1 -1
package/bin/compress.js CHANGED
@@ -1,38 +1,102 @@
1
1
  #!/usr/bin/env node
2
-
3
- import { Command } from 'commander';
4
- import chalk from 'chalk';
5
- import ora from 'ora';
6
- import fs from 'fs-extra';
7
- import path from 'path';
8
- import fg from 'fast-glob';
9
- import prettyBytes from 'pretty-bytes';
10
- import { MultiBar, Presets } from 'cli-progress';
11
- import { loadConfig } from '../src/config/loader.js';
12
- import { compressImage, addWatermarkOnly } from '../src/core/compressor.js';
13
- import { DEFAULT_EXCLUDES } from '../src/utils/constants.js';
14
- import { isSupportedImage as isSupported } from '../src/utils/file-utils.js';
15
- import { createRequire } from 'module';
16
- const require = createRequire(import.meta.url);
17
- const packageJson = require('../package.json');
18
-
19
- const program = new Command();
20
-
21
- program
22
- .name('compress')
23
- .description('Image compression CLI tool')
24
- .version(packageJson.version);
25
-
26
- program
27
- .option('-c, --config <path>', '配置文件路径')
28
- .option('--backup', '启用备份')
29
- .option('--dry-run', '仅列出将处理的文件,不写入磁盘')
30
- .option('-q, --quiet', '静默模式,仅输出错误')
31
- .option('-w, --watermark', '启用水印')
32
- .option('--keep-metadata', '保留图片元数据(拍摄时间、地点等)')
33
- .option('-o, --output <dir>', '输出目录')
34
- .action(async (options) => {
35
- try {
2
+
3
+ import { Command } from 'commander';
4
+ import chalk from 'chalk';
5
+ import ora from 'ora';
6
+ import fs from 'fs-extra';
7
+ import path from 'path';
8
+ import fg from 'fast-glob';
9
+ import prettyBytes from 'pretty-bytes';
10
+ import { MultiBar, Presets } from 'cli-progress';
11
+ import { loadConfig } from '../src/config/loader.js';
12
+ import { compressImage, addWatermarkOnly } from '../src/core/compressor.js';
13
+ import { DEFAULT_EXCLUDES } from '../src/utils/constants.js';
14
+ import { isSupportedImage as isSupported } from '../src/utils/file-utils.js';
15
+ import { createRequire } from 'module';
16
+ const require = createRequire(import.meta.url);
17
+ const packageJson = require('../package.json');
18
+
19
+ const program = new Command();
20
+
21
+ program
22
+ .name('compress')
23
+ .description(chalk.cyan('高效图片压缩工具 - 支持水印、元数据处理'))
24
+ .version(packageJson.version);
25
+
26
+ program
27
+ .addHelpText('before', `
28
+ ${chalk.bold('📦 使用示例:')}
29
+
30
+ ${chalk.gray('# 压缩当前目录图片')}
31
+ ${chalk.green('compress')}
32
+
33
+ ${chalk.gray('# 压缩并添加文本水印')}
34
+ ${chalk.green('compress -w --watermark-text "Copyright 2024"')}
35
+
36
+ ${chalk.gray('# 压缩并添加图片水印')}
37
+ ${chalk.green('compress -w --watermark-mode image --watermark-image "./logo.png"')}
38
+
39
+ ${chalk.gray('# 压缩并添加平铺水印')}
40
+ ${chalk.green('compress -w --watermark-mode tiled --watermark-image "./watermark.png"')}
41
+
42
+ ${chalk.gray('# 单独添加水印(不压缩)')}
43
+ ${chalk.green('compress watermark --watermark-text "Copyright"')}
44
+
45
+ ${chalk.gray('# 初始化配置文件')}
46
+ ${chalk.green('compress init')}
47
+ `);
48
+
49
+ program
50
+ .option('-c, --config <path>', '指定配置文件路径')
51
+ .option('--backup', '启用备份模式(将原文件复制为 .backup)')
52
+ .option('--dry-run', '演练模式,仅列出将处理的文件,不写入磁盘')
53
+ .option('-q, --quiet', '静默模式,仅输出错误信息')
54
+ .option('-w, --watermark', '启用水印(使用配置文件中的水印设置)')
55
+ .option('--keep-metadata', '保留图片元数据(拍摄时间、地点等),默认去除')
56
+ .option('-o, --output <dir>', '输出目录(默认: ./compressed)')
57
+ .option('-i, --input <pattern>', '输入文件 glob 模式(默认: **/*.{jpg,jpeg,png,webp})')
58
+ .option('--suffix <suffix>', '输出文件后缀(默认: .min)')
59
+ .allowUnknownOption()
60
+ .addHelpText('after', `
61
+ ${chalk.bold('━━━━━━━━━━ 主命令选项 ━━━━━━━━━━')}
62
+
63
+ ${chalk.yellow('【基础选项】')}
64
+ -c, --config <path> 指定配置文件路径
65
+ -i, --input <pattern> 输入文件 glob 模式
66
+ -o, --output <dir> 输出目录(默认: ./compressed)
67
+ --suffix <suffix> 输出文件后缀(默认: .min)
68
+
69
+ ${chalk.yellow('【处理选项】')}
70
+ --backup 启用备份模式
71
+ --dry-run 演练模式,仅预览不执行
72
+ -q, --quiet 静默模式,仅输出错误
73
+
74
+ ${chalk.yellow('【水印选项】')}
75
+ -w, --watermark 启用水印
76
+
77
+ ${chalk.yellow('【元数据选项】')}
78
+ --keep-metadata 保留图片元数据,默认去除
79
+
80
+ ${chalk.bold('━━━━━━━━━━ 子命令 ━━━━━━━━━━')}
81
+ init 生成默认配置文件
82
+ watermark 单独添加水印(不压缩)
83
+
84
+ ${chalk.bold('💧 水印模式说明:')}
85
+
86
+ text - 文本水印,适合版权声明
87
+ image - 单个图片水印,适合品牌 Logo
88
+ tiled - 平铺图片水印,适合防盗图
89
+
90
+ ${chalk.bold('📋 水印位置:')}
91
+
92
+ top-left / top-right / bottom-left / bottom-right / center / custom
93
+
94
+ ${chalk.bold('📖 更多信息:')}
95
+
96
+ 完整文档: https://github.com/zhaoshijun/compress
97
+ `)
98
+ .action(async (options) => {
99
+ try {
36
100
  const startTime = Date.now();
37
101
 
38
102
  // 加载配置
@@ -240,6 +304,52 @@ program
240
304
  .option('--watermark-spacing-x <spacing>', '水印水平间隔', parseInt)
241
305
  .option('--watermark-spacing-y <spacing>', '水印垂直间隔', parseInt)
242
306
  .option('--output <dir>', '输出目录(默认为 ./watermarked)')
307
+ .addHelpText('after', `
308
+ ${chalk.bold('━━━━━━━━━━ 水印命令选项 ━━━━━━━━━━')}
309
+
310
+ ${chalk.yellow('【基础选项】')}
311
+ -c, --config <path> 配置文件路径
312
+ --output <dir> 输出目录(默认: ./watermarked)
313
+
314
+ ${chalk.yellow('【水印模式】')}
315
+ --watermark-mode 水印模式: text / image / tiled
316
+
317
+ ${chalk.yellow('【文本水印选项】')}
318
+ --watermark-text 水印文本内容
319
+ --watermark-color 水印颜色 (hex/rgba)
320
+ --watermark-font-size 字体大小
321
+
322
+ ${chalk.yellow('【图片水印选项】')}
323
+ --watermark-image 水印图片路径
324
+ --watermark-width 水印宽度
325
+ --watermark-height 水印高度
326
+
327
+ ${chalk.yellow('【位置选项】】(仅 image 模式有效)')}
328
+ --watermark-position 位置: top-left / top-right / bottom-left / bottom-right / center / custom
329
+ --watermark-padding 边距
330
+ --watermark-x 自定义 X 坐标 (position=custom 时)
331
+ --watermark-y 自定义 Y 坐标 (position=custom 时)
332
+
333
+ ${chalk.yellow('【平铺选项】】(仅 tiled 模式有效)')}
334
+ --watermark-angle 倾斜角度 (-180 到 180)
335
+ --watermark-spacing-x 水平间隔
336
+ --watermark-spacing-y 垂直间隔
337
+
338
+ ${chalk.yellow('【通用选项】')}
339
+ --watermark-opacity 透明度 (0-1)
340
+ --watermark-density 密度 (1-10,文本水印平铺数量)
341
+
342
+ ${chalk.bold('💧 使用示例:')}
343
+
344
+ # 文本水印
345
+ compress watermark --watermark-text "Copyright 2024"
346
+
347
+ # 图片水印(右下角)
348
+ compress watermark --watermark-mode image --watermark-image "./logo.png" --watermark-position bottom-right
349
+
350
+ # 平铺水印
351
+ compress watermark --watermark-mode tiled --watermark-image "./watermark.png" --watermark-opacity 0.15
352
+ `)
243
353
  .action(async (options) => {
244
354
  try {
245
355
  const startTime = Date.now();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhaoshijun/compress",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "Image compression CLI and Vite plugin",
5
5
  "type": "module",
6
6
  "bin": {