-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
111 lines (94 loc) · 2.86 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const fs = require("node:fs");
const { exit } = require("node:process");
const {checkFileExists, waitLine} = require("./utils");
const axios = require('axios').default
const ConfPath = './config.json';
const outPath = "./out";
// 获取coll数量信息
async function loadInfo() {
const op = "info";
const data = (await axios.post(conf.url, {op})).data;
return data;
}
// 获取数据
async function loadBatchData(coll, skip, limit) {
const op = "data";
const data = (await axios.post(conf.url, {op, coll, skip, limit})).data;
return data;
}
// 获取一个coll的数据
async function loadColl(coll, count) {
let res = [];
console.log(coll, count);
while (res.length < count) {
const {err, data} = await loadBatchData(coll, res.length, 100);
if (err) {
console.error(err);
return null;
}
res = res.concat(data);
console.log(`[${res.length}/${count}] loading collection: ${coll}`)
}
return res;
}
var conf = null;
async function readConfig() {
var dirExists = await checkFileExists(ConfPath);
if (!dirExists) {
await inputConf();
}
conf = JSON.parse(fs.readFileSync(ConfPath, 'utf-8'));
return true;
}
async function inputConf() {
conf = {
"comment": [
"// 注释:",
"// Laf console上获取信息,注意信息安全。终端黑框中右键可以粘贴。",
"// 注意只修改双引号里的字符!源码地址:https://github.com/sysucats/export_laf_db"
],
};
console.log(conf.comment.slice(0, -1).join("\n"));
// 获取输入
conf.url = await waitLine("Laf addRecords 函数地址:");
await fs.promises.writeFile(ConfPath, JSON.stringify(conf, null, 4));
}
async function main() {
if (!fs.existsSync(outPath)) {
await fs.promises.mkdir(outPath)
}
const readSuccess = await readConfig();
if (!readSuccess) {
await waitLine(`\n======== [ERROR] read config error! Press "Enter" to exit. ========`);
exit();
}
const {err, data} = await loadInfo();
if (err) {
console.error(err);
await waitLine(`\n======== Press "Enter" to exit. ========`);
exit();
}
// 处理多个collection
for (var coll in data) {
let lines = [];
try {
const collData = await loadColl(coll, data[coll]);
lines = collData.join("\n")
} catch {
console.error(`"${coll}" 导出失败。`)
}
try {
await fs.promises.writeFile(`${outPath}/${coll}.json`, lines);
} catch {
console.error(`"${coll}" 写入失败。`)
}
}
await waitLine(`\n======== Press "Enter" to exit. ========`);
exit();
}
try {
main();
} catch (error) {
console.log(error);
waitLine(`\n======== Press "Enter" to exit. ========`);
}