Skip to content

Commit 135e034

Browse files
authored
feat(cli): small improvements (#4952)
- Allow specifying which browsers to install. This comes handy in playwright-cli. - Print "npx playwright" as a tool name in help messages, instead of "cli".
1 parent 114d586 commit 135e034

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

packages/installation-tests/installation-tests.sh

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,38 @@ function test_playwright_cli_install_should_work {
361361
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm install ${PLAYWRIGHT_TGZ}
362362

363363
local BROWSERS="$(pwd -P)/browsers"
364+
365+
echo "Running playwright install chromium"
366+
OUTPUT=$(PLAYWRIGHT_BROWSERS_PATH=${BROWSERS} npx playwright install chromium)
367+
if [[ "${OUTPUT}" != *"chromium"* ]]; then
368+
echo "ERROR: should download chromium"
369+
exit 1
370+
fi
371+
if [[ "${OUTPUT}" == *"webkit"* ]]; then
372+
echo "ERROR: should not download webkit"
373+
exit 1
374+
fi
375+
if [[ "${OUTPUT}" == *"firefox"* ]]; then
376+
echo "ERROR: should not download firefox"
377+
exit 1
378+
fi
379+
364380
echo "Running playwright install"
365-
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" npx playwright install
366-
if [[ ! -d "${BROWSERS}" ]]; then
367-
echo "Directory for shared browsers was not created!"
381+
OUTPUT=$(PLAYWRIGHT_BROWSERS_PATH=${BROWSERS} npx playwright install)
382+
if [[ "${OUTPUT}" == *"chromium"* ]]; then
383+
echo "ERROR: should not download chromium"
384+
exit 1
385+
fi
386+
if [[ "${OUTPUT}" != *"webkit"* ]]; then
387+
echo "ERROR: should download webkit"
388+
exit 1
389+
fi
390+
if [[ "${OUTPUT}" != *"firefox"* ]]; then
391+
echo "ERROR: should download firefox"
368392
exit 1
369393
fi
370-
copy_test_scripts
371394

395+
copy_test_scripts
372396
echo "Running sanity.js"
373397
node sanity.js playwright none
374398
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node sanity.js playwright

src/cli/cli.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import * as playwright from '../..';
3636

3737
program
3838
.version('Version ' + require('../../package.json').version)
39+
.name('npx playwright')
3940
.option('-b, --browser <browserType>', 'browser to use, one of cr, chromium, ff, firefox, wk, webkit', 'chromium')
4041
.option('--color-scheme <scheme>', 'emulate preferred color scheme, "light" or "dark"')
4142
.option('--device <deviceName>', 'emulate device, for example "iPhone 11"')
@@ -128,10 +129,10 @@ program
128129
});
129130

130131
program
131-
.command('install')
132+
.command('install [browserType...]')
132133
.description('Ensure browsers necessary for this version of Playwright are installed')
133-
.action(function() {
134-
installBrowsers().catch((e: any) => {
134+
.action(function(browserType) {
135+
installBrowsers(browserType.length ? browserType : undefined).catch((e: any) => {
135136
console.log(`Failed to install browsers\n${e}`);
136137
process.exit(1);
137138
});

src/cli/driver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Playwright } from '../server/playwright';
2727
import { gracefullyCloseAll } from '../server/processLauncher';
2828
import { installHarTracer } from '../trace/harTracer';
2929
import { installTracer } from '../trace/tracer';
30+
import { BrowserName } from '../utils/browserPaths';
3031

3132
export function printApiJson() {
3233
console.log(JSON.stringify(require('../../api.json')));
@@ -59,12 +60,12 @@ export function runServer() {
5960
new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), playwright);
6061
}
6162

62-
export async function installBrowsers() {
63+
export async function installBrowsers(browserNames?: BrowserName[]) {
6364
let browsersJsonDir = path.dirname(process.execPath);
6465
if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json'))) {
6566
browsersJsonDir = path.join(__dirname, '..', '..');
6667
if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json')))
6768
throw new Error('Failed to find browsers.json in ' + browsersJsonDir);
6869
}
69-
await installBrowsersWithProgressBar(browsersJsonDir);
70+
await installBrowsersWithProgressBar(browsersJsonDir, browserNames);
7071
}

src/install/installer.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
3232
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
3333
const removeFolderAsync = util.promisify(removeFolder);
3434

35-
export async function installBrowsersWithProgressBar(packagePath: string) {
35+
export async function installBrowsersWithProgressBar(packagePath: string, browserNames?: browserPaths.BrowserName[]) {
3636
// PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD should have a value of 0 or 1
3737
if (getAsBooleanFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD')) {
3838
browserFetcher.logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set');
@@ -59,11 +59,11 @@ export async function installBrowsersWithProgressBar(packagePath: string) {
5959

6060
await fsMkdirAsync(linksDir, { recursive: true });
6161
await fsWriteFileAsync(path.join(linksDir, sha1(packagePath)), packagePath);
62-
await validateCache(packagePath, browsersPath, linksDir);
62+
await validateCache(packagePath, browsersPath, linksDir, browserNames);
6363
await releaseLock();
6464
}
6565

66-
async function validateCache(packagePath: string, browsersPath: string, linksDir: string) {
66+
async function validateCache(packagePath: string, browsersPath: string, linksDir: string, browserNames?: browserPaths.BrowserName[]) {
6767
// 1. Collect used downloads and package descriptors.
6868
const usedBrowserPaths: Set<string> = new Set();
6969
for (const fileName of await fsReaddirAsync(linksDir)) {
@@ -99,7 +99,7 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
9999
}
100100

101101
// 3. Install missing browsers for this package.
102-
const myBrowsersToDownload = await readBrowsersToDownload(packagePath);
102+
const myBrowsersToDownload = await readBrowsersToDownload(packagePath, browserNames);
103103
for (const browser of myBrowsersToDownload) {
104104
await browserFetcher.downloadBrowserWithProgressBar(browsersPath, browser).catch(e => {
105105
throw new Error(`Failed to download ${browser.name}, caused by\n${e.stack}`);
@@ -108,11 +108,13 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
108108
}
109109
}
110110

111-
async function readBrowsersToDownload(packagePath: string) {
111+
async function readBrowsersToDownload(packagePath: string, browserNames?: browserPaths.BrowserName[]) {
112112
const browsers = JSON.parse((await fsReadFileAsync(path.join(packagePath, 'browsers.json'))).toString())['browsers'] as browserPaths.BrowserDescriptor[];
113113
// Older versions do not have "download" field. We assume they need all browsers
114114
// from the list. So we want to skip all browsers that are explicitly marked as "download: false".
115-
return browsers.filter(browser => browser.download !== false);
115+
return browsers.filter(browser => {
116+
return browserNames ? browserNames.includes(browser.name) : browser.download !== false;
117+
});
116118
}
117119

118120
function sha1(data: string): string {

0 commit comments

Comments
 (0)