UNPKG

1.55 kBPlain TextView Raw
1import { execSync, ExecSyncOptions } from 'child_process';
2import tmp from 'tmp';
3import createDebug from 'debug';
4import path from 'path';
5import sudoPrompt from 'sudo-prompt';
6
7import {
8 configPath,
9} from './constants';
10
11const debug = createDebug('devcert:util');
12
13export function openssl(cmd: string) {
14 return run(`openssl ${ cmd }`, {
15 stdio: 'pipe',
16 env: Object.assign({
17 RANDFILE: path.join(configPath('.rnd'))
18 }, process.env)
19 });
20}
21
22export function run(cmd: string, options: ExecSyncOptions = {}) {
23 debug(`exec: \`${ cmd }\``);
24 return execSync(cmd, options);
25}
26
27export function waitForUser() {
28 return new Promise((resolve) => {
29 process.stdin.resume();
30 process.stdin.on('data', resolve);
31 });
32}
33
34export function reportableError(message: string) {
35 return new Error(`${message} | This is a bug in devcert, please report the issue at https://github.com/davewasmer/devcert/issues`);
36}
37
38export function mktmp() {
39 // discardDescriptor because windows complains the file is in use if we create a tmp file
40 // and then shell out to a process that tries to use it
41 return tmp.fileSync({ discardDescriptor: true }).name;
42}
43
44export function sudo(cmd: string): Promise<string | null> {
45 return new Promise((resolve, reject) => {
46 sudoPrompt.exec(cmd, { name: 'devcert' }, (err: Error | null, stdout: string | null, stderr: string | null) => {
47 let error = err || (typeof stderr === 'string' && stderr.trim().length > 0 && new Error(stderr)) ;
48 error ? reject(error) : resolve(stdout);
49 });
50 });
51}