From 55cfd2a70d119fe4d9ef3352cc2208e772bbce67 Mon Sep 17 00:00:00 2001 From: Johannes Obermair Date: Wed, 8 Jan 2025 11:15:44 +0100 Subject: [PATCH] Change script to add mikro-orm script in package.json --- src/util/package-json.util.ts | 6 ++++++ src/v8/mikro-orm-dotenv.ts | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/util/package-json.util.ts b/src/util/package-json.util.ts index c84fd20..9d60d2f 100644 --- a/src/util/package-json.util.ts +++ b/src/util/package-json.util.ts @@ -3,6 +3,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs"; type Json = { dependencies?: Record; devDependencies?: Record; + scripts?: Record; }; export class PackageJson { @@ -46,4 +47,9 @@ export class PackageJson { save() { writeFileSync(this.path, JSON.stringify(this.json, null, 4)); } + + addScript(name: string, script: string) { + this.json.scripts ??= {}; + this.json.scripts[name] = script; + } } diff --git a/src/v8/mikro-orm-dotenv.ts b/src/v8/mikro-orm-dotenv.ts index aabe2d7..42c42d8 100644 --- a/src/v8/mikro-orm-dotenv.ts +++ b/src/v8/mikro-orm-dotenv.ts @@ -1,18 +1,19 @@ import { existsSync } from "fs"; -import { readFile, writeFile } from "fs/promises"; + +import { PackageJson } from "../util/package-json.util"; /** - * Add a dotenv call to config. + * Adds a `mikro-orm` script to package.json that calls dotenv. * See https://mikro-orm.io/docs/upgrading-v5-to-v6#env-files-are-no-longer-automatically-loaded. */ export default async function addDotenvCallToConfig() { - if (!existsSync("api/src/db/ormconfig.cli.ts")) { + if (!existsSync("api/package.json")) { return; } - let fileContent = await readFile("api/src/db/ormconfig.cli.ts", "utf-8"); + const packageJson = new PackageJson("api/package.json"); - fileContent = `import "dotenv/config";\n\n${fileContent}`; + packageJson.addScript("mikro-orm", "dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- mikro-orm"); - await writeFile("api/src/db/ormconfig.cli.ts", fileContent); + packageJson.save(); }