UNPKG

git-command-helper

Version:
186 lines (181 loc) 5.09 kB
// git-command-helper 2.1.0 by Dimas Lanjaka <dimaslanjaka@gmail.com> (https://www.webmanajemen.com) 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var Bluebird = require('bluebird'); var fs = require('fs-extra'); var path = require('upath'); var spawner = require('./spawner.js'); var extractSubmodule = require('./utils/extract-submodule.js'); // Use path.join and path.toUnix directly throughout the file class submodule { /** current working directory */ cwd; /** .gitmodules exist */ hasConfig; /** git-command-helper class */ github = {}; constructor(cwd) { this.cwd = cwd; this.hasConfig = fs.existsSync(path.join(this.cwd, ".gitmodules")); } spawnOpt(opt = {}) { return Object.assign({ cwd: this.cwd, stdio: "pipe" }, opt); } /** * add submodule */ async add(opt) { if (!opt.remote) throw new Error("submodule remote url required"); if (!opt.dest) throw new Error("submodule destination required"); const args = ["submodule", "add"]; if (opt.branch) args.push("-b", opt.branch); args.push(opt.remote); args.push(opt.dest); await spawner.spawn("git", args, { cwd: this.cwd, stdio: "pipe" }); } /** * remove submodule * @param submodulePath path to submodule */ async remove(submodulePath) { await spawner.spawn("git", ["submodule", "deinit", "-f", path.toUnix(submodulePath)], { cwd: this.cwd, stdio: "pipe" }); await fs.rm(path.join(this.cwd, ".git/modules", path.toUnix(submodulePath)), { recursive: true, force: true }); await spawner.spawn("git", ["rm", "-f", path.toUnix(submodulePath)], { cwd: this.cwd, stdio: "pipe" }); } /** * check has submodule * @returns */ hasSubmodule() { const gitmodules = path.join(this.cwd, ".gitmodules"); const exist = fs.existsSync(gitmodules); // check empty .gitmodules if (exist) { const size = fs.statSync(gitmodules).size; return size > 0; } return exist; } /** * git submodule update * @param args custom arguments * @param optionSpawn * @returns */ update(args = [], optionSpawn = { stdio: "inherit" }) { const arg = ["submodule", "update"]; if (Array.isArray(args)) { args.forEach(str => arg.push(str)); } else { arg.push("-i", "-r"); } return spawner.spawn("git", arg, this.spawnOpt(optionSpawn)); } /** * Update all submodule with cd method * @param reset do git reset --hard origin/branch ? */ safeUpdate(reset = false) { return new Bluebird(resolve => { const infos = this.get(); const doUp = () => { return new Bluebird(resolveDoUp => { if (!infos[0]) return; const github = infos[0]; const { branch, remote } = infos[0].github; const doReset = () => github.reset(branch); const doPull = () => github.pull(["origin", branch, "--recurse-submodule"]); // update from remote name origin if (typeof remote === "string") { github.setremote(remote, "origin").then(() => { // force checkout branch instead commit hash github.setbranch(branch, true).then(() => { if (reset) { // reset then pull doReset().then(doPull).then(resolveDoUp); } else { // pull doPull().then(resolveDoUp); } }); }); } }); }; const iterate = () => { return new Bluebird(resolveIt => { doUp().then(() => { infos.shift(); }).then(() => { if (infos.length > 0) { return iterate().then(resolveIt); } else { resolveIt(); } }); }); }; if (infos.length > 0) { resolve(iterate()); } }); } /** * git submodule status * @param optionSpawn * @returns */ status(optionSpawn = { stdio: "inherit" }) { return spawner.spawn("git", ["submodule", "status"], this.spawnOpt(optionSpawn)); } /** * git add all each submodule * @param pathOrArg ex: `-A` * @returns */ addAll(pathOrArg) { return spawner.spawn("git", ["submodule", "foreach", "git", "add", pathOrArg]); } commitAll(msg) { return spawner.spawn("git", ["submodule", "foreach", "git", "commit", "-am", msg]); } /** * get submodule informations * @returns */ get() { if (!this.hasSubmodule()) return []; //throw new Error('This directory not have submodule installed'); const extract = extractSubmodule(path.join(this.cwd, ".gitmodules")); for (let i = 0; i < extract.length; i++) { const item = extract[i]; if (!item) continue; this.github[item.cwd] = item.github; } return extract; } } exports.default = submodule; exports.gitSubmodule = submodule; exports.submodule = submodule;