jsx-readme
Version:
Generate Readme files with a React-like syntax and package.json-aware helpers.
82 lines • 4.35 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContributorsSectionFromPkg = void 0;
/* @jsx MD */
const jsx_md_1 = __importStar(require("jsx-md"));
const parse_link_header_1 = __importDefault(require("parse-link-header"));
const core_1 = require("@octokit/core");
const components_1 = require("../components");
const extractGithubOwnerAndRepo_1 = require("../components/badges/utils/extractGithubOwnerAndRepo");
// Fetch total contributors count using pagination trick. See https://stackoverflow.com/a/44347632.
const fetchTotalContributorsCount = async (client, owner, repo) => {
var _a, _b;
const resp = await client.request("GET /repos/{owner}/{repo}/contributors{?per_page}", { owner, repo, per_page: 1 });
/* istanbul ignore next: hard to test as it relies on GitHub API */
if (!resp.headers.link) {
return 0;
}
const links = (0, parse_link_header_1.default)(resp.headers.link);
/* istanbul ignore next: hard to test as it relies on GitHub API */
if (!links) {
return 0;
}
/* istanbul ignore next: hard to test as it relies on GitHub API */
return parseInt((_b = (_a = links.last) === null || _a === void 0 ? void 0 : _a.page) !== null && _b !== void 0 ? _b : "0");
};
// List repository contributors using v3 GitHub API, See https://docs.github.com/en/rest/reference/repos#list-repository-contributors.
const fetchContributors = async (client, owner, repo, fetchUntil) => {
let contributors = [];
for (let page = 1; contributors.length < fetchUntil; page++) {
const resp = await client.request("GET /repos/{owner}/{repo}/contributors", { owner, repo, page });
// No more contributors.
if (resp.data.length <= 0) {
return contributors;
}
contributors = contributors.concat(resp.data
.filter((contributor) => contributor.type !== "Bot")
.reduce((result, { id, login }) =>
/* istanbul ignore next: hard to test as it relies on GitHub API */
id && login ? result.concat({ id, name: login }) : result, []));
}
/* istanbul ignore next */
return contributors.slice(0, fetchUntil);
};
/** Displays a contributors section from a package.json file. */
exports.ContributorsSectionFromPkg = (0, jsx_md_1.awaitComponent)(async ({ title = "💼 Contributors", pkg, githubAccessToken = process.env.GITHUB_TOKEN, contributorsMaxDisplayLength = 12, }) => {
const repoData = (0, extractGithubOwnerAndRepo_1.extractGithubOwnerAndRepo)(pkg.repository);
if (repoData === undefined) {
return null;
}
const { repo, owner } = repoData;
const octokit = new core_1.Octokit({ auth: githubAccessToken });
const totalCountributors = await fetchTotalContributorsCount(octokit, owner, repo);
const contributors = await fetchContributors(octokit, owner, repo, contributorsMaxDisplayLength);
return ((0, jsx_md_1.default)(components_1.ContributorsSection, { title: title, repo: repo, owner: owner, contributors: contributors, additionalContributorsCount: totalCountributors - contributors.length }));
});
//# sourceMappingURL=ContributorsSectionFromPkg.js.map