-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.js
37 lines (35 loc) · 925 Bytes
/
Reader.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
const fs = require("fs");
const JSZip = require("jszip");
exports.read = function(file) {
if (Array.isArray(file)) {
return Promise.all(file.map(each => read(each)));
}
return JSZip.loadAsync(fs.readFileSync(file))
.then(zip => {
return Promise.all([
zip.file("document.json").async("string"),
zip.file("meta.json").async("string"),
zip.file("user.json").async("string")
]).then(result => {
return {
repo: zip,
document: JSON.parse(result[0]),
meta: JSON.parse(result[1]),
user: JSON.parse(result[2])
};
});
})
.then(data => {
return Promise.all(
data.document.pages.map(page => {
return data.repo.file(`${page._ref}.json`).async("string");
})
).then(pages => {
data.pages = pages.map(page => JSON.parse(page));
return data;
});
})
.then(data => {
return [data.repo, data.document, data.meta, data.user, data.pages];
});
};