-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
42 lines (32 loc) · 993 Bytes
/
index.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
const s3ls = require("s3-ls");
const trimEnd = (s, ch) => (s[s.length - 1] === ch ? trimEnd(s.substr(0, s.length - 1), ch) : s);
const toSafeDepth = n => {
n = Number(n);
n = Number.isNaN(n) ? Number.MAX_SAFE_INTEGER : n;
return n < 0 ? Number.MAX_SAFE_INTEGER : n;
};
function getLastPathPart(path) {
path = trimEnd(path, "/");
const lastIndex = path.lastIndexOf("/");
return path.substr(lastIndex + 1);
}
module.exports = function(options) {
const lister = s3ls(options);
async function generate(folder, depth) {
depth = toSafeDepth(depth);
let data = await lister.ls(folder);
const tree = {};
data.files.forEach(file => {
tree[getLastPathPart(file)] = file;
});
if (data.folders && data.folders.length) {
await Promise.all(
data.folders.map(async path => {
tree[getLastPathPart(path)] = depth > 0 ? await generate(path, depth - 1) : {};
})
);
}
return tree;
}
return { generate };
};