mono/packages/fs/dist/iterator.js
2025-01-23 07:22:43 +01:00

73 lines
2.6 KiB
JavaScript

import { sync as treeWalkerSync } from './utils/tree_walker.js';
import { ENodeOperationStatus, EInspectFlags } from './interfaces.js';
import { create as matcher } from './utils/matcher.js';
import { ArrayIterator } from '@polymech/core/iterator';
export async function async(from, options) {
if (options && !options.filter) {
if (options.matching) {
options.filter = matcher(from, options.matching);
}
else {
options.filter = () => true;
}
}
const collectorSync = function (path, item) {
if (!item) {
return;
}
if (options.filter(path)) {
nodes.push({
path: path,
item: item,
status: ENodeOperationStatus.COLLECTED
});
}
};
const nodes = [];
return new Promise((resolve, reject) => {
treeWalkerSync(from, {
inspectOptions: {
mode: options ? options.flags & EInspectFlags.MODE ? true : false : false,
times: options ? options.flags & EInspectFlags.TIMES ? true : false : false,
checksum: options ? options.flags & EInspectFlags.CHECKSUM ? 'md5' : null : null,
symlinks: options ? options.flags & EInspectFlags.SYMLINKS ? false : true : true,
mime: options ? options.flags & EInspectFlags.MIME ? true : false : false
}
}, collectorSync);
resolve(new ArrayIterator(nodes));
});
}
export function sync(from, options) {
if (options && !options.filter) {
if (options.matching) {
options.filter = matcher(from, options.matching);
}
else {
options.filter = () => true;
}
}
const nodes = [];
const collectorSync = function (path, item) {
if (!item) {
return;
}
if (options.filter(path)) {
nodes.push({
path: path,
item: item,
status: ENodeOperationStatus.COLLECTED
});
}
};
treeWalkerSync(from, {
inspectOptions: {
mode: options ? options.flags & EInspectFlags.MODE ? true : false : false,
times: options ? options.flags & EInspectFlags.TIMES ? true : false : false,
checksum: options ? options.flags & EInspectFlags.CHECKSUM ? 'md5' : null : null,
symlinks: options ? options.flags & EInspectFlags.SYMLINKS ? false : true : true,
mime: options ? options.flags & EInspectFlags.MIME ? true : false : false
}
}, collectorSync);
return new ArrayIterator(nodes);
}