@beenotung/tslib
Version:
utils library in Typescript
57 lines • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFSPool = void 0;
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const fs = tslib_1.__importStar(require("./fs"));
const task_pool_1 = require("./task/task-pool");
function createFSPool(concurrentSize) {
const pool = new task_pool_1.TaskPool(concurrentSize);
const res = fs;
for (const [name, func] of Object.entries(fs)) {
if (typeof func !== 'function' || name.endsWith('Sync')) {
continue;
}
;
res[name] = function () {
const args = arguments;
// tslint:disable-next-line:ban-types
return pool.runTask(() => func.apply(fs, args));
};
}
const { lstat, readdir, stat } = res;
res.scanRecursively = function scanRecursively(args) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { entryPath, onFile, onDir, onComplete, dereferenceSymbolicLinks, skipDir, } = args;
const checkStat = dereferenceSymbolicLinks ? stat : lstat;
const check = (pathname, basename) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const stat = yield checkStat(pathname);
if (stat.isDirectory()) {
if (onDir) {
yield onDir(pathname, basename);
}
if (skipDir && skipDir(pathname, basename)) {
return;
}
const names = yield readdir(pathname);
yield Promise.all(names.map(basename => {
const childPathname = path.join(pathname, basename);
return check(childPathname, basename);
}));
return;
}
if (onFile && stat.isFile()) {
yield onFile(pathname, basename);
return;
}
});
yield check(entryPath, path.basename(entryPath));
if (onComplete) {
yield onComplete();
}
});
};
return res;
}
exports.createFSPool = createFSPool;
//# sourceMappingURL=concurrent-fs.js.map