chore: add --dry-run to install-deps CLI command (#10520)

This commit is contained in:
Max Schmitt 2021-11-25 01:04:42 +01:00 committed by GitHub
parent 5d19f16601
commit da02c2e2c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 12 deletions

View file

@ -107,7 +107,7 @@ program
if (!args.length) {
const executables = registry.defaultExecutables();
if (options.withDeps)
await registry.installDeps(executables);
await registry.installDeps(executables, false);
await registry.install(executables);
} else {
const installDockerImage = args.some(arg => arg === 'docker-image');
@ -123,7 +123,7 @@ program
const executables = checkBrowsersToInstall(args);
if (options.withDeps)
await registry.installDeps(executables);
await registry.installDeps(executables, false);
await registry.install(executables);
}
} catch (e) {
@ -143,12 +143,13 @@ Examples:
program
.command('install-deps [browser...]')
.description('install dependencies necessary to run browsers (will ask for sudo permissions)')
.action(async function(args: string[]) {
.option('--dry-run', 'Do not execute installation commands, only print them')
.action(async function(args: string[], options: { dryRun?: boolean }) {
try {
if (!args.length)
await registry.installDeps(registry.defaultExecutables());
await registry.installDeps(registry.defaultExecutables(), !!options.dryRun);
else
await registry.installDeps(checkBrowsersToInstall(args));
await registry.installDeps(checkBrowsersToInstall(args), !!options.dryRun);
} catch (e) {
console.log(`Failed to install browser dependencies\n${e}`);
process.exit(1);

View file

@ -38,15 +38,21 @@ function isSupportedWindowsVersion(): boolean {
export type DependencyGroup = 'chromium' | 'firefox' | 'webkit' | 'tools';
export async function installDependenciesWindows(targets: Set<DependencyGroup>) {
export async function installDependenciesWindows(targets: Set<DependencyGroup>, dryRun: boolean): Promise<void> {
if (targets.has('chromium')) {
const { code } = await utils.spawnAsync('powershell.exe', ['-ExecutionPolicy', 'Bypass', '-File', path.join(BIN_DIRECTORY, 'install_media_pack.ps1')], { cwd: BIN_DIRECTORY, stdio: 'inherit' });
const command = 'powershell.exe';
const args = ['-ExecutionPolicy', 'Bypass', '-File', path.join(BIN_DIRECTORY, 'install_media_pack.ps1')];
if (dryRun) {
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
return;
}
const { code } = await utils.spawnAsync(command, args, { cwd: BIN_DIRECTORY, stdio: 'inherit' });
if (code !== 0)
throw new Error('Failed to install windows dependencies!');
}
}
export async function installDependenciesLinux(targets: Set<DependencyGroup>) {
export async function installDependenciesLinux(targets: Set<DependencyGroup>, dryRun: boolean) {
const libraries: string[] = [];
for (const target of targets) {
const info = deps[utils.hostPlatform];
@ -57,13 +63,18 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>) {
libraries.push(...info[target]);
}
const uniqueLibraries = Array.from(new Set(libraries));
console.log('Installing Ubuntu dependencies...'); // eslint-disable-line no-console
if (!dryRun)
console.log('Installing Ubuntu dependencies...'); // eslint-disable-line no-console
const commands: string[] = [];
commands.push('apt-get update');
commands.push(['apt-get', 'install', '-y', '--no-install-recommends',
...uniqueLibraries,
].join(' '));
const [command, args] = await buildAptProcessArgs(commands);
if (dryRun) {
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
return;
}
const child = childProcess.spawn(command, args, { stdio: 'inherit' });
await new Promise((resolve, reject) => {
child.on('exit', resolve);
@ -293,3 +304,11 @@ const MANUAL_LIBRARY_TO_PACKAGE_NAME_UBUNTU: { [s: string]: string} = {
// gstreamer1.0-libav -> libavcodec57 -> libx264-152
'libx264.so': 'gstreamer1.0-libav',
};
function quoteProcessArgs(args: string[]): string[] {
return args.map(arg => {
if (arg.includes(' '))
return `"${arg}"`;
return arg;
});
}

View file

@ -526,7 +526,7 @@ export class Registry {
return await validateDependenciesWindows(windowsExeAndDllDirectories.map(d => path.join(browserDirectory, d)));
}
async installDeps(executablesToInstallDeps: Executable[]) {
async installDeps(executablesToInstallDeps: Executable[], dryRun: boolean) {
const executables = this._addRequirementsAndDedupe(executablesToInstallDeps);
const targets = new Set<DependencyGroup>();
for (const executable of executables) {
@ -535,9 +535,9 @@ export class Registry {
}
targets.add('tools');
if (os.platform() === 'win32')
return await installDependenciesWindows(targets);
return await installDependenciesWindows(targets, dryRun);
if (os.platform() === 'linux')
return await installDependenciesLinux(targets);
return await installDependenciesLinux(targets, dryRun);
}
async install(executablesToInstall: Executable[]) {