Skip to content

Commit

Permalink
feat(console): add --watch option to wing it to watch additional di…
Browse files Browse the repository at this point in the history
…rectory (#6416)

This allows users to watch arbitrary places for file changes. This is mostly an escape hatch for #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)*.
  • Loading branch information
MarkMcCulloh authored May 7, 2024
1 parent b1d0127 commit 7efb1c4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/wing-console/console/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface CreateConsoleAppOptions {
platform?: string[];
stateDir?: string;
open?: boolean;
watchGlobs?: string[];
}

const staticDir = `${__dirname}/vite`;
Expand Down
4 changes: 4 additions & 0 deletions apps/wing-console/console/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface CreateConsoleServerOptions {
analytics?: Analytics;
requireSignIn?: () => Promise<boolean>;
notifySignedIn?: () => Promise<void>;
watchGlobs?: string[];
}

export const createConsoleServer = async ({
Expand All @@ -102,6 +103,7 @@ export const createConsoleServer = async ({
analytics,
requireSignIn,
notifySignedIn,
watchGlobs,
}: CreateConsoleServerOptions) => {
const emitter = new Emittery<{
invalidateQuery: RouteNames;
Expand Down Expand Up @@ -135,6 +137,7 @@ export const createConsoleServer = async ({
platform,
testing: false,
stateDir,
watchGlobs,
});
let isStarting = false;
let isStopping = false;
Expand All @@ -157,6 +160,7 @@ export const createConsoleServer = async ({
wingfile,
platform,
testing: true,
watchGlobs,
});
const testSimulator = createSimulator({ enableSimUpdates });
testCompiler.on("compiled", ({ simfile }) => {
Expand Down
19 changes: 11 additions & 8 deletions apps/wing-console/console/server/src/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export interface CreateCompilerProps {
platform?: string[];
testing?: boolean;
stateDir?: string;
watchGlobs?: string[];
}

export const createCompiler = ({
wingfile,
platform = [wing.BuiltinPlatform.SIM],
testing = false,
stateDir,
watchGlobs,
}: CreateCompilerProps): Compiler => {
const events = new Emittery<CompilerEvents>();
let isCompiling = false;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions apps/wing/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ async function main() {
.argument("[entrypoint]", "program .w entrypoint")
.option("-p, --port <port>", "specify port")
.option("--no-open", "Do not open the Wing Console in the browser")
.option(
"-w, --watch <globs...>",
"Watch additional paths for changes. Supports globs and '!' for negations."
)
.option(
"-t, --platform <platform> --platform <platform>",
"Target platform provider (builtin: sim)",
Expand Down
6 changes: 6 additions & 0 deletions apps/wing/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}

/**
Expand Down Expand Up @@ -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}`);
Expand Down

0 comments on commit 7efb1c4

Please sign in to comment.