forked from mrwill84/DOClever
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
executable file
·163 lines (163 loc) · 5.15 KB
/
api.js
File metadata and controls
executable file
·163 lines (163 loc) · 5.15 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const path=require("path");
var fs,request,decompress,getmac,rm;
if(location.href.startsWith("http"))
{
window.debug=true;
let curPath="/Users/sunxin/DOCleverClient";
fs=require(path.join(curPath,"./node_modules/fs-extra"));
request=require(path.join(curPath,"./node_modules/request"));
decompress =require(path.join(curPath,"./node_modules/decompress"));
getmac =require(path.join(curPath,"./node_modules/getmac"));
rm =require(path.join(curPath,"./node_modules/rimraf"));
}
else
{
fs=require("fs-extra");
request=require("request");
decompress =require("decompress");
getmac=require("getmac")
rm=require("rimraf")
}
console.log(__dirname);
const remote=require('electron').remote;
global.remote=remote;
const remoteApi=remote.getGlobal("api");
const ipc=require('electron').ipcRenderer;
const app=remote.app;
const pathEnv=path.join(app.getPath("home"),"DOClever-Desktop")
global.api={
update:{
checkIfNeededUpdate:async function (id,remoteVersion) {
let p=path.join(pathEnv,id);
let pVer=path.join(p,"ver");
if(!await fs.exists(pVer))
{
return true;
}
let verContent=(await fs.readFile(pVer)).toString();
if(verContent!=remoteVersion)
{
return true;
}
return false;
},
updateEnv:async function(id,url)
{
let p=path.join(pathEnv,id);
if(!await fs.exists(p))
{
await fs.mkdir(p);
}
ipc.removeAllListeners("updateEnvProcess");
ipc.on("updateEnvProcess",this.getProcess);
global.rootVue.$emit("startUpdate")
let zip=await remoteApi.update.updateEnv(id,url);
global.rootVue.$emit("updateUnzip")
await fs.remove(path.join(zip,"..","content"));
await decompress(zip,path.join(zip,".."));
await fs.unlink(zip);
global.rootVue.$emit("finishUpdate")
return path.join(zip,"..");
},
getProcess:async function (sender,val) {
global.rootVue.$emit("updateProcess",val)
},
handlePluginInfo:async function(envId,userId)
{
let p=path.join(pathEnv,envId,"plugin");
if(!(await fs.exists(p)))
{
await fs.mkdir(p);
}
p=path.join(p,userId);
let objPlugin={};
if(!(await fs.exists(p)))
{
await fs.mkdir(p);
}
else
{
let dirs=await fs.readdir(p);
for(let o of dirs)
{
let p1=path.join(p,o);
let stat=await fs.stat(p1);
if(stat.isDirectory())
{
let p2=path.join(p1,"ver");
if(await fs.exists(p2))
{
let verContent=(await fs.readFile(p2)).toString();
objPlugin[o]=verContent;
}
}
}
}
return objPlugin;
}
},
path:{
getEnvPath:function (id) {
return path.join(pathEnv,id);
},
delEnv:function (id) {
let p=path.join(pathEnv,id);
return new Promise(function (resolve) {
rm(p,function () {
resolve();
})
})
},
delPlugin:async function (member) {
let p=pathEnv;
let arr=await fs.readdir(p);
let ret=[];
for(let o of arr)
{
let p1=path.join(p,o);
let stat=await fs.stat(p1);
if(stat.isDirectory())
{
let p=path.join(p1,"plugin",member);
if(await fs.exists(p))
{
ret.push(new Promise(function (resolve) {
rm(p,function () {
resolve();
})
}))
}
}
}
return Promise.all(ret);
}
},
computeName:function () {
return require("os").hostname();
},
macAddress:function () {
return new Promise(function (resolve,reject) {
getmac.getMac(function (err,mac) {
if(err)
{
reject(err);
}
else
{
resolve(mac);
}
})
})
},
cookie:{
get:function (key) {
return remoteApi.cookie.get(key);
},
set:function (key,value,remember) {
return remoteApi.cookie.set(key,value,remember);
},
remove:function (key) {
return remoteApi.cookie.remove(key);
}
}
};