From 3a9d9828152ed6035cf8940bcb1e8db08ec24e96 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Tue, 20 Sep 2022 22:29:17 +0100 Subject: [PATCH] refactor: use `collections` as the base for dbseedsrepo also, change `dontDestroy` to the far more intuitive `shouldDestroy`. --- server/src/lib/database-seeds/repo.ts | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/server/src/lib/database-seeds/repo.ts b/server/src/lib/database-seeds/repo.ts index dbc7f22cb..8322ee368 100644 --- a/server/src/lib/database-seeds/repo.ts +++ b/server/src/lib/database-seeds/repo.ts @@ -22,16 +22,27 @@ export type SeedsCollections = export class DatabaseSeedsRepo { private readonly baseDir: string; private readonly logger; - private readonly dontDestroy?: "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD"; + private readonly shouldDestroy: "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD" | false; - constructor(baseDir: string, dontDestroy?: "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD") { + /** + * Create a database-seeds repository. + * + * @param baseDir - A path to the `collections` folder in database-seeds. + * @param shouldDestroy - Whether this repository should be destroyed when .Destroy() + * is called or not. This defaults to false, and will result in nothing happening + * on cleanup. This behaviour is useful for things like local database-seeds work. + */ + constructor( + baseDir: string, + shouldDestroy: "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD" | false = false + ) { this.baseDir = baseDir; this.logger = CreateLogCtx(`DatabaseSeeds:${baseDir}`); - this.dontDestroy = dontDestroy; + this.shouldDestroy = shouldDestroy; } private CollectionNameToPath(collectionName: SeedsCollections) { - return path.join(this.baseDir, "collections", `${collectionName}.json`); + return path.join(this.baseDir, `${collectionName}.json`); } /** @@ -70,7 +81,7 @@ export class DatabaseSeedsRepo { * As an example, database-seeds/collections/songs-iidx.json would be "songs-iidx". */ async ListCollections() { - const colls = await fs.readdir(path.join(this.baseDir, "collections")); + const colls = await fs.readdir(this.baseDir); return colls.map((e) => path.parse(e).name) as Array; } @@ -180,13 +191,12 @@ export class DatabaseSeedsRepo { } Destroy() { - if (this.dontDestroy !== "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD") { - this.logger.warn(`Refusing to delete seeds as they were instantiated locally.`); - return; + if (this.shouldDestroy === "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD") { + // scary + return fs.rm(this.baseDir, { recursive: true, force: true }); } - // scary - return fs.rm(this.baseDir, { recursive: true, force: true }); + this.logger.warn(`Refusing to delete seeds as they were instantiated locally.`); } } @@ -240,7 +250,7 @@ export async function PullDatabaseSeeds( } return new DatabaseSeedsRepo( - `${seedsDir}/database-seeds`, + `${seedsDir}/database-seeds/collections`, "YES_IM_SURE_PLEASE_LET_THIS_DIRECTORY_BE_RM_RFD" ); } catch ({ err, stderr }) {