-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
124 lines (119 loc) · 3.54 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
const fs = require('fs')
const timeFormat = (date, type = "yyyy-mm-dd hh:ii:ss") => {
var date = new Date(Number(date));
var o = {
"m+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"i+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
};
if (/(y+)/.test(type)) {
type = type.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(type)) {
type = type.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
};
}
return type;
}
class FileUtils {
constructor() {}
checkFile(path) {
return new Promise((resolve, reject) => {
try {
let result = fs.existsSync(path);
if (result) {
resolve(true)
} else {
resolve(false)
}
} catch (e) {
reject(e)
}
});
}
unlink(path) {
return new Promise((resolve, reject) => {
try {
let result = fs.unlink(path, err => {
if (err) resolve(false)
});
if (result) {
resolve(true)
} else {
resolve(false)
}
} catch (e) {
reject(e)
}
});
}
readFile(path, opts = 'utf8') {
return new Promise((resolve, reject) => {
try {
var read_file = fs.createReadStream(path, {
encoding: 'utf8',
autoClose: true,
});
var remaining = '';
read_file.on('data', function (data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
read_file.on('end', function () {
resolve(remaining)
});
} catch (e) {
console.error('Error writeObjectToJsonString:', e);
resolve(e)
}
})
}
writeFile(path, data, opts = 'utf8') {
return new Promise((resolve, reject) => {
var out = null;
out = fs.createWriteStream(path, {
encoding: opts
});
out.write(data, (error) => {
if (error) {
reject(error)
} else {
resolve()
}
});
out.end();
})
}
mkdirs(dirname) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(dirname)) {
fs.mkdir(dirname, {
recursive: true
}, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
});
} else {
resolve();
}
});
}
}
module.exports = {
FileUtils,
timeFormat
}