autorelease.js 2.4 KB

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