balena-cli/lib/utils/sudo.ts

27 lines
605 B
TypeScript
Raw Normal View History

import { spawn } from 'child_process';
import * as Bluebird from 'bluebird';
import * as rindle from 'rindle';
export async function executeWithPrivileges(
command: string[],
stderr?: NodeJS.WritableStream,
): Promise<string> {
const opts = {
stdio: ['inherit', 'inherit', stderr ? 'pipe' : 'inherit'],
env: process.env,
};
const args = process.argv
.slice(0, 2)
.concat(['internal', 'sudo', command.join(' ')]);
const ps = spawn(args[0], args.slice(1), opts);
if (stderr) {
ps.stderr.pipe(stderr);
}
return Bluebird.fromCallback<string>(callback => rindle.wait(ps, callback));
}