Skip to content

Commit

Permalink
Move platform resolution to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
zabil committed May 14, 2024
1 parent e4d6ce8 commit 72d0e0e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
22 changes: 5 additions & 17 deletions lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Few modifications are done on the file.
*/

const { platform: _platform, arch } = require('os');
const fs = require('fs-extra');
const { readdir, access, removeSync, existsSync } = fs;
const { join, basename } = require('path');
Expand All @@ -29,7 +28,6 @@ const { parse } = require('url');
const BrowserMetadata = require('./metadata');

const supportedPlatforms = ['mac-arm64', 'mac-x64', 'linux', 'win32', 'win64'];
const revisionInfo = new BrowserMetadata().revisionInfo();

const readdirAsync = promisify(readdir.bind(fs));

Expand All @@ -44,20 +42,10 @@ class Browser {
/**
* @param {!Browser.Options=} options
*/
constructor() {
constructor(browserMetadata = new BrowserMetadata()) {
this._downloadsFolder = join(helper.projectRoot(), '.local-chromium');
if (!this._platform) {
const platform = _platform();
if (platform === 'darwin') {
this._platform = arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
} else if (platform === 'linux') {
this._platform = 'linux';
} else if (platform === 'win32') {
this._platform = arch() === 'x64' ? 'win64' : 'win32';
}
assert(this._platform, 'Unsupported platform: ' + _platform());
}
assert(supportedPlatforms.includes(this._platform), 'Unsupported platform: ' + this._platform);
this._platform = browserMetadata.platform();
this.revisionInfo = BrowserMetadata.revisionInfo();
}

/**
Expand Down Expand Up @@ -114,11 +102,11 @@ class Browser {
};
}

const missingText = !revisionInfo.local
const missingText = !this.revisionInfo.local
? 'Chromium revision is not downloaded. Provide TAIKO_BROWSER_PATH or Install taiko again to download bundled chromium.'
: null;
return {
executablePath: revisionInfo.executablePath,
executablePath: this.revisionInfo.executablePath,
missingText,
};
}
Expand Down
26 changes: 8 additions & 18 deletions lib/browser/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Few modifications are done on the file.
*/

const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const extract = require('extract-zip');
Expand All @@ -27,7 +26,6 @@ const URL = require('url');
const { helper, assert } = require('../helper');
const ProxyAgent = require('https-proxy-agent');
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
const supportedPlatforms = ['mac-arm64', 'mac-x64', 'linux', 'win32', 'win64'];

const mkdirAsync = util.promisify(fs.mkdir.bind(fs));
const unlinkAsync = util.promisify(fs.unlink.bind(fs));
Expand All @@ -45,19 +43,11 @@ class BrowserFetcher {
/**
* @param {!BrowserFetcher.Options=} options
*/
constructor() {
constructor(metadata = new BrowserMetadata()) {
this._downloadsFolder = path.join(helper.projectRoot(), '.local-chromium');
this.browserMetadata = new BrowserMetadata();
const platform = os.platform();
if (platform === 'darwin') {
this._platform = os.arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
} else if (platform === 'linux') {
this._platform = 'linux';
} else if (platform === 'win32') {
this._platform = os.arch() === 'x64' ? 'win64' : 'win32';
}
assert(this._platform, 'Unsupported platform: ' + os.platform());
assert(supportedPlatforms.includes(this._platform), 'Unsupported platform: ' + this._platform);
this.downloadURL = metadata.downloadURL();
this.revisionInfo = metadata.revisionInfo();
this._platform = metadata.platform();
}

/**
Expand All @@ -74,7 +64,7 @@ class BrowserFetcher {
canDownload() {
let resolve;
const promise = new Promise((x) => (resolve = x));
const request = httpRequest(this.browserMetadata.downloadURL(), 'HEAD', (response) => {
const request = httpRequest(this.downloadURL, 'HEAD', (response) => {
resolve(response.statusCode === 200);
});
request.on('error', (error) => {
Expand All @@ -93,20 +83,20 @@ class BrowserFetcher {
const zipPath = path.join(this._downloadsFolder, `download-${this._platform}-${revision}.zip`);
const folderPath = this._getFolderPath(revision);
if (await existsAsync(folderPath)) {
return this.browserMetadata.revisionInfo();
return this.revisionInfo;
}
if (!(await existsAsync(this._downloadsFolder))) {
await mkdirAsync(this._downloadsFolder);
}
try {
await downloadFile(this.browserMetadata.downloadURL(), zipPath, progressCallback);
await downloadFile(this.downloadURL, zipPath, progressCallback);
await extractZip(zipPath, folderPath);
} finally {
if (await existsAsync(zipPath)) {
await unlinkAsync(zipPath);
}
}
const revisionInfo = this.browserMetadata.revisionInfo();
const revisionInfo = this.revisionInfo;
if (revisionInfo) {
await chmodAsync(revisionInfo.executablePath, 0o755);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/browser/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BrowserMetadata {
if (platform === 'darwin') {
this._platform = os.arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
} else if (platform === 'linux') {
this._platform = 'linux';
this._platform = 'linux64';
} else if (platform === 'win32') {
this._platform = os.arch() === 'x64' ? 'win64' : 'win32';
}
Expand All @@ -66,6 +66,10 @@ class BrowserMetadata {
return this.revision;
}

platform() {
return this._platform;
}

archiveName() {
if (this._platform === 'linux') {
return 'chrome-linux';
Expand Down

0 comments on commit 72d0e0e

Please sign in to comment.