From a60970b0008eb4d62e3261a9681195089f10051b Mon Sep 17 00:00:00 2001 From: Will Lucas Date: Sat, 9 Mar 2024 21:23:43 -0600 Subject: [PATCH] Add support for following symlinks during directory walk --- packages/@ionic/utils-fs/src/index.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/@ionic/utils-fs/src/index.ts b/packages/@ionic/utils-fs/src/index.ts index 8cf6076b3..c961a2040 100644 --- a/packages/@ionic/utils-fs/src/index.ts +++ b/packages/@ionic/utils-fs/src/index.ts @@ -344,6 +344,16 @@ export interface WalkerOptions { * @return `true` to include file path, otherwise it is excluded */ readonly pathFilter?: (p: string) => boolean; + + /** + * Follow symlinks during walk. + * + * Additionally, the `IONIC_UTILS_FS_FOLLOW_SYMLINKS` environment variable can + * be used to set this option as `true` or `false`. + * + * @default false + */ + readonly followSymlinks?: boolean; } export interface Walker extends stream.Readable { @@ -360,14 +370,18 @@ export class Walker extends stream.Readable { _read() { const p = this.paths.shift(); - const { pathFilter } = this.options; + const { pathFilter, followSymlinks } = this.options; if (!p) { this.push(null); return; } - fs.lstat(p, (err, stats) => { + const envFollowSymlinks = process.env.IONIC_UTILS_FS_FOLLOW_SYMLINKS && + process.env.IONIC_UTILS_FS_FOLLOW_SYMLINKS === 'true' || + process.env.IONIC_UTILS_FS_FOLLOW_SYMLINKS === '1'; + const finalPath = (followSymlinks || envFollowSymlinks) ? fs.realpathSync(p) : p; + fs.lstat(finalPath, (err, stats) => { if (err) { this.emit('error', err); return;