auto-image-converter 1.1.14 → 1.1.16
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/watcher.mjs +21 -15
- package/package.json +1 -1
package/bin/watcher.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
import chokidar from "chokidar";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import { convertImages } from "../lib/converter.js";
|
|
@@ -8,30 +9,35 @@ const config = (await import(pathToFileURL(configPath).href)).default;
|
|
|
8
9
|
|
|
9
10
|
const watchDir = config.dir || "public";
|
|
10
11
|
const absWatchDir = path.resolve(process.cwd(), watchDir);
|
|
11
|
-
const watchPath = path.join(absWatchDir, "**", "*.{png,jpg,jpeg}");
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// Для chokidar лучше передать просто папку, а не glob
|
|
14
|
+
const watchPath = absWatchDir;
|
|
15
|
+
|
|
16
|
+
console.log(`👀 Watching for image changes on directory: ${watchPath}`);
|
|
14
17
|
|
|
15
18
|
chokidar
|
|
16
19
|
.watch(watchPath, {
|
|
20
|
+
ignored: /(^|[\/\\])\../, // игнор скрытых файлов и папок
|
|
21
|
+
persistent: true,
|
|
17
22
|
ignoreInitial: true,
|
|
18
23
|
awaitWriteFinish: {
|
|
19
|
-
stabilityThreshold: 500,
|
|
24
|
+
stabilityThreshold: 500, // ждем, пока запись в файл закончится
|
|
20
25
|
pollInterval: 100,
|
|
21
26
|
},
|
|
22
27
|
})
|
|
23
28
|
.on("add", async (filePath) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
if (/\.(png|jpe?g)$/i.test(filePath)) {
|
|
30
|
+
console.log(`➕ New image: ${filePath}`);
|
|
31
|
+
try {
|
|
32
|
+
await convertImages({
|
|
33
|
+
dir: watchPath,
|
|
34
|
+
format: config.targetFormat || config.format || "webp",
|
|
35
|
+
quality: config.quality ?? 80,
|
|
36
|
+
recursive: config.recursive ?? true,
|
|
37
|
+
removeOriginal: config.removeOriginal ?? false,
|
|
38
|
+
});
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.error("Ошибка при конвертации:", err.message);
|
|
41
|
+
}
|
|
36
42
|
}
|
|
37
43
|
});
|