auto-image-converter 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/bin/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import path from "path";
2
+ import { fileURLToPath } from "url";
3
+ import { convertImages } from "../lib/converter.js";
4
+ import fs from "fs/promises";
5
+
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+
8
+ const CONFIG_PATH = path.resolve(process.cwd(), "image-converter.config.js");
9
+
10
+ try {
11
+ const { default: config } = await import(CONFIG_PATH);
12
+ await convertImages(config);
13
+ } catch (e) {
14
+ console.error("❌ Ошибка загрузки конфигурации:", e.message);
15
+ process.exit(1);
16
+ }
@@ -0,0 +1,7 @@
1
+ export default {
2
+ dir: './public', // что сканировать
3
+ format: 'webp', // или 'avif'
4
+ quality: 80,
5
+ removeOriginal: true
6
+ }
7
+
@@ -0,0 +1,26 @@
1
+ import fg from "fast-glob";
2
+ import sharp from "sharp";
3
+ import fs from "node:fs/promises";
4
+ import path from "node:path";
5
+
6
+ const supportedExt = [".png", ".jpg", ".jpeg"];
7
+
8
+ export async function convertImages({ dir, format, quality, removeOriginal }) {
9
+ const files = await fg(`${dir}/**/*.{png,jpg,jpeg}`);
10
+
11
+ for (const file of files) {
12
+ const ext = path.extname(file);
13
+ const outFile = file.replace(ext, `.${format}`);
14
+
15
+ const image = sharp(file);
16
+ const buffer =
17
+ format === "webp"
18
+ ? await image.webp({ quality }).toBuffer()
19
+ : await image.avif({ quality }).toBuffer();
20
+
21
+ await fs.writeFile(outFile, buffer);
22
+ if (removeOriginal) await fs.unlink(file);
23
+
24
+ console.log(`✓ ${file} → ${outFile}`);
25
+ }
26
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "auto-image-converter",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "convert-images": "./bin/index.js"
7
+ },
8
+ "dependencies": {
9
+ "commander": "^14.0.0",
10
+ "fast-glob": "^3.3.3",
11
+ "sharp": "^0.34.2"
12
+ }
13
+ }