turbo-gulp
Version:
Gulp tasks to boost high-quality projects.
51 lines (50 loc) • 1.8 kB
JavaScript
import del from "del";
import { copy } from "fs-extra";
import * as posixPath from "path";
import { dir as tmpDir } from "tmp";
import { toPosix } from "../project";
import { gitAdd, gitClone as gitClone, gitCommit, gitPush } from "./git";
/**
* Run executor inside a tmp directory. Clean the directory once executor is done.
*/
async function withTmpDir(executor) {
return new Promise((resolve, reject) => {
tmpDir({ unsafeCleanup: true }, async (err, path, done) => {
if (err !== null) {
reject(err);
return;
}
try {
await executor(toPosix(path));
done(() => {
resolve();
});
}
catch (err) {
done(() => {
reject(err);
});
}
});
});
}
export async function branchPublish(options) {
return withTmpDir(async (tmpDirPath) => {
console.log(`Using temporary directory: ${tmpDirPath}`);
const localPath = posixPath.join(tmpDirPath, "repo");
await gitClone({ branch: options.branch, depth: 1, repository: options.repository, directory: localPath });
await del([posixPath.join(localPath, "**", "*"), `!${posixPath.join(localPath, ".git")}`], { force: true });
await copy(options.dir, localPath);
await gitAdd({ repository: localPath, paths: ["."] });
await gitCommit({
repository: localPath,
message: options.commitMessage,
author: options.commitAuthor,
});
await gitPush({
local: localPath,
remote: "origin",
});
console.log(`Pushed to: ${options.repository} on branch ${options.branch}`);
});
}