Fixed failing build.
This commit is contained in:
@@ -2,6 +2,24 @@ import { readFileSync, write, writeFileSync } from "fs";
|
||||
import { execSync } from "child_process";
|
||||
import semver from "semver";
|
||||
|
||||
export function isGitAvailable() {
|
||||
try {
|
||||
execSync("git --version", { stdio: "ignore" });
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isGitRepo() {
|
||||
try {
|
||||
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function getGitVersion() {
|
||||
let rawGitVersion;
|
||||
let gitVersion;
|
||||
@@ -28,26 +46,38 @@ export function getGitVersion() {
|
||||
}
|
||||
|
||||
export function generateVersionFile(versionFilePath) {
|
||||
if (!isGitAvailable()) {
|
||||
throw new Error("Git is required to generate version info.");
|
||||
}
|
||||
|
||||
// Read package.json version
|
||||
const packageVersion = JSON.parse(
|
||||
readFileSync("package.json", { encoding: "utf-8" }),
|
||||
).version;
|
||||
// Get version from git repository
|
||||
const gitVersion = getGitVersion();
|
||||
const gitBaseVersion = semver.coerce(gitVersion)?.version;
|
||||
let gitVersion = packageVersion;
|
||||
let gitBaseVersion = packageVersion;
|
||||
let isRelease = true;
|
||||
|
||||
// if git returned malformed version, throw error
|
||||
if (!gitBaseVersion || gitBaseVersion === "0.0.0") {
|
||||
throw new Error(
|
||||
"Cannot determine git version. Make sure the script is run in a git repository with tags.",
|
||||
);
|
||||
}
|
||||
if (isGitRepo()) {
|
||||
// Get version from git repository
|
||||
gitVersion = getGitVersion();
|
||||
gitBaseVersion = semver.coerce(gitVersion)?.version;
|
||||
|
||||
// Compare git version with package.json version
|
||||
if (semver.neq(gitBaseVersion, packageVersion)) {
|
||||
throw new Error(
|
||||
`Version mismatch: package.json version is ${packageVersion}, but git version is ${gitBaseVersion}`,
|
||||
);
|
||||
// if git returned malformed version, throw error
|
||||
if (!gitBaseVersion || gitBaseVersion === "0.0.0") {
|
||||
throw new Error(
|
||||
"Cannot determine git version. Make sure the script is run in a git repository with tags.",
|
||||
);
|
||||
}
|
||||
|
||||
// Compare git version with package.json version
|
||||
if (semver.neq(gitBaseVersion, packageVersion)) {
|
||||
throw new Error(
|
||||
`Version mismatch: package.json version is ${packageVersion}, but git version is ${gitBaseVersion}`,
|
||||
);
|
||||
}
|
||||
|
||||
isRelease = gitVersion === packageVersion;
|
||||
}
|
||||
|
||||
// Generate version file
|
||||
@@ -58,7 +88,7 @@ export function generateVersionFile(versionFilePath) {
|
||||
// Generated at: ${buildDate}
|
||||
|
||||
export const VERSION = '${packageVersion}';
|
||||
export const IS_RELEASE = ${gitVersion === packageVersion};
|
||||
export const IS_RELEASE = ${isRelease};
|
||||
export const BUILD_TIME = '${buildDate}';
|
||||
`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user