2023-02-15 19:41:22 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2023 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-14 15:30:41 +00:00
|
|
|
import ProgressBar from 'progress';
|
2023-02-15 23:09:31 +00:00
|
|
|
import yargs from 'yargs';
|
2023-02-14 15:30:41 +00:00
|
|
|
import {hideBin} from 'yargs/helpers';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-02-21 16:15:49 +00:00
|
|
|
import {resolveBuildId} from './browsers/browsers.js';
|
2023-02-14 15:30:41 +00:00
|
|
|
import {Browser, BrowserPlatform} from './browsers/types.js';
|
2023-02-21 14:19:06 +00:00
|
|
|
import {detectBrowserPlatform} from './detectPlatform.js';
|
2023-02-14 15:30:41 +00:00
|
|
|
import {fetch} from './fetch.js';
|
2023-02-17 06:11:50 +00:00
|
|
|
import {computeExecutablePath, launch} from './launcher.js';
|
2023-02-14 15:30:41 +00:00
|
|
|
|
2023-02-17 06:11:50 +00:00
|
|
|
type InstallArgs = {
|
2023-02-14 15:30:41 +00:00
|
|
|
browser: {
|
|
|
|
name: Browser;
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: string;
|
2023-02-14 15:30:41 +00:00
|
|
|
};
|
|
|
|
path?: string;
|
|
|
|
platform?: BrowserPlatform;
|
|
|
|
};
|
|
|
|
|
2023-02-17 06:11:50 +00:00
|
|
|
type LaunchArgs = {
|
|
|
|
browser: {
|
|
|
|
name: Browser;
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: string;
|
2023-02-17 06:11:50 +00:00
|
|
|
};
|
|
|
|
path?: string;
|
|
|
|
platform?: BrowserPlatform;
|
|
|
|
detached: boolean;
|
|
|
|
};
|
|
|
|
|
2023-02-14 15:30:41 +00:00
|
|
|
export class CLI {
|
|
|
|
#cachePath;
|
|
|
|
|
|
|
|
constructor(cachePath = process.cwd()) {
|
|
|
|
this.#cachePath = cachePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
async run(argv: string[]): Promise<void> {
|
|
|
|
await yargs(hideBin(argv))
|
|
|
|
.command(
|
2023-02-17 06:11:50 +00:00
|
|
|
'install <browser>',
|
|
|
|
'Download and install the specified browser',
|
2023-02-14 15:30:41 +00:00
|
|
|
yargs => {
|
|
|
|
yargs.positional('browser', {
|
|
|
|
description: 'The browser version',
|
|
|
|
type: 'string',
|
2023-02-17 06:11:50 +00:00
|
|
|
coerce: (opt): InstallArgs['browser'] => {
|
2023-02-14 15:30:41 +00:00
|
|
|
return {
|
|
|
|
name: this.#parseBrowser(opt),
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: this.#parseBuildId(opt),
|
2023-02-14 15:30:41 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
async argv => {
|
2023-02-17 06:11:50 +00:00
|
|
|
const args = argv as unknown as InstallArgs;
|
2023-02-21 14:19:06 +00:00
|
|
|
args.platform ??= detectBrowserPlatform();
|
|
|
|
if (!args.platform) {
|
|
|
|
throw new Error(`Could not resolve the current platform`);
|
|
|
|
}
|
2023-02-21 16:15:49 +00:00
|
|
|
args.browser.buildId = await resolveBuildId(
|
2023-02-21 07:27:02 +00:00
|
|
|
args.browser.name,
|
2023-02-21 14:19:06 +00:00
|
|
|
args.platform,
|
2023-02-21 16:15:49 +00:00
|
|
|
args.browser.buildId
|
2023-02-21 07:27:02 +00:00
|
|
|
);
|
2023-02-14 15:30:41 +00:00
|
|
|
await fetch({
|
|
|
|
browser: args.browser.name,
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: args.browser.buildId,
|
2023-02-14 15:30:41 +00:00
|
|
|
platform: args.platform,
|
2023-02-15 19:41:22 +00:00
|
|
|
cacheDir: args.path ?? this.#cachePath,
|
|
|
|
downloadProgressCallback: this.#makeProgressCallback(
|
2023-02-14 15:30:41 +00:00
|
|
|
args.browser.name,
|
2023-02-21 16:15:49 +00:00
|
|
|
args.browser.buildId
|
2023-02-14 15:30:41 +00:00
|
|
|
),
|
|
|
|
});
|
2023-02-21 07:27:02 +00:00
|
|
|
console.log(
|
2023-02-21 16:15:49 +00:00
|
|
|
`${args.browser.name}@${args.browser.buildId} downloaded successfully.`
|
2023-02-21 07:27:02 +00:00
|
|
|
);
|
2023-02-14 15:30:41 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.option('path', {
|
|
|
|
type: 'string',
|
|
|
|
desc: 'Path where the browsers will be downloaded to and installed from',
|
|
|
|
default: process.cwd(),
|
|
|
|
})
|
|
|
|
.option('platform', {
|
|
|
|
type: 'string',
|
|
|
|
desc: 'Platform that the binary needs to be compatible with.',
|
|
|
|
choices: Object.values(BrowserPlatform),
|
|
|
|
defaultDescription: 'Auto-detected by default.',
|
|
|
|
})
|
2023-02-17 06:11:50 +00:00
|
|
|
.command(
|
|
|
|
'launch <browser>',
|
|
|
|
'Launch the specified browser',
|
|
|
|
yargs => {
|
|
|
|
yargs.positional('browser', {
|
|
|
|
description: 'The browser version',
|
|
|
|
type: 'string',
|
|
|
|
coerce: (opt): LaunchArgs['browser'] => {
|
|
|
|
return {
|
|
|
|
name: this.#parseBrowser(opt),
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: this.#parseBuildId(opt),
|
2023-02-17 06:11:50 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
async argv => {
|
|
|
|
const args = argv as unknown as LaunchArgs;
|
|
|
|
const executablePath = computeExecutablePath({
|
|
|
|
browser: args.browser.name,
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: args.browser.buildId,
|
2023-02-17 06:11:50 +00:00
|
|
|
cacheDir: args.path ?? this.#cachePath,
|
|
|
|
platform: args.platform,
|
|
|
|
});
|
|
|
|
launch({
|
|
|
|
executablePath,
|
|
|
|
detached: args.detached,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.option('path', {
|
|
|
|
type: 'string',
|
|
|
|
desc: 'Path where the browsers will be downloaded to and installed from',
|
|
|
|
default: process.cwd(),
|
|
|
|
})
|
|
|
|
.option('detached', {
|
|
|
|
type: 'boolean',
|
|
|
|
desc: 'Whether to detach the child process.',
|
|
|
|
default: false,
|
|
|
|
})
|
|
|
|
.option('platform', {
|
|
|
|
type: 'string',
|
|
|
|
desc: 'Platform that the binary needs to be compatible with.',
|
|
|
|
choices: Object.values(BrowserPlatform),
|
|
|
|
defaultDescription: 'Auto-detected by default.',
|
|
|
|
})
|
2023-02-14 15:30:41 +00:00
|
|
|
.parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
#parseBrowser(version: string): Browser {
|
|
|
|
return version.split('@').shift() as Browser;
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:15:49 +00:00
|
|
|
#parseBuildId(version: string): string {
|
2023-02-14 15:30:41 +00:00
|
|
|
return version.split('@').pop() ?? 'latest';
|
|
|
|
}
|
|
|
|
|
|
|
|
#toMegabytes(bytes: number) {
|
|
|
|
const mb = bytes / 1024 / 1024;
|
|
|
|
return `${Math.round(mb * 10) / 10} Mb`;
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:15:49 +00:00
|
|
|
#makeProgressCallback(browser: Browser, buildId: string) {
|
2023-02-15 19:41:22 +00:00
|
|
|
let progressBar: ProgressBar;
|
2023-02-14 15:30:41 +00:00
|
|
|
let lastDownloadedBytes = 0;
|
|
|
|
return (downloadedBytes: number, totalBytes: number) => {
|
|
|
|
if (!progressBar) {
|
|
|
|
progressBar = new ProgressBar(
|
2023-02-21 16:15:49 +00:00
|
|
|
`Downloading ${browser} r${buildId} - ${this.#toMegabytes(
|
2023-02-14 15:30:41 +00:00
|
|
|
totalBytes
|
|
|
|
)} [:bar] :percent :etas `,
|
|
|
|
{
|
|
|
|
complete: '=',
|
|
|
|
incomplete: ' ',
|
|
|
|
width: 20,
|
|
|
|
total: totalBytes,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const delta = downloadedBytes - lastDownloadedBytes;
|
|
|
|
lastDownloadedBytes = downloadedBytes;
|
|
|
|
progressBar.tick(delta);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|