|
| 1 | +import { mkdtemp, readdir, stat, rm } from "fs/promises"; |
| 2 | +import { spawn } from "child_process"; |
| 3 | +import { CliFlags, RepoOwner } from "./types"; |
| 4 | +import { getRepoAndOwner, waitOnChildProcessToExit } from "./utils"; |
| 5 | +import ArrayStringMap from "array-string-map"; |
| 6 | +import filterAsync from "node-filter-async"; |
| 7 | +import { tmpdir } from "os"; |
| 8 | + |
| 9 | +export default async function handler(script: string, flags: CliFlags) { |
| 10 | + console.log(script, flags); |
| 11 | + const { owner: defaultOwner, _: repoNames } = flags; |
| 12 | + const repos: RepoOwner[] = []; |
| 13 | + for (const repoName of repoNames) { |
| 14 | + try { |
| 15 | + const [owner, repo] = getRepoAndOwner(repoName, defaultOwner); |
| 16 | + repos.push([owner, repo]); |
| 17 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- We need the `message` attribute. |
| 18 | + } catch (e: any) { |
| 19 | + console.error(e.message); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | + } |
| 23 | + const tempDir = await mkdtemp(`${tmpdir()}/github-run-script-`); |
| 24 | + try { |
| 25 | + const directoryMapping: ArrayStringMap<RepoOwner, string> = |
| 26 | + new ArrayStringMap(); |
| 27 | + const scanDirectories: Map<string, string[]> = new Map(); |
| 28 | + if (flags.searchPath) { |
| 29 | + // If it's one path, it won't be in an array. This converts it into an array. |
| 30 | + if (typeof flags.searchPath === "string") { |
| 31 | + flags.searchPath = [flags.searchPath]; |
| 32 | + } |
| 33 | + // What this code block does is simple. It stores in `scanDirectories` |
| 34 | + // a mapping of source path name to an array of directories in that directory. |
| 35 | + await Promise.all( |
| 36 | + flags.searchPath.map(async (path) => { |
| 37 | + scanDirectories.set( |
| 38 | + path, |
| 39 | + await filterAsync(await readdir(path), async (item) => { |
| 40 | + return (await stat(`${path}/${item}`)).isDirectory(); |
| 41 | + }) |
| 42 | + ); |
| 43 | + }) |
| 44 | + ); |
| 45 | + } |
| 46 | + await Promise.all( |
| 47 | + repos.map(async ([owner, repo]) => { |
| 48 | + // First, we need to check if the repository exists in `scanDirectories`. |
| 49 | + // TODO: Handle cases where the same repo is present multiple times in |
| 50 | + // TODO: different directories, or if two repos with the same name but |
| 51 | + // TODO: different owners is provided. (Maybe we can check `.git`.) |
| 52 | + for (const [path, directories] of scanDirectories) { |
| 53 | + for (const directory of directories) { |
| 54 | + if (repo === directory) { |
| 55 | + directoryMapping.set([owner, repo], path); |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + // If we already found a match earlier, no need to re-iterate over the other |
| 60 | + // directories. |
| 61 | + if (directoryMapping.has([owner, repo])) { |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + // Deal wit the special case where we did not find a match. Time to clone. |
| 66 | + if (!directoryMapping.has([owner, repo])) { |
| 67 | + const destPath = `${tempDir}/${repo}`; |
| 68 | + console.log(`Cloning ${owner}/${repo} to ${destPath}`); |
| 69 | + const childProc = await spawn( |
| 70 | + "git", |
| 71 | + ["clone", `https://github.com/${owner}/${repo}.git`, destPath], |
| 72 | + { stdio: "inherit" } |
| 73 | + ); |
| 74 | + await waitOnChildProcessToExit(childProc); |
| 75 | + directoryMapping.set([owner, repo], destPath); |
| 76 | + } |
| 77 | + // Time to execute the script! |
| 78 | + const path = directoryMapping.get([owner, repo]); |
| 79 | + const childProc = await spawn(script, [], { |
| 80 | + cwd: path, |
| 81 | + env: { |
| 82 | + ...process.env, |
| 83 | + REPO_OWNER: owner, |
| 84 | + REPO_NAME: repo, |
| 85 | + REPO: `${owner}/${repo}`, |
| 86 | + REPO_PATH: path, |
| 87 | + }, |
| 88 | + stdio: "inherit", |
| 89 | + }); |
| 90 | + await waitOnChildProcessToExit(childProc); |
| 91 | + }) |
| 92 | + ); |
| 93 | + } finally { |
| 94 | + // We need to clean up the temporary directory. |
| 95 | + await rm(tempDir, { recursive: true, force: true }); |
| 96 | + } |
| 97 | +} |
0 commit comments