-
Notifications
You must be signed in to change notification settings - Fork 14
/
common.js
77 lines (73 loc) · 1.63 KB
/
common.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
// 用于解码 peerid 名称
// 代码来自 https://github.com/mayswind/AriaNg/blob/a091ee850ff45a56ab033f821727c1ad24049a60/src/scripts/services/ariaNgCommonService.js#L91
function decodePercentEncodedString(s) {
if (!s) {
return 'Unknow'
}
var ret = ''
for (var i = 0; i < s.length; i++) {
var ch = s.charAt(i)
if (ch === '%' && i < s.length - 2) {
var code = s.substring(i + 1, i + 3)
ret += String.fromCharCode(parseInt(code, 16))
i += 2
} else {
ret += ch
}
}
return ret
}
// foreach + async
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
function dt() {
return new Date().toLocaleString('zh')
}
const honsole = {
dev: function (...args) {
if (process.env.DEV) {
console.log('[aria2b]', ...args)
}
},
log: function (...args) {
console.log('[aria2b]', ...args)
},
logt: function (...args) {
if (process.env.HIDE_TIME_PREFIX) {
return this.log(...args)
}
console.log('[aria2b]', dt(), ...args)
},
error: function (...args) {
console.error('[aria2b]', ...args)
},
warn: function (...args) {
console.warn('[aria2b]', ...args)
}
}
const exec = require('util').promisify((require('child_process')).exec)
// const exec = (cmd)=>{
// console.log(cmd)
// return {
// stdout: '1'
// }
// }
const execR = async (cmd) => {
try {
return await exec(cmd)
} catch (error) {
honsole.dev(error)
return 'error'
}
}
module.exports = {
decodePercentEncodedString,
asyncForEach,
dt,
honsole,
exec,
execR
}