@lenne.tech/cli
Version:
lenne.Tech CLI: lt
71 lines (70 loc) • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildDevEnv = buildDevEnv;
/**
* Build environment variables for `lt dev up`.
*
* URL-first: API and App processes receive complete URLs (not just ports)
* so they can configure CORS, BetterAuth trusted origins, OpenAPI servers,
* Vite proxies and storage prefixes consistently — without ever needing
* to know which internal port Caddy proxies them to.
*
* Cross-wiring protection:
* - `BASE_URL`/`APP_URL` lock the API to its own App origin (CORS + BetterAuth)
* - `NUXT_PUBLIC_*` lock the App to its own API
* - `NUXT_PUBLIC_STORAGE_PREFIX` namespaces localStorage/sessionStorage
* - `NSC__MONGOOSE__URI` / `DATABASE_URL` namespace the database per project
*
* CA trust for SSR fetches:
* - Both API and App receive `NODE_EXTRA_CA_CERTS` pointing at the
* Caddy local root CA so server-side fetches between the two
* subdomains succeed. Without this Nuxt SSR fails with "unable to
* get local issuer certificate" when the app calls its own API.
*/
const dev_env_bridge_1 = require("./dev-env-bridge");
/**
* Build the environment maps for both API and App processes.
*
* Both processes inherit `baseEnv` (typically `process.env`) so user-set
* vars survive. `lt dev`-managed keys win on top.
*/
function buildDevEnv(input) {
const { apiInternalPort, appInternalPort, baseEnv = {}, dbName, identity } = input;
const apiSub = identity.subdomains.api;
const appSub = identity.subdomains.app;
const apiUrl = apiSub ? `https://${apiSub.hostname}` : '';
const appUrl = appSub ? `https://${appSub.hostname}` : '';
const caPath = (0, dev_env_bridge_1.detectCaddyRootCa)();
const sharedKeys = Object.assign(Object.assign(Object.assign(Object.assign({
// Marks the API + App processes as running under `lt dev`. Consumed by the
// backend to relax dev-only behaviour (rate limiting, Better-Auth
// user-cache) so E2E suites run without a separate VITEST/PLAYWRIGHT flag.
// (Also written to the .lt-dev/.env bridge for external test runners.)
LT_DEV_ACTIVE: 'true' }, (apiUrl ? { BASE_URL: apiUrl, NSC__BASE_URL: apiUrl } : {})), (appUrl ? { APP_URL: appUrl, NSC__APP_URL: appUrl } : {})), (dbName ? { DATABASE_URL: buildPostgresUrl(dbName), NSC__MONGOOSE__URI: `mongodb://127.0.0.1/${dbName}` } : {})), (caPath ? { NODE_EXTRA_CA_CERTS: caPath } : {}));
return {
api: {
env: Object.assign(Object.assign(Object.assign({}, baseEnv), sharedKeys), {
// Force IPv4 loopback binding so Caddy's `127.0.0.1` upstream
// (see `caddy.ts#renderProjectBlock`) always matches the
// listener. Without this, Nuxt + Nest sometimes bind to
// `[::1]` only, and Caddy gets connection-refused on IPv4.
HOST: '127.0.0.1', NITRO_HOST: '127.0.0.1', PORT: String(apiInternalPort) }),
internalPort: apiInternalPort,
},
app: {
env: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, baseEnv), sharedKeys), {
// See API note above: pin the dev server to IPv4 so Caddy's
// `127.0.0.1` upstream is unambiguous.
HOST: '127.0.0.1', NITRO_HOST: '127.0.0.1' }), (apiUrl ? { API_URL: apiUrl, NUXT_API_URL: apiUrl, NUXT_PUBLIC_API_URL: apiUrl } : {})), (appUrl ? { NUXT_PUBLIC_SITE_URL: appUrl, SITE_URL: appUrl } : {})), {
// Vite-API-Proxy is OFF by default in lt dev mode — Caddy serves
// both subdomains under HTTPS with shared cookie domain, so
// same-origin trickery is no longer required.
NUXT_PUBLIC_API_PROXY: 'false', NUXT_PUBLIC_STORAGE_PREFIX: identity.slug, PORT: String(appInternalPort) }),
internalPort: appInternalPort,
},
};
}
/** Postgres convenience URL — used by Postgres-based projects (e.g. nest-base). */
function buildPostgresUrl(dbName) {
return `postgresql://${dbName}:${dbName}@localhost:5432/${dbName}`;
}