Skip to content

Commit

Permalink
feat: add optional "tempDirectory" download request option (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykuzmin authored and MarshallOfSound committed Nov 16, 2019
1 parent 49a3181 commit 790db6a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Cache } from './Cache';
import { getDownloaderForSystem } from './downloader-resolver';
import { initializeProxy } from './proxy';
import {
withTempDirectory,
withTempDirectoryIn,
normalizeVersion,
getHostArch,
ensureIsTruthyString,
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function downloadArtifact(
console.warn('For more info: https://electronjs.org/blog/linux-32bit-support');
}

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

const downloader = artifactDetails.downloader || (await getDownloaderForSystem());
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export interface ElectronDownloadRequestOptions {
* built-in [[GotDownloader]].
*/
downloader?: Downloader<any>;
/**
* A temporary directory for downloads.
* It is used before artifacts are put into cache.
*/
tempDirectory?: string;
}

export type ElectronPlatformArtifactDetails = {
Expand Down
29 changes: 18 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@ import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';

export async function withTempDirectory<T>(fn: (directory: string) => Promise<T>): Promise<T> {
const directory = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-download-'));

async function useAndRemoveDirectory<T>(
directory: string,
fn: (directory: string) => Promise<T>,
): Promise<T> {
let result: T;
try {
result = await fn(directory);
} catch (err) {
await fs.remove(directory);
throw err;
}

try {
} finally {
await fs.remove(directory);
} catch {
// Ignore error, it's just a temp dir
}
return result;
}

export async function withTempDirectoryIn<T>(
parentDirectory: string = os.tmpdir(),
fn: (directory: string) => Promise<T>,
): Promise<T> {
const tempDirectoryPrefix = 'electron-download-';
const tempDirectory = await fs.mkdtemp(path.resolve(parentDirectory, tempDirectoryPrefix));
return useAndRemoveDirectory(tempDirectory, fn);
}

export async function withTempDirectory<T>(fn: (directory: string) => Promise<T>): Promise<T> {
return withTempDirectoryIn(undefined, fn);
}

export function normalizeVersion(version: string) {
if (!version.startsWith('v')) {
return `v${version}`;
Expand Down

0 comments on commit 790db6a

Please sign in to comment.