mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-09 15:19:50 +03:00
Improve export scripts
This commit is contained in:
@@ -4,11 +4,15 @@ const { StaticConfig } = require("tachi-common");
|
||||
const logger = require("./logger");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const DeterministicCollectionSort = require("./deterministic-collection-sort");
|
||||
const { RemoveUnderscoreID } = require("./remove-_id");
|
||||
|
||||
const program = new Command();
|
||||
program
|
||||
.option("-c, --connection <127.0.0.1:27017/my_database>")
|
||||
.option("-t, --useTabs");
|
||||
.option("-t, --useTabs")
|
||||
.option("-a, --all")
|
||||
.argument("[collection]");
|
||||
|
||||
program.parse(process.argv);
|
||||
const options = program.opts();
|
||||
@@ -23,15 +27,19 @@ const db = monk(options.connection);
|
||||
|
||||
const collectionsDir = path.join(__dirname, "../collections");
|
||||
|
||||
const collections = [
|
||||
"folders",
|
||||
"tables",
|
||||
];
|
||||
const collections = [];
|
||||
|
||||
if (options.all) {
|
||||
collections.push("folders", "tables");
|
||||
collections.push(...StaticConfig.allSupportedGames.map(e => `songs-${e}`));
|
||||
collections.push(...StaticConfig.allSupportedGames.map(e => `charts-${e}`));
|
||||
} else if (program.args[0]) {
|
||||
collections.push(program.args[0]);
|
||||
}
|
||||
|
||||
// Add the songs-{game} and charts-{game} collections.
|
||||
|
||||
collections.push(...StaticConfig.allSupportedGames.map(e => `songs-${e}`));
|
||||
collections.push(...StaticConfig.allSupportedGames.map(e => `charts-${e}`));
|
||||
|
||||
|
||||
(async () => {
|
||||
logger.info(`Exporting ${collections.length} collections.`);
|
||||
@@ -39,16 +47,23 @@ collections.push(...StaticConfig.allSupportedGames.map(e => `charts-${e}`));
|
||||
for (const collection of collections) {
|
||||
logger.info(`Exporting ${collection}.`);
|
||||
|
||||
const data = await db.get(collection).find({});
|
||||
logger.info(`Got ${data.length} documents.`);
|
||||
|
||||
const pt = path.join(collectionsDir, `${collection}.json`);
|
||||
fs.writeFileSync(pt, JSON.stringify(data, null, options.useTabs ? "\t" : undefined));
|
||||
|
||||
logger.info(`Wrote to ${pt}.`);
|
||||
await ExportCollection(collection);
|
||||
}
|
||||
|
||||
DeterministicCollectionSort();
|
||||
RemoveUnderscoreID();
|
||||
|
||||
logger.info(`Done!`);
|
||||
|
||||
process.exit(0);
|
||||
})();
|
||||
|
||||
async function ExportCollection(collection) {
|
||||
const data = await db.get(collection).find({});
|
||||
logger.info(`Got ${data.length} documents.`);
|
||||
|
||||
const pt = path.join(collectionsDir, `${collection}.json`);
|
||||
fs.writeFileSync(pt, JSON.stringify(data, null, options.useTabs ? "\t" : undefined));
|
||||
|
||||
logger.info(`Wrote to ${pt}.`);
|
||||
}
|
||||
@@ -1,9 +1,19 @@
|
||||
const { IterateCollections } = require("./util");
|
||||
|
||||
IterateCollections((data) => {
|
||||
for (const d of data) {
|
||||
delete d._id;
|
||||
}
|
||||
function RemoveUnderscoreID() {
|
||||
IterateCollections((data) => {
|
||||
for (const d of data) {
|
||||
delete d._id;
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
RemoveUnderscoreID();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
RemoveUnderscoreID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user