-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
34 lines (31 loc) · 801 Bytes
/
Copy pathutils.js
File metadata and controls
34 lines (31 loc) · 801 Bytes
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
const fs = require('fs');
const zlib = require('zlib');
function tryParseJSON(data, file) {
try {
var parsedFile = JSON.parse(data);
if (parsedFile && typeof parsedFile === 'object') {
return parsedFile;
}
} catch (err) {
console.log('Not valid JSON', file, err);
return false;
}
return false;
}
async function readFile(filePath, file) {
return new Promise(function(resolve, reject) {
// unzip file and parse JSON
var content = '';
fs
.createReadStream(filePath)
.pipe(zlib.createGunzip())
.on('data', function(data) {
content += data.toString();
})
.on('end', function() {
var result = tryParseJSON(content, file);
resolve(result);
});
});
}
module.exports = { tryParseJSON, readFile };