api-client/build.js

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-02-24 19:54:32 +00:00
const axios = require("axios");
const fs = require("fs");
const { spawnSync } = require("child_process");
const runCommand = (command, args) =>
spawnSync(command, args)
2019-11-28 15:11:52 +00:00
.stdout.toString()
2020-02-24 19:54:32 +00:00
.replace(/\n/g, "");
2020-02-24 19:54:32 +00:00
const FAIL_ON_ERROR = false;
const PW_BUILD_DATA_DIR = "./.postwoman";
const IS_DEV_MODE = process.argv.includes("--dev");
try {
2020-02-24 19:54:32 +00:00
(async () => {
// Create the build data directory if it does not exist.
if (!fs.existsSync(PW_BUILD_DATA_DIR)) {
2020-02-24 19:54:32 +00:00
fs.mkdirSync(PW_BUILD_DATA_DIR);
}
2020-02-24 19:54:32 +00:00
let version = {};
// Get the current version name as the tag from Git.
2019-11-28 15:11:52 +00:00
version.name =
2020-02-24 19:54:32 +00:00
process.env.TRAVIS_TAG ||
runCommand("git", ["tag --sort=committerdate | tail -1"]);
// FALLBACK: If version.name was unset, let's grab it from GitHub.
if (!version.name) {
2019-11-28 15:11:52 +00:00
version.name = (
await axios
2020-02-24 19:54:32 +00:00
.get("https://api.github.com/repos/liyasthomas/postwoman/releases")
2019-11-28 15:11:52 +00:00
// If we can't get it from GitHub, we'll resort to getting it from package.json
.catch(ex => ({
data: [
{
2020-02-24 19:54:32 +00:00
tag_name: require("./package.json").version
}
]
2019-11-28 15:11:52 +00:00
}))
2020-02-24 19:54:32 +00:00
).data[0]["tag_name"];
}
// Get the current version hash as the short hash from Git.
2020-02-24 19:54:32 +00:00
version.hash = runCommand("git", ["rev-parse", "--short", "HEAD"]);
// Get the 'variant' name as the branch, if it's not master.
version.variant =
process.env.TRAVIS_BRANCH ||
2020-02-24 19:54:32 +00:00
runCommand("git", ["branch"])
.split("* ")[1]
.split(" ")[0] + (IS_DEV_MODE ? " - DEV MODE" : "");
if (["", "master"].includes(version.variant)) {
delete version.variant;
2019-11-24 13:15:26 +00:00
}
// Write version data into a file
2020-02-24 19:54:32 +00:00
fs.writeFileSync(
`${PW_BUILD_DATA_DIR}/version.json`,
JSON.stringify(version)
);
})();
} catch (ex) {
2020-02-24 19:54:32 +00:00
console.error(ex);
process.exit(FAIL_ON_ERROR ? 1 : 0);
}