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