auto-image-converter 2.0.0 → 2.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.
- package/README.md +7 -5
- package/bin/index.js +2 -1
- package/bin/watcher.mjs +24 -11
- package/image-converter.config.mjs +2 -0
- package/lib/converter.js +50 -14
- package/package.json +14 -1
- package/sz-auto-image-converter-1.0.0.tgz +0 -0
package/README.md
CHANGED
|
@@ -26,11 +26,13 @@ Create a `image-converter.config.mjs` file in the root of your project:
|
|
|
26
26
|
|
|
27
27
|
```// Default configuration example
|
|
28
28
|
export default {
|
|
29
|
-
dir: "public",
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
dir: "public", // Directory to scan for images
|
|
30
|
+
converted: "*.{png,jpg,jpeg}", // Glob pattern for source image files to convert
|
|
31
|
+
format: "webp", // Output image format: 'webp' or 'avif'
|
|
32
|
+
quality: 80, // Quality of output images (0–100)
|
|
33
|
+
recursive: true, // Whether to search subdirectories recursively
|
|
34
|
+
removeOriginal: true, // Delete original files after successful conversion
|
|
35
|
+
ignoreOnStart: false, // If true, ignore existing files on watcher startup
|
|
34
36
|
};
|
|
35
37
|
```
|
|
36
38
|
|
package/bin/index.js
CHANGED
|
@@ -16,7 +16,8 @@ try {
|
|
|
16
16
|
|
|
17
17
|
await convertImages({
|
|
18
18
|
dir: absDir,
|
|
19
|
-
|
|
19
|
+
converted: config.converted ?? "*.{png,jpg,jpeg}",
|
|
20
|
+
targetFormat: config.targetFormat ?? "webp",
|
|
20
21
|
quality: config.quality ?? 80,
|
|
21
22
|
recursive: config.recursive ?? true,
|
|
22
23
|
removeOriginal: config.removeOriginal ?? false,
|
package/bin/watcher.mjs
CHANGED
|
@@ -9,35 +9,48 @@ const config = (await import(pathToFileURL(configPath).href)).default;
|
|
|
9
9
|
|
|
10
10
|
const watchDir = config.dir || "public";
|
|
11
11
|
const absWatchDir = path.resolve(process.cwd(), watchDir);
|
|
12
|
+
const extensions = extractExtensions(config.converted ?? "*.{png,jpg,jpeg}");
|
|
12
13
|
|
|
13
|
-
// Для chokidar лучше передать просто папку, а не glob
|
|
14
14
|
const watchPath = absWatchDir;
|
|
15
15
|
|
|
16
16
|
console.log(`👀 Watching for image changes on directory: ${watchPath}`);
|
|
17
17
|
|
|
18
|
+
let debounceTimeout;
|
|
19
|
+
|
|
18
20
|
chokidar
|
|
19
|
-
.watch(
|
|
20
|
-
ignored: /(^|[\/\\])\../,
|
|
21
|
+
.watch(absWatchDir, {
|
|
22
|
+
ignored: /(^|[\/\\])\../,
|
|
21
23
|
persistent: true,
|
|
22
|
-
ignoreInitial:
|
|
24
|
+
ignoreInitial: config.ignoreOnStart ?? false,
|
|
23
25
|
awaitWriteFinish: {
|
|
24
|
-
stabilityThreshold:
|
|
25
|
-
pollInterval:
|
|
26
|
+
stabilityThreshold: 1500,
|
|
27
|
+
pollInterval: 500,
|
|
26
28
|
},
|
|
27
29
|
})
|
|
28
30
|
.on("add", async (filePath) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
const ext = path.extname(filePath).slice(1).toLowerCase(); // без точки
|
|
32
|
+
if (!extensions.includes(ext)) return;
|
|
33
|
+
if (ext === (config.targetFormat ?? "webp").toLowerCase()) return;
|
|
34
|
+
|
|
35
|
+
console.log(`➕ New image: ${filePath}`);
|
|
36
|
+
clearTimeout(debounceTimeout);
|
|
37
|
+
debounceTimeout = setTimeout(async () => {
|
|
31
38
|
try {
|
|
32
39
|
await convertImages({
|
|
33
40
|
dir: watchPath,
|
|
34
|
-
|
|
41
|
+
converted: config.converted ?? "*.{png,jpg,jpeg}",
|
|
42
|
+
format: config.format ?? "webp",
|
|
35
43
|
quality: config.quality ?? 80,
|
|
36
44
|
recursive: config.recursive ?? true,
|
|
37
45
|
removeOriginal: config.removeOriginal ?? false,
|
|
38
46
|
});
|
|
39
47
|
} catch (err) {
|
|
40
|
-
console.error("
|
|
48
|
+
console.error("Error covertation:", err.message);
|
|
41
49
|
}
|
|
42
|
-
}
|
|
50
|
+
}, 1000);
|
|
43
51
|
});
|
|
52
|
+
|
|
53
|
+
function extractExtensions(pattern) {
|
|
54
|
+
const match = pattern.match(/\*\.\{(.+?)\}/);
|
|
55
|
+
return match ? match[1].split(",").map((s) => s.trim().toLowerCase()) : [];
|
|
56
|
+
}
|
package/lib/converter.js
CHANGED
|
@@ -3,34 +3,70 @@ import sharp from "sharp";
|
|
|
3
3
|
import fs from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
|
|
6
|
+
sharp.cache(false);
|
|
7
|
+
|
|
6
8
|
const supportedExt = [".png", ".jpg", ".jpeg"];
|
|
7
9
|
|
|
10
|
+
const processingFiles = new Set();
|
|
11
|
+
|
|
12
|
+
async function safeUnlink(file, retries = 3, delayMs = 200) {
|
|
13
|
+
for (let i = 0; i < retries; i++) {
|
|
14
|
+
try {
|
|
15
|
+
await fs.unlink(file);
|
|
16
|
+
return;
|
|
17
|
+
} catch (err) {
|
|
18
|
+
if (i === retries - 1) throw err;
|
|
19
|
+
await new Promise((res) => setTimeout(res, delayMs));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
export async function convertImages({
|
|
9
25
|
dir,
|
|
26
|
+
converted,
|
|
10
27
|
format,
|
|
11
28
|
quality,
|
|
12
29
|
recursive,
|
|
13
30
|
removeOriginal,
|
|
14
31
|
}) {
|
|
15
32
|
const rawPattern = recursive
|
|
16
|
-
? path.join(dir, "**",
|
|
17
|
-
: path.join(dir,
|
|
33
|
+
? path.join(dir, "**", converted)
|
|
34
|
+
: path.join(dir, converted);
|
|
18
35
|
|
|
19
36
|
const pattern = rawPattern.split(path.sep).join(path.posix.sep);
|
|
20
37
|
const files = await fg(pattern, { caseSensitiveMatch: false });
|
|
21
38
|
|
|
22
39
|
for (const file of files) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
if (processingFiles.has(file)) continue;
|
|
41
|
+
processingFiles.add(file);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const ext = path.extname(file);
|
|
45
|
+
const outFile = file.replace(ext, `.${format}`);
|
|
46
|
+
|
|
47
|
+
// Игнорируем файлы, которые уже в целевом формате
|
|
48
|
+
if (ext.toLowerCase() === `.${format.toLowerCase()}`) {
|
|
49
|
+
processingFiles.delete(file);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const image = sharp(file);
|
|
54
|
+
const buffer =
|
|
55
|
+
format === "webp"
|
|
56
|
+
? await image.webp({ quality }).toBuffer()
|
|
57
|
+
: await image.avif({ quality }).toBuffer();
|
|
58
|
+
|
|
59
|
+
await fs.writeFile(outFile, buffer);
|
|
60
|
+
|
|
61
|
+
if (removeOriginal) {
|
|
62
|
+
await safeUnlink(file);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log(`✓ ${file} → ${outFile}`);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error("Error covertation:", err.message);
|
|
68
|
+
} finally {
|
|
69
|
+
processingFiles.delete(file);
|
|
70
|
+
}
|
|
35
71
|
}
|
|
36
72
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-image-converter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"image",
|
|
6
|
+
"converter",
|
|
7
|
+
"webp",
|
|
8
|
+
"avif",
|
|
9
|
+
"sharp",
|
|
10
|
+
"watch",
|
|
11
|
+
"optimization",
|
|
12
|
+
"png",
|
|
13
|
+
"jpg",
|
|
14
|
+
"jpeg",
|
|
15
|
+
"for frontend"
|
|
16
|
+
],
|
|
4
17
|
"type": "module",
|
|
5
18
|
"bin": {
|
|
6
19
|
"auto-convert-images": "./bin/index.js",
|
|
Binary file
|