Skip to content

Commit 790db6a

Browse files
alexeykuzminMarshallOfSound
authored andcommitted
feat: add optional "tempDirectory" download request option (#127)
1 parent 49a3181 commit 790db6a

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Cache } from './Cache';
1111
import { getDownloaderForSystem } from './downloader-resolver';
1212
import { initializeProxy } from './proxy';
1313
import {
14-
withTempDirectory,
14+
withTempDirectoryIn,
1515
normalizeVersion,
1616
getHostArch,
1717
ensureIsTruthyString,
@@ -99,7 +99,7 @@ export async function downloadArtifact(
9999
console.warn('For more info: https://electronjs.org/blog/linux-32bit-support');
100100
}
101101

102-
return await withTempDirectory(async tempFolder => {
102+
return await withTempDirectoryIn(artifactDetails.tempDirectory, async tempFolder => {
103103
const tempDownloadPath = path.resolve(tempFolder, getArtifactFileName(artifactDetails));
104104

105105
const downloader = artifactDetails.downloader || (await getDownloaderForSystem());

src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export interface ElectronDownloadRequestOptions {
6060
* built-in [[GotDownloader]].
6161
*/
6262
downloader?: Downloader<any>;
63+
/**
64+
* A temporary directory for downloads.
65+
* It is used before artifacts are put into cache.
66+
*/
67+
tempDirectory?: string;
6368
}
6469

6570
export type ElectronPlatformArtifactDetails = {

src/utils.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@ import * as fs from 'fs-extra';
33
import * as os from 'os';
44
import * as path from 'path';
55

6-
export async function withTempDirectory<T>(fn: (directory: string) => Promise<T>): Promise<T> {
7-
const directory = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-download-'));
8-
6+
async function useAndRemoveDirectory<T>(
7+
directory: string,
8+
fn: (directory: string) => Promise<T>,
9+
): Promise<T> {
910
let result: T;
1011
try {
1112
result = await fn(directory);
12-
} catch (err) {
13-
await fs.remove(directory);
14-
throw err;
15-
}
16-
17-
try {
13+
} finally {
1814
await fs.remove(directory);
19-
} catch {
20-
// Ignore error, it's just a temp dir
2115
}
2216
return result;
2317
}
2418

19+
export async function withTempDirectoryIn<T>(
20+
parentDirectory: string = os.tmpdir(),
21+
fn: (directory: string) => Promise<T>,
22+
): Promise<T> {
23+
const tempDirectoryPrefix = 'electron-download-';
24+
const tempDirectory = await fs.mkdtemp(path.resolve(parentDirectory, tempDirectoryPrefix));
25+
return useAndRemoveDirectory(tempDirectory, fn);
26+
}
27+
28+
export async function withTempDirectory<T>(fn: (directory: string) => Promise<T>): Promise<T> {
29+
return withTempDirectoryIn(undefined, fn);
30+
}
31+
2532
export function normalizeVersion(version: string) {
2633
if (!version.startsWith('v')) {
2734
return `v${version}`;

0 commit comments

Comments
 (0)