mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 07:04:53 +09:00
Remove sync functions that should not be used as well as of functions that provide functionality that is already implemented by fs-extra.
31 lines
744 B
JavaScript
31 lines
744 B
JavaScript
const fs = require("fs-extra");
|
|
const path = require("path");
|
|
|
|
/**
|
|
* Returns the newest file in a directory
|
|
*
|
|
* @param {string} directory - The directory to look for the newest file in.
|
|
* @returns {string|null}
|
|
* Name of the file with the newest timestamp or null if the
|
|
* directory contains no files.
|
|
*/
|
|
async function getNewestFile(directory) {
|
|
let newestTime = new Date(0);
|
|
let newestFile = null;
|
|
for (let entry of await fs.readdir(directory, { withFileTypes: true })) {
|
|
if (entry.isFile()) {
|
|
let stat = await fs.stat(path.join(directory, entry.name));
|
|
if (stat.mtime > newestTime) {
|
|
newestTime = stat.mtime;
|
|
newestFile = entry.name;
|
|
}
|
|
}
|
|
}
|
|
|
|
return newestFile;
|
|
}
|
|
|
|
module.exports = {
|
|
getNewestFile,
|
|
}
|