sync-shared-modules.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import fs from "fs";
  2. import path from "path";
  3. import { fileURLToPath } from "url";
  4. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  5. const targetDir = path.resolve(__dirname, "../shared");
  6. const sourceDir = path.resolve(__dirname, "../../shared");
  7. if (!fs.existsSync(sourceDir)) {
  8. console.log(
  9. "[sync-shared] skip: repo shared/ not found, keep downLoadServer/shared as deployed copy",
  10. );
  11. process.exit(0);
  12. }
  13. fs.mkdirSync(targetDir, { recursive: true });
  14. const sourceFiles = fs
  15. .readdirSync(sourceDir)
  16. .filter((name) => !name.startsWith(".") && fs.statSync(path.join(sourceDir, name)).isFile());
  17. sourceFiles.forEach((name) => {
  18. fs.copyFileSync(path.join(sourceDir, name), path.join(targetDir, name));
  19. });
  20. const stale = fs
  21. .readdirSync(targetDir)
  22. .filter((name) => !name.startsWith(".") && !sourceFiles.includes(name));
  23. stale.forEach((name) => {
  24. fs.unlinkSync(path.join(targetDir, name));
  25. });
  26. console.log(
  27. `[sync-shared] copied ${sourceFiles.length} files to downLoadServer/shared${
  28. stale.length ? `, removed ${stale.length} stale` : ""
  29. }`,
  30. );
  31. sourceFiles.forEach((name) => console.log(` - ${name}`));