Skip to content

Commit

Permalink
feat: allow only passing a base URL to downloadArtifact (#156)
Browse files Browse the repository at this point in the history
In some cases the url passed with a mirror will be fully-formed and not have path/name at the end, meaning that the current logic will tack it on and cause failures. This fixes that by optionally allowing only the base url to be passed to got for download.
  • Loading branch information
codebytere authored May 6, 2020
1 parent e847d27 commit 8857e40
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ const zipFilePath = await downloadArtifact({

### Specifying a mirror

To specify another location to download Electron assets from, the following options are
available:

* `mirrorOptions` Object
* `mirror` String (optional) - The base URL of the mirror to download from.
* `nightly_mirror` String (optional) - The Electron nightly-specific mirror URL.
* `customDir` String (optional) - The name of the directory to download from, often scoped by version number.
* `customFilename` String (optional) - The name of the asset to download.
* `baseOnly` Boolean (optional) - Whether to download from the base URL only.

Anatomy of a download URL, in terms of `mirrorOptions`:

```
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"prettier": "^1.17.1",
"semantic-release": "^15.13.12",
"ts-jest": "^24.0.0",
"typedoc": "^0.14.2",
"typescript": "^3.4.5"
"typedoc": "^0.17.2",
"typescript": "^3.8.0"
},
"lint-staged": {
"*.ts": [
Expand Down
8 changes: 6 additions & 2 deletions src/artifact-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export function getArtifactFileName(details: ElectronArtifactDetails) {
].join('-')}.zip`;
}

function mirrorVar(name: keyof MirrorOptions, options: MirrorOptions, defaultValue: string) {
function mirrorVar(
name: keyof Omit<MirrorOptions, 'baseOnly'>,
options: MirrorOptions,
defaultValue: string,
) {
// Convert camelCase to camel_case for env var reading
const lowerName = name.replace(/([a-z])([A-Z])/g, (_, a, b) => `${a}_${b}`).toLowerCase();

Expand All @@ -50,5 +54,5 @@ export function getArtifactRemoteURL(details: ElectronArtifactDetails): string {
);
const file = mirrorVar('customFilename', opts, getArtifactFileName(details));

return `${base}${path}/${file}`;
return opts.baseOnly ? `${base}` : `${base}${path}/${file}`;
}
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,26 @@ export interface MirrorOptions {
* The Electron nightly-specific mirror URL.
*/
nightly_mirror?: string;
/**
* The base URL of the mirror to download from,
* e.g https://github.com/electron/electron/releases/download
*/
mirror?: string;
/**
* The name of the directory to download from,
* often scoped by version number e.g 'v4.0.4'
*/
customDir?: string;
/**
* The name of the asset to download,
* e.g 'electron-v4.0.4-linux-x64.zip'
*/
customFilename?: string;
/**
* Whether to download from the base URL only,
* ignoring customDir and customFilename
*/
baseOnly?: boolean;
}

export interface ElectronDownloadRequest {
Expand Down
17 changes: 17 additions & 0 deletions test/artifact-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ describe('artifact-utils', () => {
).toMatchInlineSnapshot(`"https://mirror.example.com/v6.0.0/electron-v6.0.0-linux-x64.zip"`);
});

it('should allow for setting only a base url when mirrorOptions.baseOnly is set', () => {
expect(
getArtifactRemoteURL({
arch: 'x64',
artifactName: 'electron',
mirrorOptions: {
mirror: 'https://mirror.example.com',
customDir: 'v1.2.3',
customFilename: 'custom-built-electron.zip',
baseOnly: true,
},
platform: 'linux',
version: 'v6.0.0',
}),
).toMatchInlineSnapshot(`"https://mirror.example.com"`);
});

it('should replace the nightly base URL when mirrorOptions.nightly_mirror is set', () => {
expect(
getArtifactRemoteURL({
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7004,10 +7004,10 @@ [email protected]:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.4.tgz#c585cb952912263d915b462726ce244ba510ef3d"
integrity sha512-0RNDbSdEokBeEAkgNbxJ+BLwSManFy9TeXz8uW+48j/xhEXv1ePME60olyzw2XzUqUBNAYFeJadIqAgNqIACwg==

typescript@^3.4.5:
version "3.4.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==
typescript@^3.8.0:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==

uglify-js@^3.1.4:
version "3.7.0"
Expand Down

0 comments on commit 8857e40

Please sign in to comment.