Created
August 28, 2018 08:03
-
-
Save roc/8e6db5d3ed4105112e88d32b19fd4cb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// async fs directory listing | |
// https://nodejs.org/api/fs.html#fs_fs_promises_api | |
const fs = require('fs').promises; | |
const bytes = require('bytes'); | |
const relativeDate = require('tiny-relative-date'); | |
const getHumanReadable = stats => { | |
// filesize | |
// date modified | |
// kind | |
// console.log(stats); | |
const { size, mtime, ctime } = stats; | |
return { | |
filesize: bytes(size), | |
dateModified: relativeDate(mtime), | |
dateCreated: relativeDate(ctime), | |
kind: stats.isFile() ? 'file' : 'directory' | |
}; | |
}; | |
export default async function getDirectoryContents(folder = __dirname) { | |
const files = await fs.readdir(folder); | |
try { | |
return await Promise.all( | |
files.map(async file => { | |
const stats = await fs.stat(file); | |
return { | |
filename: file, | |
info: getHumanReadable(stats) | |
}; | |
}) | |
); | |
} catch (e) { | |
console.error(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment