Skip to content

Commit

Permalink
chore: simplify and restructure downloads (microsoft#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Apr 25, 2020
1 parent a43eac3 commit b60c006
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 246 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ yarn.lock
/src/webkit/protocol.ts
lib/
playwright-*.tgz
/web.js
/web.js.map
/types/*
85 changes: 23 additions & 62 deletions download-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');

const path = require('path');
const browserFetcher = require('./lib/server/browserFetcher.js');
const packageJSON = require('./package.json');

function localDownloadOptions(browserName) {
const revision = packageJSON.playwright[`${browserName}_revision`];
const downloadPath = path.join(__dirname, '.local-browsers', `${browserName}-${revision}`);
return {
browser: browserName,
progressBarBrowserName: `${browserName} r${revision}`,
revision,
downloadPath,
executablePath: browserFetcher.executablePath({browser: browserName, downloadPath}),
};
}

function downloadOptionsFromENV(packagePath, browserName) {
function resolveBrowser(packagePath, browserName) {
const browsersPath = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
const downloadPath = browsersPath ?
path.join(browsersPath, 'v' + packageJSON.version, browserName) :
path.join(packagePath, '.local-browsers', browserName);
return {
downloadPath,
skipBrowserDownload: getFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD'),
progressBarBrowserName: `${browserName} for playwright v${packageJSON.version}`,
revision: packageJSON.playwright[`${browserName}_revision`],
browser: browserName,
host: getFromENV('PLAYWRIGHT_DOWNLOAD_HOST'),
executablePath: browserFetcher.executablePath({browser: browserName, downloadPath}),
};
const baseDir = browsersPath || path.join(packagePath, '.local-browsers');
const browserRevision = packageJSON.playwright[`${browserName}_revision`];
return { baseDir, browserRevision };
}

async function downloadBrowserWithProgressBar(options) {
if (options.skipBrowserDownload) {
logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set');
return;
}
let progressBar = null;
let lastDownloadedBytes = 0;
function progress(downloadedBytes, totalBytes) {
if (!progressBar) {
const ProgressBar = require('progress');
progressBar = new ProgressBar(`Downloading ${options.progressBarBrowserName} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, {
complete: '=',
incomplete: ' ',
width: 20,
total: totalBytes,
});
}
const delta = downloadedBytes - lastDownloadedBytes;
lastDownloadedBytes = downloadedBytes;
progressBar.tick(delta);
}
await browserFetcher.downloadBrowser({...options, progress}).catch(e => {
process.exitCode = 1;
throw e;
});
logPolitely(`${options.progressBarBrowserName} downloaded to ${options.downloadPath}`);
function executablePath(packagePath, browserName) {
const { baseDir, browserRevision } = resolveBrowser(packagePath, browserName);
return browserFetcher.executablePath(baseDir, browserName, browserRevision);
}

function toMegabytes(bytes) {
const mb = bytes / 1024 / 1024;
return `${Math.round(mb * 10) / 10} Mb`;
function targetDirectory(packagePath, browserName) {
const { baseDir, browserRevision } = resolveBrowser(packagePath, browserName);
return browserFetcher.targetDirectory(baseDir, browserName, browserRevision);
}

function logPolitely(toBeLogged) {
const logLevel = process.env.npm_config_loglevel;
const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel) > -1;

if (!logLevelDisplay)
console.log(toBeLogged);
async function downloadBrowserWithProgressBar(packagePath, browserName) {
const { baseDir, browserRevision } = resolveBrowser(packagePath, browserName);
if (getFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD'))
return browserFetcher.downloadBrowserWithProgressBar(null);
return browserFetcher.downloadBrowserWithProgressBar({
baseDir,
browserName,
browserRevision,
progressBarName: `${browserName} for playwright v${packageJSON.version}`,
serverHost: getFromENV('PLAYWRIGHT_DOWNLOAD_HOST'),
});
}

function getFromENV(name) {
Expand All @@ -94,4 +55,4 @@ function getFromENV(name) {
return value;
}

module.exports = {downloadBrowserWithProgressBar, downloadOptionsFromENV, localDownloadOptions};
module.exports = { targetDirectory, executablePath, downloadBrowserWithProgressBar };
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
*/
const fs = require('fs');
const path = require('path');
const {Playwright} = require('./lib/server/playwright.js');
const {localDownloadOptions} = require('./download-browser.js');
const { Playwright } = require('./lib/server/playwright.js');
const { executablePath } = require('./download-browser.js');

const playwright = new Playwright({
browsers: ['webkit', 'chromium', 'firefox'],
});

if (fs.existsSync(path.join(__dirname, '.local-browsers'))) {
playwright.chromium._executablePath = localDownloadOptions('chromium').executablePath;
playwright.firefox._executablePath = localDownloadOptions('firefox').executablePath;
playwright.webkit._executablePath = localDownloadOptions('webkit').executablePath;
}
playwright.chromium._executablePath = executablePath(__dirname, 'chromium');
playwright.firefox._executablePath = executablePath(__dirname, 'firefox');
playwright.webkit._executablePath = executablePath(__dirname, 'webkit');

module.exports = playwright;

33 changes: 10 additions & 23 deletions install-from-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,20 @@ async function listFiles(dirpath) {
}

async function downloadAllBrowsersAndGenerateProtocolTypes() {
const {downloadBrowserWithProgressBar, localDownloadOptions} = require('./download-browser');
const { targetDirectory, executablePath, downloadBrowserWithProgressBar } = require('./download-browser');
const protocolGenerator = require('./utils/protocol-types-generator');
const chromiumOptions = localDownloadOptions('chromium');
const firefoxOptions = localDownloadOptions('firefox');
const webkitOptions = localDownloadOptions('webkit');
if (!(await existsAsync(chromiumOptions.downloadPath))) {
await downloadBrowserWithProgressBar(chromiumOptions);
await protocolGenerator.generateChromiumProtocol(chromiumOptions.executablePath).catch(console.warn);
}
if (!(await existsAsync(firefoxOptions.downloadPath))) {
await downloadBrowserWithProgressBar(firefoxOptions);
await protocolGenerator.generateFirefoxProtocol(firefoxOptions.executablePath).catch(console.warn);
}
if (!(await existsAsync(webkitOptions.downloadPath))) {
await downloadBrowserWithProgressBar(webkitOptions);
await protocolGenerator.generateWebKitProtocol(webkitOptions.downloadPath).catch(console.warn);
}
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);

// Cleanup stale revisions.
const directories = new Set(await readdirAsync(path.join(__dirname, '.local-browsers')));
directories.delete(chromiumOptions.downloadPath);
directories.delete(firefoxOptions.downloadPath);
directories.delete(webkitOptions.downloadPath);
// cleanup old browser directories.
directories.add(path.join(__dirname, '.local-chromium'));
directories.add(path.join(__dirname, '.local-firefox'));
directories.add(path.join(__dirname, '.local-webkit'));
directories.delete(targetDirectory(__dirname, 'chromium'));
directories.delete(targetDirectory(__dirname, 'firefox'));
directories.delete(targetDirectory(__dirname, 'webkit'));
await Promise.all([...directories].map(directory => rmAsync(directory)));

try {
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@types/mime": "^2.0.1",
"@types/node": "^10.17.17",
"@types/pngjs": "^3.4.0",
"@types/progress": "^2.0.3",
"@types/proxy-from-env": "^1.0.0",
"@types/rimraf": "^2.0.2",
"@types/ws": "^6.0.1",
Expand Down
9 changes: 4 additions & 5 deletions packages/playwright-chromium/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const { Playwright } = require('playwright-core/lib/server/playwright.js');
const { executablePath } = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['chromium'],
});

playwright.chromium._executablePath = downloadOptionsFromENV(__dirname, 'chromium').executablePath;
playwright.chromium._executablePath = executablePath(__dirname, 'chromium');

module.exports = playwright;

7 changes: 3 additions & 4 deletions packages/playwright-chromium/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar, downloadOptionsFromENV} = require('playwright-core/download-browser');

const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser');

(async function() {
await downloadBrowserWithProgressBar(downloadOptionsFromENV(__dirname, 'chromium'));
await downloadBrowserWithProgressBar(__dirname, 'chromium');
})();
9 changes: 4 additions & 5 deletions packages/playwright-firefox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const { Playwright } = require('playwright-core/lib/server/playwright.js');
const { executablePath } = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['firefox'],
});

playwright.firefox._executablePath = downloadOptionsFromENV(__dirname, 'firefox').executablePath;
playwright.firefox._executablePath = executablePath(__dirname, 'firefox');

module.exports = playwright;

7 changes: 3 additions & 4 deletions packages/playwright-firefox/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar, downloadOptionsFromENV} = require('playwright-core/download-browser');

const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser');

(async function() {
await downloadBrowserWithProgressBar(downloadOptionsFromENV(__dirname, 'firefox'));
await downloadBrowserWithProgressBar(__dirname, 'firefox');
})();
9 changes: 4 additions & 5 deletions packages/playwright-webkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const { Playwright } = require('playwright-core/lib/server/playwright.js');
const { executablePath } = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['webkit'],
});

playwright.webkit._executablePath = downloadOptionsFromENV(__dirname, 'webkit').executablePath;
playwright.webkit._executablePath = executablePath(__dirname, 'webkit');

module.exports = playwright;

7 changes: 3 additions & 4 deletions packages/playwright-webkit/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar, downloadOptionsFromENV} = require('playwright-core/download-browser');

const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser');

(async function() {
await downloadBrowserWithProgressBar(downloadOptionsFromENV(__dirname, 'webkit'));
await downloadBrowserWithProgressBar(__dirname, 'webkit');
})();
12 changes: 6 additions & 6 deletions packages/playwright/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

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 = downloadOptionsFromENV(__dirname, 'chromium').executablePath;
playwright.webkit._executablePath = downloadOptionsFromENV(__dirname, 'webkit').executablePath;
playwright.firefox._executablePath = downloadOptionsFromENV(__dirname, 'firefox').executablePath;
playwright.chromium._executablePath = executablePath(__dirname, 'chromium');
playwright.webkit._executablePath = executablePath(__dirname, 'webkit');
playwright.firefox._executablePath = executablePath(__dirname, 'firefox');

module.exports = playwright;

11 changes: 5 additions & 6 deletions packages/playwright/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar, downloadOptionsFromENV} = require('playwright-core/download-browser');

const { downloadBrowserWithProgressBar } = require('playwright-core/download-browser');

(async function() {
await downloadBrowserWithProgressBar(downloadOptionsFromENV(__dirname, 'chromium'));
await downloadBrowserWithProgressBar(downloadOptionsFromENV(__dirname, 'firefox'));
await downloadBrowserWithProgressBar(downloadOptionsFromENV(__dirname, 'webkit'));
await downloadBrowserWithProgressBar(__dirname, 'chromium');
await downloadBrowserWithProgressBar(__dirname, 'firefox');
await downloadBrowserWithProgressBar(__dirname, 'webkit');
})();
Loading

0 comments on commit b60c006

Please sign in to comment.