Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] Update download function to cache builds #156

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### 🎉 New features

- Add support for launching Expo updates. ([#134](https://github.com/expo/orbit/pull/134), [#137](https://github.com/expo/orbit/pull/137), [#138](https://github.com/expo/orbit/pull/138), [#144](https://github.com/expo/orbit/pull/144), [#148](https://github.com/expo/orbit/pull/148) by [@gabrieldonadel](https://github.com/gabrieldonadel))
- Cache builds by default. ([#156](https://github.com/expo/orbit/pull/156) by [@gabrieldonadel](https://github.com/gabrieldonadel))

### 🐛 Bug fixes

Expand Down
35 changes: 21 additions & 14 deletions packages/eas-shared/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,27 @@ async function downloadFileWithProgressTrackerAsync(
}
}

async function maybeCacheAppAsync(appPath: string, cachedAppPath?: string): Promise<string> {
if (cachedAppPath) {
await fs.ensureDir(path.dirname(cachedAppPath));
await fs.move(appPath, cachedAppPath);
return cachedAppPath;
}
return appPath;
function _downloadsCacheDirectory() {
const dir = path.join(getTmpDirectory(), 'downloads-cache');
fs.mkdirpSync(dir);
return dir;
}

export async function downloadAndMaybeExtractAppAsync(
url: string,
cachedAppPath?: string
): Promise<string> {
const outputDir = path.join(getTmpDirectory(), uuidv4());
export async function downloadAndMaybeExtractAppAsync(url: string): Promise<string> {
const name = encodeURIComponent(url.replace(/^[^:]+:\/\//, ''));

const outputDir = path.join(_downloadsCacheDirectory(), `${name}`);
if (await fs.pathExists(outputDir)) {
try {
const appPath = await getAppPathAsync(outputDir, '(apk|app|ipa)');
return appPath;
} catch (error) {
if (error instanceof InternalError) {
throw error;
}
}
}

await fs.promises.mkdir(outputDir, { recursive: true });

if (url.endsWith('apk')) {
Expand All @@ -139,7 +146,7 @@ export async function downloadAndMaybeExtractAppAsync(
(ratio, total) => `Downloading app (${formatBytes(total * ratio)} / ${formatBytes(total)})`,
'Successfully downloaded app'
);
return maybeCacheAppAsync(apkFilePath, cachedAppPath);
return apkFilePath;
} else {
const tmpArchivePathDir = path.join(getTmpDirectory(), uuidv4());
await fs.mkdir(tmpArchivePathDir, { recursive: true });
Expand All @@ -157,7 +164,7 @@ export async function downloadAndMaybeExtractAppAsync(

const appPath = await getAppPathAsync(outputDir, '(apk|app|ipa)');

return maybeCacheAppAsync(appPath, cachedAppPath);
return appPath;
}
}

Expand Down
Loading