gajus / turbowatch

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

allow a dynamic interruptible setting #14

Open gajus opened 1 year ago

gajus commented 1 year ago

There may be use cases where the desired behavior is that the service is restarted only when certain files change, e.g.

void watch({
  project: __dirname,
  triggers: [
    {
      expression: [
        'anyof',
        ['match', '*', 'basename'],
      ],
      // Marking this routine as non-interruptible will ensure that
      // next dev is not restarted when file changes are detected.
      interruptible: false,
      name: 'start-server',
      onChange: async ({ spawn }) => {
        await spawn`next dev`;
      },
    },
  ],
});

Setting { interruptible: false } is going to prevent next dev from being restarted. However, what if we want to restart the service when next.config.js changes? Then we could do something like this:

void watch({
  project: __dirname,
  triggers: [
    {
      expression: [
        'anyof',
        ['match', '*', 'basename'],
      ],
      interruptible: ({ files }) => {
        if (includes(files, 'next.config.ts')) {
          return true;
        }

        return false;
      },
      name: 'start-server',
      onChange: async ({ spawn }) => {
        await spawn`next dev`;
      },
    },
  ],
});