@@ -7,6 +7,26 @@ import type {ExecSyncOptions} from 'child_process';
77const debug = getDebug ( 'postgres-local' ) ;
88const PD_TEMP_DATA_PATH = `/tmp/postgres-local` ;
99
10+ function getHomebrewPrefix ( ) : string {
11+ try {
12+ // Use execSync to get the Homebrew prefix, trim to remove newline
13+ return execSync ( 'brew --prefix' , { encoding : 'utf8' } ) . trim ( ) ;
14+ } catch ( error ) {
15+ console . error ( error ) ;
16+ throw new Error (
17+ 'Homebrew is not installed or not found in PATH. Please install Homebrew to use this tool.'
18+ ) ;
19+ }
20+ }
21+
22+ function getPostgresBinPath ( version : number , binary : 'initdb' | 'pg_ctl' ) : string {
23+ const brewPrefix = getHomebrewPrefix ( ) ;
24+ const pgPrefix = `${ brewPrefix } /opt/postgresql@${ version } /bin` ;
25+
26+ return `${ pgPrefix } /${ binary } ` ;
27+ }
28+
29+ // eslint-disable-next-line complexity
1030export async function start ( options : {
1131 seedPath ?: string ;
1232 version ?: number ;
@@ -67,46 +87,79 @@ export function stop({
6787 execSync ( getStopScript ( { version} ) , execOptions ) ;
6888}
6989
90+ function getMacOSScript ( {
91+ version,
92+ port,
93+ includeInstallation,
94+ } : {
95+ version : number ;
96+ port : number ;
97+ includeInstallation : boolean ;
98+ } ) : string {
99+ const installation = includeInstallation ? `brew install postgresql@${ version } ;` : '' ;
100+ const initdbCmd = getPostgresBinPath ( version , 'initdb' ) ;
101+ const pgCtlCmd = getPostgresBinPath ( version , 'pg_ctl' ) ;
102+
103+ return `
104+ ${ installation }
105+ mkdir -p ${ PD_TEMP_DATA_PATH } /data;
106+ ${ initdbCmd } -D ${ PD_TEMP_DATA_PATH } /data;
107+ ${ pgCtlCmd } -D ${ PD_TEMP_DATA_PATH } /data -o "-F -p ${ port } " -l ${ PD_TEMP_DATA_PATH } /logfile start;
108+ ` ;
109+ }
110+
111+ function getLinuxScript ( {
112+ version,
113+ port,
114+ includeInstallation,
115+ } : {
116+ version : number ;
117+ port : number ;
118+ includeInstallation : boolean ;
119+ } ) : string {
120+ let installation = '' ;
121+
122+ if ( includeInstallation ) {
123+ installation = `sudo apt update; sudo apt install postgresql-${ version } ;` ;
124+ }
125+
126+ return `
127+ ${ installation }
128+ sudo -u postgres mkdir -p ${ PD_TEMP_DATA_PATH } /data;
129+ sudo -u postgres /usr/lib/postgresql/${ version } /bin/initdb -D ${ PD_TEMP_DATA_PATH } /data;
130+ sudo -u postgres /usr/lib/postgresql/${ version } /bin/pg_ctl -o "-F -p ${ port } " -D ${ PD_TEMP_DATA_PATH } /data -l ${ PD_TEMP_DATA_PATH } /logfile start;
131+ sudo -u postgres createuser -p ${ port } -s $(whoami);
132+ sudo -u postgres createdb -p ${ port } $(whoami);
133+ ` ;
134+ }
135+
136+ // eslint-disable-next-line complexity
70137export function getInstallationScript ( {
71138 version = 17 ,
72139 port = 5555 ,
73- includeInstallation : includeInstallation = false ,
140+ includeInstallation = false ,
141+ } : {
142+ version ?: number ;
143+ port ?: number ;
144+ includeInstallation ?: boolean ;
74145} ) : string {
75146 switch ( platform ( ) ) {
76- case 'darwin' : {
77- const installation = includeInstallation ? `brew install postgresql@${ version } ;` : '' ;
78-
79- return `
80- ${ installation }
81- mkdir -p ${ PD_TEMP_DATA_PATH } /data;
82- initdb-${ version } -D ${ PD_TEMP_DATA_PATH } /data;
83- pg_ctl-${ version } -D ${ PD_TEMP_DATA_PATH } /data -o "-F -p ${ port } " -l ${ PD_TEMP_DATA_PATH } /logfile start;
84- ` ;
85- }
86- case 'win32' : {
147+ case 'darwin' :
148+ return getMacOSScript ( { version, port, includeInstallation} ) ;
149+ case 'win32' :
87150 throw new Error ( 'Unsupported OS, try run on OS X or Linux' ) ;
88- }
89- default : {
90- // eslint-disable-next-line
91- const installation = includeInstallation ? `sudo apt update; sudo apt install postgresql-${ version } ;` : '' ;
92-
93- return `
94- ${ installation }
95- sudo -u postgres mkdir -p ${ PD_TEMP_DATA_PATH } /data;
96- sudo -u postgres /usr/lib/postgresql/${ version } /bin/initdb -D ${ PD_TEMP_DATA_PATH } /data;
97- sudo -u postgres /usr/lib/postgresql/${ version } /bin/pg_ctl -o "-F -p ${ port } " -D ${ PD_TEMP_DATA_PATH } /data -l ${ PD_TEMP_DATA_PATH } /logfile start;
98- sudo -u postgres createuser -p ${ port } -s $(whoami);
99- sudo -u postgres createdb -p ${ port } $(whoami);
100- ` ;
101- }
151+ default :
152+ return getLinuxScript ( { version, port, includeInstallation} ) ;
102153 }
103154}
104155
105156export function getStopScript ( { version = 17 } ) : string {
106157 switch ( platform ( ) ) {
107158 case 'darwin' : {
159+ const pgCtlCmd = getPostgresBinPath ( version , 'pg_ctl' ) ;
160+
108161 return `
109- pg_ctl- ${ version } stop -D ${ PD_TEMP_DATA_PATH } /data
162+ ${ pgCtlCmd } stop -D ${ PD_TEMP_DATA_PATH } /data
110163 rm -rf ${ PD_TEMP_DATA_PATH }
111164 ` ;
112165 }
0 commit comments