build.js 1.9 KB

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