From 4049f9d06081b2d58a91c233fa7599197889a4f2 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 26 Feb 2020 13:44:18 -0800 Subject: [PATCH] fix(yarn): download browsers to package directories This patch makes it so all our packages, like `playwright` and browser-specific flavors, download browsers to their directories rather then using directory of `playwright-core`. This way yarn@1 caches are not busted: they didn't expect that directory content might change after packages's explicit install step is failed, there's that was what we were doing. Fixes #1085 --- index.js | 25 ++--------- packages/playwright-chromium/index.js | 10 ++--- packages/playwright-firefox/index.js | 10 ++--- packages/playwright-webkit/index.js | 10 ++--- packages/playwright/index.js | 5 ++- src/playwright.ts | 62 +++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 src/playwright.ts diff --git a/index.js b/index.js index 7ab40e101897e9..c7df1ebd2e03d5 100644 --- a/index.js +++ b/index.js @@ -14,25 +14,8 @@ * limitations under the License. */ -const { helper } = require('./lib/helper'); -const api = require('./lib/api'); -const packageJson = require('./package.json'); -const { DeviceDescriptors } = require('./lib/deviceDescriptors'); -const { TimeoutError } = require('./lib/errors'); -const { Chromium } = require('./lib/server/chromium'); -const { Firefox } = require('./lib/server/firefox'); -const { WebKit } = require('./lib/server/webkit'); +module.exports = require('./lib/playwright.js').exportAPI({ + downloadPath: __dirname, + browsers: ['webkit', 'chromium', 'firefox'], +}); -for (const className in api) { - if (typeof api[className] === 'function') - helper.installApiHooks(className[0].toLowerCase() + className.substring(1), api[className]); -} - -module.exports = { - devices: DeviceDescriptors, - errors: { TimeoutError }, - selectors: api.Selectors._instance(), - chromium: new Chromium(__dirname, packageJson.playwright.chromium_revision), - firefox: new Firefox(__dirname, packageJson.playwright.firefox_revision), - webkit: new WebKit(__dirname, packageJson.playwright.webkit_revision), -} diff --git a/packages/playwright-chromium/index.js b/packages/playwright-chromium/index.js index a784afc3bd63a3..db9ad935be79be 100644 --- a/packages/playwright-chromium/index.js +++ b/packages/playwright-chromium/index.js @@ -14,9 +14,7 @@ * limitations under the License. */ -module.exports = { - ...require('playwright-core'), - // Keep exporting Chromium and nullify other browsers. - webkit: undefined, - firefox: undefined, -} +module.exports = require('playwright-core/lib/playwright.js').exportAPI({ + downloadPath: __dirname, + browsers: ['chromium'], +}); diff --git a/packages/playwright-firefox/index.js b/packages/playwright-firefox/index.js index 5eca62b2091e4c..33443dc4fff90e 100644 --- a/packages/playwright-firefox/index.js +++ b/packages/playwright-firefox/index.js @@ -14,9 +14,7 @@ * limitations under the License. */ -module.exports = { - ...require('playwright-core'), - // Keep exporting firefox and nullify other browsers. - chromium: undefined, - webkit: undefined, -} +module.exports = require('playwright-core').exportAPI({ + downloadPath: __dirname, + browsers: ['firefox'], +}); diff --git a/packages/playwright-webkit/index.js b/packages/playwright-webkit/index.js index 530904cd890b59..74ae072803da2c 100644 --- a/packages/playwright-webkit/index.js +++ b/packages/playwright-webkit/index.js @@ -14,9 +14,7 @@ * limitations under the License. */ -module.exports = { - ...require('playwright-core'), - // Keep exporting webkit and nullify other browsers. - chromium: undefined, - firefox: undefined, -} +module.exports = require('playwright-core').exportAPI({ + downloadPath: __dirname, + browsers: ['webkit'], +}); diff --git a/packages/playwright/index.js b/packages/playwright/index.js index a85e0cb7f7a4e0..6bded12c92477f 100644 --- a/packages/playwright/index.js +++ b/packages/playwright/index.js @@ -13,4 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright-core'); +module.exports = require('playwright-core/lib/playwright.js').exportAPI({ + downloadPath: __dirname, + browsers: ['webkit', 'chromium', 'firefox'], +}); diff --git a/src/playwright.ts b/src/playwright.ts new file mode 100644 index 00000000000000..a47c7ec1743d42 --- /dev/null +++ b/src/playwright.ts @@ -0,0 +1,62 @@ +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { helper } from './helper'; +import * as api from './api'; + +import { DeviceDescriptors } from './deviceDescriptors'; +import { TimeoutError } from './errors'; +import { Chromium } from './server/chromium'; +import { Firefox } from './server/firefox'; +import { WebKit } from './server/webkit'; + +const packageJson = require('../package.json'); + +for (const className in api) { + if (typeof (api as any)[className] === 'function') + helper.installApiHooks(className[0].toLowerCase() + className.substring(1), (api as any)[className]); +} + +type API = { + devices: typeof DeviceDescriptors, + errors: { TimeoutError: typeof TimeoutError }, + chromium?: Chromium, + firefox?: Firefox, + webkit?: WebKit, + selectors: api.Selectors, +}; + +export function exportAPI(options: {downloadPath: string, browsers: Array<('firefox'|'webkit'|'chromium')>}): API { + const { + downloadPath = __dirname, + browsers = ['webkit', 'chromium', 'firefox'], + } = options; + + const result: API = { + devices: DeviceDescriptors, + errors: { TimeoutError }, + selectors: api.Selectors._instance(), + }; + + if (browsers.includes('webkit')) + result.webkit = new WebKit(downloadPath, packageJson.playwright.webkit_revision); + if (browsers.includes('chromium')) + result.chromium = new Chromium(downloadPath, packageJson.playwright.chromium_revision); + if (browsers.includes('firefox')) + result.firefox = new Firefox(downloadPath, packageJson.playwright.firefox_revision); + return result; +} +