Skip to content

Commit

Permalink
feat: add environment variable to override version downloaded (#155)
Browse files Browse the repository at this point in the history
* feat: add environment variable to override version downloaded

* Fixup test and remove logging
  • Loading branch information
John Kleinschmidt authored Apr 8, 2020
1 parent 9521f51 commit eb52c3b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ const zipFilePath = await download('4.0.4', {
// Will download from https://mirror.example.com/electron/version-4.0.4/electron-v4.0.4-linux-x64.zip
```

#### Using environment variables for mirror options
Mirror options can also be specified via the following environment variables:
* `ELECTRON_CUSTOM_DIR` - Specifies the custom directory to download from.
* `ELECTRON_CUSTOM_FILENAME` - Specifies the custom file name to download.
* `ELECTRON_MIRROR` - Specifies the URL of the server to download from if the version is not a nightly version.
* `ELECTRON_NIGHTLY_MIRROR` - Specifies the URL of the server to download from if the version is a nightly version.

### Overriding the version downloaded

The version downloaded can be overriden by setting the `ELECTRON_CUSTOM_VERSION` environment variable.
Setting this environment variable will override the version passed in to `download` or `downloadArtifact`.

## How It Works

This module downloads Electron to a known place on your system and caches it
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ export async function downloadArtifact(
}
}
ensureIsTruthyString(artifactDetails, 'version');
artifactDetails.version = normalizeVersion(artifactDetails.version);

artifactDetails.version = normalizeVersion(
process.env.ELECTRON_CUSTOM_VERSION || artifactDetails.version,
);
const fileName = getArtifactFileName(artifactDetails);
const url = getArtifactRemoteURL(artifactDetails);
const cache = new Cache(artifactDetails.cacheRoot);
Expand Down Expand Up @@ -134,7 +136,6 @@ export async function downloadArtifact(
downloader: artifactDetails.downloader,
mirrorOptions: artifactDetails.mirrorOptions,
});

await sumchecker('sha256', shasumPath, path.dirname(tempDownloadPath), [
path.basename(tempDownloadPath),
]);
Expand Down
30 changes: 30 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ describe('Public API', () => {
downloadOptions: downloadOpts,
});
});

it('should download a custom version of a zip file', async () => {
process.env.ELECTRON_CUSTOM_VERSION = '2.0.10';
const zipPath = await download('2.0.3', {
cacheRoot,
downloader,
});
expect(typeof zipPath).toEqual('string');
expect(await fs.pathExists(zipPath)).toEqual(true);
expect(path.basename(zipPath)).toMatch(/v2.0.10/);
expect(path.extname(zipPath)).toEqual('.zip');
process.env.ELECTRON_CUSTOM_VERSION = '';
});
});

describe('downloadArtifact()', () => {
Expand Down Expand Up @@ -179,5 +192,22 @@ describe('Public API', () => {
);
expect(path.extname(driverPath)).toEqual('.zip');
});

it('should download a custom version of a zip file', async () => {
process.env.ELECTRON_CUSTOM_VERSION = '2.0.10';
const zipPath = await downloadArtifact({
artifactName: 'electron',
cacheRoot,
downloader,
platform: 'darwin',
arch: 'x64',
version: '2.0.3',
});
expect(typeof zipPath).toEqual('string');
expect(await fs.pathExists(zipPath)).toEqual(true);
expect(path.basename(zipPath)).toMatchInlineSnapshot(`"electron-v2.0.10-darwin-x64.zip"`);
expect(path.extname(zipPath)).toEqual('.zip');
process.env.ELECTRON_CUSTOM_VERSION = '';
});
});
});

0 comments on commit eb52c3b

Please sign in to comment.