Skip to content

Instantly share code, notes, and snippets.

@roc
Created August 28, 2018 08:03
Show Gist options
  • Save roc/8e6db5d3ed4105112e88d32b19fd4cb8 to your computer and use it in GitHub Desktop.
Save roc/8e6db5d3ed4105112e88d32b19fd4cb8 to your computer and use it in GitHub Desktop.
// 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