41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
const VERSION_REGEX = /^\d+\.\d+\.\d+$/;
|
|
|
|
export function bumpVersion(current: string, command: "major" | "minor" | "patch" | "set", target?: string): string {
|
|
validateVersion(current);
|
|
const [major, minor, patch] = parseVersion(current);
|
|
|
|
switch (command) {
|
|
case "major":
|
|
return formatVersion(major + 1, 0, 0);
|
|
case "minor":
|
|
return formatVersion(major, minor + 1, 0);
|
|
case "patch":
|
|
return formatVersion(major, minor, patch + 1);
|
|
case "set": {
|
|
if (!target) {
|
|
throw new Error("set command requires a target version");
|
|
}
|
|
validateVersion(target);
|
|
return target;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function formatVersion(major: number, minor: number, patch: number): string {
|
|
return `${major}.${minor}.${patch}`;
|
|
}
|
|
|
|
export function parseVersion(version: string): [number, number, number] {
|
|
const parts = version.split(".").map((p) => parseInt(p, 10));
|
|
if (parts.length !== 3 || parts.some(isNaN)) {
|
|
throw new Error(`Invalid version format: ${version}`);
|
|
}
|
|
return [parts[0]!, parts[1]!, parts[2]!];
|
|
}
|
|
|
|
export function validateVersion(version: string): void {
|
|
if (!VERSION_REGEX.test(version)) {
|
|
throw new Error(`Invalid version format: ${version}. Expected MAJOR.MINOR.PATCH (e.g., 0.1.0)`);
|
|
}
|
|
}
|