Skip to content

Commit

Permalink
Change script to add mikro-orm script in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyomair committed Jan 8, 2025
1 parent f0558d5 commit 55cfd2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/util/package-json.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs";
type Json = {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
scripts?: Record<string, string>;
};

export class PackageJson {
Expand Down Expand Up @@ -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;
}
}
13 changes: 7 additions & 6 deletions src/v8/mikro-orm-dotenv.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 55cfd2a

Please sign in to comment.