-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.js
69 lines (55 loc) · 1.6 KB
/
merge.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
const fs = require("fs");
const axios = require("axios");
async function read(file, id, ob, type = undefined) {
try {
const { _type, ...data } = JSON.parse(await fs.promises.readFile(file));
const $type = type || _type;
ob[$type] = ob[$type] || {};
ob[$type][id] = [data.text];
} catch (e) {
console.error(e);
}
}
module.exports = async function go(path = ".", org, repo) {
const files = await fs.promises.readdir(`${path}/nodes`);
let ob = {};
const submodules = new Set();
for (let file of files) {
const [id, extension] = file.split(".");
if (extension) {
if (extension === "json") await read(`${path}/nodes/${file}`, id, ob);
} else {
// submodule
submodules.add(id);
// await read(`${path}/nodes/${id}/nodes/${id}.json`, id, ob, 400);
}
}
const results = await Promise.all(
Array.from(submodules).map(
id =>
new Promise((res, rej) => {
axios
.get(
`http://localhost:3330/${org}/${id}/raw/branch/master/nodes/${id}.json`
)
.then(({ data }) => res({ id, data }))
.catch(rej);
})
)
);
results.forEach(({ id, data: { text } }) => {
const _type = "400";
ob[_type] = ob[_type] || {};
ob[_type][id] = [text];
console.log(`ob[${_type}][${id}] = [${text}];`);
});
// for (let id of submodules) {
// const { data } = await ;
// console.log(data);
// }
return ob;
};
// const filepath = `${org}__${repo}.json`;
// const stream = fs.createWriteStream(filepath);
// stream.write(JSON.stringify(ob));
// stream.end();