UNPKG

@pobuca/xsc

Version:

A cli tool with common git command sets.

85 lines 3.27 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const url_1 = require("url"); const GitRemoteHost_1 = require("../../interfaces/GitRemoteHost"); function inferRepoRemoteHostDetails(terminal) { return __awaiter(this, void 0, void 0, function* () { const gitConfig = getParsedGitConfig(terminal); const remoteUrl = new url_1.URL(gitConfig['[remote "origin"]'].url); return { account: getRepoAccount(remoteUrl), host: getRepoRemoteHost(remoteUrl), project: getAzureDevOpsProject(remoteUrl), repo: getRepoName(remoteUrl), url: remoteUrl }; }); } exports.default = inferRepoRemoteHostDetails; function getRepoAccount(url) { return url.pathname.split('/')[1]; } function getRepoName(url) { return url.pathname.split('/').pop(); } function getAzureDevOpsProject(url) { const remoteHost = getRepoRemoteHost(url); let result = null; if (remoteHost === GitRemoteHost_1.GitRemoteHost.AzureDevOps) { result = decodeURIComponent(url.pathname.split('/')[2]); } return result; } function getRepoRemoteHost(url) { const host = url.host; if (host in GitRemoteHostMap) { return GitRemoteHostMap[host]; } else { throw new Error(`Unknown remote host: ${url.host}`); } } function getParsedGitConfig(terminal) { const gitConfig = getGitConfig(terminal); const parsed = {}; let lastKey; for (const line of gitConfig.split('\n')) { if (line) { const parsedLine = parseGitConfigLine(lastKey, parsed[lastKey], line); lastKey = parsedLine.lastKey; parsed[lastKey] = parsedLine.parsed; } } return parsed; } function parseGitConfigLine(lastKey, lastHashObject, line) { var _a, _b; if (line.match(/^\[/)) { lastKey = line; lastHashObject = {}; } else { const key = (_a = line.match(/^\t([^ ]+) \=/)) === null || _a === void 0 ? void 0 : _a[1]; const value = (_b = line.match(/^\t[^ ]+ \= (.*)$/)) === null || _b === void 0 ? void 0 : _b[1]; lastHashObject[key] = value; } return { lastKey, parsed: lastHashObject }; } function getGitConfig(terminal) { return terminal.readFileSync(path_1.resolve(terminal.cwd, './.git/config')).toString(); } const GitRemoteHostMap = { 'dev.azure.com': GitRemoteHost_1.GitRemoteHost.AzureDevOps, 'github.com': GitRemoteHost_1.GitRemoteHost.GitHub }; //# sourceMappingURL=inferRepoRemoteHostDetails.js.map