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-14 15:30:41 +00:00
|
|
|
import {Browser, BrowserPlatform} from './browsers/types.js';
|
|
|
|
import {fetch} from './fetch.js';
|
|
|
|
|
|
|
|
type Arguments = {
|
|
|
|
browser: {
|
|
|
|
name: Browser;
|
|
|
|
revision: string;
|
|
|
|
};
|
|
|
|
path?: string;
|
|
|
|
platform?: BrowserPlatform;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class CLI {
|
|
|
|
#cachePath;
|
|
|
|
|
|
|
|
constructor(cachePath = process.cwd()) {
|
|
|
|
this.#cachePath = cachePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
async run(argv: string[]): Promise<void> {
|
|
|
|
await yargs(hideBin(argv))
|
|
|
|
.command(
|
|
|
|
'$0 install <browser>',
|
|
|
|
'run files',
|
|
|
|
yargs => {
|
|
|
|
yargs.positional('browser', {
|
|
|
|
description: 'The browser version',
|
|
|
|
type: 'string',
|
|
|
|
coerce: (opt): Arguments['browser'] => {
|
|
|
|
return {
|
|
|
|
name: this.#parseBrowser(opt),
|
|
|
|
revision: this.#parseRevision(opt),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
async argv => {
|
|
|
|
const args = argv as unknown as Arguments;
|
|
|
|
await fetch({
|
|
|
|
browser: args.browser.name,
|
|
|
|
revision: args.browser.revision,
|
|
|
|
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,
|
|
|
|
args.browser.revision
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.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.',
|
|
|
|
})
|
|
|
|
.parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
#parseBrowser(version: string): Browser {
|
|
|
|
return version.split('@').shift() as Browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
#parseRevision(version: string): string {
|
|
|
|
return version.split('@').pop() ?? 'latest';
|
|
|
|
}
|
|
|
|
|
|
|
|
#toMegabytes(bytes: number) {
|
|
|
|
const mb = bytes / 1024 / 1024;
|
|
|
|
return `${Math.round(mb * 10) / 10} Mb`;
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:41:22 +00:00
|
|
|
#makeProgressCallback(browser: Browser, revision: string) {
|
|
|
|
let progressBar: ProgressBar;
|
2023-02-14 15:30:41 +00:00
|
|
|
let lastDownloadedBytes = 0;
|
|
|
|
return (downloadedBytes: number, totalBytes: number) => {
|
|
|
|
if (!progressBar) {
|
|
|
|
progressBar = new ProgressBar(
|
|
|
|
`Downloading ${browser} r${revision} - ${this.#toMegabytes(
|
|
|
|
totalBytes
|
|
|
|
)} [:bar] :percent :etas `,
|
|
|
|
{
|
|
|
|
complete: '=',
|
|
|
|
incomplete: ' ',
|
|
|
|
width: 20,
|
|
|
|
total: totalBytes,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const delta = downloadedBytes - lastDownloadedBytes;
|
|
|
|
lastDownloadedBytes = downloadedBytes;
|
|
|
|
progressBar.tick(delta);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|