CLI.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. "use strict";
  2. /**
  3. * Copyright 2023 Google Inc. All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  18. if (k2 === undefined) k2 = k;
  19. var desc = Object.getOwnPropertyDescriptor(m, k);
  20. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  21. desc = { enumerable: true, get: function() { return m[k]; } };
  22. }
  23. Object.defineProperty(o, k2, desc);
  24. }) : (function(o, m, k, k2) {
  25. if (k2 === undefined) k2 = k;
  26. o[k2] = m[k];
  27. }));
  28. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  29. Object.defineProperty(o, "default", { enumerable: true, value: v });
  30. }) : function(o, v) {
  31. o["default"] = v;
  32. });
  33. var __importStar = (this && this.__importStar) || function (mod) {
  34. if (mod && mod.__esModule) return mod;
  35. var result = {};
  36. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  37. __setModuleDefault(result, mod);
  38. return result;
  39. };
  40. var __importDefault = (this && this.__importDefault) || function (mod) {
  41. return (mod && mod.__esModule) ? mod : { "default": mod };
  42. };
  43. Object.defineProperty(exports, "__esModule", { value: true });
  44. exports.makeProgressCallback = exports.CLI = void 0;
  45. const process_1 = require("process");
  46. const readline = __importStar(require("readline"));
  47. const progress_1 = __importDefault(require("progress"));
  48. const helpers_1 = require("yargs/helpers");
  49. const yargs_1 = __importDefault(require("yargs/yargs"));
  50. const browser_data_js_1 = require("./browser-data/browser-data.js");
  51. const Cache_js_1 = require("./Cache.js");
  52. const detectPlatform_js_1 = require("./detectPlatform.js");
  53. const install_js_1 = require("./install.js");
  54. const launch_js_1 = require("./launch.js");
  55. /**
  56. * @public
  57. */
  58. class CLI {
  59. #cachePath;
  60. #rl;
  61. constructor(cachePath = process.cwd(), rl) {
  62. this.#cachePath = cachePath;
  63. this.#rl = rl;
  64. }
  65. #defineBrowserParameter(yargs) {
  66. yargs.positional('browser', {
  67. description: 'Which browser to install <browser>[@<buildId|latest>]. `latest` will try to find the latest available build. `buildId` is a browser-specific identifier such as a version or a revision.',
  68. type: 'string',
  69. coerce: (opt) => {
  70. return {
  71. name: this.#parseBrowser(opt),
  72. buildId: this.#parseBuildId(opt),
  73. };
  74. },
  75. });
  76. }
  77. #definePlatformParameter(yargs) {
  78. yargs.option('platform', {
  79. type: 'string',
  80. desc: 'Platform that the binary needs to be compatible with.',
  81. choices: Object.values(browser_data_js_1.BrowserPlatform),
  82. defaultDescription: 'Auto-detected',
  83. });
  84. }
  85. #definePathParameter(yargs, required = false) {
  86. yargs.option('path', {
  87. type: 'string',
  88. desc: 'Path to the root folder for the browser downloads and installation. The installation folder structure is compatible with the cache structure used by Puppeteer.',
  89. defaultDescription: 'Current working directory',
  90. ...(required ? {} : { default: process.cwd() }),
  91. });
  92. if (required) {
  93. yargs.demandOption('path');
  94. }
  95. }
  96. async run(argv) {
  97. const yargsInstance = (0, yargs_1.default)((0, helpers_1.hideBin)(argv));
  98. await yargsInstance
  99. .scriptName('@puppeteer/browsers')
  100. .command('install <browser>', 'Download and install the specified browser. If successful, the command outputs the actual browser buildId that was installed and the absolute path to the browser executable (format: <browser>@<buildID> <path>).', yargs => {
  101. this.#defineBrowserParameter(yargs);
  102. this.#definePlatformParameter(yargs);
  103. this.#definePathParameter(yargs);
  104. yargs.option('base-url', {
  105. type: 'string',
  106. desc: 'Base URL to download from',
  107. });
  108. yargs.example('$0 install chrome', 'Install the latest available build of the Chrome browser.');
  109. yargs.example('$0 install chrome@latest', 'Install the latest available build for the Chrome browser.');
  110. yargs.example('$0 install chromium@1083080', 'Install the revision 1083080 of the Chromium browser.');
  111. yargs.example('$0 install firefox', 'Install the latest available build of the Firefox browser.');
  112. yargs.example('$0 install firefox --platform mac', 'Install the latest Mac (Intel) build of the Firefox browser.');
  113. yargs.example('$0 install firefox --path /tmp/my-browser-cache', 'Install to the specified cache directory.');
  114. }, async (argv) => {
  115. const args = argv;
  116. args.platform ??= (0, detectPlatform_js_1.detectBrowserPlatform)();
  117. if (!args.platform) {
  118. throw new Error(`Could not resolve the current platform`);
  119. }
  120. args.browser.buildId = await (0, browser_data_js_1.resolveBuildId)(args.browser.name, args.platform, args.browser.buildId);
  121. await (0, install_js_1.install)({
  122. browser: args.browser.name,
  123. buildId: args.browser.buildId,
  124. platform: args.platform,
  125. cacheDir: args.path ?? this.#cachePath,
  126. downloadProgressCallback: makeProgressCallback(args.browser.name, args.browser.buildId),
  127. baseUrl: args.baseUrl,
  128. });
  129. console.log(`${args.browser.name}@${args.browser.buildId} ${(0, launch_js_1.computeExecutablePath)({
  130. browser: args.browser.name,
  131. buildId: args.browser.buildId,
  132. cacheDir: args.path ?? this.#cachePath,
  133. platform: args.platform,
  134. })}`);
  135. })
  136. .command('launch <browser>', 'Launch the specified browser', yargs => {
  137. this.#defineBrowserParameter(yargs);
  138. this.#definePlatformParameter(yargs);
  139. this.#definePathParameter(yargs);
  140. yargs.option('detached', {
  141. type: 'boolean',
  142. desc: 'Detach the child process.',
  143. default: false,
  144. });
  145. yargs.option('system', {
  146. type: 'boolean',
  147. desc: 'Search for a browser installed on the system instead of the cache folder.',
  148. default: false,
  149. });
  150. yargs.example('$0 launch chrome@1083080', 'Launch the Chrome browser identified by the revision 1083080.');
  151. yargs.example('$0 launch firefox@112.0a1', 'Launch the Firefox browser identified by the milestone 112.0a1.');
  152. yargs.example('$0 launch chrome@1083080 --detached', 'Launch the browser but detach the sub-processes.');
  153. yargs.example('$0 launch chrome@canary --system', 'Try to locate the Canary build of Chrome installed on the system and launch it.');
  154. }, async (argv) => {
  155. const args = argv;
  156. const executablePath = args.system
  157. ? (0, launch_js_1.computeSystemExecutablePath)({
  158. browser: args.browser.name,
  159. // TODO: throw an error if not a ChromeReleaseChannel is provided.
  160. channel: args.browser.buildId,
  161. platform: args.platform,
  162. })
  163. : (0, launch_js_1.computeExecutablePath)({
  164. browser: args.browser.name,
  165. buildId: args.browser.buildId,
  166. cacheDir: args.path ?? this.#cachePath,
  167. platform: args.platform,
  168. });
  169. (0, launch_js_1.launch)({
  170. executablePath,
  171. detached: args.detached,
  172. });
  173. })
  174. .command('clear', 'Removes all installed browsers from the specified cache directory', yargs => {
  175. this.#definePathParameter(yargs, true);
  176. }, async (argv) => {
  177. const args = argv;
  178. const cacheDir = args.path ?? this.#cachePath;
  179. const rl = this.#rl ?? readline.createInterface({ input: process_1.stdin, output: process_1.stdout });
  180. rl.question(`Do you want to permanently and recursively delete the content of ${cacheDir} (yes/No)? `, answer => {
  181. rl.close();
  182. if (!['y', 'yes'].includes(answer.toLowerCase().trim())) {
  183. console.log('Cancelled.');
  184. return;
  185. }
  186. const cache = new Cache_js_1.Cache(cacheDir);
  187. cache.clear();
  188. console.log(`${cacheDir} cleared.`);
  189. });
  190. })
  191. .demandCommand(1)
  192. .help()
  193. .wrap(Math.min(120, yargsInstance.terminalWidth()))
  194. .parse();
  195. }
  196. #parseBrowser(version) {
  197. return version.split('@').shift();
  198. }
  199. #parseBuildId(version) {
  200. const parts = version.split('@');
  201. return parts.length === 2 ? parts[1] : 'latest';
  202. }
  203. }
  204. exports.CLI = CLI;
  205. /**
  206. * @public
  207. */
  208. function makeProgressCallback(browser, buildId) {
  209. let progressBar;
  210. let lastDownloadedBytes = 0;
  211. return (downloadedBytes, totalBytes) => {
  212. if (!progressBar) {
  213. progressBar = new progress_1.default(`Downloading ${browser} r${buildId} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, {
  214. complete: '=',
  215. incomplete: ' ',
  216. width: 20,
  217. total: totalBytes,
  218. });
  219. }
  220. const delta = downloadedBytes - lastDownloadedBytes;
  221. lastDownloadedBytes = downloadedBytes;
  222. progressBar.tick(delta);
  223. };
  224. }
  225. exports.makeProgressCallback = makeProgressCallback;
  226. function toMegabytes(bytes) {
  227. const mb = bytes / 1000 / 1000;
  228. return `${Math.round(mb * 10) / 10} MB`;
  229. }
  230. //# sourceMappingURL=CLI.js.map