UNPKG

@nodelib/fs.scandir

Version:

List files and directories inside the specified directory

31 lines (30 loc) 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirentFromStats = exports.createDirentFromStats = void 0; const fs = require("node:fs"); const kStats = Symbol('stats'); function createDirentFromStats(name, stats) { return new DirentFromStats(name, stats); } exports.createDirentFromStats = createDirentFromStats; // Adapting an internal class in Node.js to mimic the behavior of `fs.Dirent` when creating it manually from `fs.Stats`. // https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L199C1-L213 class DirentFromStats extends fs.Dirent { [kStats]; constructor(name, stats) { // @ts-expect-error The constructor has parameters, but they are not represented in types. // https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L164 super(name, null); this[kStats] = stats; } } exports.DirentFromStats = DirentFromStats; for (const key of Reflect.ownKeys(fs.Dirent.prototype)) { const name = key; if (name === 'constructor') { continue; } DirentFromStats.prototype[name] = function () { return this[kStats][name](); }; }