autorelease.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const fs = require("fs");
  2. const { exec, execSync } = require("child_process");
  3. const core = require("@actions/core");
  4. const excalidrawDir = `${__dirname}/../src/packages/excalidraw`;
  5. const excalidrawPackage = `${excalidrawDir}/package.json`;
  6. const pkg = require(excalidrawPackage);
  7. const getShortCommitHash = () => {
  8. return execSync("git rev-parse --short HEAD").toString().trim();
  9. };
  10. const publish = () => {
  11. try {
  12. execSync(`yarn --frozen-lockfile`);
  13. execSync(`yarn --frozen-lockfile`, { cwd: excalidrawDir });
  14. execSync(`yarn run build:umd`, { cwd: excalidrawDir });
  15. execSync(`yarn --cwd ${excalidrawDir} publish`);
  16. console.info("Published 🎉");
  17. core.setOutput(
  18. "result",
  19. `**Preview version has been shipped** :rocket:
  20. You can use [@excalidraw/excalidraw-preview@${pkg.version}](https://www.npmjs.com/package/@excalidraw/excalidraw-preview/v/${pkg.version}) for testing!`,
  21. );
  22. } catch (error) {
  23. core.setOutput("result", "package couldn't be published :warning:!");
  24. console.error(error);
  25. process.exit(1);
  26. }
  27. };
  28. // get files changed between prev and head commit
  29. exec(`git diff --name-only HEAD^ HEAD`, async (error, stdout, stderr) => {
  30. if (error || stderr) {
  31. console.error(error);
  32. core.setOutput("result", ":warning: Package couldn't be published!");
  33. process.exit(1);
  34. }
  35. const changedFiles = stdout.trim().split("\n");
  36. const filesToIgnoreRegex = /src\/excalidraw-app|packages\/utils/;
  37. const excalidrawPackageFiles = changedFiles.filter((file) => {
  38. return (
  39. (file.indexOf("src") >= 0 || file.indexOf("package.json")) >= 0 &&
  40. !filesToIgnoreRegex.test(file)
  41. );
  42. });
  43. if (!excalidrawPackageFiles.length) {
  44. console.info("Skipping release as no valid diff found");
  45. core.setOutput("result", "Skipping release as no valid diff found");
  46. process.exit(0);
  47. }
  48. // update package.json
  49. pkg.name = "@excalidraw/excalidraw-next";
  50. let version = `${pkg.version}-${getShortCommitHash()}`;
  51. // update readme
  52. let data = fs.readFileSync(`${excalidrawDir}/README_NEXT.md`, "utf8");
  53. const isPreview = process.argv.slice(2)[0] === "preview";
  54. if (isPreview) {
  55. // use pullNumber-commithash as the version for preview
  56. const pullRequestNumber = process.argv.slice(3)[0];
  57. version = `${pkg.version}-${pullRequestNumber}-${getShortCommitHash()}`;
  58. // replace "excalidraw-next" with "excalidraw-preview"
  59. pkg.name = "@excalidraw/excalidraw-preview";
  60. data = data.replace(/excalidraw-next/g, "excalidraw-preview");
  61. data = data.trim();
  62. }
  63. pkg.version = version;
  64. fs.writeFileSync(excalidrawPackage, JSON.stringify(pkg, null, 2), "utf8");
  65. fs.writeFileSync(`${excalidrawDir}/README.md`, data, "utf8");
  66. console.info("Publish in progress...");
  67. publish();
  68. });