UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

98 lines 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.gitInfo = void 0; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const path_1 = require("path"); const env_ci_1 = tslib_1.__importDefault(require("env-ci")); const git_parse_1 = require("git-parse"); function splitPath(path) { const parts = path.split(/(\/|\\)/); if (!parts.length) { return parts; } // when path starts with a slash, the first part is empty string return !parts[0].length ? parts.slice(1) : parts; } function findParentDir(currentFullPath, clue) { function testDir(parts) { if (parts.length === 0) { return null; } const p = parts.join(''); const itdoes = (0, fs_1.existsSync)((0, path_1.join)(p, clue)); return itdoes ? p : testDir(parts.slice(0, -1)); } return testDir(splitPath(currentFullPath)); } function useGitHubAction() { return { detect() { // eslint-disable-next-line no-process-env return !!process.env.GITHUB_ACTIONS; }, env() { const isPr = // eslint-disable-next-line no-process-env process.env.GITHUB_EVENT_NAME === 'pull_request' || // eslint-disable-next-line no-process-env process.env.GITHUB_EVENT_NAME === 'pull_request_target'; if (isPr) { try { // eslint-disable-next-line no-process-env const event = process.env.GITHUB_EVENT_PATH ? // eslint-disable-next-line no-process-env JSON.parse((0, fs_1.readFileSync)(process.env.GITHUB_EVENT_PATH, 'utf-8')) : undefined; if (event === null || event === void 0 ? void 0 : event.pull_request) { return { commit: event.pull_request.head.sha, }; } } catch (_a) { // Noop } } return { commit: undefined }; }, }; } async function gitInfo(noGit) { var _a, _b, _c; let commit = null; let author = null; const env = (0, env_ci_1.default)(); const githubAction = useGitHubAction(); if (githubAction.detect()) { commit = (_a = githubAction.env().commit) !== null && _a !== void 0 ? _a : null; } if (!commit) { commit = (_b = env.commit) !== null && _b !== void 0 ? _b : null; } if (!commit || !author) { const rootFromEnv = 'root' in env ? env.root : null; const git = (_c = rootFromEnv !== null && rootFromEnv !== void 0 ? rootFromEnv : findParentDir(__dirname, '.git')) !== null && _c !== void 0 ? _c : findParentDir(process.cwd(), '.git'); if (git) { const commits = await (0, git_parse_1.gitToJs)(git); if (commits && commits.length) { const lastCommit = commits[0]; if (!commit) { commit = lastCommit.hash; } if (!author) { author = `${lastCommit.authorName || ''} ${lastCommit.authorEmail ? `<${lastCommit.authorEmail}>` : ''}`.trim(); } } } else { noGit(); } } return { commit, author, }; } exports.gitInfo = gitInfo; //# sourceMappingURL=git.js.map