/
Config.ts
109 lines (95 loc) · 3.47 KB
/
Config.ts
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
/*
cpbooster "Competitive Programming Booster"
Copyright (C) 2020 Sergio G. Sanchez V.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as fs from "fs";
import * as Path from "path";
import * as os from "os";
import { exit } from "process";
import { Languages } from "./Types/Languages";
export default class Config {
static readonly defaultConfigFilePaths = [
Path.join(os.homedir(), "cpbooster-config.json"),
Path.join(os.homedir(), ".cpbooster", "cpbooster-config.json"),
Path.join(os.homedir(), ".config", "cpbooster", "cpbooster-config.json")
];
contestsDirectory: string;
port: number;
terminal: string;
closeAfterClone: boolean;
showStatusPageOnSubmit: boolean;
useUserDefaultBrowser: boolean;
preferredLang: string;
languages: Languages;
constructor() {
this.contestsDirectory = Path.join(os.homedir(), "Contests");
this.port = 1327;
this.terminal = "konsole";
this.closeAfterClone = false;
this.showStatusPageOnSubmit = true;
this.useUserDefaultBrowser = true;
this.preferredLang = "cpp";
this.languages = {
cpp: {
template: "",
command: "g++ -std=gnu++17 -O2",
debugCommand: "g++ -std=gnu++17 -DDEBUG -Wshadow -Wall",
aliases: {
codeforces: "54",
atcoder: "4003",
omegaup: "cpp17-gcc"
}
},
py: {
template: "",
command: "python3",
debugCommand: "python3 -O",
aliases: {
codeforces: "31",
atcoder: "4006",
omegaup: "py3"
}
}
};
}
private static printAlreadyExistsMsg(configFilePath: string) {
console.log(`"${configFilePath}" already exists`);
}
private static printConfigWrittenMsg(configFilePath: string) {
console.log(`Your configuration file has been written in: "${configFilePath}"`);
}
static write(configFilePath: string = this.defaultConfigFilePaths[0]): void {
if (fs.existsSync(configFilePath)) {
this.printAlreadyExistsMsg(configFilePath);
} else {
fs.writeFileSync(configFilePath, JSON.stringify(new Config(), null, 2));
this.printConfigWrittenMsg(configFilePath);
}
}
static read(configFilePath: string | undefined | null): Config {
const configFilePaths = configFilePath ? [configFilePath] : [];
configFilePaths.push(...this.defaultConfigFilePaths);
for (const configPath of configFilePaths) {
if (fs.existsSync(configPath)) {
// for now we are assuming that all the properties are defined in the config file
return JSON.parse(fs.readFileSync(configPath, "utf8"));
}
}
console.log("\nconfiguration file not found in any of the following locations:\n");
for (const configPath of configFilePaths) {
console.log("->", configPath);
}
console.log("\nYou can create one in your $HOME directory by running 'cpbooster init'\n");
exit(0);
}
}