-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
220 lines (201 loc) · 5.28 KB
/
utils.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const cts = require('./constant');
const path = require('path');
const fs = require('fs');
let copySync = function (src, dst) {
mkdirSync(path.dirname(dst))
fs.writeFileSync(dst, fs.readFileSync(src));
return true;
}
let mkdirSync = function (dirpath, mode) {
if (!fs.existsSync(dirpath)) {
let pathtmp;
let arr = dirpath.split(/[/\\]/);
arr.forEach(function (dirname) {
if (dirname === '') {
if (!pathtmp) {
pathtmp = '/';
}
return false;
}
if (pathtmp) {
pathtmp = path.join(pathtmp, dirname);
} else {
pathtmp = dirname;
}
if (!fs.existsSync(pathtmp)) {
if (!fs.mkdirSync(pathtmp, mode)) {
return false;
}
}
});
}
return true;
}
let rmdirSync = function (dirpath) {
var files = [];
if (fs.existsSync(dirpath)) {
files = fs.readdirSync(dirpath);
files.forEach(function (file, index) {
var curPath = path.join(dirpath, file);
let stat = fs.statSync(curPath)
if (stat && stat.isDirectory()) { // recurse
rmdirSync(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dirpath);
}
return true;
};
let loadSSHKeys = function (dirpath) {
let keys = [];
let files = fs.readdirSync(dirpath);
files.forEach(function (file, index) {
var curPath = path.join(dirpath, file);
if (fs.statSync(curPath).isDirectory()) {
let key = loadSingleKey(curPath)
if (key) {
keys[file] = key;
}
}
});
return keys;
}
let loadSingleKey = function (dirpath) {
let key = {};
let files = fs.readdirSync(dirpath);
files.forEach(function (file, index) {
var curPath = path.join(dirpath, file);
if (!fs.statSync(curPath).isDirectory()) {
if (file.indexOf('.pub') >= 0) {
key.PublicKey = curPath;
} else {
key.PrivateKey = curPath;
}
}
});
return key;
}
let loadConf = function (filepath) {
if (!existsFile(filepath)) {
return false;
}
let content = fs.readFileSync(filepath, 'utf-8');
// 去除tab符 根据回车分割
let contentArr = content.replace(/\t+/g, '').split('\r\n');
let configs = [];
let tmp = {};
contentArr.forEach((item) => {
if (item.trim().substr(0, 1) !== '#') {
let prop = item.replace(/\s+/g, ' ').split(' ');
if (prop && prop.length === 2) {
if (prop[0] === 'Host' && tmp.Host) {
configs[tmp.Host] = Object.assign({}, tmp);
tmp = {};
}
tmp[prop[0]] = prop[1];
}
}
});
// 最后需要再添加一次
if (tmp.Host) {
configs[tmp.Host] = Object.assign({}, tmp);
}
return configs;
};
let confToStr = function (conf) {
let fileContent = '\r\n\r\n';
for (let key in conf) {
fileContent += key + ' ' + conf[key] + '\r\n';
}
return fileContent;
}
let saveConf = function (confs, savePath) {
if (!confs) {
return false;
}
let fileContent = '';
for (let key in confs) {
fileContent += confToStr(confs[key]);
}
if (fileContent === '') {
return false;
}
fs.writeFileSync(savePath, fileContent);
return true;
}
// check file is exists, return stat
let existsFile = function (filePath) {
try {
let stat = fs.lstatSync(filePath);
if (stat && (stat.isSymbolicLink() || stat.isFile())) {
return stat
}
} catch (err) {
return false
}
return false
}
// create symlink
let createLink = function (linkPath, filePath) {
stat = existsFile(linkPath);
if (stat) {
// remove symlink
fs.unlinkSync(linkPath);
}
// create symlink in ssh floder
fs.symlinkSync(filePath, linkPath)
return true;
}
let getLinkPath = function (linkPath) {
let stat = existsFile(linkPath);
if (stat && stat.isSymbolicLink()) {
return fs.readlinkSync(linkPath);
}
return false;
}
let getHost = function (str, pg) {
if (!str || str === '') {
return false;
}
let host = {
address: str,
user: 'root',
port: 22,
}
if (pg && pg.user) {
host.user = pg.user;
} else if (str.indexOf('@') > 0) {
host.user = str.split('@')[0];
}
if (pg && pg.port) {
host.port = pg.port;
} else if (str.indexOf(':') > 0) {
host.port = str.split(':')[1];
}
if (str.indexOf('@') > 0) {
host.address.substr(str.indexOf('@'))
}
if (str.indexOf('@') > 0) {
host.address = host.address.substr(host.address.indexOf('@') + 1)
}
if (str.indexOf(':') > 0) {
host.address = host.address.substr(0, host.address.indexOf(':'))
}
return host;
}
module.exports = {
getHost,
getLinkPath,
existsFile,
createLink,
copySync,
mkdirSync,
rmdirSync,
loadSSHKeys,
loadSingleKey,
loadConf,
confToStr,
saveConf
};