This repository was archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-handler.js
More file actions
145 lines (139 loc) · 6.61 KB
/
Copy pathconfig-handler.js
File metadata and controls
145 lines (139 loc) · 6.61 KB
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
136
137
138
139
140
141
142
143
144
145
/*
Kiwi's Co-Op Mod for Half-Life: Alyx
Copyright (c) 2022 KiwifruitDev
All rights reserved.
This software is licensed under the MIT License.
-----------------------------------------------------------------------------
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-----------------------------------------------------------------------------
*/
// Modules
import * as fs from 'fs';
import * as readline from 'readline';
import chalk from 'chalk';
// Exports
export let config = {};
// Variables
const configloc = (process.argv[2] !== undefined) ? process.argv[2] : ((process.env.NODE_ENV === 'production') ? './kiwi_config.json' : './kiwi_config.dev.json');
const configtemplate = JSON.parse(fs.readFileSync('./kiwi_config.template.json', 'utf8'));
const nodepackage = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const validtypes = ["string", "number", "boolean", "note"];
let activeprompts = [];
/**
* Loads the config file.
* @returns {Promise} The config object.
*/
export function LoadConfig() {
if(fs.existsSync(configloc)) {
config = JSON.parse(fs.readFileSync(configloc, 'utf8'));
if(config.version !== nodepackage.version && config.disable_config_setup === 'true') {
console.log(chalk.red.bold(`\nThe config file is out of date. The config wizard will now begin, please open the previous config file for reference.\nAssuming a major update was made, please update to the newest addon release as well.`));
return new Promise((resolve, reject) => {
StartConfigSetup(nodepackage.version).then((config) => {
resolve(config);
});
});
}
}
if(!(config.disable_config_setup === 'true')) {
return new Promise(async (resolve, reject) => {
StartConfigSetup(nodepackage.version).then((config) => {
resolve(config);
});
});
}
return Promise.resolve(config);
}
/**
* This function starts a guided config wizard for the end user.
* @returns {boolean} Whether or not the config wizard was successful.
*/
function StartConfigSetup() {
return new Promise((resolve, reject) => {
console.log(chalk.red.bold(`\nThis version of KCOM is due to be replaced by a port to the .NET framework.\nPlease keep a look out for a future update.\nWhen this happens, updates will no longer be available for this version.\n`));
console.log(chalk.magenta.bold("\nWelcome to KCOM's config wizard! This will guide you through the process of setting up the config JSON file."));
console.log(chalk.magenta("Pay attention to the prompts and the types of values you can enter, invalid values will output their default value.\nPress enter to use the default value.\n"));
for(let key in configtemplate) {
if(configtemplate.hasOwnProperty(key)) {
let value = configtemplate[key];
activeprompts.push({key: key, value: value});
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let next = activeprompts.shift();
ConfigChoice(next.key, next.value, rl).then((config) => {
resolve(config);
});
});
}
/**
* Proceed with user prompt for a config option.
* @param {string} key The key of the config option.
* @param {string} value The value of the config option.
* @returns {boolean} Whether or not the config wizard was successful.
*/
function ConfigChoice(key, value, rl) {
return new Promise((resolve, reject) => {
if(value.name !== undefined)
console.log(chalk.yellow.bold(`${value.name || ""}`));
if(value.description !== undefined)
console.log(chalk.green(`${value.description || ""}`));
if(value.default !== undefined)
console.log(chalk.blueBright(`Default: ${value.default || ""}`));
if(value.type !== undefined && validtypes.includes(value.type)) {
// Readline is used to prompt the user for input.
// We'll pause the process until the user has inputted a value.
// FIXME: Placing the rest of the note's description here.
rl.question(chalk.blueBright((value.type != "note") && `[${value.type}] > ` || chalk.green("Otherwise, press Ctrl+C to exit without saving.")), (answer) => {
switch(value.type) {
case "string":
config[key] = (answer === "") ? value.default : answer;
break;
case "number":
config[key] = (isNaN(parseInt(answer))) ? parseInt(value.default) : parseInt(answer);
break;
case "boolean":
config[key] = (answer === "") ? value.default.toLowerCase() : answer.toLowerCase();
break;
}
console.log(chalk.blue.italic(`${key}: ${config[key]}`));
// Check whether to skip or continue.
if(activeprompts.length > 0) {
let next = activeprompts.shift();
if(value.skip && value.skip[config[key]]) {
// Skip to key in skip variable.
let i = activeprompts.length;
while(i--) {
if(next.key === value.skip[config[key].toString()]) {
break;
}
next = activeprompts.shift();
}
}
// Proceed with next prompt.
ConfigChoice(next.key, next.value, rl).then((config) => {
resolve(config);
});
}
});
} else {
// Add or update package version for config.
config.version = nodepackage.version;
// Write the config to file.
fs.writeFileSync(configloc, JSON.stringify(config, null, 2));
rl.close();
// Accept input from the command prompt normally.
process.stdin.resume();
resolve(config);
}
});
}