-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
150 lines (128 loc) · 3.58 KB
/
util.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
/**
*
* some common util method
*
* Created by kimown on 16-9-14.
*/
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
let request = require('request');
let logger = require('./logger');
let loggerfile=require('./loggerfile')();
/**
* promise fs read event
* https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_readfile_file_options_callback
* @param path
* @returns {Promise}
*/
exports.readFile = function (path) {
checkIsAbsoultePath(path);
return new Promise((resolve, reject)=> {
fs.readFile(path, (err, data) => {
if (err) {
reject(err);
}else{
resolve(data.toString());
}
});
})
};
/**
* promise fs write event
* https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback
* @param path
* @returns {Promise}
*/
exports.writeFile = function (path, data) {
checkIsAbsoultePath(path);
return new Promise((resolve, reject)=> {
fs.writeFile(path, data, (err) => {
if (err) {
logger.error(`保存文件 ${path} 失败,err: ${err}`);
reject(err);
}
});
})
};
/**
* http://stackoverflow.com/questions/30780216/writing-to-a-txt-file-before-node-js-exits
* https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback
* @param path
* @param data
* @returns {Promise}
*/
exports.writeFileSync = function (path, data) {
checkIsAbsoultePath(path);
fs.writeFileSync(path, data,'utf8', (err) => {
if (err) {
logger.error(`保存文件 ${path} 失败,err: ${err}`);
}else{
logger.info(`保存文件 ${path} 成功`);
}
});
};
/**
*
* reference:
* http://stackoverflow.com/questions/16188137/how-should-i-pass-json-data-in-the-request-payload-of-http-post-request
* https://github.com/request/request#streaming
*
* @param options
* @returns {Promise}
*/
exports.request = function (options) {
// logger.data('request:', options.body);
return new Promise((resolve, reject)=> {
request(options, function (err, res) {
if (err) {
reject(`请求发送失败,err:${err}`);
} else {
let {statusCode} = res;
let {body}=res;
logger.data('response:', body);
resolve({
ok: statusCode == 200 ? true : false,
body
});
}
})
})
}
/**
* https://nodejs.org/dist/latest-v4.x/docs/api/os.html#os_os_eol
*/
exports.os = os;
exports.fs=fs;
exports.path=path;
exports.rmFileSync = function (absolutePath) {
checkIsAbsoultePath(absolutePath);
try {
fs.unlinkSync(absolutePath);
} catch (err) {
logger.info(`删除文件 ${absolutePath} 失败,err ${err}`);
return;
}
logger.info(`删除文件 ${absolutePath} 成功`);
};
// http://unix.stackexchange.com/questions/554/how-to-monitor-cpu-memory-usage-of-a-single-process
/**
* https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_access_path_mode_callback
* @param absoultePath
*/
exports.existFileSync=function (absoultePath) {
checkIsAbsoultePath(absoultePath);
let flag=true;
try{
fs.accessSync(absoultePath);
}catch (err){
flag=false;
}
return flag;
};
let checkIsAbsoultePath=exports.checkIsAbsoultePath=function(path) {
if(!path.startsWith('/')){
logger.info(`传入的路径 ${path} 不是绝对路径`);
}
};