import { execFileSync , ExecFileSyncOptions } from 'child_process' ; import tmp from 'tmp' ; import createDebug from 'debug' ; import path from 'path' ; import sudoPrompt from 'sudo-prompt' ; import { configPath } from './constants' ; const debug = createDebug ( 'devcert:util' ) ; export function openssl ( args ) { return run ( 'openssl' , args , { stdio : 'pipe' , env : Object . assign ( { RANDFILE : path . join ( configPath ( '.rnd' ) ) } , process . env ) } ) ; } export function run ( cmd , args , options = { } ) { debug ( `execFileSync: \`${ cmd } ${ args . join ( ' ' ) }\`` ) ; return execFileSync ( cmd , args , options ) ; } export function sudoAppend ( file , input ) { run ( 'sudo' , [ 'tee' , '-a' , file ] , { input } ) ; } export function waitForUser ( ) { return new Promise ( ( resolve ) => { process . stdin . resume ( ) ; process . stdin . on ( 'data' , resolve ) ; } ) ; } export function reportableError ( message ) { return new Error ( `${ message } | This is a bug in devcert, please report the issue at https://github.com/davewasmer/devcert/issues` ) ; } export function mktmp ( ) { return tmp . fileSync ( { discardDescriptor : true } ) . name ; } export function sudo ( cmd ) { return new Promise ( ( resolve , reject ) => { sudoPrompt . exec ( cmd , { name : 'devcert' } , ( err , stdout , stderr ) => { let error = err || ( typeof stderr === 'string' && stderr . trim ( ) . length > 0 && new Error ( stderr ) ) ; error ? reject ( error ) : resolve ( stdout ) ; } ) ; } ) ; } export const numericHash = ( str ) : number => { let hash = 5381 ; let i = str . length ; while ( i ) { hash = hash * 33 ^ str . charCodeAt ( -- i ) ; } return hash >>> 0 ; } ;