|
| 1 | +import { dirname, join } from 'path' |
| 2 | +import { Octokit } from '@octokit/rest' |
| 3 | +import { writeFile } from './fs.js' |
| 4 | + |
| 5 | +/** |
| 6 | + * @typedef {import('./parse.js').SourceItem} Repo |
| 7 | + */ |
| 8 | + |
| 9 | +const OK = 200 |
| 10 | +const { GITHUB_TOKEN } = process.env |
| 11 | +const client = new Octokit({ |
| 12 | + auth: GITHUB_TOKEN ? `token ${GITHUB_TOKEN}` : '' |
| 13 | +}) |
| 14 | + |
| 15 | +/** |
| 16 | + * @typedef RepoFile |
| 17 | + * @property {string} path - File path. |
| 18 | + * @property {string} name - File name. |
| 19 | + * @property {string} sha - The SHA of the file. |
| 20 | + * @property {string} download_url - Download url. |
| 21 | + * @property {string} html_url - Html url. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Get files list from repository. |
| 26 | + * @param {Repo} repo |
| 27 | + * @param {string} path |
| 28 | + * @returns {Promise<RepoFile[]>} File from the repository. |
| 29 | + */ |
| 30 | +export async function getDirectory(repo, path) { |
| 31 | + let status |
| 32 | + let data |
| 33 | + |
| 34 | + try { |
| 35 | + ({ |
| 36 | + status, |
| 37 | + data |
| 38 | + } = await client.request('GET /repos/{owner}/{repo}/contents/{path}', { |
| 39 | + owner: repo.owner, |
| 40 | + repo: repo.repo, |
| 41 | + path: path === '.' ? '' : path |
| 42 | + })) |
| 43 | + } catch (err) { |
| 44 | + return [] |
| 45 | + } |
| 46 | + |
| 47 | + if (status !== OK || !data) { |
| 48 | + return [] |
| 49 | + } |
| 50 | + |
| 51 | + return data |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Get files filtered by sha from repository. |
| 56 | + * @param {Repo[]} repos |
| 57 | + * @param {string[]} paths |
| 58 | + * @returns {Promise<Map<string, RepoFile[]>>} Files from the repository. |
| 59 | + */ |
| 60 | +export async function getFiles(repos, paths) { |
| 61 | + const dirs = new Set() |
| 62 | + const tasks = [] |
| 63 | + const shas = [] |
| 64 | + const filesMap = new Map() |
| 65 | + let task |
| 66 | + let dir |
| 67 | + let file |
| 68 | + let sha |
| 69 | + let files |
| 70 | + |
| 71 | + paths.forEach((path) => { |
| 72 | + dirs.add(dirname(path)) |
| 73 | + }) |
| 74 | + |
| 75 | + repos.forEach((repo) => { |
| 76 | + dirs.forEach((dir) => { |
| 77 | + tasks.push(getDirectory(repo, dir)) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + for (task of tasks) { |
| 82 | + dir = await task |
| 83 | + |
| 84 | + for (file of dir) { |
| 85 | + if (paths.includes(file.path)) { |
| 86 | + sha = file.sha + file.path |
| 87 | + |
| 88 | + if (!shas.includes(sha)) { |
| 89 | + shas.push(sha) |
| 90 | + |
| 91 | + files = filesMap.get(file.path) |
| 92 | + |
| 93 | + if (!files) { |
| 94 | + files = [] |
| 95 | + filesMap.set(file.path, files) |
| 96 | + } |
| 97 | + |
| 98 | + files.push(file) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return filesMap |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Download file. |
| 109 | + * @param {RepoFile} file |
| 110 | + * @param {boolean | string} write |
| 111 | + * @returns {Promise<string>} File contents. |
| 112 | + */ |
| 113 | +export async function downloadFile(file, write) { |
| 114 | + const { data } = await client.request(`GET ${file.download_url}`) |
| 115 | + |
| 116 | + if (write) { |
| 117 | + await writeFile( |
| 118 | + join( |
| 119 | + write === true |
| 120 | + ? '.' |
| 121 | + : write, |
| 122 | + file.path |
| 123 | + ), |
| 124 | + data |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + return data |
| 129 | +} |
0 commit comments