mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
Moved fileOps to _app/fileOps
This commit is contained in:
1 parent
802c21f41e
commit
fff1989897
8 files changed
+101
-40
No files matched your search
@@ -16,7 +16,7 @@ const ioClient = require("socket.io-client");
|
|||||||
|
|
||||||
// internal libraries
|
// internal libraries
|
||||||
const objectOps = require("./lib/objectOps.js");
|
const objectOps = require("./lib/objectOps.js");
|
||||||
const fileOps = require("./lib/fileOps.js");
|
const fileOps = require("_app/fileOps");
|
||||||
const stringUtils = require("./lib/stringUtils.js");
|
const stringUtils = require("./lib/stringUtils.js");
|
||||||
const modManager = require("./lib/manager/modManager.js");
|
const modManager = require("./lib/manager/modManager.js");
|
||||||
const configManager = require("./lib/manager/configManager.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
|
// this line is probably not needed anymore but Im not gonna remove it
|
||||||
fs.copySync('sharedMods', instancedirectory + "/mods");
|
fs.copySync('sharedMods', instancedirectory + "/mods");
|
||||||
let instconf = {
|
let instconf = {
|
||||||
"factorioPort": process.env.FACTORIOPORT | Math.floor(Math.random() * 65535),
|
"factorioPort": process.env.FACTORIOPORT || Math.floor(Math.random() * 65535),
|
||||||
"clientPort": process.env.RCONPORT | 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),
|
"clientPassword": Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8),
|
||||||
}
|
}
|
||||||
console.log("Clusterio | Created instance with settings:")
|
console.log("Clusterio | Created instance with settings:")
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
|
||||||
var fileOps = require("./fileOps.js");
|
var fileOps = require("_app/fileOps");
|
||||||
var generateSSLcert = require("./generateSSLcert.js")
|
var generateSSLcert = require("./generateSSLcert.js")
|
||||||
|
|
||||||
describe("generateSSLcert.js(options)", ()=>{
|
describe("generateSSLcert.js(options)", ()=>{
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
const fileOps = require("./fileOps.js");
|
const fileOps = require("_app/fileOps");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sync: function(){
|
sync: function(){
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ const https = require('follow-redirects').https;
|
|||||||
const factorioAPI = require("node-factorio-api");
|
const factorioAPI = require("node-factorio-api");
|
||||||
const sLog = require("single-line-log").stdout;
|
const sLog = require("single-line-log").stdout;
|
||||||
|
|
||||||
const fileOps = require("./../fileOps.js");
|
const fileOps = require("_app/fileOps");
|
||||||
const config = require("./../../config");
|
const config = require("./../../config");
|
||||||
|
|
||||||
const instances = require("./../getInstances.js").sync();
|
const instances = require("./../getInstances.js").sync();
|
||||||
|
|||||||
+44
-6
@@ -1,5 +1,5 @@
|
|||||||
var fs = require("fs");
|
const fs = require("fs");
|
||||||
var path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
// gets newest file in a directory
|
// gets newest file in a directory
|
||||||
// dir is directory
|
// dir is directory
|
||||||
@@ -70,10 +70,48 @@ var deleteFolderRecursiveSync = function(path) {
|
|||||||
return false;
|
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 = {
|
module.exports = {
|
||||||
getNewestFile:getNewestFile,
|
getNewestFile,
|
||||||
getDirectoriesSync:getDirectoriesSync,
|
getDirectoriesSync,
|
||||||
deleteFolderRecursiveSync:deleteFolderRecursiveSync,
|
deleteFolderRecursiveSync,
|
||||||
getFileNamesSync:getFileNamesSync,
|
deleteFolderRecursive,
|
||||||
|
getFileNamesSync,
|
||||||
}
|
}
|
||||||
+47
-26
@@ -1,30 +1,32 @@
|
|||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var fileOps = require('./fileOps');
|
const fs = require('fs');
|
||||||
var fs = require('fs');
|
const path = require('path');
|
||||||
|
|
||||||
|
const fileOps = require('./fileOps');
|
||||||
|
|
||||||
// create folders for testing
|
// create folders for testing
|
||||||
function setupTestingEnv(){
|
function setupTestingEnv(){
|
||||||
if (!fs.existsSync("database")){
|
if (!fs.existsSync(path.resolve(__dirname, "database"))){
|
||||||
fs.mkdirSync("database");
|
fs.mkdirSync(path.resolve(__dirname, "database"));
|
||||||
}
|
}
|
||||||
if (!fs.existsSync("lib/test")){
|
if (!fs.existsSync(path.resolve(__dirname, "test"))){
|
||||||
fs.mkdirSync("lib/test");
|
fs.mkdirSync(path.resolve(__dirname, "test"));
|
||||||
}
|
}
|
||||||
if (!fs.existsSync("lib/test/folder")){
|
if (!fs.existsSync(path.resolve(__dirname, "test/folder"))){
|
||||||
fs.mkdirSync("lib/test/folder");
|
fs.mkdirSync(path.resolve(__dirname, "test/folder"));
|
||||||
}
|
}
|
||||||
if (!fs.existsSync("lib/test/another folder")){
|
if (!fs.existsSync(path.resolve(__dirname, "test/another folder"))){
|
||||||
fs.mkdirSync("lib/test/another folder");
|
fs.mkdirSync(path.resolve(__dirname, "test/another folder"));
|
||||||
}
|
}
|
||||||
if (!fs.existsSync("lib/test/file.txt")){
|
if (!fs.existsSync(path.resolve(__dirname, "test/file.txt"))){
|
||||||
fs.writeFileSync("lib/test/file.txt", "contents");
|
fs.writeFileSync(path.resolve(__dirname, "test/file.txt"), "contents");
|
||||||
}
|
}
|
||||||
if (!fs.existsSync("lib/test/another file.txt")){
|
if (!fs.existsSync(path.resolve(__dirname, "test/another file.txt"))){
|
||||||
fs.writeFileSync("lib/test/another file.txt", "more contents");
|
fs.writeFileSync(path.resolve(__dirname, "test/another file.txt"), "more contents");
|
||||||
}
|
}
|
||||||
// we need this xfile to test a branch in fileOps.getNewestFile()s propagation
|
// we need this xfile to test a branch in fileOps.getNewestFile()s propagation
|
||||||
if (!fs.existsSync("lib/test/xfile.txt")){
|
if (!fs.existsSync(path.resolve(__dirname, "test/xfile.txt"))){
|
||||||
fs.writeFileSync("lib/test/xfile.txt", "contents");
|
fs.writeFileSync(path.resolve(__dirname, "test/xfile.txt"), "contents");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setupTestingEnv();
|
setupTestingEnv();
|
||||||
@@ -32,7 +34,7 @@ setupTestingEnv(); // run twice to get branch coverage
|
|||||||
describe("fileOps.js", function(){
|
describe("fileOps.js", function(){
|
||||||
describe("fileOps.getDirectoriesSync()", function(){
|
describe("fileOps.getDirectoriesSync()", function(){
|
||||||
it("gets an array of directory names from a path", 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
|
// because there are no arrays for some reason
|
||||||
assert(typeof directories == "object");
|
assert(typeof directories == "object");
|
||||||
@@ -41,7 +43,7 @@ describe("fileOps.js", function(){
|
|||||||
assert(directories[0] == "another folder" && directories[1] == "folder");
|
assert(directories[0] == "another folder" && directories[1] == "folder");
|
||||||
});
|
});
|
||||||
it("does not return any filenames", function(){
|
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.
|
// this is a function to return directories, not files. So yeah.
|
||||||
assert(!directories.includes("file.txt") && !directories.includes("another file.txt"));
|
assert(!directories.includes("file.txt") && !directories.includes("another file.txt"));
|
||||||
@@ -54,7 +56,7 @@ describe("fileOps.js", function(){
|
|||||||
});
|
});
|
||||||
describe("fileOps.getFileNamesSync(path)", function(){
|
describe("fileOps.getFileNamesSync(path)", function(){
|
||||||
it("gets names of files in directory", 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.length > 0);
|
||||||
assert(files.includes("file.txt") && files.includes("another file.txt"));
|
assert(files.includes("file.txt") && files.includes("another file.txt"));
|
||||||
});
|
});
|
||||||
@@ -66,24 +68,24 @@ describe("fileOps.js", function(){
|
|||||||
});
|
});
|
||||||
describe("fileOps.getNewestFile()", function(){
|
describe("fileOps.getNewestFile()", function(){
|
||||||
it("gets the newest file in a directory async", function(done){
|
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");
|
assert(newest && typeof newest.file == "string");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("throws if callback is missing", function(){
|
it("throws if callback is missing", function(){
|
||||||
assert.throws(function(){
|
assert.throws(function(){
|
||||||
fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test"));
|
fileOps.getNewestFile("test", fileOps.getFileNamesSync(path.resolve(__dirname, "test")));
|
||||||
}, Error);
|
}, Error);
|
||||||
});
|
});
|
||||||
it("executes callback(err) if no files are provided", function(done){
|
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");
|
assert(err && typeof err == "object");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("executes callback(err) if launched without a directory", function(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);
|
assert(err);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -98,9 +100,9 @@ describe("fileOps.js", function(){
|
|||||||
});
|
});
|
||||||
describe("fileOps.deleteFolderRecursiveSync(path)", function(){
|
describe("fileOps.deleteFolderRecursiveSync(path)", function(){
|
||||||
it("deletes a folder and all files and folders in it", 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(){
|
assert.throws(function(){
|
||||||
fs.statSync("lib/test");
|
fs.statSync(path.resolve(__dirname, "test"));
|
||||||
}, Error);
|
}, Error);
|
||||||
assert(x);
|
assert(x);
|
||||||
// give it some extra time because its sometimes very slow
|
// give it some extra time because its sometimes very slow
|
||||||
@@ -110,4 +112,23 @@ describe("fileOps.js", function(){
|
|||||||
assert(!x);
|
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;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
const fileOps = require("./fileOps");
|
||||||
|
module.exports = fileOps;
|
||||||
+2
-2
@@ -5,9 +5,9 @@
|
|||||||
"repository": "https://github.com/Danielv123/factorioClusterio",
|
"repository": "https://github.com/Danielv123/factorioClusterio",
|
||||||
"main": "master.js",
|
"main": "master.js",
|
||||||
"scripts": {
|
"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",
|
"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",
|
"docs": "documentation build index.js --config documentation.yml -f html -o docs",
|
||||||
"livedocs": "documentation serve --watch *.js lib/** --config documentation.yml",
|
"livedocs": "documentation serve --watch *.js lib/** --config documentation.yml",
|
||||||
"install": "node lib/npmPostinstall.js"
|
"install": "node lib/npmPostinstall.js"
|
||||||
|
|||||||
Reference in new issue
Block a user