OpenblockDesktopLink.js
2.62 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
import {app} from 'electron';
import path from 'path';
import os from 'os';
import {execFile, spawn} from 'child_process';
import fs from 'fs-extra';
import sudo from 'sudo-prompt';
import {productName} from '../../package.json';
import OpenBlockLink from 'openblock-link';
import OpenblockResourceServer from 'openblock-resource';
class OpenblockDesktopLink {
constructor () {
this._resourceServer = null;
this.appPath = app.getAppPath();
if (this.appPath.search(/app/g) !== -1) {
// Normal app
this.appPath = path.join(this.appPath, '../../');
} else if (this.appPath.search(/main/g) !== -1) { // eslint-disable-line no-negated-condition
// Start by start script in debug mode.
this.appPath = path.join(this.appPath, '../../');
} else {
// App in dir mode
this.appPath = path.join(this.appPath, '../');
}
const userDataPath = app.getPath(
'userData'
);
this.dataPath = path.join(userDataPath, 'Data');
this._link = new OpenBlockLink(this.dataPath, path.join(this.appPath, 'tools'));
this._resourceServer = new OpenblockResourceServer(this.dataPath,
path.join(this.appPath, 'external-resources'),
app.getLocaleCountryCode());
}
get resourceServer () {
return this._resourceServer;
}
installDriver (callback = null) {
const driverPath = path.join(this.appPath, 'drivers');
if ((os.platform() === 'win32') && (os.arch() === 'x64')) {
execFile('install_x64.bat', [], {cwd: driverPath});
} else if ((os.platform() === 'win32') && (os.arch() === 'ia32')) {
execFile('install_x86.bat', [], {cwd: driverPath});
} else if ((os.platform() === 'darwin')) {
spawn('sh', ['install.sh'], {shell: true, cwd: driverPath});
} else if ((os.platform() === 'linux')) {
sudo.exec(`sh ${path.join(driverPath, 'linux_setup.sh')} yang`, {name: productName},
error => {
if (error) throw error;
if (callback) {
callback();
}
}
);
}
}
clearCache (reboot = true) {
if (fs.existsSync(this.dataPath)) {
fs.rmSync(this.dataPath, {recursive: true, force: true});
}
if (reboot){
app.relaunch();
app.exit();
}
}
start () {
this._link.listen();
// start resource server
this._resourceServer.listen();
}
}
export default OpenblockDesktopLink;