build.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const axios = require("axios");
  2. const fs = require("fs");
  3. const { spawnSync } = require("child_process");
  4. const runCommand = (command, args) =>
  5. spawnSync(command, args)
  6. .stdout.toString()
  7. .replace(/\n/g, "");
  8. const FAIL_ON_ERROR = false;
  9. const PW_BUILD_DATA_DIR = "./.postwoman";
  10. const IS_DEV_MODE = process.argv.includes("--dev");
  11. try {
  12. (async () => {
  13. // Create the build data directory if it does not exist.
  14. if (!fs.existsSync(PW_BUILD_DATA_DIR)) {
  15. fs.mkdirSync(PW_BUILD_DATA_DIR);
  16. }
  17. let version = {};
  18. // Get the current version name as the tag from Git.
  19. version.name = process.env.TRAVIS_TAG || runCommand("git", ["tag"]);
  20. // FALLBACK: If version.name was unset, let's grab it from GitHub.
  21. if (!version.name) {
  22. version.name = (await axios
  23. .get("https://api.github.com/repos/liyasthomas/postwoman/releases")
  24. // If we can't get it from GitHub, we'll resort to getting it from package.json
  25. .catch(ex => ({
  26. data: [{ tag_name: require("./package.json").version }]
  27. }))).data[0]["tag_name"];
  28. }
  29. // Get the current version hash as the short hash from Git.
  30. version.hash = runCommand("git", ["rev-parse", "--short", "HEAD"]);
  31. // Get the 'variant' name as the branch, if it's not master.
  32. version.variant =
  33. process.env.TRAVIS_BRANCH ||
  34. runCommand("git", ["branch"])
  35. .split("* ")[1]
  36. .split(" ")[0] + (IS_DEV_MODE ? " - DEV MODE" : "");
  37. if (["", "master"].includes(version.variant))
  38. delete version.variant;
  39. // Write version data into a file
  40. fs.writeFileSync(
  41. PW_BUILD_DATA_DIR + "/version.json",
  42. JSON.stringify(version)
  43. );
  44. })();
  45. } catch (ex) {
  46. console.error(ex);
  47. process.exit(FAIL_ON_ERROR ? 1 : 0);
  48. }