repl
The conf/repl.ts file is used for bootstrapping the REPL (which happens when you run the pnpm console script):
// conf/repl.ts
import './loadEnv.js'
import * as repl from 'node:repl'
import { DreamCLI } from '@rvoh/dream/system'
import initializePsychicApp from './system/initializePsychicApp.js'
const replServer = repl.start('> ')
export default (async function () {
await initializePsychicApp()
await DreamCLI.loadRepl(replServer.context)
})()
The boot order matters: env is loaded first (the import './loadEnv.js' side effect), then the Psychic app is initialized, and only then does DreamCLI.loadRepl — imported from @rvoh/dream/system — populate the REPL's global context. DreamCLI.loadRepl is what automatically loads all models and services to the global context (though beware, the names of the classes will shift in this context to be based around file path, rather than the name of the class itself).
Adding custom globals
If you need to bind anything else to the global context, you can simply attach it to replServer.context, like so:
import MyCustomClass from '../services/MyCustomClass'
export default (async function () {
await initializePsychicApp()
await DreamCLI.loadRepl(replServer.context)
replServer.context.MyCustomClass = MyCustomClass
})()
This will enable access once in the repl, like so:
NODE_ENV=development pnpm console
> console.log(MyCustomClass)
Dynamically importing files
When in the REPL, you may find you need to import something into the REPL context that doesn't exist. For example, a helper function within your app somewhere. In this case, you can use the dynamic import syntax to bring those files in dynamically, like so:
myHelper = (await import('./src/app/services/Host/pricingHelpers.js')).default