Skip to content

Commit 23bb771

Browse files
committed
async processPath
1 parent 75fac20 commit 23bb771

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

src/utils/processPath.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1-
import * as fs from 'fs';
1+
import * as fs from 'fs/promises';
22
import * as path from 'path';
33
import { WorkerPool } from './workerPool';
44

55
/**
66
* Processes all files in the specified directory with the given image processing options.
77
*/
8-
export function processPath(
8+
export async function processPath(
99
directoryPath: string,
1010
options: Options,
1111
maxWorkers: number,
12-
): void {
12+
): Promise<void> {
1313
const workerPool = new WorkerPool(maxWorkers);
1414

15-
// Read the contents of the directory
16-
fs.readdir(directoryPath, (err, files) => {
17-
if (err) {
18-
console.error(`Error reading directory: ${directoryPath}`, err);
19-
return;
20-
}
15+
try {
16+
const files = await fs.readdir(directoryPath);
2117

2218
// Add each file as a task to the worker pool
23-
files.forEach((file) => {
19+
for (const file of files) {
2420
const filePath = path.join(directoryPath, file);
2521

2622
// Check if it's a file (not a subdirectory)
27-
if (fs.statSync(filePath).isFile()) {
23+
if ((await fs.stat(filePath)).isFile()) {
2824
workerPool.addTask(filePath, options);
2925
}
30-
});
26+
}
3127

3228
// Wait for all tasks to complete before exiting
3329
workerPool.waitForCompletion();
34-
});
30+
} catch (err) {
31+
console.error(`Error reading directory: ${directoryPath}`, err);
32+
}
3533
}

utils/processPath.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.processPath = void 0;
4-
const fs = require("fs");
4+
const fs = require("fs/promises");
55
const path = require("path");
66
const workerPool_1 = require("./workerPool");
77
/**
88
* Processes all files in the specified directory with the given image processing options.
99
*/
10-
function processPath(directoryPath, options, maxWorkers) {
10+
async function processPath(directoryPath, options, maxWorkers) {
1111
const workerPool = new workerPool_1.WorkerPool(maxWorkers);
12-
// Read the contents of the directory
13-
fs.readdir(directoryPath, (err, files) => {
14-
if (err) {
15-
console.error(`Error reading directory: ${directoryPath}`, err);
16-
return;
17-
}
12+
try {
13+
const files = await fs.readdir(directoryPath);
1814
// Add each file as a task to the worker pool
19-
files.forEach((file) => {
15+
for (const file of files) {
2016
const filePath = path.join(directoryPath, file);
2117
// Check if it's a file (not a subdirectory)
22-
if (fs.statSync(filePath).isFile()) {
18+
if ((await fs.stat(filePath)).isFile()) {
2319
workerPool.addTask(filePath, options);
2420
}
25-
});
21+
}
2622
// Wait for all tasks to complete before exiting
2723
workerPool.waitForCompletion();
28-
});
24+
}
25+
catch (err) {
26+
console.error(`Error reading directory: ${directoryPath}`, err);
27+
}
2928
}
3029
exports.processPath = processPath;

0 commit comments

Comments
 (0)