diff --git a/client.js b/client.js index 0647474a..cef8ebd2 100644 --- a/client.js +++ b/client.js @@ -16,7 +16,7 @@ const ioClient = require("socket.io-client"); // internal libraries const objectOps = require("./lib/objectOps.js"); -const fileOps = require("./lib/fileOps.js"); +const fileOps = require("_app/fileOps"); const stringUtils = require("./lib/stringUtils.js"); const modManager = require("./lib/manager/modManager.js"); const configManager = require("./lib/manager/configManager.js"); @@ -232,8 +232,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // this line is probably not needed anymore but Im not gonna remove it fs.copySync('sharedMods', instancedirectory + "/mods"); let instconf = { - "factorioPort": process.env.FACTORIOPORT | Math.floor(Math.random() * 65535), - "clientPort": process.env.RCONPORT | Math.floor(Math.random() * 65535), + "factorioPort": process.env.FACTORIOPORT || Math.floor(Math.random() * 65535), + "clientPort": process.env.RCONPORT || Math.floor(Math.random() * 65535), "clientPassword": Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8), } console.log("Clusterio | Created instance with settings:") diff --git a/lib/generateSSLcert.spec.js b/lib/generateSSLcert.spec.js index 0e468c10..d56402c1 100644 --- a/lib/generateSSLcert.spec.js +++ b/lib/generateSSLcert.spec.js @@ -1,7 +1,7 @@ var assert = require("assert"); var fs = require("fs"); -var fileOps = require("./fileOps.js"); +var fileOps = require("_app/fileOps"); var generateSSLcert = require("./generateSSLcert.js") describe("generateSSLcert.js(options)", ()=>{ diff --git a/lib/getInstances.js b/lib/getInstances.js index 5e1d9449..6aeb540f 100644 --- a/lib/getInstances.js +++ b/lib/getInstances.js @@ -1,4 +1,4 @@ -const fileOps = require("./fileOps.js"); +const fileOps = require("_app/fileOps"); module.exports = { sync: function(){ diff --git a/lib/manager/modManager.js b/lib/manager/modManager.js index f7fc0294..1f8d53a8 100644 --- a/lib/manager/modManager.js +++ b/lib/manager/modManager.js @@ -12,7 +12,7 @@ const https = require('follow-redirects').https; const factorioAPI = require("node-factorio-api"); const sLog = require("single-line-log").stdout; -const fileOps = require("./../fileOps.js"); +const fileOps = require("_app/fileOps"); const config = require("./../../config"); const instances = require("./../getInstances.js").sync(); diff --git a/lib/fileOps.js b/node_modules/_app/fileOps/fileOps.js similarity index 64% rename from lib/fileOps.js rename to node_modules/_app/fileOps/fileOps.js index 65250fcf..3cbc43b3 100644 --- a/lib/fileOps.js +++ b/node_modules/_app/fileOps/fileOps.js @@ -1,5 +1,5 @@ -var fs = require("fs"); -var path = require("path"); +const fs = require("fs"); +const path = require("path"); // gets newest file in a directory // dir is directory @@ -70,10 +70,48 @@ var deleteFolderRecursiveSync = function(path) { return false; } }; +var deleteFolderRecursive = function(path){ + return new Promise((resolve, reject) => { + fs.readdir(path, (err, files) => { + if(err) reject(err); + 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(); + }); + } 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:getNewestFile, - getDirectoriesSync:getDirectoriesSync, - deleteFolderRecursiveSync:deleteFolderRecursiveSync, - getFileNamesSync:getFileNamesSync, + getNewestFile, + getDirectoriesSync, + deleteFolderRecursiveSync, + deleteFolderRecursive, + getFileNamesSync, } diff --git a/lib/fileOps.spec.js b/node_modules/_app/fileOps/fileOps.spec.js similarity index 51% rename from lib/fileOps.spec.js rename to node_modules/_app/fileOps/fileOps.spec.js index 83f09277..4d6cf9d1 100644 --- a/lib/fileOps.spec.js +++ b/node_modules/_app/fileOps/fileOps.spec.js @@ -1,30 +1,32 @@ -var assert = require('assert'); -var fileOps = require('./fileOps'); -var fs = require('fs'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const fileOps = require('./fileOps'); // create folders for testing function setupTestingEnv(){ - if (!fs.existsSync("database")){ - fs.mkdirSync("database"); + if (!fs.existsSync(path.resolve(__dirname, "database"))){ + fs.mkdirSync(path.resolve(__dirname, "database")); } - if (!fs.existsSync("lib/test")){ - fs.mkdirSync("lib/test"); + if (!fs.existsSync(path.resolve(__dirname, "test"))){ + fs.mkdirSync(path.resolve(__dirname, "test")); } - if (!fs.existsSync("lib/test/folder")){ - fs.mkdirSync("lib/test/folder"); + if (!fs.existsSync(path.resolve(__dirname, "test/folder"))){ + fs.mkdirSync(path.resolve(__dirname, "test/folder")); } - if (!fs.existsSync("lib/test/another folder")){ - fs.mkdirSync("lib/test/another folder"); + if (!fs.existsSync(path.resolve(__dirname, "test/another folder"))){ + fs.mkdirSync(path.resolve(__dirname, "test/another folder")); } - if (!fs.existsSync("lib/test/file.txt")){ - fs.writeFileSync("lib/test/file.txt", "contents"); + if (!fs.existsSync(path.resolve(__dirname, "test/file.txt"))){ + fs.writeFileSync(path.resolve(__dirname, "test/file.txt"), "contents"); } - if (!fs.existsSync("lib/test/another file.txt")){ - fs.writeFileSync("lib/test/another file.txt", "more contents"); + if (!fs.existsSync(path.resolve(__dirname, "test/another file.txt"))){ + fs.writeFileSync(path.resolve(__dirname, "test/another file.txt"), "more contents"); } // we need this xfile to test a branch in fileOps.getNewestFile()s propagation - if (!fs.existsSync("lib/test/xfile.txt")){ - fs.writeFileSync("lib/test/xfile.txt", "contents"); + if (!fs.existsSync(path.resolve(__dirname, "test/xfile.txt"))){ + fs.writeFileSync(path.resolve(__dirname, "test/xfile.txt"), "contents"); } } setupTestingEnv(); @@ -32,7 +34,7 @@ setupTestingEnv(); // run twice to get branch coverage describe("fileOps.js", function(){ describe("fileOps.getDirectoriesSync()", function(){ it("gets an array of directory names from a path", function(){ - let directories = fileOps.getDirectoriesSync("lib/test"); + let directories = fileOps.getDirectoriesSync(path.resolve(__dirname, "test")); // because there are no arrays for some reason assert(typeof directories == "object"); @@ -41,7 +43,7 @@ describe("fileOps.js", function(){ assert(directories[0] == "another folder" && directories[1] == "folder"); }); it("does not return any filenames", function(){ - let directories = fileOps.getDirectoriesSync("lib/test"); + let directories = fileOps.getDirectoriesSync(path.resolve(__dirname, "test")); // this is a function to return directories, not files. So yeah. assert(!directories.includes("file.txt") && !directories.includes("another file.txt")); @@ -54,7 +56,7 @@ describe("fileOps.js", function(){ }); describe("fileOps.getFileNamesSync(path)", function(){ it("gets names of files in directory", function(){ - let files = fileOps.getFileNamesSync("lib/test"); + let files = fileOps.getFileNamesSync(path.resolve(__dirname, "test")); assert(files.length > 0); assert(files.includes("file.txt") && files.includes("another file.txt")); }); @@ -66,24 +68,24 @@ describe("fileOps.js", function(){ }); describe("fileOps.getNewestFile()", function(){ it("gets the newest file in a directory async", function(done){ - fileOps.getNewestFile("lib/test/", fileOps.getFileNamesSync("lib/test"), function(err, newest){ + fileOps.getNewestFile(path.resolve(__dirname, "test/"), fileOps.getFileNamesSync(path.resolve(__dirname, "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")); + fileOps.getNewestFile("test", fileOps.getFileNamesSync(path.resolve(__dirname, "test"))); }, Error); }); it("executes callback(err) if no files are provided", function(done){ - fileOps.getNewestFile("lib/test", [], function(err, newest){ + fileOps.getNewestFile(path.resolve(__dirname, "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){ + fileOps.getNewestFile("", fileOps.getFileNamesSync(path.resolve(__dirname, "test")), function(err, newest){ assert(err); done(); }); @@ -98,9 +100,9 @@ describe("fileOps.js", function(){ }); describe("fileOps.deleteFolderRecursiveSync(path)", function(){ it("deletes a folder and all files and folders in it", function(){ - let x = fileOps.deleteFolderRecursiveSync("lib/test"); + let x = fileOps.deleteFolderRecursiveSync(path.resolve(__dirname, "test")); assert.throws(function(){ - fs.statSync("lib/test"); + fs.statSync(path.resolve(__dirname, "test")); }, Error); assert(x); // give it some extra time because its sometimes very slow @@ -110,4 +112,23 @@ describe("fileOps.js", function(){ assert(!x); }); }); + describe("fileOps.deleteFolderRecursive(path)", function(){ + it("deletes a folder and all files and folders in it, just like sync version", async function(){ + setupTestingEnv(); + let x = await fileOps.deleteFolderRecursive(path.resolve(__dirname, "test")); + assert.throws(function(){ + fs.statSync(path.resolve(__dirname, "test")); + }, Error); + assert(x === undefined, "Promise version of this function does not have a return value upon success"); + return true; + }).timeout(5000); + it("throws like a promise if you give it an invalid path", async function(){ + let x = await fileOps.deleteFolderRecursive("Look this path is fake!") + .catch(e => { + assert(e); + assert.equal(e.code, "ENOENT"); + return true; + }); + }); + }); }); diff --git a/node_modules/_app/fileOps/index.js b/node_modules/_app/fileOps/index.js new file mode 100644 index 00000000..5df8775d --- /dev/null +++ b/node_modules/_app/fileOps/index.js @@ -0,0 +1,2 @@ +const fileOps = require("./fileOps"); +module.exports = fileOps; \ No newline at end of file diff --git a/package.json b/package.json index 11e9347c..451dbc6e 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,9 @@ "repository": "https://github.com/Danielv123/factorioClusterio", "main": "master.js", "scripts": { - "test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- \"./{,!(node_modules)/**/}*.spec.js\" -R spec || echo .", + "test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- \"./{{,node_modules/_app/**/},!(node_modules)/**/}*.spec.js\" -R spec || echo .", "start": "echo Run node client.js start [instance] to start slaves && node master.js", - "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- \"./{,!(node_modules)/**/}*.spec.js\" -R spec || echo . && codecov -t f7b38085-36eb-4d72-84e6-64721910171e", + "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- \"./{{,node_modules/_app/**/},!(node_modules)/**/}*.spec.js\" -R spec || echo . && codecov -t f7b38085-36eb-4d72-84e6-64721910171e", "docs": "documentation build index.js --config documentation.yml -f html -o docs", "livedocs": "documentation serve --watch *.js lib/** --config documentation.yml", "install": "node lib/npmPostinstall.js"