Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - Allow watching files outside of the module graph #12197

Closed
bebraw opened this issue Sep 23, 2021 · 10 comments · Fixed by #13087
Closed

Feature request - Allow watching files outside of the module graph #12197

bebraw opened this issue Sep 23, 2021 · 10 comments · Fixed by #13087
Labels
--watch related to the watch feature of the CLI feat new feature (which has been agreed to/accepted) good first issue Good for newcomers help wanted community help requested

Comments

@bebraw
Copy link

bebraw commented Sep 23, 2021

I am reading JSON files that then affect what Deno serves. The problem is that the operation is outside of Deno's module graph and the watch feature won't notice these type of changes.

Based on #2401 (comment) I was thinking patterns are supported already but looking at the source (

pub watch: bool,
) they are not.

Therefore I would propose adding support to --watch=./**/*.ts,./**/*.tsx kind of syntax (i.e. watch would accept a string of globs) to allow watching arbitrary files even outside of Deno's module graph.

Related to jurassiscripts/velociraptor#80.

@bartlomieju bartlomieju added the suggestion suggestions for new features (yet to be agreed) label Oct 5, 2021
@bebraw
Copy link
Author

bebraw commented Oct 7, 2021

I found a good workaround for this. Consider the code below:

async function watch(
  directory: string,
  extension: string,
  handler: (path: string) => void,
) {
  const watcher = Deno.watchFs(directory);

  for await (const event of watcher) {
    event.paths.forEach((p) => p.endsWith(extension) && handler(p));
  }
}

Then at mod.ts (my server):

function main() {
  // Touch this file if any json file in the project changes to trigger a rebuild.
  watch(
    ".",
    ".json",
    () => Deno.run({ cmd: ["touch", new URL("", import.meta.url).pathname] }),
  );

  ...
}

@kitsonk kitsonk added the --watch related to the watch feature of the CLI label Oct 7, 2021
@lucacasonato
Copy link
Member

I think this is a really useful feature. We should add it. Would be tremendously useful for fresh: https://fresh.deno.dev.

@lucacasonato lucacasonato added feat new feature (which has been agreed to/accepted) and removed suggestion suggestions for new features (yet to be agreed) labels Oct 26, 2021
@bartlomieju
Copy link
Member

I think this is a really useful feature. We should add it. Would be tremendously useful for fresh: https://fresh.deno.dev.

How do you propose we do this? Accept arguments to the --watch flag?

@bebraw
Copy link
Author

bebraw commented Oct 27, 2021

@bartlomieju As mentioned in the issue, I would go with something like --watch=./**/*.ts,./**/*.tsx (i.e. a list of globs). That would resolve the files to watch against the project root.

@bartlomieju
Copy link
Member

The thing is we don't really support globs in any of the CLI flags - it's up to your shell to expand them. I can see the value of this feature, but we need to be careful considering what can be delivered.

@lucacasonato
Copy link
Member

@bartlomieju let's just start with individual files and folders in the arguments to --watch, and go from there.

@bartlomieju
Copy link
Member

@bartlomieju let's just start with individual files and folders in the arguments to --watch, and go from there.

Sounds good to me, PRs are welcome

@bartlomieju bartlomieju added the help wanted community help requested label Oct 30, 2021
@bartlomieju
Copy link
Member

For anyone interested in tackling this issue, here are some some pointers how to do it:

In cli/flags.rs:

  • change watch_arg() function to accept values, and update the description that passing values has only effect on the deno run subcommand
  • change watch field in Flags struct to accept Option<Vec<PathBuf>> instead of a bool
  • in run_parse change parsing logic to create a vector of paths
  • in other functions that parse watch flag they should set watch equal to Some(vec![]) is "watch" flag is present

In cli/main.rs:

  • change run_with_watch function to extend paths_to_watch with the value of vector from Flags::watch

Add a test to cli/tests/integration/watcher_tests.rs that verifies that additional files passed to the flag actually cause the process to restart.

I'm going to mark this as a "good first issue", even though it's on the harder spectrum of "first issues".

@bartlomieju bartlomieju added the good first issue Good for newcomers label Dec 11, 2021
@jespertheend
Copy link
Contributor

I'd like to have a go at this if that's ok!

@lucacasonato
Copy link
Member

@jespertheend Please do! If you need some more guidance, please hop into the #dev channel on Discord: https://discord.gg/deno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
--watch related to the watch feature of the CLI feat new feature (which has been agreed to/accepted) good first issue Good for newcomers help wanted community help requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants