MCPcopy Create free account
hub / github.com/craftreactnative/ui / downloadFromGitHub

Function downloadFromGitHub

cli/src/utils/github-downloader.ts:158–224  ·  view source on GitHub ↗
(
  options: DownloadOptions = {}
)

Source from the content-addressed store, hash-verified

156 * Downloads and extracts the craftrn-ui directory from GitHub
157 */
158export async function downloadFromGitHub(
159 options: DownloadOptions = {}
160): Promise<string> {
161 const branch = options.branch || DEFAULT_BRANCH;
162 const cachePath = getCachePath(branch);
163 const craftrnUiPath = path.join(cachePath, "demo-app", "craftrn-ui");
164
165 const spinner = options.silent
166 ? null
167 : ora("Checking cache...").start();
168
169 // Check if cache exists and is fresh
170 if (await fs.pathExists(craftrnUiPath)) {
171 if (spinner) spinner.text = "Checking cache...";
172 const stale = await isCacheStale(branch, options.forceLatest || false);
173 if (!stale) {
174 if (spinner) spinner.succeed("Using cached components");
175 return craftrnUiPath;
176 }
177 // Cache is stale (older than 1 day), remove it
178 if (spinner) spinner.text = "Cache is outdated (older than 1 day), downloading latest...";
179 await fs.remove(cachePath);
180 }
181
182 if (spinner) spinner.text = "Downloading components from GitHub...";
183
184 const tarballUrl = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/archive/refs/heads/${branch}.tar.gz`;
185 const tempTarPath = path.join(cachePath, "archive.tar.gz");
186 const extractPath = path.join(cachePath, "extracted");
187
188 await fs.ensureDir(cachePath);
189
190 // Download tarball
191 await downloadFile(tarballUrl, tempTarPath, spinner);
192
193 // Extract tarball
194 await fs.ensureDir(extractPath);
195 await tar.extract({ file: tempTarPath, cwd: extractPath });
196 await fs.remove(tempTarPath);
197
198 // Move extracted directory to final location
199 const extractedRepoPath = path.join(
200 extractPath,
201 `${GITHUB_REPO}-${branch}`,
202 "demo-app",
203 "craftrn-ui"
204 );
205
206 if (!(await fs.pathExists(extractedRepoPath))) {
207 if (spinner) spinner.fail("Failed to extract components");
208 throw new Error("Failed to extract craftrn-ui directory from tarball");
209 }
210
211 const finalPath = path.join(cachePath, "demo-app", "craftrn-ui");
212 await fs.ensureDir(path.dirname(finalPath));
213 if (await fs.pathExists(finalPath)) {
214 await fs.remove(finalPath);
215 }

Callers 1

Calls 4

getCachePathFunction · 0.85
isCacheStaleFunction · 0.85
downloadFileFunction · 0.85
saveCacheMetadataFunction · 0.85

Tested by

no test coverage detected