diff --git a/README.md b/README.md index ed2db4a83..6db7f6ac2 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,6 @@ In use by `@comet/create-app` to create new Comet DXP projects. Find more inform // import fixtures npm run --prefix api fixtures + + // start repl + npm run --prefix api repl diff --git a/api/.gitignore b/api/.gitignore index 45c7c5009..fc49f5083 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -43,3 +43,5 @@ junit.xml /uploads src/site-configs.d.ts + +.nestjs_repl_history \ No newline at end of file diff --git a/api/package.json b/api/package.json index c627be372..0700b7407 100644 --- a/api/package.json +++ b/api/package.json @@ -17,6 +17,7 @@ "lint:generated-files-not-modified": "npm run api-generator && git diff --exit-code HEAD --", "lint:prettier": "npx prettier --check './**/*.{js,json,md,yml,yaml}'", "lint:tsc": "tsc --project ./tsconfig.lint.json", + "repl": "npm run start -- --watch --entryFile repl", "start": "npm run prebuild && dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- nest start", "start:debug": "npm run prebuild && dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- nest start --debug --watch --preserveWatchOutput", "start:dev": "npm run prebuild && npm run db:migrate && npm run console createBlockIndexViews && dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- nest start --watch --preserveWatchOutput", diff --git a/api/src/repl.ts b/api/src/repl.ts new file mode 100644 index 000000000..d11dec913 --- /dev/null +++ b/api/src/repl.ts @@ -0,0 +1,36 @@ +import "tsconfig-paths/register"; + +import { MikroORM } from "@mikro-orm/core"; +import { NestFactory } from "@nestjs/core"; +import { repl } from "@nestjs/core/repl"; +import { NestExpressApplication } from "@nestjs/platform-express"; + +import { AppModule } from "./app.module"; +import { createConfig } from "./config/config"; + +async function bootstrap() { + if (process.env.NODE_ENV === "production") { + throw new Error("Can't use REPL in production"); + } + + const config = createConfig(process.env); + const appModule = AppModule.forRoot(config); + const app = await NestFactory.create(appModule); + const replServer = await repl(appModule); + + // preserve history between refreshes of the repl + replServer.setupHistory(".nestjs_repl_history", (err) => { + if (err) { + console.error(err); + } + }); + + // Get the MikroORM instance from the app + const orm = app.get(MikroORM); + + // Context shortcuts + const replContext = replServer.context; + replContext.orm = orm; + replContext.em = orm.em; +} +bootstrap();