gajus / turbowatch

Extremely fast file change detector and task orchestrator for Node.js.
Other
938 stars 23 forks source link

Provide a way to unregister watcher #28

Open gajus opened 1 year ago

gajus commented 1 year ago

Not sure if necessary, but there are cases when we want to run a particular trigger only once.

Consider how to combine watch mode with manual task execution: There are steps in our setup that need to run once, but then may need to be re-run during the lifetime of the watch mode, e.g. We have a script to download assets from Figma. It makes no sense to re-run this logic based on local file changes, but need to provide a way for users to re-run this script when they need to. In our case, we run it once when initiating Turbowatch and then anyone can run it manually by using turbo generate, which will be picked up by Turbowarch. For Turborepo, this probably means that watch scripts just utilize dependsOn to run some tasks once before going into the watch mode.

As far as I can think, this is true only for commands that need to once at the start. We are currently handling this with:

{
  expression: ['match', '*'],
  interruptible: false,
  name: 'generate',
  onChange: async ({ spawn, first }) => {
    if (first) {
      await spawn`pnpm turbo run generate`;
    }
  },
},

This works, but it will keep logging generate every time it detects relevant changes. A more elegant solution would allow trigger to deregister or simply indicate that trigger only has to run once at the start.