diff --git a/src/artifact-utils.ts b/src/artifact-utils.ts index c43dd62d9..bf574a4ad 100644 --- a/src/artifact-utils.ts +++ b/src/artifact-utils.ts @@ -1,5 +1,5 @@ import { ElectronArtifactDetails, MirrorOptions } from './types'; -import { ensureIsTruthyString } from './utils'; +import { ensureIsTruthyString, normalizeVersion } from './utils'; const BASE_URL = 'https://github.com/electron/electron/releases/download/'; const NIGHTLY_BASE_URL = 'https://github.com/electron/nightlies/releases/download/'; @@ -30,13 +30,18 @@ function mirrorVar( defaultValue: string, ): string { // Convert camelCase to camel_case for env var reading - const lowerName = name.replace(/([a-z])([A-Z])/g, (_, a, b) => `${a}_${b}`).toLowerCase(); + const snakeName = name.replace(/([a-z])([A-Z])/g, (_, a, b) => `${a}_${b}`).toLowerCase(); return ( - process.env[`NPM_CONFIG_ELECTRON_${lowerName.toUpperCase()}`] || - process.env[`npm_config_electron_${lowerName}`] || - process.env[`npm_package_config_electron_${lowerName}`] || - process.env[`ELECTRON_${lowerName.toUpperCase()}`] || + // .npmrc + process.env[`npm_config_electron_${name.toLowerCase()}`] || + process.env[`NPM_CONFIG_ELECTRON_${snakeName.toUpperCase()}`] || + process.env[`npm_config_electron_${snakeName}`] || + // package.json + process.env[`npm_package_config_electron_${name}`] || + process.env[`npm_package_config_electron_${snakeName.toLowerCase()}`] || + // env + process.env[`ELECTRON_${snakeName.toUpperCase()}`] || options[name] || defaultValue ); @@ -68,3 +73,7 @@ export async function getArtifactRemoteURL(details: ElectronArtifactDetails): Pr return `${base}${path}/${file}`; } + +export function getArtifactVersion(details: ElectronArtifactDetails): string { + return normalizeVersion(mirrorVar('customVersion', details.mirrorOptions || {}, details.version)); +} diff --git a/src/index.ts b/src/index.ts index 9d2a86850..a506da57b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import * as path from 'path'; import * as semver from 'semver'; import * as sumchecker from 'sumchecker'; -import { getArtifactFileName, getArtifactRemoteURL } from './artifact-utils'; +import { getArtifactFileName, getArtifactRemoteURL, getArtifactVersion } from './artifact-utils'; import { ElectronArtifactDetails, ElectronDownloadRequestOptions, @@ -16,7 +16,6 @@ import { getDownloaderForSystem } from './downloader-resolver'; import { initializeProxy } from './proxy'; import { withTempDirectoryIn, - normalizeVersion, getHostArch, getNodeArch, ensureIsTruthyString, @@ -61,9 +60,7 @@ export async function downloadArtifact( } ensureIsTruthyString(artifactDetails, 'version'); - artifactDetails.version = normalizeVersion( - process.env.ELECTRON_CUSTOM_VERSION || artifactDetails.version, - ); + artifactDetails.version = getArtifactVersion(artifactDetails); const fileName = getArtifactFileName(artifactDetails); const url = await getArtifactRemoteURL(artifactDetails); const cache = new Cache(artifactDetails.cacheRoot); diff --git a/src/types.ts b/src/types.ts index 561029659..438f552a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -27,6 +27,11 @@ export interface MirrorOptions { * e.g 'electron-v4.0.4-linux-x64.zip' */ customFilename?: string; + /** + * The version of the asset to download, + * e.g '4.0.4' + */ + customVersion?: string; /** * A function allowing customization of the url returned * from getArtifactRemoteURL(). diff --git a/test/checksums.spec.ts b/test/checksums.spec.ts index 827fe34a1..015c77277 100644 --- a/test/checksums.spec.ts +++ b/test/checksums.spec.ts @@ -1,4 +1,3 @@ -import * as crypto from 'crypto'; import * as fs from 'fs-extra'; import * as os from 'os'; import * as path from 'path'; diff --git a/test/index.spec.ts b/test/index.spec.ts index f5fc9adcd..58ed8c534 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -215,6 +215,22 @@ describe('Public API', () => { process.env.ELECTRON_CUSTOM_VERSION = ''; }); + it('should download a custom version specified via mirror options', async () => { + const zipPath = await downloadArtifact({ + artifactName: 'electron', + cacheRoot, + downloader, + platform: 'darwin', + arch: 'x64', + version: '2.0.3', + mirrorOptions: { + customVersion: '2.0.10', + }, + }); + expect(typeof zipPath).toEqual('string'); + expect(path.basename(zipPath)).toMatchInlineSnapshot(`"electron-v2.0.10-darwin-x64.zip"`); + }); + describe('sumchecker', () => { beforeEach(() => { jest.clearAllMocks();