import * as fs from 'fs' ; export namespace SymlinkSupport { export interface IStats { stat : fs . Stats ; symbolicLink ? : { dangling : boolean ; } ; } /**
     * Resolves the `fs.Stats` of the provided path. If the path is a
     * symbolic link, the `fs.Stats` will be from the target it points
     * to. If the target does not exist, `dangling: true` will be returned
     * as `symbolicLink` value.
     */ export async function stat ( path ) { let lstats ; try { lstats = await fs . promises . lstat ( path ) ; if ( ! lstats . isSymbolicLink ( ) ) { return { stat : lstats } ; } } catch ( error ) { } try { const stats = await fs . promises . stat ( path ) ; return { stat : stats , symbolicLink : lstats ?. isSymbolicLink ( ) ? { dangling : false } : undefined } ; } catch ( error ) { if ( error . code === 'ENOENT' && lstats ) { return { stat : lstats , symbolicLink : { dangling : true } } ; } if ( process . platform === 'win32' && error . code === 'EACCES' ) { try { const stats = await fs . promises . stat ( await fs . promises . readlink ( path ) ) ; return { stat : stats , symbolicLink : { dangling : false } } ; } catch ( error ) { if ( error . code === 'ENOENT' && lstats ) { return { stat : lstats , symbolicLink : { dangling : true } } ; } throw error ; } } throw error ; } } /**
     * Figures out if the `path` exists and is a file with support
     * for symlinks.
     *
     * Note: this will return `false` for a symlink that exists on
     * disk but is dangling (pointing to a non-existing path).
     *
     * Use `exists` if you only care about the path existing on disk
     * or not without support for symbolic links.
     */ export async function existsFile ( path ) { try { const { stat , symbolicLink } = await SymlinkSupport . stat ( path ) ; return stat . isFile ( ) && symbolicLink ?. dangling !== true ; } catch ( error ) { } return false ; } /**
     * Figures out if the `path` exists and is a directory with support for
     * symlinks.
     *
     * Note: this will return `false` for a symlink that exists on
     * disk but is dangling (pointing to a non-existing path).
     *
     * Use `exists` if you only care about the path existing on disk
     * or not without support for symbolic links.
     */ export async function existsDirectory ( path ) { try { const { stat , symbolicLink } = await SymlinkSupport . stat ( path ) ; return stat . isDirectory ( ) && symbolicLink ?. dangling !== true ; } catch ( error ) { } return false ; } }