@zenos.chen/img-compress 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,4 @@
1
+ # @zenos/img-compress
2
+
3
+ 这是一个图片压缩cli工具,用来实现最简单的压缩功能。
4
+
package/bin/cli ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require('../src/cli.js');
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@zenos.chen/img-compress",
3
+ "version": "1.0.0",
4
+ "description": "这是一个非常简单的,上手可用的图片压缩cli工具",
5
+ "author": "zenos,chen",
6
+ "main": "./src/compress.js",
7
+ "keywords": [
8
+ "img",
9
+ "compress"
10
+ ],
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "cac": "^6.7.14",
14
+ "sharp": "^0.33.5"
15
+ },
16
+ "bin": {
17
+ "img-compress": "bin/cli"
18
+ },
19
+ "files": [
20
+ "bin",
21
+ "src"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public",
25
+ "registry": "https://registry.npmjs.org/"
26
+ }
27
+ }
package/src/cli.js ADDED
@@ -0,0 +1,36 @@
1
+ const { compressImage } = require("./compress");
2
+ const cac = require("cac");
3
+ const path = require("path");
4
+
5
+ console.log("This is a image compress script! \n");
6
+
7
+ const cli = cac();
8
+
9
+ cli.command("[...args]", "input args")
10
+ .option("-i, --input [input]", "input file name")
11
+ .option("-o, --output [output]", "output file name")
12
+ .option("-r, --ratio [ratio]", "the compress ratio")
13
+ .action((args, options) => {
14
+ const res = {};
15
+ res.input = args[0] || options.input || null;
16
+ res.output =
17
+ args[1] ||
18
+ options.output ||
19
+ (res.input ? res.input.replace(path.extname(res.input), "-low" + path.extname(res.input)) : null);
20
+ res.ratio = args[2] || options.ratio || 0.1;
21
+ const NotArgsOk = Object.values(res).some((value) => !value);
22
+ if (NotArgsOk) {
23
+ console.log("args not ok");
24
+ return;
25
+ } else {
26
+ console.log("input image:", res.input);
27
+ console.log("output image:", res.output);
28
+ console.log("compress ratio:", res.ratio);
29
+ console.log("\n");
30
+ compressImage(res.input, res.output, Number(res.ratio));
31
+ }
32
+ });
33
+
34
+ cli.help().version("0.0.1");
35
+
36
+ cli.parse();
@@ -0,0 +1,19 @@
1
+ const sharp = require("sharp");
2
+ const path = require("path");
3
+
4
+ async function compressImage(inputPath, outputPath, ratio) {
5
+ const cwd = process.cwd();
6
+ try {
7
+ const metadata = await sharp(path.resolve(cwd, inputPath)).metadata();
8
+ const newWidth = Math.round(metadata.width * ratio);
9
+ const newHeight = Math.round(metadata.height * ratio);
10
+ console.log(`Original Size: ${metadata.width}x${metadata.height}`);
11
+ console.log(`Compressed Size: ${newWidth}x${newHeight}`);
12
+ await sharp(inputPath).resize(newWidth).toFile(path.resolve(cwd, outputPath));
13
+ console.log("Compressed!");
14
+ } catch (error) {
15
+ console.log(error);
16
+ }
17
+ }
18
+
19
+ exports.compressImage = compressImage;