Skip to content

Commit

Permalink
Merge pull request #221 from electron/ddramone/custom-version
Browse files Browse the repository at this point in the history
fix: support npmrc for custom-version env variable
  • Loading branch information
felixrieseberg authored Sep 9, 2022
2 parents a8bb6f1 + cd8eb66 commit f0cb42f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/artifact-utils.ts
Original file line number Diff line number Diff line change
@@ -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/';
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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));
}
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,7 +16,6 @@ import { getDownloaderForSystem } from './downloader-resolver';
import { initializeProxy } from './proxy';
import {
withTempDirectoryIn,
normalizeVersion,
getHostArch,
getNodeArch,
ensureIsTruthyString,
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
1 change: 0 additions & 1 deletion test/checksums.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
16 changes: 16 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f0cb42f

Please sign in to comment.