fs: expose glob and globSync

PR-URL: https://github.com/nodejs/node/pull/51912
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Moshe Atlow 2024-03-04 01:15:35 +02:00 committed by GitHub
parent 2a33e95093
commit 151d365ad1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 443 additions and 34 deletions

View file

@ -86,6 +86,8 @@ const {
const { toPathIfFileURL } = require('internal/url');
const {
customPromisifyArgs: kCustomPromisifyArgsSymbol,
emitExperimentalWarning,
getLazy,
kEmptyObject,
promisify: {
custom: kCustomPromisifiedSymbol,
@ -3102,6 +3104,38 @@ function createWriteStream(path, options) {
return new WriteStream(path, options);
}
const lazyGlob = getLazy(() => require('internal/fs/glob').Glob);
function glob(pattern, options, callback) {
emitExperimentalWarning('glob');
if (typeof options === 'function') {
callback = options;
options = undefined;
}
callback = makeCallback(callback);
const Glob = lazyGlob();
// TODO: Use iterator helpers when available
(async () => {
try {
const res = [];
for await (const entry of new Glob(pattern, options).glob()) {
ArrayPrototypePush(res, entry);
}
callback(null, res);
} catch (err) {
callback(err);
}
})();
}
function globSync(pattern, options) {
emitExperimentalWarning('globSync');
const Glob = lazyGlob();
return new Glob(pattern, options).globSync();
}
module.exports = fs = {
appendFile,
appendFileSync,
@ -3135,6 +3169,8 @@ module.exports = fs = {
ftruncateSync,
futimes,
futimesSync,
glob,
globSync,
lchown,
lchownSync,
lchmod: constants.O_SYMLINK !== undefined ? lchmod : undefined,