Added reengineered version file creating module.
This commit is contained in:
65
scripts/version.mjs
Normal file
65
scripts/version.mjs
Normal file
@@ -0,0 +1,65 @@
|
||||
import { readFileSync, write, writeFileSync } from "fs";
|
||||
import { execSync } from "child_process";
|
||||
import semver from "semver";
|
||||
|
||||
export function getGitVersion() {
|
||||
let rawGitVersion;
|
||||
let gitVersion;
|
||||
|
||||
try {
|
||||
rawGitVersion = execSync("git describe --tags --dirty").toString().trim();
|
||||
gitVersion = semver.coerce(rawGitVersion) || semver.coerce("0.0.0");
|
||||
} catch (e) {
|
||||
return "0.0.0";
|
||||
}
|
||||
|
||||
// Git describe may return versions like v1.2.3-4-gabcdef
|
||||
// or v1.2.3-dirty or v1.2.3 or v1.2.3-4-gabcdef-dirty.
|
||||
// We need to return either a clean version or
|
||||
// append -dev for modified versions and
|
||||
// -dirty for dirty working tree.
|
||||
if (rawGitVersion.endsWith("-dirty")) {
|
||||
return gitVersion.version + "-dirty";
|
||||
} else if (rawGitVersion.includes("-")) {
|
||||
return gitVersion.version + "-dev";
|
||||
} else {
|
||||
return gitVersion.version || "0.0.0";
|
||||
}
|
||||
}
|
||||
|
||||
export function generateVersionFile(versionFilePath) {
|
||||
// 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;
|
||||
|
||||
// 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}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Generate version file
|
||||
const buildDate = new Date().toISOString();
|
||||
writeFileSync(
|
||||
versionFilePath,
|
||||
`// Auto-generated version file - do not edit manually
|
||||
// Generated at: ${buildDate}
|
||||
|
||||
export const VERSION = '${packageVersion}';
|
||||
export const IS_RELEASE = ${gitVersion === packageVersion};
|
||||
export const BUILD_TIME = '${buildDate}';
|
||||
`,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user