11import envPaths from 'env-paths' ;
22import { mkdirSync } from 'fs' ;
33import { access , readFile } from 'fs/promises' ;
4+ import { exec , ExecOptions } from 'node:child_process' ;
45import { join } from 'path' ;
5- import { $ } from 'zx' ;
66
7- const p = envPaths ( 'drizzle-studio' , {
8- suffix : '' ,
9- } ) ;
10-
11- $ . verbose = false ;
12- $ . cwd = p . data ;
13- mkdirSync ( p . data , { recursive : true } ) ;
7+ export function runCommand ( command : string , options : ExecOptions = { } ) {
8+ return new Promise < { exitCode : number } > ( ( resolve ) => {
9+ exec ( command , options , ( error ) => {
10+ return resolve ( { exitCode : error ?. code ?? 0 } ) ;
11+ } ) ;
12+ } ) ;
13+ }
1414
1515export const certs = async ( ) => {
16- const res = await $ `mkcert --help` . nothrow ( ) ;
17-
18- // ~/.local/share/drizzle-studio
19- const keyPath = join ( p . data , 'localhost-key.pem' ) ;
20- const certPath = join ( p . data , 'localhost.pem' ) ;
16+ const res = await runCommand ( 'mkcert --help' ) ;
2117
2218 if ( res . exitCode === 0 ) {
19+ const p = envPaths ( 'drizzle-studio' , {
20+ suffix : '' ,
21+ } ) ;
22+
23+ // create ~/.local/share/drizzle-studio
24+ mkdirSync ( p . data , { recursive : true } ) ;
25+
26+ // ~/.local/share/drizzle-studio
27+ const keyPath = join ( p . data , 'localhost-key.pem' ) ;
28+ const certPath = join ( p . data , 'localhost.pem' ) ;
29+
2330 try {
31+ // check if the files exist
2432 await Promise . all ( [ access ( keyPath ) , access ( certPath ) ] ) ;
2533 } catch ( e ) {
26- await $ `mkcert localhost` . nothrow ( ) ;
34+ // if not create them
35+ await runCommand ( `mkcert localhost` , { cwd : p . data } ) ;
2736 }
2837 const [ key , cert ] = await Promise . all ( [
2938 readFile ( keyPath , { encoding : 'utf-8' } ) ,
@@ -33,5 +42,3 @@ export const certs = async () => {
3342 }
3443 return null ;
3544} ;
36-
37- certs ( ) ;
0 commit comments