-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackupDesignDocs.js
68 lines (61 loc) · 2.15 KB
/
backupDesignDocs.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
const { wrappedRun } = require("./entryPoint");
const fs = require("fs");
const path = require("path");
const { CouchStorage, MODE_GAME } = require("./couchStorage");
function fromEntries (iterable) {
return [...iterable].reduce((obj, [key, val]) => {
obj[key] = val;
return obj;
}, {});
}
function processDesignObject (obj, dir = process.env.OUTPUT_DIR) {
const result = Object.entries(obj).map(([key, value]) => {
if (key[0] === "_") {
return undefined;
}
if (typeof value === "object" && value.length === undefined) {
return [key, processDesignObject(value, path.join(dir, key))];
} else if (typeof value === "string") {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const extension = value.indexOf("\n") > -1 ? ".js" : "";
fs.writeFileSync(path.join(dir, key + extension), value);
return undefined;
}
return [key, value];
}).filter(x => x && x[1] !== undefined);
if (!result.length) {
return undefined;
}
return fromEntries(result);
}
async function main () {
fs.mkdirSync(path.join(process.env.OUTPUT_DIR, "_raw/basic/_design"), { recursive: true });
fs.mkdirSync(path.join(process.env.OUTPUT_DIR, "_raw/extended/_design"), { recursive: true });
const storage = new CouchStorage({mode: MODE_GAME, suffix: "_meta"});
for (const { doc } of (await storage.db.allDocs({
include_docs: true,
startkey: "_design/",
endkey: "_design/\uffff",
})).rows) {
fs.writeFileSync(path.join(process.env.OUTPUT_DIR, "_raw/basic", doc._id), JSON.stringify(doc, undefined, 2));
processDesignObject(doc, path.join(process.env.OUTPUT_DIR, doc._id));
}
for (const { doc } of (await storage._dbExtended.allDocs({
include_docs: true,
startkey: "_design/",
endkey: "_design/\uffff",
})).rows) {
fs.writeFileSync(path.join(process.env.OUTPUT_DIR, "_raw/extended", doc._id), JSON.stringify(doc, undefined, 2));
processDesignObject(doc, path.join(process.env.OUTPUT_DIR, doc._id));
}
}
if (require.main === module) {
wrappedRun(main);
} else {
module.exports = {
generateRateRanking,
};
}
// vim: sw=2:ts=2:expandtab:fdm=syntax