const fs = require('fs').promises
// Returns a Promise<number> that computes the most recent
// modification time for any file inside a directory
async function modifiedTime(path) {
// First, we wait for the fs.stat() Promise
const stats = await fs.stat(path)
// Use the modification time of the file or directory itself
let latestTime = stats.mtimeMs
// If this is a directory, check all files inside for later modified times
if (stats.isDirectory()) {
// Wait to get a list of all files/subdirectories in the directory
const files = await fs.readdir(path)
// Then get the modified time of each file/subdirectory
for (const file of files) {
const fileModifiedTime = await modifiedTime(path + '/' + file)
// update latestTime if the modification time is later
latestTime = Math.max(latestTime, fileModifiedTime)
}
}
// Return the latest modification time
return latestTime
}
// Even though we return a number from modifiedTime, the number
// is wrapped in a Promise because it is an async function
modifiedTime('.').then(mTime => {
console.log(`Most recent modification: ${new Date(mTime)}`)
})
-
Caleb C. Sander authoredecfb12b9