turbo-gulp
Version:
Gulp tasks to boost high-quality projects.
77 lines (76 loc) • 2.92 kB
JavaScript
import { Incident } from "incident";
import { SpawnedProcess } from "./node-async";
export async function execGit(cmd, args = [], options) {
return new SpawnedProcess("git", [cmd, ...args], Object.assign({ stdio: "pipe" }, options)).toPromise();
}
export async function assertCleanBranch(allowedBranches) {
let stdout;
stdout = (await execGit("symbolic-ref", ["HEAD"])).stdout;
let onAllowedBranch = false;
for (const branch of allowedBranches) {
if (stdout.toString("utf8").trim() === `refs/heads/${branch}`) {
onAllowedBranch = true;
}
}
if (!onAllowedBranch) {
throw new Error(`HEAD must be on one of the branches: ${JSON.stringify(allowedBranches)}`);
}
stdout = (await execGit("status", ["--porcelain"])).stdout;
if (stdout.toString("utf8").trim().length > 0) {
throw new Error("Working copy is dirty");
}
}
/**
* Get the hash of the HEAD commit.
*
* @return The hash of the HEAD commit.
*/
export async function getHeadHash() {
return (await execGit("rev-parse", ["--verify", "HEAD"])).stdout.toString("utf8").trim();
}
export async function tagExists(tag) {
return (await execGit("tag", ["-l", tag])).stdout.toString("utf8").trim().length > 0;
}
/**
* Clone a repository into a new directory
*/
export async function gitClone(options) {
const args = [];
if (options.branch !== undefined) {
args.push("--branch", options.branch);
}
if (options.depth !== undefined) {
args.push("--depth", options.depth.toString(10));
}
args.push(options.repository, options.directory);
const result = await execGit("clone", args);
if (result.exit.type === "code" && result.exit.code !== 0) {
throw new Incident("GitClone", { options, result }, result.stderr.toString("utf8"));
}
}
/**
* Clone a repository into a new directory
*/
export async function gitAdd(options) {
const args = ["--", ...options.paths];
const result = await execGit("add", args, { cwd: options.repository });
if (result.exit.type === "code" && result.exit.code !== 0) {
throw new Incident("GitAdd", { options, result }, result.stderr.toString("utf8"));
}
}
export async function gitCommit(options) {
const args = ["-m", options.message];
if (options.author !== undefined) {
args.push("--author", options.author);
}
const result = await execGit("commit", args, { cwd: options.repository });
if (result.exit.type === "code" && result.exit.code !== 0) {
throw new Incident("GitCommit", { options, result }, result.stderr.toString("utf8"));
}
}
export async function gitPush(options) {
const result = await execGit("push", [options.remote], { cwd: options.local });
if (result.exit.type === "code" && result.exit.code !== 0) {
throw new Incident("GitPush", { options, result }, result.stderr.toString("utf8"));
}
}