From a896b032aa3e8cb5fbad18be8b54f6229ea6707f Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Wed, 21 Dec 2016 14:08:21 +0100 Subject: [PATCH] Added list and delete commands to client.js for managing instances client.js list client.js delete [instance] also adde --help to show usage --- client.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- package.json | 3 ++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index 425fcba4..a0e3f784 100644 --- a/client.js +++ b/client.js @@ -1,20 +1,61 @@ var fs = require('fs'); var needle = require("needle"); var child_process = require('child_process'); +var path = require('path') // require config.json var config = require('./config'); var instance = process.argv[2]; -if (!instance) { - console.error("Usage: node client.js [instance name]") +if (!instance || instance == "help" || instance == "--help") { + console.error("Usage: ") + console.error("node client.js [instance name]") + console.error("node client.js list") + console.error("node client.js delete [instance]") process.exit(1) } +if (process.argv[2] == "list") { + console.dir(getDirectories("./instances/")); + process.exit(1) +} +var deleteFolderRecursive = 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); + } else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(path); + } +}; +if (process.argv[2] == "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] != "") { + deleteFolderRecursive("./instances/"+process.argv[3]); + console.log("Deleted instance " + process.argv[3]) + process.exit(1) + } else { + console.error("Instance not found: " + process.argv[3]); + process.exit(1) + } +} +function getDirectories(srcpath) { + return fs.readdirSync(srcpath).filter(function (file) { + return fs.statSync(path.join(srcpath, file)).isDirectory(); + }); +} if (!fs.existsSync("./instances/")) { fs.mkdirSync("instances"); } var instancedirectory = './instances/' + instance; +// if instance does not exist, create it if (!fs.existsSync(instancedirectory)) { + console.log("Creating instance...") fs.mkdirSync(instancedirectory); fs.mkdirSync(instancedirectory + "/script-output/"); fs.writeFileSync(instancedirectory + "/script-output/output.txt", "") diff --git a/package.json b/package.json index 9d8c3ff9..829c3e21 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Factorio cluster???", "main": "master.js", "scripts": { - "test": "nodemon master.js" + "test": "nodemon master.js", + "start": "echo Run node client.js [instance] to start slaves && node master.js" }, "keywords": [ "factorio"