All files / scripts/lib/readme readmeFallback.js

95% Statements 19/20
86.66% Branches 13/15
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  16x   12x 12x                           12x 11x 17x 17x 17x 13x     4x     16x  
#!/usr/bin/env node
let axios = null;
function getAxios() {
  Iif (axios) return axios;
  try { const _a = require('axios'); axios = _a && _a.default ? _a.default : _a; return axios; } catch (e) { axios = null; return null; }
}
 
/**
 * Fetches the raw README.md text for a repository by trying each branch in order.
 * Used as a fallback when the GraphQL response does not include the README object.
 *
 * @param {string} org - GitHub organization or username
 * @param {string} repo - Repository name
 * @param {string[]} [branches=['main','master']] - Branch names to try in order
 * @param {number} [timeout=8000] - Request timeout in milliseconds
 * @returns {Promise<string|null>} Raw README text, or null if all branches fail
 */
async function fetchReadmeFromRaw(org, repo, branches = ['main','master'], timeout = 8000) {
  const ax = getAxios(); if (!ax) return null;
  for (const br of branches) {
    try {
      const url = `https://raw.githubusercontent.com/${org}/${repo}/${br}/README.md`;
      const r = await ax.get(url, { responseType: 'text', timeout });
      if (r && r.status === 200 && r.data) return r.data;
    } catch (e) { /* ignore */ }
  }
  return null;
}
 
module.exports = { fetchReadmeFromRaw };