Skip to content

Commit

Permalink
chore: read browser revisions off package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Apr 28, 2020
1 parent 030c217 commit b958234
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 124 deletions.
63 changes: 38 additions & 25 deletions download-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
14 changes: 2 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
17 changes: 7 additions & 10 deletions install-from-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 2 additions & 9 deletions packages/playwright-chromium/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
6 changes: 2 additions & 4 deletions packages/playwright-chromium/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
3 changes: 3 additions & 0 deletions packages/playwright-chromium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 2 additions & 9 deletions packages/playwright-firefox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
6 changes: 2 additions & 4 deletions packages/playwright-firefox/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
3 changes: 3 additions & 0 deletions packages/playwright-firefox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 2 additions & 9 deletions packages/playwright-webkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
6 changes: 2 additions & 4 deletions packages/playwright-webkit/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
3 changes: 3 additions & 0 deletions packages/playwright-webkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
14 changes: 2 additions & 12 deletions packages/playwright/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
8 changes: 2 additions & 6 deletions packages/playwright/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
5 changes: 5 additions & 0 deletions packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
Expand Down
15 changes: 15 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 2 additions & 14 deletions src/server/browserFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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<boolean> {
if (!options) {
logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set');
return false;
}
export async function downloadBrowserWithProgressBar(options: DownloadOptions): Promise<boolean> {
const {
baseDir,
browserName,
Expand Down Expand Up @@ -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<boolean> {
const url = revisionURL({
baseDir: '',
Expand Down
Loading

0 comments on commit b958234

Please sign in to comment.