turbo-gulp
Version:
Gulp tasks to boost high-quality projects.
59 lines (58 loc) • 2.13 kB
JavaScript
import * as semver from "semver";
import * as git from "./git";
import { readText, writeText } from "./node-async";
export async function assertUnusedTag(tag) {
if (await git.tagExists(tag)) {
throw new Error(`Tag ${tag} already exists`);
}
}
export function getVersionTag(version) {
return `v${version}`;
}
export function getVersionMessage(version) {
return `Release v${version}`;
}
export async function commitVersion(version, projectRoot) {
const tag = getVersionTag(version);
const message = getVersionMessage(version);
await git.execGit("add", ["."]);
await git.execGit("commit", ["-m", message]);
await git.execGit("tag", ["-a", tag, "-m", message]);
}
export async function release(version, locations) {
await Promise.all([
assertUnusedTag(getVersionTag(version)),
git.assertCleanBranch(["master", getVersionTag(version)]),
]);
await setPackageVersion(version, locations);
await commitVersion(version, locations.root);
}
export async function readJsonFile(filePath) {
return JSON.parse(await readText(filePath));
}
export async function writeJsonFile(filePath, data) {
return writeText(filePath, JSON.stringify(data, null, 2) + "\n");
}
export async function readPackage(locations) {
return readJsonFile(locations.packageJson);
}
export async function writePackage(pkg, locations) {
return writeJsonFile(locations.packageJson, pkg);
}
export async function setPackageVersion(version, locations) {
const packageData = await readPackage(locations);
packageData.version = version;
return writePackage(packageData, locations);
}
export async function getNextVersion(bumpKind, locations) {
const packageData = await readPackage(locations);
const result = semver.inc(packageData.version, bumpKind);
if (typeof result !== "string") {
throw new Error("FailedAssertion: Unable to increment package version");
}
return result;
}
export async function bumpVersion(bumpKind, locations) {
const nextVersion = await getNextVersion(bumpKind, locations);
await release(nextVersion, locations);
}