/* eslint-disable @bigbinary/neeto/no-dangling-constants */

const path = require("path");

const { watch } = require("chokidar");

const nanoDevelopmentPlugin = () => ({
  name: "vite-plugin-nano-development",
  configureServer: server => {
    const bigBinaryPackages = path.resolve(
      process.cwd(),
      "node_modules",
      "@bigbinary"
    );

    watch(bigBinaryPackages, { ignoreInitial: true })
      .on("all", async event => {
        if (event === "change" || event === "add") {
          await server.restart(true);
        }
      })
      .on("error", error =>
        /* eslint-disable no-console */
        console.log(`[nano-development-plugin] watcher error: ${error}`)
      );
  },
});

module.exports = { nanoDevelopmentPlugin };
