From ee7753c1e03fe74bc133ecbf7eb149fb53841f58 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Mon, 27 Dec 2021 01:10:18 +0100 Subject: added working cli arguments I think? --- src/cli.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 25 ++++++++++++++++++------- src/utils.js | 34 ++++++++++++++++++++++++---------- 3 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 src/cli.js diff --git a/src/cli.js b/src/cli.js new file mode 100644 index 0000000..b550a8f --- /dev/null +++ b/src/cli.js @@ -0,0 +1,55 @@ +const fs = require("fs"); +const { app, ipcMain } = require("electron"); + +const Emitter = require("events"); +const events = new Emitter(); + +const cli = app.commandLine; + +function hasArgs() { + if (cli.hasSwitch("cli") || + cli.hasSwitch("help") || + cli.hasSwitch("update") || + cli.hasSwitch("setpath") || + cli.hasSwitch("gamepath")) { + return true; + } else {return false} +} + +function exit(code) { + if (hasArgs()) {process.exit(code)} +} + +async function init() { + if (cli.hasSwitch("help")) { + console.log(`options: + --help shows this help message + + --cli forces the CLI to enable + --update updates Northstar from your set game path + --setpath sets your game path`) + // In the future --setpath should be able to understand + // relative paths, instead of just absolute ones. + exit(); + } + + if (cli.hasSwitch("update")) { + ipcMain.emit("update"); + } + + if (cli.hasSwitch("setpath")) { + if (cli.getSwitchValue("setpath") != "") { + ipcMain.emit("setpathcli", cli.getSwitchValue("setpath")); + } else { + console.error("error: No argumment provided for --setpath"); + } + } +} + +module.exports = { + hasArgs, + init, exit, + param: (arg) => { + return cli.getSwitchValue(arg) + } +} diff --git a/src/index.js b/src/index.js index 5586633..9fe7610 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,11 @@ const fs = require("fs"); const path = require("path"); const { app, dialog, ipcMain, BrowserWindow } = require("electron"); -const utils = require("./utils") +const Emitter = require("events"); +const events = new Emitter(); + +const utils = require("./utils"); +const cli = require("./cli"); function start() { win = new BrowserWindow({ @@ -20,12 +24,19 @@ function start() { win.loadFile(__dirname + "/app/index.html"); win.webContents.once("dom-ready", () => {win.show()}); - ipcMain.on("update", (event) => {utils.update(win)}) ipcMain.on("setpath", (event) => {utils.setpath(win)}) } -app.on("ready", () => { - process.chdir(app.getPath("appData")); - app.setPath("userData", path.join(app.getPath("cache"), app.name)); - start(); -}) +ipcMain.on("setpathcli", (event) => {utils.setpath()}) +ipcMain.on("update", (event) => {utils.update()}) + +process.chdir(app.getPath("appData")); + +if (cli.hasArgs()) { + cli.init(); +} else { + app.on("ready", () => { + app.setPath("userData", path.join(app.getPath("cache"), app.name)); + start(); + }) +} diff --git a/src/utils.js b/src/utils.js index c38cbcc..000c911 100644 --- a/src/utils.js +++ b/src/utils.js @@ -2,6 +2,11 @@ const fs = require("fs"); const path = require("path"); const { app, dialog, ipcMain } = require("electron"); +const Emitter = require("events"); +const events = new Emitter(); + +const cli = require("./cli"); + const unzip = require("unzipper"); const request = require("request"); const { https } = require("follow-redirects"); @@ -17,22 +22,28 @@ var settings = { if (fs.existsSync(settings.file)) { settings.gamepath = JSON.parse(fs.readFileSync(settings.file, "utf8")).path; settings.zip = path.join(settings.gamepath + "/northstar.zip"); -console.log(settings) } else { - console.log("Game path is not set! Please select the path!"); + console.log("Game path is not set! Please select the path."); } function setpath(win) { - dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { - fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({path: res.filePaths[0]})) + if (! win) { + fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({path: cli.param("setpath")})); + settings.gamepath = cli.param("setpath"); + cli.exit(); + } else { + dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { + fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({path: res.filePaths[0]})); - win.webContents.send("newpath", res.filePaths[0]); - settings.gamepath = res.filePaths[0]; - }).catch(err => {console.error(err)}) + win.webContents.send("newpath", res.filePaths[0]); + settings.gamepath = res.filePaths[0]; + }).catch(err => {console.error(err)}) + } } function update() { + console.log("Downloading..."); request({ json: true, headers: {"User-Agent": "Viper"}, @@ -41,12 +52,14 @@ function update() { https.get(body.assets[0].browser_download_url, (res) => { let stream = fs.createWriteStream(settings.zip); res.pipe(stream); - stream.on("finish",() => { + stream.on("finish", () => { stream.close(); - console.log("Download done!"); + console.log("Download done! Extracting..."); fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath})) .on("finish", () => { - console.log("Installation finished!") + console.log("Installation/Update finished!"); + events.emit("updated"); + cli.exit(); }); }) }) @@ -56,4 +69,5 @@ function update() { module.exports = { update, setpath, + settings, } -- cgit v1.2.3