diff options
author | 0neGal <mail@0negal.com> | 2021-12-27 01:10:18 +0100 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2021-12-27 01:14:08 +0100 |
commit | ee7753c1e03fe74bc133ecbf7eb149fb53841f58 (patch) | |
tree | c6fa10188cc01c6c1ac31eb50816b5aa6d32e318 /src | |
parent | df7b714d27e5d23a6ac3e381e6684856bc818d1c (diff) | |
download | Viper-ee7753c1e03fe74bc133ecbf7eb149fb53841f58.tar.gz Viper-ee7753c1e03fe74bc133ecbf7eb149fb53841f58.zip |
added working cli arguments
I think?
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.js | 55 | ||||
-rw-r--r-- | src/index.js | 25 | ||||
-rw-r--r-- | src/utils.js | 34 |
3 files changed, 97 insertions, 17 deletions
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, } |