From b9582348a9c514b10b5a83fa94660530fe6ee804 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 27 Apr 2020 21:05:32 -0700 Subject: [PATCH] chore: read browser revisions off package.json --- download-browser.js | 63 ++++++++++++++--------- index.js | 14 +---- install-from-github.js | 17 +++--- package.json | 19 +++++-- packages/playwright-chromium/index.js | 11 +--- packages/playwright-chromium/install.js | 6 +-- packages/playwright-chromium/package.json | 3 ++ packages/playwright-firefox/index.js | 11 +--- packages/playwright-firefox/install.js | 6 +-- packages/playwright-firefox/package.json | 3 ++ packages/playwright-webkit/index.js | 11 +--- packages/playwright-webkit/install.js | 6 +-- packages/playwright-webkit/package.json | 3 ++ packages/playwright/index.js | 14 +---- packages/playwright/install.js | 8 +-- packages/playwright/package.json | 5 ++ src/helper.ts | 15 ++++++ src/server/browserFetcher.ts | 16 +----- utils/protocol-types-generator/index.js | 11 +++- 19 files changed, 118 insertions(+), 124 deletions(-) diff --git a/download-browser.js b/download-browser.js index 0e98d1bde19776..3e8b50d7aa481a 100644 --- a/download-browser.js +++ b/download-browser.js @@ -15,44 +15,57 @@ */ const path = require('path'); +const { getFromENV, logPolitely } = require('./lib/helper.js'); +const { Playwright } = require('./lib/server/playwright.js'); const browserFetcher = require('./lib/server/browserFetcher.js'); const packageJSON = require('./package.json'); -function resolveBrowser(packagePath, browserName) { - const browsersPath = getFromENV('PLAYWRIGHT_BROWSERS_PATH'); - const baseDir = browsersPath || path.join(packagePath, '.local-browsers'); - const browserRevision = packageJSON.playwright[`${browserName}_revision`]; - return { baseDir, browserRevision }; +function browsersPath(packagePath) { + const result = getFromENV('PLAYWRIGHT_BROWSERS_PATH'); + return result || path.join(packagePath, '.local-browsers'); } -function executablePath(packagePath, browserName) { - const { baseDir, browserRevision } = resolveBrowser(packagePath, browserName); - return browserFetcher.executablePath(baseDir, browserName, browserRevision); +function executablePath(packagePath, browser) { + return browserFetcher.executablePath(browsersPath(packagePath), browser.name, browser.revision); } -function targetDirectory(packagePath, browserName) { - const { baseDir, browserRevision } = resolveBrowser(packagePath, browserName); - return browserFetcher.targetDirectory(baseDir, browserName, browserRevision); +function targetDirectory(packagePath, browser) { + return browserFetcher.targetDirectory(browsersPath(packagePath), browser.name, browser.revision); } -async function downloadBrowserWithProgressBar(packagePath, browserName) { - const { baseDir, browserRevision } = resolveBrowser(packagePath, browserName); - if (getFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD')) - return browserFetcher.downloadBrowserWithProgressBar(null); +async function downloadBrowsersWithProgressBar(packagePath, packageJSON) { + if (getFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD')) { + logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set'); + return false; + } + for (const browser of packageJSON.browsers) + await downloadBrowserWithProgressBar(packagePath, browser); +} + +async function downloadBrowserWithProgressBar(packagePath, browser) { return browserFetcher.downloadBrowserWithProgressBar({ - baseDir, - browserName, - browserRevision, - progressBarName: `${browserName} for playwright v${packageJSON.version}`, + baseDir: browsersPath(packagePath), + browserName: browser.name, + browserRevision: browser.revision, + progressBarName: `${browser.name} v${browser.revision} for playwright v${packageJSON.version}`, serverHost: getFromENV('PLAYWRIGHT_DOWNLOAD_HOST'), }); } -function getFromENV(name) { - let value = process.env[name]; - value = value || process.env[`npm_config_${name.toLowerCase()}`]; - value = value || process.env[`npm_package_config_${name.toLowerCase()}`]; - return value; +function initializePlaywright(packagePath, packageJSON) { + const browsers = packageJSON.browsers; + const playwright = new Playwright({ + browsers: browsers.map(browser => browser.name), + }); + for (const browser of browsers) + playwright[browser.name]._executablePath = executablePath(packagePath, browser); + return playwright; } -module.exports = { targetDirectory, executablePath, downloadBrowserWithProgressBar }; +module.exports = { + executablePath, + targetDirectory, + downloadBrowserWithProgressBar, + downloadBrowsersWithProgressBar, + initializePlaywright +}; diff --git a/index.js b/index.js index 05a5e7b874ac55..96b1c90306b1f2 100644 --- a/index.js +++ b/index.js @@ -13,17 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -const fs = require('fs'); -const path = require('path'); -const { Playwright } = require('./lib/server/playwright.js'); -const { executablePath } = require('./download-browser.js'); -const playwright = new Playwright({ - browsers: ['webkit', 'chromium', 'firefox'], -}); +const { initializePlaywright } = require('./download-browser'); -playwright.chromium._executablePath = executablePath(__dirname, 'chromium'); -playwright.firefox._executablePath = executablePath(__dirname, 'firefox'); -playwright.webkit._executablePath = executablePath(__dirname, 'webkit'); - -module.exports = playwright; +module.exports = initializePlaywright(__dirname, require('./package.json')); diff --git a/install-from-github.js b/install-from-github.js index b84fa3e5573faa..a353369e41bb60 100644 --- a/install-from-github.js +++ b/install-from-github.js @@ -22,7 +22,6 @@ const path = require('path'); const fs = require('fs'); const util = require('util'); const rmAsync = util.promisify(require('rimraf')); -const existsAsync = path => fs.promises.access(path).then(() => true, e => false); (async () => { const SRC_FOLDER = path.join(__dirname, 'src'); @@ -66,18 +65,16 @@ async function listFiles(dirpath) { async function downloadAllBrowsersAndGenerateProtocolTypes() { const { targetDirectory, executablePath, downloadBrowserWithProgressBar } = require('./download-browser'); const protocolGenerator = require('./utils/protocol-types-generator'); - if (await downloadBrowserWithProgressBar(__dirname, 'chromium')) - await protocolGenerator.generateChromiumProtocol(executablePath(__dirname, 'chromium')).catch(console.warn); - if (await downloadBrowserWithProgressBar(__dirname, 'firefox')) - await protocolGenerator.generateFirefoxProtocol(executablePath(__dirname, 'firefox')).catch(console.warn); - if (await downloadBrowserWithProgressBar(__dirname, 'webkit')) - await protocolGenerator.generateWebKitProtocol(executablePath(__dirname, 'webkit')).catch(console.warn); + const browsers = require('./package.json')['browsers']; + for (const browser of browsers) { + if (await downloadBrowserWithProgressBar(__dirname, browser)) + await protocolGenerator.generateProtocol(browser.name, executablePath(__dirname, browser)).catch(console.warn); + } // Cleanup stale revisions. const directories = new Set(await readdirAsync(path.join(__dirname, '.local-browsers'))); - directories.delete(targetDirectory(__dirname, 'chromium')); - directories.delete(targetDirectory(__dirname, 'firefox')); - directories.delete(targetDirectory(__dirname, 'webkit')); + for (const browser of browsers) + directories.delete(targetDirectory(__dirname, browser)); await Promise.all([...directories].map(directory => rmAsync(directory))); try { diff --git a/package.json b/package.json index c2329811a43f11..32df8a3f78e0c2 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,20 @@ "node": ">=10.15.0" }, "main": "index.js", - "playwright": { - "chromium_revision": "762211", - "firefox_revision": "1087", - "webkit_revision": "1211" - }, + "browsers": [ + { + "name": "chromium", + "revision": "762211" + }, + { + "name": "firefox", + "revision": "1087" + }, + { + "name": "webkit", + "revision": "1211" + } + ], "scripts": { "ctest": "cross-env BROWSER=chromium node --unhandled-rejections=strict test/test.js", "ftest": "cross-env BROWSER=firefox node --unhandled-rejections=strict test/test.js", diff --git a/packages/playwright-chromium/index.js b/packages/playwright-chromium/index.js index 63494f4c50e04c..4b50cfe5f7773b 100644 --- a/packages/playwright-chromium/index.js +++ b/packages/playwright-chromium/index.js @@ -14,13 +14,6 @@ * limitations under the License. */ -const { Playwright } = require('playwright-core/lib/server/playwright.js'); -const { executablePath } = require('playwright-core/download-browser.js'); +const { initializePlaywright } = require('playwright-core/download-browser'); -const playwright = new Playwright({ - browsers: ['chromium'], -}); - -playwright.chromium._executablePath = executablePath(__dirname, 'chromium'); - -module.exports = playwright; +module.exports = initializePlaywright(__dirname, require('./package.json')); \ No newline at end of file diff --git a/packages/playwright-chromium/install.js b/packages/playwright-chromium/install.js index eacdcb3199f620..4c64cd1bde6f64 100644 --- a/packages/playwright-chromium/install.js +++ b/packages/playwright-chromium/install.js @@ -14,8 +14,6 @@ * limitations under the License. */ -const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser'); +const { downloadBrowsersWithProgressBar } = require('playwright-core/download-browser'); -(async function() { - await downloadBrowserWithProgressBar(__dirname, 'chromium'); -})(); +downloadBrowsersWithProgressBar(__dirname, require('./package.json')); diff --git a/packages/playwright-chromium/package.json b/packages/playwright-chromium/package.json index b0d55b6f370b22..a3cc5e78427c04 100644 --- a/packages/playwright-chromium/package.json +++ b/packages/playwright-chromium/package.json @@ -4,6 +4,9 @@ "description": "A high-level API to automate Chromium", "repository": "github:Microsoft/playwright", "main": "index.js", + "browsers": [ + { "name": "chromium", "revision": "762211" } + ], "scripts": { "install": "node install.js" }, diff --git a/packages/playwright-firefox/index.js b/packages/playwright-firefox/index.js index 0e0c97a380ca25..4b50cfe5f7773b 100644 --- a/packages/playwright-firefox/index.js +++ b/packages/playwright-firefox/index.js @@ -14,13 +14,6 @@ * limitations under the License. */ -const { Playwright } = require('playwright-core/lib/server/playwright.js'); -const { executablePath } = require('playwright-core/download-browser.js'); +const { initializePlaywright } = require('playwright-core/download-browser'); -const playwright = new Playwright({ - browsers: ['firefox'], -}); - -playwright.firefox._executablePath = executablePath(__dirname, 'firefox'); - -module.exports = playwright; +module.exports = initializePlaywright(__dirname, require('./package.json')); \ No newline at end of file diff --git a/packages/playwright-firefox/install.js b/packages/playwright-firefox/install.js index 8d899ac10e437b..4c64cd1bde6f64 100644 --- a/packages/playwright-firefox/install.js +++ b/packages/playwright-firefox/install.js @@ -14,8 +14,6 @@ * limitations under the License. */ -const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser'); +const { downloadBrowsersWithProgressBar } = require('playwright-core/download-browser'); -(async function() { - await downloadBrowserWithProgressBar(__dirname, 'firefox'); -})(); +downloadBrowsersWithProgressBar(__dirname, require('./package.json')); diff --git a/packages/playwright-firefox/package.json b/packages/playwright-firefox/package.json index 8e3b5327a08834..743248f619a2f6 100644 --- a/packages/playwright-firefox/package.json +++ b/packages/playwright-firefox/package.json @@ -4,6 +4,9 @@ "description": "A high-level API to automate Firefox", "repository": "github:Microsoft/playwright", "main": "index.js", + "browsers": [ + { "name": "firefox", "revision": "1087" } + ], "scripts": { "install": "node install.js" }, diff --git a/packages/playwright-webkit/index.js b/packages/playwright-webkit/index.js index 4b71f572d43ee8..602d6390a086c3 100644 --- a/packages/playwright-webkit/index.js +++ b/packages/playwright-webkit/index.js @@ -14,13 +14,6 @@ * limitations under the License. */ -const { Playwright } = require('playwright-core/lib/server/playwright.js'); -const { executablePath } = require('playwright-core/download-browser.js'); +const { initializePlaywright } = require('playwright-core/download-browser'); -const playwright = new Playwright({ - browsers: ['webkit'], -}); - -playwright.webkit._executablePath = executablePath(__dirname, 'webkit'); - -module.exports = playwright; +module.exports = initializePlaywright(__dirname, require('./package.json')); diff --git a/packages/playwright-webkit/install.js b/packages/playwright-webkit/install.js index 2fb07427784e97..4c64cd1bde6f64 100644 --- a/packages/playwright-webkit/install.js +++ b/packages/playwright-webkit/install.js @@ -14,8 +14,6 @@ * limitations under the License. */ -const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser'); +const { downloadBrowsersWithProgressBar } = require('playwright-core/download-browser'); -(async function() { - await downloadBrowserWithProgressBar(__dirname, 'webkit'); -})(); +downloadBrowsersWithProgressBar(__dirname, require('./package.json')); diff --git a/packages/playwright-webkit/package.json b/packages/playwright-webkit/package.json index e35230cc3a99a8..f50b2d8ff7ae11 100644 --- a/packages/playwright-webkit/package.json +++ b/packages/playwright-webkit/package.json @@ -4,6 +4,9 @@ "description": "A high-level API to automate WebKit", "repository": "github:Microsoft/playwright", "main": "index.js", + "browsers": [ + { "name": "webkit", "revision": "1211" } + ], "scripts": { "install": "node install.js" }, diff --git a/packages/playwright/index.js b/packages/playwright/index.js index 3ff74a36f11726..602d6390a086c3 100644 --- a/packages/playwright/index.js +++ b/packages/playwright/index.js @@ -14,16 +14,6 @@ * limitations under the License. */ -const { Playwright } = require('playwright-core/lib/server/playwright.js'); -const { executablePath } = require('playwright-core/download-browser.js'); - -const playwright = new Playwright({ - browsers: ['webkit', 'chromium', 'firefox'], -}); - -playwright.chromium._executablePath = executablePath(__dirname, 'chromium'); -playwright.webkit._executablePath = executablePath(__dirname, 'webkit'); -playwright.firefox._executablePath = executablePath(__dirname, 'firefox'); - -module.exports = playwright; +const { initializePlaywright } = require('playwright-core/download-browser'); +module.exports = initializePlaywright(__dirname, require('./package.json')); diff --git a/packages/playwright/install.js b/packages/playwright/install.js index 7a16e96ef2560d..4c64cd1bde6f64 100644 --- a/packages/playwright/install.js +++ b/packages/playwright/install.js @@ -14,10 +14,6 @@ * limitations under the License. */ - const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser'); +const { downloadBrowsersWithProgressBar } = require('playwright-core/download-browser'); -(async function() { - await downloadBrowserWithProgressBar(__dirname, 'chromium'); - await downloadBrowserWithProgressBar(__dirname, 'firefox'); - await downloadBrowserWithProgressBar(__dirname, 'webkit'); -})(); +downloadBrowsersWithProgressBar(__dirname, require('./package.json')); diff --git a/packages/playwright/package.json b/packages/playwright/package.json index 9c590652eabe30..835e06e8b2fd1b 100644 --- a/packages/playwright/package.json +++ b/packages/playwright/package.json @@ -4,6 +4,11 @@ "description": "A high-level API to automate web browsers", "repository": "github:Microsoft/playwright", "main": "index.js", + "browsers": [ + { "name": "chromium", "revision": "762211" }, + { "name": "firefox", "revision": "1087" }, + { "name": "webkit", "revision": "1211" } + ], "scripts": { "install": "node install.js", "prepublishOnly": "cp ../../README.md ." diff --git a/src/helper.ts b/src/helper.ts index fac74aaac83700..5a78434185c59f 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -337,6 +337,21 @@ export function assert(value: any, message?: string): asserts value { throw new Error(message); } +export function getFromENV(name: string) { + let value = process.env[name]; + value = value || process.env[`npm_config_${name.toLowerCase()}`]; + value = value || process.env[`npm_package_config_${name.toLowerCase()}`]; + return value; +} + +export function logPolitely(toBeLogged: string) { + const logLevel = process.env.npm_config_loglevel; + const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel || '') > -1; + + if (!logLevelDisplay) + console.log(toBeLogged); // eslint-disable-line no-console +} + const escapeGlobChars = new Set(['/', '$', '^', '+', '.', '(', ')', '=', '!', '|']); export const helper = Helper; diff --git a/src/server/browserFetcher.ts b/src/server/browserFetcher.ts index f50de20e2da4e5..5a943d1a681b2f 100644 --- a/src/server/browserFetcher.ts +++ b/src/server/browserFetcher.ts @@ -25,7 +25,7 @@ import * as ProgressBar from 'progress'; import { getProxyForUrl } from 'proxy-from-env'; import * as URL from 'url'; import * as util from 'util'; -import { assert } from '../helper'; +import { assert, logPolitely } from '../helper'; const unlinkAsync = util.promisify(fs.unlink.bind(fs)); const chmodAsync = util.promisify(fs.chmod.bind(fs)); @@ -155,11 +155,7 @@ export function executablePath(baseDir: string, browserName: BrowserName, browse return path.join(targetDirectory(baseDir, browserName, browserRevision), ...relativePath); } -export async function downloadBrowserWithProgressBar(options: DownloadOptions | null): Promise { - if (!options) { - logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set'); - return false; - } +export async function downloadBrowserWithProgressBar(options: DownloadOptions): Promise { const { baseDir, browserName, @@ -212,14 +208,6 @@ function toMegabytes(bytes: number) { return `${Math.round(mb * 10) / 10} Mb`; } -function logPolitely(toBeLogged: string) { - const logLevel = process.env.npm_config_loglevel; - const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel || '') > -1; - - if (!logLevelDisplay) - console.log(toBeLogged); // eslint-disable-line no-console -} - export async function canDownload(browserName: BrowserName, browserRevision: string, platform: BrowserPlatform): Promise { const url = revisionURL({ baseDir: '', diff --git a/utils/protocol-types-generator/index.js b/utils/protocol-types-generator/index.js index 483979e245cf6f..3aeb8bcd529659 100644 --- a/utils/protocol-types-generator/index.js +++ b/utils/protocol-types-generator/index.js @@ -6,6 +6,15 @@ const vm = require('vm'); const os = require('os'); const util = require('util'); +async function generateProtocol(name, executablePath) { + if (name === 'chromium') + return generateChromiumProtocol(executablePath); + if (name === 'firefox') + return generateFirefoxProtocol(executablePath); + if (name === 'webkit') + return generateWebKitProtocol(executablePath); +} + async function generateChromiumProtocol(executablePath) { const outputPath = path.join(__dirname, '..', '..', 'src', 'chromium', 'protocol.ts'); const playwright = await require('../../index').chromium; @@ -215,4 +224,4 @@ function firefoxTypeToString(type, indent=' ') { return type['$type']; } -module.exports = {generateChromiumProtocol, generateFirefoxProtocol, generateWebKitProtocol}; +module.exports = { generateProtocol };