( repository: string, ref: string, destDir: string, subDir?: string, )
| 21 | * @return Absolute path to the checked‑out source directory |
| 22 | */ |
| 23 | export async function getRemoteSource( |
| 24 | repository: string, |
| 25 | ref: string, |
| 26 | destDir: string, |
| 27 | subDir?: string, |
| 28 | ): Promise<string> { |
| 29 | logger.debug( |
| 30 | `Downloading remote source: ${repository}@${ref} (destDir: ${destDir}, subDir: ${subDir || "."})`, |
| 31 | ); |
| 32 | |
| 33 | const gitHubInfo = parseGitHubUrl(repository); |
| 34 | if (!gitHubInfo) { |
| 35 | throw new FirebaseError( |
| 36 | `Could not parse GitHub repository URL: ${repository}. ` + |
| 37 | `Only GitHub repositories are supported.`, |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | let rootDir = destDir; |
| 42 | try { |
| 43 | logger.debug(`Attempting to download via GitHub Archive API for ${repository}@${ref}...`); |
| 44 | const archiveUrl = `https://github.com/${gitHubInfo.owner}/${gitHubInfo.repo}/archive/${ref}.zip`; |
| 45 | const archivePath = await downloadUtils.downloadToTmp(archiveUrl); |
| 46 | logger.debug(`Downloaded archive to ${archivePath}, unzipping...`); |
| 47 | |
| 48 | await unzipModule.unzip(archivePath, destDir); |
| 49 | |
| 50 | // GitHub archives usually wrap content in a top-level directory (e.g. repo-ref). |
| 51 | // We need to find it and use it as the root. |
| 52 | const files = fs.readdirSync(destDir); |
| 53 | |
| 54 | if (files.length === 1 && fs.statSync(path.join(destDir, files[0])).isDirectory()) { |
| 55 | rootDir = path.join(destDir, files[0]); |
| 56 | logger.debug(`Found top-level directory in archive: ${files[0]}`); |
| 57 | } |
| 58 | } catch (err: unknown) { |
| 59 | throw new FirebaseError( |
| 60 | `Failed to download GitHub archive for ${repository}@${ref}. ` + |
| 61 | `Make sure the repository is public and the ref exists. ` + |
| 62 | `Private repositories are not supported via this method.`, |
| 63 | { original: err as Error }, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | const sourceDir = subDir |
| 68 | ? resolveWithin( |
| 69 | rootDir, |
| 70 | subDir, |
| 71 | `Subdirectory '${subDir}' in remote source must not escape the repository root.`, |
| 72 | ) |
| 73 | : rootDir; |
| 74 | |
| 75 | if (subDir && !dirExistsSync(sourceDir)) { |
| 76 | throw new FirebaseError(`Directory '${subDir}' not found in repository ${repository}@${ref}`); |
| 77 | } |
| 78 | |
| 79 | const origin = `${repository}@${ref}${subDir ? `/${subDir}` : ""}`; |
| 80 | logLabeledBullet("functions", `downloaded remote source (${origin})`); |
no test coverage detected
searching dependent graphs…