This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUtils.js
81 lines (75 loc) · 2.73 KB
/
FileUtils.js
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
class FileUtils {
static getThemeLocation(){
return FileUtils.getDataLocations().themes + "/" + SmartDashboard.options.theme + ".css";
}
static getDataLocations(){
var dataPath = require("nw.gui").App.dataPath;
return {
themes: dataPath + "/themes/",
plugins: dataPath + "/plugins/",
save: dataPath + "/save.json",
dsScript: dataPath + "\\ds.bat",
frcdata: "C:\\Users\\Public\\Documents\\FRC"
};
}
static forAllFilesInDirectory(dir, cb){
try {
var normalizedPath = dir;
fs.readdirSync(normalizedPath).forEach(function (file) {
cb(dir, file);
});
} catch (e) {
SmartDashboard.handleError(e);
}
}
static dashboardFileExists() {
try {
var frcLocation = FileUtils.getDataLocations().frcdata + "\\FRC DS Data Storage.ini";
return fs.existsSync(frcLocation);
} catch(e) {
SmartDashboard.handleError(e);
}
}
static isDefaultDashboard(){
try {
var frcLocation = FileUtils.getDataLocations().frcdata + "\\FRC DS Data Storage.ini";
if(!fs.existsSync(frcLocation)) {
return false;
}
var dsIni = fs.readFileSync(frcLocation).toString();
var line = dsIni.match(/DashboardCmdLine[^\r\n]*\r\n/);
return line != null && line[0].indexOf("ds.bat") > -1;
} catch (e) {
SmartDashboard.handleError(e);
}
}
static setDefaultDashboard(isSdJs){
try {
var frcLocation = FileUtils.getDataLocations().frcdata + "\\FRC DS Data Storage.ini";
if(!fs.existsSync(frcLocation)) {
return;
}
var run;
if(isSdJs){
var nwjs = process.execPath.replace("app\\nw.exe", "SmartDashboard.exe");
var scriptLocation = FileUtils.getDataLocations().dsScript;
fs.writeFileSync(scriptLocation, "\"" + nwjs + "\" --ds-mode %*");
run = scriptLocation;
run = "\"\"" + run.replace(/\\/g, "\\\\") + "\"\"";
} else {
run = "\"\"C:\\Program Files (x86)\\FRC Dashboard\\Dashboard.exe\"\"";
}
var dsIni = fs.readFileSync(frcLocation).toString();
var regex = /DashboardCmdLine[^\r\n]*\r\n/;
var hasSetting = dsIni.match(regex) != null;
if(!hasSetting) {
throw new Error("Unable to read DriverStation configuration file");
}
dsIni = dsIni.replace(regex, "DashboardCmdLine = " + run + "\r\n");
fs.writeFileSync(FileUtils.getDataLocations().frcdata + "\\FRC DS Data Storage.ini", dsIni);
} catch (e) {
SmartDashboard.handleError(e);
}
}
}
global.FileUtils = FileUtils;