src/read-dir-structure.js
async function readDirStructure(dirPath) {
    if (!dirPath) {
        throw new Error('Please specify a path to the directory')
    }
    const lstat = await makePromise(fs.lstat, dirPath)
    if (!lstat.isDirectory()) {
        const err = new Error('Path is not a directory')
        err.code = 'ENOTDIR'
        throw err
    }
    const dir = await makePromise(fs.readdir, dirPath)
    const lstatRes = await lib.lstatFiles(dirPath, dir)

    const directories = lstatRes.filter(isDirectory)
    const notDirectories = lstatRes.filter(isNotDirectory)

    const files = notDirectories.reduce((acc, lstatRes) => Object.assign(acc, {
        [lstatRes.relativePath]: {
            type: getType(lstatRes),
        },
    }), {})

    const dirPromises = directories.map(async ({ path, relativePath }) => {
        const structure = await readDirStructure(path)
        return [relativePath, structure]
    })
    const dirsArray = await Promise.all(dirPromises)
    const dirs = dirsArray.reduce(
        (acc, [relativePath, structure]) => {
            const d = { [relativePath]: structure }
            return Object.assign(acc, d)
        }, {}
    )
    const merged = Object.assign({}, files, dirs)
    return {
        type: 'Directory',
        content: merged,
    }
}
async function readDirStructure(dirPath) {
    if (!dirPath) {
        throw new Error('Please specify a path to the directory')
    }
    const lstat = await makePromise(fs.lstat, dirPath)
    if (!lstat.isDirectory()) {
        const err = new Error('Path is not a directory')
        err.code = 'ENOTDIR'
        throw err
    }
    const dir = await makePromise(fs.readdir, dirPath)
    const lstatRes = await lib.lstatFiles(dirPath, dir)

    const directories = lstatRes.filter(isDirectory)
    const notDirectories = lstatRes.filter(isNotDirectory)

    const files = notDirectories.reduce((acc, lstatRes) => Object.assign(acc, {
        [lstatRes.relativePath]: {
            type: getType(lstatRes),
        },
    }), {})

    const dirPromises = directories.map(async ({ path, relativePath }) => {
        const structure = await readDirStructure(path)
        return [relativePath, structure]
    })
    const dirsArray = await Promise.all(dirPromises)
    const dirs = dirsArray.reduce(
        (acc, [relativePath, structure]) => {
            const d = { [relativePath]: structure }
            return Object.assign(acc, d)
        }, {}
    )
    const merged = Object.assign({}, files, dirs)
    return {
        type: 'Directory',
        content: merged,
    }
}
async function readDirStructure ( dirPath ) { if ( ! dirPath ) { throw new Error ( 'Please specify a path to the directory' ) } const lstat = await makePromise ( fs . lstat , dirPath ) if ( ! lstat . isDirectory ( ) ) { const err = new Error ( 'Path is not a directory' ) err . code = 'ENOTDIR' throw err } const dir = await makePromise ( fs . readdir , dirPath ) const lstatRes = await lib . lstatFiles ( dirPath , dir ) const directories = lstatRes . filter ( isDirectory ) const notDirectories = lstatRes . filter ( isNotDirectory ) const files = notDirectories . reduce ( ( acc , lstatRes ) => Object . assign ( acc , { [ lstatRes . relativePath ] : { type : getType ( lstatRes ) , } , } ) , { } ) const dirPromises = directories . map ( async ( { path , relativePath } ) => { const structure = await readDirStructure ( path ) return [ relativePath , structure ] } ) const dirsArray = await Promise . all ( dirPromises ) const dirs = dirsArray . reduce ( ( acc , [ relativePath , structure ] ) => { const d = { [ relativePath ] : structure } return Object . assign ( acc , d ) } , { } ) const merged = Object . assign ( { } , files , dirs ) return { type : 'Directory' , content : merged , } }
Read a directory, and return its structure as an object
@param {string} dirPath Path to the directory
@returns {Promise.<DirectoryStructure>} An object reflecting directory structure
Read a directory and return its structure as an object