fix(install-deps): make it work without sudo (#8914)

This commit is contained in:
Max Schmitt 2021-09-14 14:09:37 +02:00 committed by GitHub
parent 299a55c562
commit 6a3d2b9fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View File

@ -69,11 +69,22 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>) {
commands.push(['apt-get', 'install', '-y', '--no-install-recommends',
...uniqueLibraries,
].join(' '));
const isRoot = (process.getuid() === 0);
const child = isRoot ?
childProcess.spawn('sh', ['-c', `${commands.join('; ')}`], { stdio: 'inherit' }) :
childProcess.spawn('sudo', ['--', 'sh', '-c', `${commands.join('; ')}`], { stdio: 'inherit' });
await new Promise(f => child.on('exit', f));
const [command, args] = await buildAptProcessArgs(commands);
const child = childProcess.spawn(command, args, { stdio: 'inherit' });
await new Promise((resolve, reject) => {
child.on('exit', resolve);
child.on('error', reject);
});
}
async function buildAptProcessArgs(commands: string[]): Promise<[string, string[]]> {
const isRoot = process.getuid() === 0;
if (isRoot)
return ['sh', ['-c', `${commands.join('&& ')}`]];
const sudoExists = await utils.spawnAsync('which', ['sudo']);
if (sudoExists.code === 0)
return ['sudo', ['--', 'sh', '-c', `${commands.join('&& ')}`]];
return ['su', ['root', '-c', `${commands.join('&& ')}`]];
}
export async function validateDependenciesWindows(windowsExeAndDllDirectories: string[]) {

View File

@ -20,7 +20,7 @@ import stream from 'stream';
import removeFolder from 'rimraf';
import * as crypto from 'crypto';
import os from 'os';
import { spawn } from 'child_process';
import { spawn, SpawnOptions } from 'child_process';
import { getProxyForUrl } from 'proxy-from-env';
import * as URL from 'url';
import { getUbuntuVersionSync } from './ubuntuVersion';
@ -131,7 +131,7 @@ export function downloadFile(url: string, destinationPath: string, options: {pro
}
}
export function spawnAsync(cmd: string, args: string[], options: any): Promise<{stdout: string, stderr: string, code: number, error?: Error}> {
export function spawnAsync(cmd: string, args: string[], options?: SpawnOptions): Promise<{stdout: string, stderr: string, code: number, error?: Error}> {
const process = spawn(cmd, args, options);
return new Promise(resolve => {