-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
47 lines (44 loc) · 1.03 KB
/
parser.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
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
var fs = Promise.promisifyAll(require("fs"));
function parseFile(options) {
return new Promise(function (resolve, reject) {
fs.readFileAsync(options.filePath, 'utf-8').then(function(content) {
resolve({
filePath: options.filePath,
body: content
});
}).catch(function (error) {
reject(error);
});
});
}
function parseUrl(options) {
if (!options.uri) {
return Promise.reject(new TypeError('uri is required'));
}
return new Promise(function (resolve, reject) {
request.getAsync(options.uri).get(1).then(function (body) {
if (options.saveToFile) {
fs.writeFileAsync(options.saveToFile, body).then(function () {
resolve({
filePath: options.saveToFile,
body: body
});
}).catch(function (error) {
reject(error);
});
} else {
resolve({
body: body
});
}
}).catch(function (error) {
reject(error);
});
});
}
module.exports = {
parseFile: parseFile,
parseUrl: parseUrl
};