diff --git a/client.js b/client.js index 2292dc2a..7d3dc780 100644 --- a/client.js +++ b/client.js @@ -82,14 +82,14 @@ if (!command || command == "help" || command == "--help") { console.error("node client.js download"); process.exit(1); } else if (command == "list") { - console.dir(fileOps.getDirectories("./instances/")); + console.dir(fileOps.getDirectoriesSync("./instances/")); process.exit(1); } else if (command == "delete") { if (!process.argv[3]) { console.error("Usage: node client.js delete [instance]"); process.exit(1); } else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") { - fileOps.deleteFolderRecursive("./instances/" + process.argv[3]); + fileOps.deleteFolderRecursiveSync("./instances/" + process.argv[3]); console.log("Deleted instance " + process.argv[3]); process.exit(1); } else { @@ -216,7 +216,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // Spawn factorio server //var serverprocess = child_process.exec(commandline); - fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { + fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(err, latestSave) { + if(err) throw err; // implicit global serverprocess = child_process.spawn( './' + config.factorioDirectory + '/bin/x64/factorio', [ @@ -301,7 +302,7 @@ _.once(instanceManagement); function instanceManagement() { console.log("Started instanceManagement();"); // load plugins and execute onLoad event - let pluginDirectories = fileOps.getDirectories("./sharedPlugins/"); + let pluginDirectories = fileOps.getDirectoriesSync("./sharedPlugins/"); let plugins = []; for(i=0; i newest.mtime.getTime()) { - newest = { file : file, mtime : stats.mtime }; - } - if (checked == files.length) { - callback(newest); - } - }); - })(dir + file); + 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 all directories in folder -var getDirectories = function(srcpath) { +// 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 deleteFolderRecursive = function(path) { +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 - deleteFolderRecursive(curPath); + deleteFolderRecursiveSync(curPath); } else { // delete file fs.unlinkSync(curPath); } @@ -59,7 +70,7 @@ var deleteFolderRecursive = function(path) { module.exports = { getNewestFile:getNewestFile, - getDirectories:getDirectories, - deleteFolderRecursive:deleteFolderRecursive, + getDirectoriesSync:getDirectoriesSync, + deleteFolderRecursiveSync:deleteFolderRecursiveSync, getFileNamesSync:getFileNamesSync, -} \ No newline at end of file +} diff --git a/lib/fileOps.spec.js b/lib/fileOps.spec.js index 7297f11c..112e140e 100644 --- a/lib/fileOps.spec.js +++ b/lib/fileOps.spec.js @@ -2,9 +2,9 @@ var assert = require('assert'); var fileOps = require('./fileOps'); describe("fileOps.js", function(){ - describe("fileOps.getDirectories()", function(){ + describe("fileOps.getDirectoriesSync()", function(){ it("gets an array of directory names from a path", function(){ - let directories = fileOps.getDirectories("lib/test"); + let directories = fileOps.getDirectoriesSync("lib/test"); // because there are no arrays for some reason assert(typeof directories == "object"); @@ -13,11 +13,16 @@ describe("fileOps.js", function(){ assert(directories[0] == "another folder" && directories[1] == "folder"); }); it("does not return any filenames", function(){ - let directories = fileOps.getDirectories("lib/test"); + let directories = fileOps.getDirectoriesSync("lib/test"); // this is a function to return directories, not files. So yeah. assert(!directories.includes("file.txt") && !directories.includes("another file.txt")); }); + it("throws when lacking first argument", function(){ + assert.throws(function(){ + let files = fileOps.getDirectoriesSync(); + }, Error); + }); }); describe("fileOps.getFileNamesSync(path)", function(){ it("gets names of files in directory", function(){ @@ -31,13 +36,36 @@ describe("fileOps.js", function(){ }, Error); }); }); - /*describe("fileOps.getNewestFile()", function(){ + describe("fileOps.getNewestFile()", function(){ it("gets the newest file in a directory async", function(done){ - fileOps.getNewestFile("lib/test", fileOps.getFileNames("lib/test"), function(newest){ - assert(newest); + fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test"), function(err, newest){ + assert(newest && typeof newest.file == "string"); done(); - }) - + }); }); - });*/ + it("throws if callback is missing", function(){ + assert.throws(function(){ + fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test")); + }, Error); + }); + it("executes callback(err) if no files are provided", function(done){ + fileOps.getNewestFile("lib/test", [], function(err, newest){ + assert(err && typeof err == "object"); + done(); + }); + }); + it("executes callback(err) if launched without a directory", function(done){ + fileOps.getNewestFile("", fileOps.getFileNamesSync("lib/test"), function(err, newest){ + assert(err); + done(); + }); + }); + it("executes callback(err) when you do stupid shit", function(done){ + fileOps.getNewestFile({thisWontWork: "but not crash either"}, {lolz:"yeah..."}, function(err, newest){ + assert(err); + // ENOENT: no such file or directory, stat 'C:\**\factorioClusterio\lib\test\undefined' + done(); + }); + }); + }); });