Node updated. Some todos.

This commit is contained in:
Norm Rasmussen
2024-09-23 20:52:09 -04:00
parent 8bfaca8375
commit f25622067f
2041 changed files with 124145 additions and 110445 deletions

View File

@ -27,14 +27,16 @@ import {
launch,
} from './launch.js';
interface InstallBrowser {
name: Browser;
buildId: string;
}
interface InstallArgs {
browser: {
name: Browser;
buildId: string;
};
browser?: InstallBrowser;
path?: string;
platform?: BrowserPlatform;
baseUrl?: string;
installDeps?: boolean;
}
interface LaunchArgs {
@ -60,7 +62,12 @@ export class CLI {
#rl?: readline.Interface;
#scriptName = '';
#allowCachePathOverride = true;
#pinnedBrowsers?: Partial<{[key in Browser]: string}>;
#pinnedBrowsers?: Partial<{
[key in Browser]: {
buildId: string;
skipDownload: boolean;
};
}>;
#prefixCommand?: {cmd: string; description: string};
constructor(
@ -71,7 +78,12 @@ export class CLI {
scriptName?: string;
prefixCommand?: {cmd: string; description: string};
allowCachePathOverride?: boolean;
pinnedBrowsers?: Partial<{[key in Browser]: string}>;
pinnedBrowsers?: Partial<{
[key in Browser]: {
buildId: string;
skipDownload: boolean;
};
}>;
},
rl?: readline.Interface
) {
@ -152,9 +164,11 @@ export class CLI {
#build(yargs: Yargs.Argv<unknown>): Yargs.Argv<unknown> {
const latestOrPinned = this.#pinnedBrowsers ? 'pinned' : 'latest';
// If there are pinned browsers allow the positional arg to be optional
const browserArgType = this.#pinnedBrowsers ? '[browser]' : '<browser>';
return yargs
.command(
'install <browser>',
`install ${browserArgType}`,
'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 => {
this.#defineBrowserParameter(yargs);
@ -164,6 +178,14 @@ export class CLI {
type: 'string',
desc: 'Base URL to download from',
});
if (this.#pinnedBrowsers) {
yargs.example('$0 install', 'Install all pinned browsers');
}
yargs.option('install-deps', {
type: 'boolean',
desc: 'Whether to attempt installing system dependencies (only supported on Linux, requires root privileges).',
default: false,
});
yargs.example(
'$0 install chrome',
`Install the ${latestOrPinned} available build of the Chrome browser.`
@ -172,6 +194,18 @@ export class CLI {
'$0 install chrome@latest',
'Install the latest available build for the Chrome browser.'
);
yargs.example(
'$0 install chrome@stable',
'Install the latest available build for the Chrome browser from the stable channel.'
);
yargs.example(
'$0 install chrome@beta',
'Install the latest available build for the Chrome browser from the beta channel.'
);
yargs.example(
'$0 install chrome@dev',
'Install the latest available build for the Chrome browser from the dev channel.'
);
yargs.example(
'$0 install chrome@canary',
'Install the latest available build for the Chrome Canary browser.'
@ -210,7 +244,31 @@ export class CLI {
);
yargs.example(
'$0 install firefox',
'Install the latest available build of the Firefox browser.'
'Install the latest nightly available build of the Firefox browser.'
);
yargs.example(
'$0 install firefox@stable',
'Install the latest stable build of the Firefox browser.'
);
yargs.example(
'$0 install firefox@beta',
'Install the latest beta build of the Firefox browser.'
);
yargs.example(
'$0 install firefox@devedition',
'Install the latest devedition build of the Firefox browser.'
);
yargs.example(
'$0 install firefox@esr',
'Install the latest ESR build of the Firefox browser.'
);
yargs.example(
'$0 install firefox@nightly',
'Install the latest nightly build of the Firefox browser.'
);
yargs.example(
'$0 install firefox@stable_111.0.1',
'Install a specific version of the Firefox browser.'
);
yargs.example(
'$0 install firefox --platform mac',
@ -225,45 +283,35 @@ export class CLI {
},
async argv => {
const args = argv as unknown as InstallArgs;
args.platform ??= detectBrowserPlatform();
if (!args.platform) {
throw new Error(`Could not resolve the current platform`);
}
if (args.browser.buildId === 'pinned') {
const pinnedVersion = this.#pinnedBrowsers?.[args.browser.name];
if (!pinnedVersion) {
throw new Error(
`No pinned version found for ${args.browser.name}`
);
if (this.#pinnedBrowsers && !args.browser) {
// Use allSettled to avoid scenarios that
// a browser may fail early and leave the other
// installation in a faulty state
const result = await Promise.allSettled(
Object.entries(this.#pinnedBrowsers).map(
async ([browser, options]) => {
if (options.skipDownload) {
return;
}
await this.#install({
...argv,
browser: {
name: browser as Browser,
buildId: options.buildId,
},
});
}
)
);
for (const install of result) {
if (install.status === 'rejected') {
throw install.reason;
}
}
args.browser.buildId = pinnedVersion;
} else {
await this.#install(args);
}
args.browser.buildId = await resolveBuildId(
args.browser.name,
args.platform,
args.browser.buildId
);
await install({
browser: args.browser.name,
buildId: args.browser.buildId,
platform: args.platform,
cacheDir: args.path ?? this.#cachePath,
downloadProgressCallback: makeProgressCallback(
args.browser.name,
args.browser.buildId
),
baseUrl: args.baseUrl,
});
console.log(
`${args.browser.name}@${
args.browser.buildId
} ${computeExecutablePath({
browser: args.browser.name,
buildId: args.browser.buildId,
cacheDir: args.path ?? this.#cachePath,
platform: args.platform,
})}`
);
}
)
.command(
@ -364,6 +412,51 @@ export class CLI {
? 'pinned'
: 'latest';
}
async #install(args: InstallArgs) {
args.platform ??= detectBrowserPlatform();
if (!args.browser) {
throw new Error(`No browser arg proveded`);
}
if (!args.platform) {
throw new Error(`Could not resolve the current platform`);
}
if (args.browser.buildId === 'pinned') {
const options = this.#pinnedBrowsers?.[args.browser.name];
if (!options || !options.buildId) {
throw new Error(`No pinned version found for ${args.browser.name}`);
}
args.browser.buildId = options.buildId;
}
const originalBuildId = args.browser.buildId;
args.browser.buildId = await resolveBuildId(
args.browser.name,
args.platform,
args.browser.buildId
);
await install({
browser: args.browser.name,
buildId: args.browser.buildId,
platform: args.platform,
cacheDir: args.path ?? this.#cachePath,
downloadProgressCallback: makeProgressCallback(
args.browser.name,
args.browser.buildId
),
baseUrl: args.baseUrl,
buildIdAlias:
originalBuildId !== args.browser.buildId ? originalBuildId : undefined,
installDeps: args.installDeps,
});
console.log(
`${args.browser.name}@${args.browser.buildId} ${computeExecutablePath({
browser: args.browser.name,
buildId: args.browser.buildId,
cacheDir: args.path ?? this.#cachePath,
platform: args.platform,
})}`
);
}
}
/**
@ -378,7 +471,7 @@ export function makeProgressCallback(
return (downloadedBytes: number, totalBytes: number) => {
if (!progressBar) {
progressBar = new ProgressBar(
`Downloading ${browser} r${buildId} - ${toMegabytes(
`Downloading ${browser} ${buildId} - ${toMegabytes(
totalBytes
)} [:bar] :percent :etas `,
{