git-command-helper
Version:
github command helper for nodejs
35 lines (32 loc) • 900 B
JavaScript
// git-command-helper 2.1.0 by Dimas Lanjaka <dimaslanjaka@gmail.com> (https://www.webmanajemen.com)
import * as spawn from 'cross-spawn';
/**
* git status
* @param opt
* @returns
*/
function gitStatus(opt) {
// set porcelain true
if (typeof opt.porcelain !== "boolean") opt.porcelain = true;
let result;
if (opt.porcelain) {
result = spawn.sync("git", ["status", "--porcelain"], opt);
} else {
result = spawn.sync("git", ["status"], opt);
}
if (!opt.raw) {
return String(result.stdout).split(/\r?\n/gm).filter(str => str.length > 0).map(str => {
const exec = Array.from(/^([MD\\?]{1,2})\s(.*)/g.exec(str.trim()) || []);
// set `??` as untracked
if (exec[1] === "??") exec[1] = "U";
// convert as object
const obj = {
[exec[1]]: exec[2]
};
return obj;
});
} else {
return result;
}
}
export { gitStatus };