diff --git a/packages/core/src/cli/cli-executable.ts b/packages/core/src/cli/cli-executable.ts index ee100f91..40d04587 100644 --- a/packages/core/src/cli/cli-executable.ts +++ b/packages/core/src/cli/cli-executable.ts @@ -44,14 +44,17 @@ program.addCommand(ClearCacheCommand); program.addCommand(PluginCommand); program.addCommand(PrintEnvCommand); - - // have to pass through the root program for this one so we can access all the subcommands addDocsCommand(program); - customizeHelp(program); -initCliRunCtx(); +program + .hook('preAction', (thisCommand, actionCommand) => { + // we need to know up front whether to enable the file watchers when initializing the vite server + const enableWatch = actionCommand.name() === 'dev' || actionCommand.opts().watch; + initCliRunCtx(enableWatch); + }); + debug(`finish loading - begin parse ${+new Date() - startBoot}ms`); try { await program.parseAsync(); diff --git a/packages/core/src/cli/commands/resolve.command.ts b/packages/core/src/cli/commands/resolve.command.ts index 4fdc8b2b..c12d368e 100644 --- a/packages/core/src/cli/commands/resolve.command.ts +++ b/packages/core/src/cli/commands/resolve.command.ts @@ -33,7 +33,6 @@ addWatchMode(program); // must be first addCacheFlags(program); addServiceSelection(program, { disablePrompt: isSubshell() }); - program.action(async (opts: { // these args should be handled already by the helpers // service?: string, diff --git a/packages/core/src/cli/commands/run.command.ts b/packages/core/src/cli/commands/run.command.ts index bd97b3b3..07e2b227 100644 --- a/packages/core/src/cli/commands/run.command.ts +++ b/packages/core/src/cli/commands/run.command.ts @@ -1,11 +1,8 @@ -import kleur from 'kleur'; import _ from 'lodash-es'; import { ExecaChildProcess, execa } from 'execa'; import which from 'which'; -import { tryCatch } from '@dmno/ts-lib'; import { DmnoCommand } from '../lib/dmno-command'; -import { formatError, formattedValue, getItemSummary } from '../lib/formatting'; import { addServiceSelection } from '../lib/selection-helpers'; import { getCliRunCtx } from '../lib/cli-ctx'; import { addCacheFlags } from '../lib/cache-helpers'; @@ -75,6 +72,7 @@ program.action(async (_command, opts: { } } fullInjectedEnv.DMNO_INJECTED_ENV = JSON.stringify(service.configraphEntity.getInjectedEnvJSON()); + fullInjectedEnv.DMNO_PROCESS_UUID = 'abc123'; commandProcess = execa(pathAwareCommand || rawCommand, commandArgsOnly, { stdio: 'inherit', diff --git a/packages/core/src/cli/lib/cli-ctx.ts b/packages/core/src/cli/lib/cli-ctx.ts index f730517f..0baeffec 100644 --- a/packages/core/src/cli/lib/cli-ctx.ts +++ b/packages/core/src/cli/lib/cli-ctx.ts @@ -40,11 +40,9 @@ const ctxHelpers = { export const cliRunContext = new AsyncLocalStorage(); -export function initCliRunCtx() { +export function initCliRunCtx(enableWatch = false) { cliRunContext.enterWith({ - // not sure about this... - // configLoader: new ConfigLoaderProcess(), - configLoader: new ConfigLoader(), + configLoader: new ConfigLoader(enableWatch), ...ctxHelpers, }); } diff --git a/packages/core/src/config-loader/config-loader.ts b/packages/core/src/config-loader/config-loader.ts index 7d7f7a4d..c62ff0b8 100644 --- a/packages/core/src/config-loader/config-loader.ts +++ b/packages/core/src/config-loader/config-loader.ts @@ -37,7 +37,7 @@ export class ConfigLoader { // get isReady() { return this.isReadyDeferred.promise; } isReady: Promise; - constructor() { + constructor(private enableWatch: boolean) { this.isReady = this.finishInit(); this.startAt = new Date(); } @@ -67,7 +67,11 @@ export class ConfigLoader { // TODO: we may want to do this on demand // so it does not slow down `dmno init` or other commands that don't need it - const { viteRunner } = await setupViteServer(this.workspaceRootPath, (ctx) => this.viteHotReloadHandler(ctx)); + const { viteRunner } = await setupViteServer({ + workspaceRootPath: this.workspaceRootPath, + enableWatch: this.enableWatch, + hotReloadHandler: (ctx) => this.viteHotReloadHandler(ctx), + }); this.viteRunner = viteRunner; } diff --git a/packages/core/src/config-loader/vite-server.ts b/packages/core/src/config-loader/vite-server.ts index f08c837c..07f1f3a1 100644 --- a/packages/core/src/config-loader/vite-server.ts +++ b/packages/core/src/config-loader/vite-server.ts @@ -4,10 +4,11 @@ import { ViteNodeServer } from 'vite-node/server'; import { installSourcemapsSupport } from 'vite-node/source-map'; import MagicString from 'magic-string'; -export async function setupViteServer( +export async function setupViteServer(opts: { workspaceRootPath: string, hotReloadHandler: (ctx: HmrContext) => Promise, -) { + enableWatch: boolean +}) { const customPlugin: Plugin = { name: 'dmno-config-loader-plugin', @@ -26,7 +27,7 @@ export async function setupViteServer( return { // pointing at dist/index is hard-coded... // we could extract the main entry point from the resolution instead? - id: `${workspaceRootPath}/node_modules/dmno/dist/index.js`, + id: `${opts.workspaceRootPath}/node_modules/dmno/dist/index.js`, external: 'absolute', }; } @@ -68,14 +69,13 @@ export async function setupViteServer( if (m.id) viteRunner.moduleCache.deleteByModuleId(m.id); }); - await hotReloadHandler(ctx); + await opts.hotReloadHandler(ctx); }, }; - // create vite server const server = await createServer({ - root: workspaceRootPath, + root: opts.workspaceRootPath, appType: 'custom', clearScreen: false, logLevel: 'warn', @@ -95,7 +95,7 @@ export async function setupViteServer( // }, // ssr: true, }, - + ...!opts.enableWatch && { server: { watch: null } }, }); // console.log(server.config);