@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
48 lines (47 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.gitProjectInformation = exports.getGitRemoteHostAndPath = exports.getProjectRootPath = void 0;
const gitConfig_1 = require("./gitConfig");
const child_process_promise_1 = require("child-process-promise");
/**
* Find the absolute path of the current git project root.
*/
const getProjectRootPath = async () => {
const {
stdout
} = await (0, child_process_promise_1.exec)(`git rev-parse --show-toplevel`);
return stdout.trim();
};
exports.getProjectRootPath = getProjectRootPath;
/**
* Get the git remote hostname and project path from the git config.
*/
const getGitRemoteHostAndPath = async () => {
var _a;
const remoteUrl = await (0, gitConfig_1.gitConfigGet)("remote.origin.url");
const remoteReg = /(https:\/\/|git@)([^:/]+)[:/]([^.]*)(\.git)?/;
const match = (_a = remoteUrl.match(remoteReg)) !== null && _a !== void 0 ? _a : [];
const [,, gitRemoteHost, gitRemotePath] = match;
if (!(gitRemoteHost === null || gitRemoteHost === void 0 ? void 0 : gitRemoteHost.length) || !(gitRemotePath === null || gitRemotePath === void 0 ? void 0 : gitRemotePath.length)) {
throw new Error(`Failed to parse git remote hostname and path from git configs remote.origin.url! ${remoteUrl}`);
}
return {
gitRemoteHost,
gitRemotePath
};
};
exports.getGitRemoteHostAndPath = getGitRemoteHostAndPath;
const gitProjectInformation = async () => {
const [{
gitRemoteHost,
gitRemotePath
}, projectRootPath] = await Promise.all([(0, exports.getGitRemoteHostAndPath)(), (0, exports.getProjectRootPath)()]);
return {
gitRemoteHost,
gitRemotePath,
projectRootPath
};
};
exports.gitProjectInformation = gitProjectInformation;