andykais
andykais13mo ago

How to detect when --watch flag has been passed to `deno run` programatically?

I am writing a small cli script, and I have a few bits of code that do not play nicely with deno run --watch <script>. (e.g. Deno.addSignalListener('SIGINT', graceful_shutdown). What I want to do is only set up that listener when I am running my cli app in 'production mode'. E.g. while I am developing the cli app with --watch I do not want to set up shutdown listeners. Is there a way to accomplish this with the Deno global perhaps? My current workaround is to pass some custom flag to my cli script when I am in development, like --mode=development or --no-signal, but this is a bit confusing for users because this flag is only important for development.
2 Replies
Deno AI Helper
Deno AI Helper13mo ago
Based on the documentation, there isn't a direct way to programmatically detect if the --watch flag has been passed to deno run. The Deno global does not provide this information. Your current workaround of passing a custom flag like --mode=development or --no-signal is a valid approach. You can check for this flag in your script using Deno.args, which is an array of command-line arguments passed to the script. Here's a simple example:
if (Deno.args.includes("--mode=development")) {
console.log("Running in development mode");
} else {
Deno.addSignalListener("SIGINT", graceful_shutdown);
}
if (Deno.args.includes("--mode=development")) {
console.log("Running in development mode");
} else {
Deno.addSignalListener("SIGINT", graceful_shutdown);
}
In this example, the SIGINT signal listener is only added if the --mode=development flag is not present in the command-line arguments. Remember to clearly document this behavior for your users, as this is not a standard Deno feature and might not be expected.
andykais
andykaisOP13mo ago
Thanks, thats what I ended up doing. If other people run into this workflow, maybe we can consider a feature request. For now though, this is good enough