chore: resolve Firefox revisions for the latest keyword (#9708)

This commit is contained in:
Alex Rudenko 2023-02-21 08:27:02 +01:00 committed by GitHub
parent e0330d82bd
commit f1b337e78b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import ProgressBar from 'progress';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers';
import {resolveRevision} from './browsers/browsers.js';
import {Browser, BrowserPlatform} from './browsers/types.js';
import {fetch} from './fetch.js';
import {computeExecutablePath, launch} from './launcher.js';
@ -67,6 +68,10 @@ export class CLI {
},
async argv => {
const args = argv as unknown as InstallArgs;
args.browser.revision = await resolveRevision(
args.browser.name,
args.browser.revision
);
await fetch({
browser: args.browser.name,
revision: args.browser.revision,
@ -77,6 +82,9 @@ export class CLI {
args.browser.revision
),
});
console.log(
`${args.browser.name}@${args.browser.revision} downloaded successfully.`
);
}
)
.option('path', {

View File

@ -16,7 +16,7 @@
import * as chrome from './chrome.js';
import * as firefox from './firefox.js';
import {Browser, BrowserPlatform} from './types.js';
import {Browser, BrowserPlatform, BrowserTag} from './types.js';
export const downloadUrls = {
[Browser.CHROME]: chrome.resolveDownloadUrl,
@ -29,3 +29,18 @@ export const executablePathByBrowser = {
};
export {Browser, BrowserPlatform};
export async function resolveRevision(
browser: Browser,
tag: string
): Promise<string> {
switch (browser) {
case Browser.FIREFOX:
switch (tag as BrowserTag) {
case BrowserTag.LATEST:
return await firefox.resolveRevision('FIREFOX_NIGHTLY');
}
}
// We assume the tag is the revision if it didn't match any keywords.
return tag;
}

View File

@ -16,6 +16,8 @@
import path from 'path';
import {httpRequest} from '../httpUtil.js';
import {BrowserPlatform} from './types.js';
function archive(platform: BrowserPlatform, revision: string): string {
@ -54,3 +56,35 @@ export function relativeExecutablePath(
return path.join('firefox', 'firefox.exe');
}
}
export async function resolveRevision(
channel: 'FIREFOX_NIGHTLY' = 'FIREFOX_NIGHTLY'
): Promise<string> {
return new Promise((resolve, reject) => {
const request = httpRequest(
new URL('https://product-details.mozilla.org/1.0/firefox_versions.json'),
'GET',
response => {
let data = '';
if (response.statusCode && response.statusCode >= 400) {
return reject(new Error(`Got status code ${response.statusCode}`));
}
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
try {
const versions = JSON.parse(data);
return resolve(versions[channel]);
} catch {
return reject(new Error('Firefox version not found'));
}
});
},
false
);
request.on('error', err => {
reject(err);
});
});
}

View File

@ -41,3 +41,7 @@ export const downloadUrls = {
[Browser.CHROME]: chrome.resolveDownloadUrl,
[Browser.FIREFOX]: firefox.resolveDownloadUrl,
};
export enum BrowserTag {
LATEST = 'latest',
}

View File

@ -22,7 +22,7 @@ import path from 'path';
import {CLI} from '../../lib/cjs/CLI.js';
describe('CLI', function () {
this.timeout(60000);
this.timeout(90000);
let tmpDir = '/tmp/puppeteer-browsers-test';
const testChromeRevision = '1083080';
@ -72,4 +72,15 @@ describe('CLI', function () {
)
);
});
it('should download latest Firefox binaries', async () => {
await new CLI(tmpDir).run([
'npx',
'@puppeteer/browsers',
'install',
`firefox@latest`,
`--path=${tmpDir}`,
'--platform=linux',
]);
});
});