copy-annexation.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const MIN_NODE_VERSION = 12;
  2. const [major] = process.versions.node.split(".").map(Number);
  3. if (major < MIN_NODE_VERSION) {
  4. console.log(
  5. `[skip] Node v${process.versions.node} < v${MIN_NODE_VERSION}, skipping copy+zip step`
  6. );
  7. process.exit(0);
  8. }
  9. const fs = require("fs");
  10. const path = require("path");
  11. const root = path.join(__dirname, "..");
  12. const src = path.join(root, "buildAnnexation");
  13. const dest = path.join(root, "yos");
  14. function copyDir(srcDir, destDir) {
  15. fs.mkdirSync(destDir, { recursive: true });
  16. const entries = fs.readdirSync(srcDir, { withFileTypes: true });
  17. for (const entry of entries) {
  18. const srcPath = path.join(srcDir, entry.name);
  19. const destPath = path.join(destDir, entry.name);
  20. if (entry.isDirectory()) {
  21. copyDir(srcPath, destPath);
  22. } else {
  23. fs.copyFileSync(srcPath, destPath);
  24. }
  25. }
  26. }
  27. function zipDir(dir, zipPath) {
  28. return new Promise((resolve, reject) => {
  29. try {
  30. const archiver = require("archiver");
  31. const output = fs.createWriteStream(zipPath);
  32. const archive = archiver("zip", { zlib: { level: 1 } });
  33. output.on("close", resolve);
  34. archive.on("error", reject);
  35. archive.pipe(output);
  36. archive.directory(dir, false);
  37. archive.finalize();
  38. } catch (err) {
  39. reject(err);
  40. }
  41. });
  42. }
  43. async function main() {
  44. // 1. copy buildAnnexation into yos
  45. const entries = fs.readdirSync(src, { withFileTypes: true });
  46. for (const entry of entries) {
  47. const srcPath = path.join(src, entry.name);
  48. const destPath = path.join(dest, entry.name);
  49. if (entry.isDirectory()) {
  50. copyDir(srcPath, destPath);
  51. } else {
  52. fs.copyFileSync(srcPath, destPath);
  53. }
  54. console.log("copied:", entry.name);
  55. }
  56. console.log("buildAnnexation -> yos done");
  57. // 2. zip yos/ contents (without the yos folder itself)
  58. const zipPath = path.join(root, "yos.zip");
  59. try {
  60. await zipDir(dest, zipPath);
  61. console.log("zip: yos.zip created");
  62. } catch (err) {
  63. console.log("[skip] archiver not available, skipping zip step");
  64. }
  65. }
  66. main().catch((err) => {
  67. console.error(err);
  68. process.exit(1);
  69. });