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

128 lines
5.3 KiB
JavaScript

import { createHash } from 'node:crypto';
import * as pathUtil from 'node:path';
import { sync as inspectSync, async as inspectASync, supportedChecksumAlgorithms } from './inspect.js';
import { ENodeType } from './interfaces.js';
import { sync as listSync, async as listASync } from './list.js';
import { validateArgument, validateOptions } from './utils/validate.js';
export function validateInput(methodName, path, options) {
const methodSignature = methodName + '(path, options)';
validateArgument(methodSignature, 'path', path, ['string']);
validateOptions(methodSignature, 'options', options, {
checksum: ['string'],
relativePath: ['boolean']
});
if (options && options.checksum !== undefined
&& supportedChecksumAlgorithms.indexOf(options.checksum) === -1) {
throw new Error('Argument "options.checksum" passed to ' + methodSignature
+ ' must have one of values: ' + supportedChecksumAlgorithms.join(', '));
}
}
function generateTreeNodeRelativePath(parent, path) {
if (!parent) {
return '.';
}
return parent.relativePath + '/' + pathUtil.basename(path);
}
// Creates checksum of a directory by using
// checksums and names of all its children inside.
const checksumOfDir = (inspectList, algo) => {
const hash = createHash(algo);
inspectList.forEach(function (inspectObj) {
hash.update(inspectObj.name + inspectObj[algo]);
});
return hash.digest('hex');
};
// ---------------------------------------------------------
// Sync
// ---------------------------------------------------------
function inspectTreeNodeSync(path, options, parent) {
const treeBranch = inspectSync(path, { checksum: options.checksum, symlinks: options.symlinks });
if (treeBranch) {
if (options.relativePath) {
treeBranch.relativePath = generateTreeNodeRelativePath(parent, path);
}
if (treeBranch.type === ENodeType.DIR /*|| (options.symlinks && treeBranch.type === 'symlink')*/) {
treeBranch.size = 0;
treeBranch.children = (listSync(path) || []).map(function (filename) {
const subBranchPath = pathUtil.join(path, filename);
const treeSubBranch = inspectTreeNodeSync(subBranchPath, options, treeBranch);
// Add together all childrens' size to get directory combined size.
treeBranch.size += treeSubBranch.size || 0;
// treeBranch.total += treeSubBranch.total;
return treeSubBranch;
});
if (options.checksum) {
treeBranch[options.checksum] = checksumOfDir(treeBranch.children, options.checksum);
}
}
}
return treeBranch;
}
export function sync(path, options) {
options = options || {};
options.symlinks = true;
return inspectTreeNodeSync(path, options, undefined);
}
// ---------------------------------------------------------
// Async
// ---------------------------------------------------------
function inspectTreeNodeAsync(path, options, parent) {
return new Promise((resolve, reject) => {
const inspectAllChildren = (treeBranch) => {
return new Promise((resolve, reject) => {
listASync(path).then((children) => {
const doNext = (index) => {
let subPath;
if (index === children.length) {
if (options.checksum) {
// We are done, but still have to calculate checksum of whole directory.
[options.checksum] = checksumOfDir(treeBranch.children, options.checksum);
}
resolve(1);
}
else {
subPath = pathUtil.join(path, children[index]);
inspectTreeNodeAsync(subPath, options, treeBranch)
.then((treeSubBranch) => {
children[index] = treeSubBranch;
treeBranch.size += treeSubBranch.size || 0;
doNext(index + 1);
})
.catch(reject);
}
};
treeBranch.children = children;
treeBranch.size = 0;
doNext(0);
});
});
};
inspectASync(path, options)
.then((treeBranch) => {
if (!treeBranch) {
// Given path doesn't exist. We are done.
resolve(treeBranch);
}
else {
if (options.relativePath) {
treeBranch.relativePath = generateTreeNodeRelativePath(parent, path);
}
if (treeBranch.type !== ENodeType.DIR) {
resolve(treeBranch);
}
else {
inspectAllChildren(treeBranch)
.then(() => resolve(treeBranch))
.catch(reject);
}
}
})
.catch(reject);
});
}
export function async(path, options) {
options = options || {};
options.symlinks = true;
return inspectTreeNodeAsync(path, options);
}