@omlet/cli
Version:
Omlet (https://omlet.dev) is a component analytics tool that uses a CLI to scan your codebase to detect components and their usage. Get real usage insights from customizable charts to measure adoption across all projects and identify opportunities to impr
79 lines (78 loc) • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGitRoot = exports.getRepoInfo = exports.getInitialCommitHash = void 0;
const child_process_1 = require("child_process");
const git_repo_info_1 = __importDefault(require("git-repo-info"));
const hosted_git_info_1 = __importDefault(require("hosted-git-info"));
const util_1 = __importDefault(require("util"));
const logger_1 = require("./logger");
const exec = util_1.default.promisify(child_process_1.exec);
async function getRemoteOriginUrl(repoPath) {
try {
const cmd = "git config --get remote.origin.url";
const { stdout } = await exec(cmd, { cwd: repoPath });
return stdout.trim();
}
catch (err) {
return;
}
}
async function getInitialCommitHash(repoRoot) {
try {
const { stdout } = await exec("git rev-list --max-parents=0 HEAD", { cwd: repoRoot });
return stdout.trim();
}
catch (error) {
logger_1.logger.debug("Error while finding initial commit:");
(0, logger_1.logError)(error);
return;
}
}
exports.getInitialCommitHash = getInitialCommitHash;
async function getCurrentBranch(repoPath) {
try {
const cmd = "git branch --show-current";
const { stdout } = await exec(cmd, { cwd: repoPath });
return stdout.trim();
}
catch (err) {
return;
}
}
async function getRepoInfo(repoPath) {
const initialCommitHash = await getInitialCommitHash(repoPath);
if (!initialCommitHash) {
return;
}
const branch = await getCurrentBranch(repoPath);
const url = await getRemoteOriginUrl(repoPath);
if (!url) {
return {
initialCommitHash,
branch,
};
}
const info = hosted_git_info_1.default.fromUrl(url);
if (!info) {
return {
url,
initialCommitHash,
branch,
};
}
return {
scope: info.user,
name: info.project,
url: info.https({ noGitPlus: true }),
branch,
initialCommitHash,
};
}
exports.getRepoInfo = getRepoInfo;
function getGitRoot(path) {
return (0, git_repo_info_1.default)(path).root;
}
exports.getGitRoot = getGitRoot;