Skip to content

Commit

Permalink
Implement a REPL for the API (#408)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Karnutsch <[email protected]>
Co-authored-by: Niko Sams <[email protected]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 4d410a3 commit 1ba83a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ junit.xml
/uploads

src/site-configs.d.ts

.nestjs_repl_history
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions api/src/repl.ts
Original file line number Diff line number Diff line change
@@ -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<NestExpressApplication>(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();

0 comments on commit 1ba83a9

Please sign in to comment.