Skip to content

Commit eb52c3b

Browse files
author
John Kleinschmidt
authored
feat: add environment variable to override version downloaded (#155)
* feat: add environment variable to override version downloaded * Fixup test and remove logging
1 parent 9521f51 commit eb52c3b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ const zipFilePath = await download('4.0.4', {
8787
// Will download from https://mirror.example.com/electron/version-4.0.4/electron-v4.0.4-linux-x64.zip
8888
```
8989

90+
#### Using environment variables for mirror options
91+
Mirror options can also be specified via the following environment variables:
92+
* `ELECTRON_CUSTOM_DIR` - Specifies the custom directory to download from.
93+
* `ELECTRON_CUSTOM_FILENAME` - Specifies the custom file name to download.
94+
* `ELECTRON_MIRROR` - Specifies the URL of the server to download from if the version is not a nightly version.
95+
* `ELECTRON_NIGHTLY_MIRROR` - Specifies the URL of the server to download from if the version is a nightly version.
96+
97+
### Overriding the version downloaded
98+
99+
The version downloaded can be overriden by setting the `ELECTRON_CUSTOM_VERSION` environment variable.
100+
Setting this environment variable will override the version passed in to `download` or `downloadArtifact`.
101+
90102
## How It Works
91103

92104
This module downloads Electron to a known place on your system and caches it

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ export async function downloadArtifact(
7676
}
7777
}
7878
ensureIsTruthyString(artifactDetails, 'version');
79-
artifactDetails.version = normalizeVersion(artifactDetails.version);
8079

80+
artifactDetails.version = normalizeVersion(
81+
process.env.ELECTRON_CUSTOM_VERSION || artifactDetails.version,
82+
);
8183
const fileName = getArtifactFileName(artifactDetails);
8284
const url = getArtifactRemoteURL(artifactDetails);
8385
const cache = new Cache(artifactDetails.cacheRoot);
@@ -134,7 +136,6 @@ export async function downloadArtifact(
134136
downloader: artifactDetails.downloader,
135137
mirrorOptions: artifactDetails.mirrorOptions,
136138
});
137-
138139
await sumchecker('sha256', shasumPath, path.dirname(tempDownloadPath), [
139140
path.basename(tempDownloadPath),
140141
]);

test/index.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ describe('Public API', () => {
112112
downloadOptions: downloadOpts,
113113
});
114114
});
115+
116+
it('should download a custom version of a zip file', async () => {
117+
process.env.ELECTRON_CUSTOM_VERSION = '2.0.10';
118+
const zipPath = await download('2.0.3', {
119+
cacheRoot,
120+
downloader,
121+
});
122+
expect(typeof zipPath).toEqual('string');
123+
expect(await fs.pathExists(zipPath)).toEqual(true);
124+
expect(path.basename(zipPath)).toMatch(/v2.0.10/);
125+
expect(path.extname(zipPath)).toEqual('.zip');
126+
process.env.ELECTRON_CUSTOM_VERSION = '';
127+
});
115128
});
116129

117130
describe('downloadArtifact()', () => {
@@ -179,5 +192,22 @@ describe('Public API', () => {
179192
);
180193
expect(path.extname(driverPath)).toEqual('.zip');
181194
});
195+
196+
it('should download a custom version of a zip file', async () => {
197+
process.env.ELECTRON_CUSTOM_VERSION = '2.0.10';
198+
const zipPath = await downloadArtifact({
199+
artifactName: 'electron',
200+
cacheRoot,
201+
downloader,
202+
platform: 'darwin',
203+
arch: 'x64',
204+
version: '2.0.3',
205+
});
206+
expect(typeof zipPath).toEqual('string');
207+
expect(await fs.pathExists(zipPath)).toEqual(true);
208+
expect(path.basename(zipPath)).toMatchInlineSnapshot(`"electron-v2.0.10-darwin-x64.zip"`);
209+
expect(path.extname(zipPath)).toEqual('.zip');
210+
process.env.ELECTRON_CUSTOM_VERSION = '';
211+
});
182212
});
183213
});

0 commit comments

Comments
 (0)