| 
									
										
										
										
											2020-07-02 15:40:47 +08:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2022-04-07 13:21:27 +08:00
										 |  |  | const { Registry } = require('../packages/playwright-core/lib/server'); | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const protocolGenerator = require('./protocol-types-generator'); | 
					
						
							| 
									
										
										
										
											2020-07-23 02:03:35 +08:00
										 |  |  | const {execSync} = require('child_process'); | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  | const playwright = require('playwright-core'); | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const SCRIPT_NAME = path.basename(__filename); | 
					
						
							| 
									
										
										
										
											2021-10-11 22:52:17 +08:00
										 |  |  | const CORE_PATH = path.resolve(path.join(__dirname, '..', 'packages', 'playwright-core')); | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function usage() { | 
					
						
							|  |  |  |   return `
 | 
					
						
							|  |  |  | usage: ${SCRIPT_NAME} <browser> <revision> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Roll the <browser> to a specific <revision> and generate new protocol. | 
					
						
							| 
									
										
										
										
											2021-06-16 14:58:30 +08:00
										 |  |  | Supported browsers: chromium, firefox, webkit, ffmpeg, firefox-beta. | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							|  |  |  |   ${SCRIPT_NAME} chromium 123456 | 
					
						
							|  |  |  | `;
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							|  |  |  |   // 1. Parse CLI arguments
 | 
					
						
							|  |  |  |   const args = process.argv.slice(2); | 
					
						
							|  |  |  |   if (args.some(arg => arg === '--help')) { | 
					
						
							|  |  |  |     console.log(usage()); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } else if (args.length < 1) { | 
					
						
							|  |  |  |     console.log(`Please specify the browser name, e.g. 'chromium'.`); | 
					
						
							|  |  |  |     console.log(`Try running ${SCRIPT_NAME} --help`); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } else if (args.length < 2) { | 
					
						
							|  |  |  |     console.log(`Please specify the revision`); | 
					
						
							|  |  |  |     console.log(`Try running ${SCRIPT_NAME} --help`); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-10-11 22:52:17 +08:00
										 |  |  |   const browsersJSON = require(path.join(CORE_PATH, 'browsers.json')); | 
					
						
							| 
									
										
										
										
											2023-04-14 04:57:47 +08:00
										 |  |  |   const browserName = { | 
					
						
							|  |  |  |     'cr': 'chromium', | 
					
						
							|  |  |  |     'ff': 'firefox', | 
					
						
							|  |  |  |     'ff-beta': 'firefox-beta', | 
					
						
							|  |  |  |     'wk': 'webkit', | 
					
						
							|  |  |  |   }[args[0].toLowerCase()] ?? args[0].toLowerCase(); | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  |   const descriptors = [browsersJSON.browsers.find(b => b.name === browserName)]; | 
					
						
							|  |  |  |   if (browserName === 'chromium') | 
					
						
							|  |  |  |     descriptors.push(browsersJSON.browsers.find(b => b.name === 'chromium-with-symbols')); | 
					
						
							| 
									
										
										
										
											2023-07-19 20:53:52 +08:00
										 |  |  |   if (browserName === 'firefox') | 
					
						
							|  |  |  |     descriptors.push(browsersJSON.browsers.find(b => b.name === 'firefox-asan')); | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!descriptors.every(d => !!d)) { | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  |     console.log(`Unknown browser "${browserName}"`); | 
					
						
							|  |  |  |     console.log(`Try running ${SCRIPT_NAME} --help`); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  |   const revision = args[1]; | 
					
						
							| 
									
										
										
										
											2020-07-23 02:03:35 +08:00
										 |  |  |   console.log(`Rolling ${browserName} to ${revision}`); | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  |   // 2. Update browser revisions in browsers.json.
 | 
					
						
							|  |  |  |   console.log('\nUpdating revision in browsers.json...'); | 
					
						
							|  |  |  |   for (const descriptor of descriptors) | 
					
						
							|  |  |  |     descriptor.revision = String(revision); | 
					
						
							| 
									
										
										
										
											2021-10-11 22:52:17 +08:00
										 |  |  |   fs.writeFileSync(path.join(CORE_PATH, 'browsers.json'), JSON.stringify(browsersJSON, null, 2) + '\n'); | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  |   // 3. Download new browser.
 | 
					
						
							|  |  |  |   console.log('\nDownloading new browser...'); | 
					
						
							|  |  |  |   const registry = new Registry(browsersJSON); | 
					
						
							|  |  |  |   const executable = registry.findExecutable(browserName); | 
					
						
							|  |  |  |   await registry.install([...registry.defaultExecutables(), executable]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 4. Update browser version if rolling WebKit / Firefox / Chromium.
 | 
					
						
							|  |  |  |   const browserType = playwright[browserName.split('-')[0]]; | 
					
						
							|  |  |  |   if (browserType) { | 
					
						
							|  |  |  |     const browser = await browserType.launch({ | 
					
						
							|  |  |  |       executablePath: executable.executablePath('javascript'), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const browserVersion = await browser.version(); | 
					
						
							|  |  |  |     await browser.close(); | 
					
						
							|  |  |  |     console.log('\nUpdating browser version in browsers.json...'); | 
					
						
							|  |  |  |     for (const descriptor of descriptors) | 
					
						
							|  |  |  |       descriptor.browserVersion = browserVersion; | 
					
						
							|  |  |  |     fs.writeFileSync(path.join(CORE_PATH, 'browsers.json'), JSON.stringify(browsersJSON, null, 2) + '\n'); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-02 06:22:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  |   if (browserType && descriptors[0].installByDefault) { | 
					
						
							|  |  |  |     // 5. Generate types.
 | 
					
						
							| 
									
										
										
										
											2021-04-23 08:45:34 +08:00
										 |  |  |     console.log('\nGenerating protocol types...'); | 
					
						
							| 
									
										
										
										
											2021-07-15 09:39:39 +08:00
										 |  |  |     const executablePath = registry.findExecutable(browserName).executablePathOrDie(); | 
					
						
							| 
									
										
										
										
											2021-04-23 08:45:34 +08:00
										 |  |  |     await protocolGenerator.generateProtocol(browserName, executablePath).catch(console.warn); | 
					
						
							| 
									
										
										
										
											2020-07-23 02:03:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 19:17:13 +08:00
										 |  |  |     // 6. Update docs.
 | 
					
						
							| 
									
										
										
										
											2021-04-23 08:45:34 +08:00
										 |  |  |     console.log('\nUpdating documentation...'); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       process.stdout.write(execSync('npm run --silent doc')); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-23 02:03:35 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   console.log(`\nRolled ${browserName} to ${revision}`); | 
					
						
							| 
									
										
										
										
											2021-09-02 00:17:06 +08:00
										 |  |  | })().catch(err => { | 
					
						
							|  |  |  |   console.error(err); | 
					
						
							|  |  |  |   process.exit(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 |