aboutsummaryrefslogtreecommitdiff
path: root/src/cli.js
blob: c3d957e8c70f4134a8e4b1402a0b69a220b8a0bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const fs = require("fs");
const { app, ipcMain } = require("electron");

const Emitter = require("events");
const events = new Emitter();

const cli = app.commandLine;
const lang = require("./lang");
const json = require("./modules/json");
console = require("./modules/console");

function hasArgs() {
	// Makes sure the GUI isn't launched.
	// TODO: Perhaps we should get a better way of doing this, at the
	// very least we should use a switch case here.
	if (cli.hasSwitch("cli") ||
		cli.hasSwitch("help") ||
		cli.hasSwitch("mods") ||
		cli.hasSwitch("update") ||
		cli.hasSwitch("launch") ||
		cli.hasSwitch("setpath") ||
		cli.hasSwitch("version") ||
		cli.hasSwitch("gamepath") ||
		cli.hasSwitch("togglemod") ||
		cli.hasSwitch("removemod") ||
		cli.hasSwitch("installmod") ||
		cli.hasSwitch("update-viper")) {
		return true;
	} else {return false}
}

// Exits the CLI, when run without CLI being on it'll do nothing, this
// is needed as without even if no code is executed it'll continue to
// run as Electron is still technically running.
function exit(code) {
	if (hasArgs()) {process.exit(code)}
}

// Ensures the gamepath exists, it's called by options that require the
// gamepath to be able to work.
function gamepathExists() {
	if (fs.existsSync("viper.json")) {
		gamepath = json("viper.json").gamepath;

		if (! fs.existsSync(gamepath)) {
			console.error(lang("cli.gamepath.lost"));
			exit(1);
		} else {
			return true;
		}
	}
}

// General CLI initialization
//
// A lot of the CLI is handled through events sent back to the main process for
// it to handle, this is because we re-use these events for the renderer as.
async function init() {
	// --help menu/argument
	if (cli.hasSwitch("help")) {
	console.log(`options:
  --help          ${lang("cli.help.help")}
  --version       ${lang("cli.help.version")}
  --devtools      ${lang("cli.help.devtools")}

  --cli           ${lang("cli.help.cli")}
  --update        ${lang("cli.help.update")}
  --update-viper  ${lang("cli.help.update_vp")}
  --setpath       ${lang("cli.help.setpath")}
  --no-vp-updates ${lang("cli.help.no_vp_updates")}

  --installmod    ${lang("cli.help.install_mod")}
  --removemod     ${lang("cli.help.remove_mod")}
  --togglemod     ${lang("cli.help.toggle_mod")}`)
		// In the future --setpath should be able to understand
		// relative paths, instead of just absolute ones.
		exit();
	}

	// --update
	if (cli.hasSwitch("update") && gamepathExists()) {ipcMain.emit("update")}
	// --version
	if (cli.hasSwitch("version") && gamepathExists()) {
		let version = require("./modules/version");

		console.log("Viper: v" + require("../package.json").version);
		console.log("Titanfall 2: " + version.titanfall());
		console.log("Northstar: " + version.northstar());
		console.log("Node: " + process.version);
		console.log("Electron: v" + process.versions.electron);
		exit();
	}

	// --setpath
	if (cli.hasSwitch("setpath")) {
		// Checks to verify the path is legitimate
		if (cli.getSwitchValue("setpath") != "") {
			ipcMain.emit("setpathcli", cli.getSwitchValue("setpath"));
		} else {
			console.error(lang("cli.setpath.no_arg"));
			exit(1);
		}
	}

	// --launch
	if (gamepathExists() && cli.hasSwitch("launch")) {
		switch(cli.getSwitchValue("launch")) {
			case "vanilla":
				ipcMain.emit("launchVanilla");
				break;
			default:
				ipcMain.emit("launch");
				break;
		}
	}

	// Mod related args, --installmod, --removemod, --togglemod
	if (cli.hasSwitch("installmod") && gamepathExists()) {ipcMain.emit("installmod")}
	if (cli.hasSwitch("removemod") && gamepathExists()) {ipcMain.emit("removemod", "", cli.getSwitchValue("removemod"))}
	if (cli.hasSwitch("togglemod") && gamepathExists()) {ipcMain.emit("togglemod", "", cli.getSwitchValue("togglemod"))}

	// Prints out the list of mods
	if (cli.hasSwitch("mods") && gamepathExists()) {ipcMain.emit("getmods")}
}

module.exports = {
	hasArgs,
	init, exit, 
	hasParam: (arg) => {
		return cli.hasSwitch(arg);
	},
	param: (arg) => {
		return cli.getSwitchValue(arg);
	}
}