auto-image-converter 1.1.15 → 2.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,69 @@
1
+ # 🖼️ Auto Image Converter
2
+
3
+ Automatically convert PNG, JPG, and JPEG images to WebP or AVIF format with real-time file watching.
4
+
5
+ ### 🔗 GitHub repository: https://github.com/StoneZol/auto-image-converter
6
+
7
+ ## 🚀 Features
8
+
9
+ 🔄 Convert images to .webp or .avif
10
+
11
+ 🔍 Recursive folder support
12
+
13
+ 🗑️ Optional removal of original files
14
+
15
+ 👀 Watch mode – automatically converts newly added images
16
+
17
+ ⚙️ Easy configuration via image-converter.config.mjs
18
+
19
+ ## 📦 Installation
20
+
21
+ `npm install auto-image-converter --save-dev`
22
+
23
+ ## ⚙️ Configuration
24
+
25
+ Create a `image-converter.config.mjs` file in the root of your project:
26
+
27
+ ```// Default configuration example
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
34
+ };
35
+ ```
36
+
37
+ ## 🛠️ Usage
38
+
39
+ Add the following to your package.json:
40
+
41
+ ```
42
+ "scripts": {
43
+ "aciw": "auto-convert-images-watch"
44
+ }
45
+ ```
46
+
47
+ To start the image watcher: `npm run aciw`
48
+
49
+ Or run a one-time conversion: `npx auto-convert-images`
50
+
51
+ ## With Next.js
52
+
53
+ To run the watcher alongside the development server, install concurrently:
54
+
55
+ `npm install concurrently --save-dev`
56
+
57
+ Then update your dev script like so:
58
+
59
+ ```
60
+ "scripts": {
61
+ "dev": "concurrently \"npm run aciw\" \"next dev\""
62
+ }
63
+ ```
64
+
65
+ ## 📄 License
66
+
67
+ This project is open source and available under the MIT License.
68
+
69
+ You are free to use, modify, and distribute it for personal and commercial purposes with proper attribution.
package/bin/watcher.mjs CHANGED
@@ -9,30 +9,35 @@ 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 watchPath = path.join(absWatchDir, "**", "*.{png,jpg,jpeg}");
13
12
 
14
- console.log(`👀 Watching for image changes on directory: ${absWatchDir}`);
13
+ // Для chokidar лучше передать просто папку, а не glob
14
+ const watchPath = absWatchDir;
15
+
16
+ console.log(`👀 Watching for image changes on directory: ${watchPath}`);
15
17
 
16
18
  chokidar
17
19
  .watch(watchPath, {
20
+ ignored: /(^|[\/\\])\../, // игнор скрытых файлов и папок
21
+ persistent: true,
18
22
  ignoreInitial: true,
19
23
  awaitWriteFinish: {
20
- stabilityThreshold: 500,
24
+ stabilityThreshold: 500, // ждем, пока запись в файл закончится
21
25
  pollInterval: 100,
22
26
  },
23
27
  })
24
28
  .on("add", async (filePath) => {
25
- console.log(`➕ New image: ${filePath}`);
26
- try {
27
- // Запускаем полную конвертацию всех файлов из конфига
28
- await convertImages({
29
- dir: absDir,
30
- format: config.targetFormat || config.format || "webp",
31
- quality: config.quality ?? 80,
32
- recursive: config.recursive ?? true,
33
- removeOriginal: config.removeOriginal ?? false,
34
- });
35
- } catch (err) {
36
- console.error("Ошибка при конвертации:", err.message);
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
+ }
37
42
  }
38
43
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-image-converter",
3
- "version": "1.1.15",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "auto-convert-images": "./bin/index.js",