mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Move the contents of node_modules/_app into lib/ as there shouldn't be any content in node_modules. This directory is automatically managed and pruned by npm. Add lib/ as a local package in package.json, this allows scripts everywhere to require it's content as "lib/path" instead of relying on local paths. This shadows the StdLib Node.js Bindings package, but I could care less. Change all requires with lib as a local path and _app to use just lib.
121 lines
3.1 KiB
JavaScript
121 lines
3.1 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
// gets newest file in a directory
|
|
// dir is directory
|
|
// files is array of filenames
|
|
// callback(err, filename string);
|
|
var getNewestFile = function(dir, files, callback) {
|
|
if (!callback) throw new Error("Missing callback");
|
|
if (!files || (files && files.length === 0)) {
|
|
callback(new Error("No files"));
|
|
return;
|
|
}
|
|
// we are appending the file name to the directory, without the
|
|
// "/" we would get paths like /folderfile.txt instead of /folder/file.txt
|
|
if(dir[dir.length-1] != "/"){
|
|
dir += "/";
|
|
}
|
|
var newest = { file: files[0] };
|
|
var checked = 0;
|
|
fs.stat(dir + newest.file, function(err, stats) {
|
|
if(err) {
|
|
callback(err);
|
|
} else {
|
|
newest.mtime = stats.mtime;
|
|
for (var i = 0; i < files.length; i++) {
|
|
var file = files[i];
|
|
(function(file) {
|
|
fs.stat(file, function(err, stats) {
|
|
++checked;
|
|
if (stats.mtime.getTime() > newest.mtime.getTime()) {
|
|
newest = { file : file, mtime : stats.mtime };
|
|
}
|
|
if (checked == files.length) {
|
|
callback(undefined,newest);
|
|
}
|
|
});
|
|
})(dir + file);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// get names of all directories in directory
|
|
var getDirectoriesSync = function(srcpath) {
|
|
return fs.readdirSync(srcpath).filter(function (file) {
|
|
return fs.statSync(path.join(srcpath, file)).isDirectory();
|
|
});
|
|
}
|
|
// get names of all files in a directory
|
|
var getFileNamesSync = function(srcpath) {
|
|
return fs.readdirSync(srcpath).filter(function (file) {
|
|
return !fs.statSync(path.join(srcpath, file)).isDirectory();
|
|
});
|
|
}
|
|
// does what it says on the tin
|
|
var deleteFolderRecursiveSync = function(path) {
|
|
if (fs.existsSync(path)) {
|
|
fs.readdirSync(path).forEach(function (file, index) {
|
|
var curPath = path + "/" + file;
|
|
if (fs.lstatSync(curPath).isDirectory()) { // recurse
|
|
deleteFolderRecursiveSync(curPath);
|
|
} else { // delete file
|
|
fs.unlinkSync(curPath);
|
|
}
|
|
});
|
|
fs.rmdirSync(path);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
var deleteFolderRecursive = function(path){
|
|
return new Promise((resolve, reject) => {
|
|
fs.readdir(path, (err, files) => {
|
|
if(err){
|
|
reject(err);
|
|
} else {
|
|
let thingsToDelete = files.length;
|
|
files.forEach(file => {
|
|
let curPath = path + "/" + file;
|
|
fs.stat(curPath, (err, stat) => {
|
|
if(!err){
|
|
if(stat.isDirectory()){
|
|
// recurse
|
|
deleteFolderRecursive(curPath).then(() => {
|
|
whenFilesDeleted();
|
|
}).catch(e => console.log(e));
|
|
} else {
|
|
fs.unlink(curPath, () => {
|
|
// console.log("Deleted "+curPath);
|
|
whenFilesDeleted();
|
|
});
|
|
}
|
|
} else {
|
|
resolve(false);
|
|
}
|
|
});
|
|
});
|
|
whenFilesDeleted();
|
|
function whenFilesDeleted(){
|
|
if(!thingsToDelete--){// var-- instead of --var because we are calling the function once too much and need the delay
|
|
fs.rmdir(path, () => {
|
|
resolve();
|
|
// console.log("Deleted "+path);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
getNewestFile,
|
|
getDirectoriesSync,
|
|
deleteFolderRecursiveSync,
|
|
deleteFolderRecursive,
|
|
getFileNamesSync,
|
|
}
|