develop.mjs 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { execa } from "execa";
  2. import chokidar from "chokidar";
  3. import { resolve } from "path";
  4. const cliPath = resolve("node_modules", ".bin", "medusa");
  5. const devServer = {
  6. childProcess: null,
  7. watcher: null,
  8. start() {
  9. this.childProcess = execa({
  10. cwd: process.cwd(),
  11. env: { ...process.env },
  12. stdout: "inherit",
  13. stderr: "inherit",
  14. })`node -r ts-node/register ${cliPath} start`;
  15. },
  16. restart() {
  17. if (this.childProcess) {
  18. this.childProcess.removeAllListeners();
  19. this.childProcess.kill();
  20. }
  21. this.start();
  22. },
  23. watch() {
  24. this.watcher = chokidar.watch(['.'], {
  25. ignoreInitial: true,
  26. cwd: process.cwd(),
  27. ignored: [/(^|[\/\\])\../, "node_modules", "dist", "src/admin/**/*"],
  28. });
  29. this.watcher.on("change", (file) => {
  30. console.log("file changed", file);
  31. this.restart();
  32. });
  33. this.watcher.on("ready", function () {
  34. console.log("ready to watch files");
  35. });
  36. },
  37. };
  38. devServer.start();
  39. devServer.watch();