refactor: use a util for script exec

This commit is contained in:
zkldi
2022-07-17 16:17:51 +01:00
parent c25ad6345a
commit b6003f7e87
13 changed files with 71 additions and 113 deletions
+27 -13
View File
@@ -102,20 +102,34 @@ function CloneDB(nsFrom: string, nsTo: string) {
}
if (require.main === module) {
CloneDB(options.nsFrom, options.nsTo);
// Don't run any risks -- there's no way we're ever accidentally anonymising the production database.
// any nsTo argument MUST start with anon-
if (!options.nsTo.startsWith("anon-")) {
AnonymiseDB(options.nsTo)
.then(() => {
logger.info(`Anonymised database successfully. Saved to ${options.nsTo}.`);
process.exit(0);
})
.catch((err: unknown) => {
logger.error(`Failed to anonymise database.`, { err }, () => {
process.exit(1);
logger.error(
`Tried to clone to and anonymise ${options.nsTo}, which is illegal. Anonymised DBs must start with anon-.`,
() => {
process.exit(1);
}
);
} else {
// else guard here is important, as the above statement doesn't immediately halt
// execution
// -- return isn't valid at top level, and i can't be bothered wrapping this in
// a useless fn.
CloneDB(options.nsFrom, options.nsTo);
// Don't run any risks -- there's no way we're ever accidentally anonymising the production database.
// any nsTo argument MUST start with anon-
if (options.nsTo.startsWith("anon-")) {
AnonymiseDB(options.nsTo)
.then(() => {
logger.info(`Anonymised database successfully. Saved to ${options.nsTo}.`);
process.exit(0);
})
.catch((err: unknown) => {
logger.error(`Failed to anonymise database.`, { err }, () => {
process.exit(1);
});
});
});
}
}
}
+4 -7
View File
@@ -1,12 +1,9 @@
import { InitaliseFolderChartLookup } from "../../utils/folder";
import CreateLogCtx from "lib/logger/logger";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__filename);
InitaliseFolderChartLookup()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to initialise folder chart lookup.`, { err }, () => {
process.exit(1);
});
});
if (require.main === module) {
WrapScriptPromise(InitaliseFolderChartLookup(), logger);
}
@@ -5,6 +5,7 @@ import db from "external/mongo/db";
import CreateLogCtx from "lib/logger/logger";
import fetch from "node-fetch";
import p from "prudence";
import { WrapScriptPromise } from "utils/misc";
import { FindChartWithPTDF } from "utils/queries/charts";
import { FindSongOnTitle } from "utils/queries/songs";
import type { ChartDocument, Difficulties, integer } from "tachi-common";
@@ -221,11 +222,5 @@ async function HumanisedTitleLookup(originalTitle: string) {
}
if (require.main === module) {
FetchSP12Data()
.then(process.exit(0))
.catch((err) => {
logger.error(`Failed to fetch sp12 data. ${err}`, err, () => {
process.exit(1);
});
});
WrapScriptPromise(FetchSP12Data(), logger);
}
+2 -7
View File
@@ -1,14 +1,9 @@
import { InitSequenceDocs } from "external/mongo/sequence-docs";
import CreateLogCtx from "lib/logger/logger";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__filename);
if (require.main === module) {
InitSequenceDocs()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to initialise sequence documents.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(InitSequenceDocs(), logger);
}
+4 -7
View File
@@ -2,6 +2,7 @@
import { SetIndexes } from "../external/mongo/indexes";
import { Command } from "commander";
import CreateLogCtx from "lib/logger/logger";
import { WrapScriptPromise } from "utils/misc";
const program = new Command();
@@ -16,10 +17,6 @@ program.option(
program.parse(process.argv);
const options: { db: string; reset?: boolean } = program.opts();
SetIndexes(options.db, options.reset === true)
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to set indexes.`, { err }, () => {
process.exit(1);
});
});
if (require.main === module) {
WrapScriptPromise(SetIndexes(options.db, options.reset === true), logger);
}
@@ -1,5 +1,6 @@
import db from "external/mongo/db";
import CreateLogCtx from "lib/logger/logger";
import { WrapScriptPromise } from "utils/misc";
import type { IObjectID } from "monk";
import type { integer } from "tachi-common";
@@ -34,14 +35,6 @@ async function DedupeScoreIDs() {
}
}
DedupeScoreIDs()
.then(() => {
logger.info(`Finished deduping score IDs.`, () => {
process.exit(0);
});
})
.catch((err: unknown) => {
logger.error(`Failed to dedupe score IDs.`, { err }, () => {
process.exit(1);
});
});
if (require.main === module) {
WrapScriptPromise(DedupeScoreIDs(), logger);
}
@@ -1,18 +1,9 @@
import CreateLogCtx from "lib/logger/logger";
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__filename);
if (require.main === module) {
RecalcAllScores()
.then(() => {
logger.info(`Successfully recalced all scores.`, () => {
process.exit(0);
});
})
.catch((err: unknown) => {
logger.error(`Failed to recalc all scores.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(RecalcAllScores(), logger);
}
@@ -2,6 +2,7 @@
import db from "external/mongo/db";
import CreateLogCtx from "lib/logger/logger";
import { UpdateUsersGamePlaytypeStats } from "lib/score-import/framework/user-game-stats/update-ugs";
import { WrapScriptPromise } from "utils/misc";
import { FormatUserDoc } from "utils/user";
import type { Game, Playtype, ScoreDocument } from "tachi-common";
@@ -42,11 +43,5 @@ export async function RecalcGameProfiles() {
}
if (require.main === module) {
RecalcGameProfiles()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to recalc game profiles.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(RecalcGameProfiles(), logger);
}
@@ -1,14 +1,9 @@
import CreateLogCtx from "lib/logger/logger";
import { RecalcSessions } from "utils/calculations/recalc-sessions";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__filename);
if (require.main === module) {
RecalcSessions()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to recalc sessions.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(RecalcSessions(), logger);
}
@@ -1,18 +1,10 @@
/* eslint-disable no-await-in-loop */
import CreateLogCtx from "lib/logger/logger";
import { UpdateAllPBs } from "utils/calculations/recalc-scores";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__filename);
if (require.main === module) {
UpdateAllPBs()
.then(() => {
logger.info(`Successfully updated all PBs.`);
process.exit(0);
})
.catch((err: unknown) => {
logger.error(`Failed to update all PBs.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(UpdateAllPBs(), logger);
}
+2 -8
View File
@@ -12,7 +12,7 @@ import { UpdateMilestoneSubscriptions } from "lib/targets/milestones";
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import { UpdateGameSongIDCounter } from "utils/db";
import { InitaliseFolderChartLookup } from "utils/folder";
import { ArrayDiff, IsSupported } from "utils/misc";
import { ArrayDiff, IsSupported, WrapScriptPromise } from "utils/misc";
import type { KtLogger } from "lib/logger/logger";
import type { BulkWriteOperation, DeleteWriteOpResultObject } from "mongodb";
import type { ICollection } from "monk";
@@ -385,11 +385,5 @@ async function SynchroniseDBWithSeeds() {
}
if (require.main === module) {
SynchroniseDBWithSeeds()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(String(err), () => {
process.exit(1);
});
});
WrapScriptPromise(SynchroniseDBWithSeeds(), logger);
}
+3 -18
View File
@@ -4,6 +4,7 @@ import { Command } from "commander";
import { monkDB } from "external/mongo/db";
import { DatabaseSchemas } from "external/mongo/schemas";
import CreateLogCtx from "lib/logger/logger";
import { WrapScriptPromise } from "utils/misc";
import { FormatPrError } from "utils/prudence";
import type { Databases } from "external/mongo/db";
import type { FindResult } from "monk";
@@ -64,24 +65,8 @@ export async function ValidateAllCollections() {
if (require.main === module) {
if (typeof options.collection === "string") {
// @hack This should be typechecked and warned about outside of ValidateCollection.
ValidateCollection(options.collection as Databases)
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(
`Failed to validate collection ${options.collection}?`,
{ err },
() => {
process.exit(1);
}
);
});
WrapScriptPromise(ValidateCollection(options.collection as Databases), logger);
} else {
ValidateAllCollections()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to validate all collections?`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(ValidateAllCollections(), logger);
}
}