auto-image-converter 2.0.1 → 2.1.1

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 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", // Directory to scan for images
30
- format: "webp", // Output format: 'webp' or 'avif'
31
- quality: 80, // Quality (0–100)
32
- recursive: true, // Search subdirectories
33
- removeOriginal: true, // Remove original files after conversion
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
- format: config.targetFormat || config.format || "webp",
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(watchPath, {
20
- ignored: /(^|[\/\\])\../, // игнор скрытых файлов и папок
21
+ .watch(absWatchDir, {
22
+ ignored: /(^|[\/\\])\../,
21
23
  persistent: true,
22
- ignoreInitial: true,
24
+ ignoreInitial: config.ignoreOnStart ?? false,
23
25
  awaitWriteFinish: {
24
- stabilityThreshold: 500, // ждем, пока запись в файл закончится
25
- pollInterval: 100,
26
+ stabilityThreshold: 1500,
27
+ pollInterval: 500,
26
28
  },
27
29
  })
28
30
  .on("add", async (filePath) => {
29
- if (/\.(png|jpe?g)$/i.test(filePath)) {
30
- console.log(`➕ New image: ${filePath}`);
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
- format: config.targetFormat || config.format || "webp",
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("Ошибка при конвертации:", err.message);
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
+ }
@@ -1,7 +1,9 @@
1
1
  export default {
2
2
  dir: "./public",
3
+ converted: "*.{png,jpg,jpeg}",
3
4
  format: "webp",
4
5
  quality: 80,
5
6
  removeOriginal: true,
6
7
  recursive: true,
8
+ ignoreOnStart: true,
7
9
  };
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, "**", "*.{png,jpg,jpeg}")
17
- : path.join(dir, "*.{png,jpg,jpeg}");
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
- const ext = path.extname(file);
24
- const outFile = file.replace(ext, `.${format}`);
25
-
26
- const image = sharp(file);
27
- const buffer =
28
- format === "webp"
29
- ? await image.webp({ quality }).toBuffer()
30
- : await image.avif({ quality }).toBuffer();
31
-
32
- await fs.writeFile(outFile, buffer);
33
- if (removeOriginal) await fs.unlink(file);
34
- console.log(`✓ ${file} → ${outFile}`);
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,6 @@
1
1
  {
2
2
  "name": "auto-image-converter",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "keywords": [
5
5
  "image",
6
6
  "converter",
@@ -12,7 +12,7 @@
12
12
  "png",
13
13
  "jpg",
14
14
  "jpeg",
15
- "cli"
15
+ "for frontend"
16
16
  ],
17
17
  "type": "module",
18
18
  "bin": {
Binary file