1 | import { execSync, ExecSyncOptions } from 'child_process';
|
2 | import tmp from 'tmp';
|
3 | import createDebug from 'debug';
|
4 | import path from 'path';
|
5 | import sudoPrompt from 'sudo-prompt';
|
6 |
|
7 | import {
|
8 | configPath,
|
9 | } from './constants';
|
10 |
|
11 | const debug = createDebug('devcert:util');
|
12 |
|
13 | export 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 |
|
22 | export function run(cmd: string, options: ExecSyncOptions = {}) {
|
23 | debug(`exec: \`${ cmd }\``);
|
24 | return execSync(cmd, options);
|
25 | }
|
26 |
|
27 | export function waitForUser() {
|
28 | return new Promise((resolve) => {
|
29 | process.stdin.resume();
|
30 | process.stdin.on('data', resolve);
|
31 | });
|
32 | }
|
33 |
|
34 | export 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 |
|
38 | export function mktmp() {
|
39 |
|
40 |
|
41 | return tmp.fileSync({ discardDescriptor: true }).name;
|
42 | }
|
43 |
|
44 | export 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 | }
|