From 7efb1c4a8bf5d4689a738873900f7878a19a88f9 Mon Sep 17 00:00:00 2001 From: Mark McCulloh Date: Tue, 7 May 2024 09:15:09 -0400 Subject: [PATCH] feat(console): add --watch option to `wing it` to watch additional directory (#6416) This allows users to watch arbitrary places for file changes. This is mostly an escape hatch for https://github.com/winglang/wing/issues/3730, but even when that is implemented there will probably continue to be a desire to watch arbitrary stuff. *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- apps/wing-console/console/app/src/index.ts | 1 + apps/wing-console/console/server/src/index.ts | 4 ++++ .../console/server/src/utils/compiler.ts | 19 +++++++++++-------- apps/wing/src/cli.ts | 4 ++++ apps/wing/src/commands/run.ts | 6 ++++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/apps/wing-console/console/app/src/index.ts b/apps/wing-console/console/app/src/index.ts index 2bf9d0ce910..443fbc26af3 100644 --- a/apps/wing-console/console/app/src/index.ts +++ b/apps/wing-console/console/app/src/index.ts @@ -40,6 +40,7 @@ export interface CreateConsoleAppOptions { platform?: string[]; stateDir?: string; open?: boolean; + watchGlobs?: string[]; } const staticDir = `${__dirname}/vite`; diff --git a/apps/wing-console/console/server/src/index.ts b/apps/wing-console/console/server/src/index.ts index 29294c50024..900551692d4 100644 --- a/apps/wing-console/console/server/src/index.ts +++ b/apps/wing-console/console/server/src/index.ts @@ -82,6 +82,7 @@ export interface CreateConsoleServerOptions { analytics?: Analytics; requireSignIn?: () => Promise; notifySignedIn?: () => Promise; + watchGlobs?: string[]; } export const createConsoleServer = async ({ @@ -102,6 +103,7 @@ export const createConsoleServer = async ({ analytics, requireSignIn, notifySignedIn, + watchGlobs, }: CreateConsoleServerOptions) => { const emitter = new Emittery<{ invalidateQuery: RouteNames; @@ -135,6 +137,7 @@ export const createConsoleServer = async ({ platform, testing: false, stateDir, + watchGlobs, }); let isStarting = false; let isStopping = false; @@ -157,6 +160,7 @@ export const createConsoleServer = async ({ wingfile, platform, testing: true, + watchGlobs, }); const testSimulator = createSimulator({ enableSimUpdates }); testCompiler.on("compiled", ({ simfile }) => { diff --git a/apps/wing-console/console/server/src/utils/compiler.ts b/apps/wing-console/console/server/src/utils/compiler.ts index a855053ef16..47786262c76 100644 --- a/apps/wing-console/console/server/src/utils/compiler.ts +++ b/apps/wing-console/console/server/src/utils/compiler.ts @@ -26,6 +26,7 @@ export interface CreateCompilerProps { platform?: string[]; testing?: boolean; stateDir?: string; + watchGlobs?: string[]; } export const createCompiler = ({ @@ -33,6 +34,7 @@ export const createCompiler = ({ platform = [wing.BuiltinPlatform.SIM], testing = false, stateDir, + watchGlobs, }: CreateCompilerProps): Compiler => { const events = new Emittery(); let isCompiling = false; @@ -77,19 +79,20 @@ export const createCompiler = ({ }; const dirname = path.dirname(wingfile); - //TODO: infer React App resource folders from source files https://github.com/winglang/wing/issues/3956 - const ignoreList = [ - `${dirname}/target/**`, - "**/node_modules/**", - "**/.git/**", + + const pathsToWatch = [ + `!**/node_modules/**`, + `!**/.git/**`, + `!${dirname}/target/**`, + dirname, + ...(watchGlobs ?? []), ]; if (stateDir) { - ignoreList.push(stateDir); + pathsToWatch.push(`!${stateDir}`); } - const watcher = chokidar.watch(dirname, { - ignored: ignoreList, + const watcher = chokidar.watch(pathsToWatch, { ignoreInitial: true, }); watcher.on("change", recompile); diff --git a/apps/wing/src/cli.ts b/apps/wing/src/cli.ts index 7ba699e752f..684859ad75c 100644 --- a/apps/wing/src/cli.ts +++ b/apps/wing/src/cli.ts @@ -144,6 +144,10 @@ async function main() { .argument("[entrypoint]", "program .w entrypoint") .option("-p, --port ", "specify port") .option("--no-open", "Do not open the Wing Console in the browser") + .option( + "-w, --watch ", + "Watch additional paths for changes. Supports globs and '!' for negations." + ) .option( "-t, --platform --platform ", "Target platform provider (builtin: sim)", diff --git a/apps/wing/src/commands/run.ts b/apps/wing/src/commands/run.ts index a43cfd4fc50..b6a3c0a7d6d 100644 --- a/apps/wing/src/commands/run.ts +++ b/apps/wing/src/commands/run.ts @@ -31,6 +31,11 @@ export interface RunOptions { * @default wingCompiler.BuiltinPlatform.SIM */ readonly platform?: string[]; + + /** + * Additional paths to watch or ignore for changes. Supports globs. + */ + readonly watch?: string[]; } /** @@ -87,6 +92,7 @@ export async function run(entrypoint?: string, options?: RunOptions) { platform: options?.platform, requireAcceptTerms: !!process.stdin.isTTY, open: openBrowser, + watchGlobs: options?.watch, }); const url = `http://localhost:${port}/`; console.log(`The Wing Console is running at ${url}`);