1
1
import getDebug from 'debug' ;
2
- import { exec } from 'child_process' ;
2
+ import { spawnSync } from 'child_process' ;
3
3
import postgres from 'postgres' ;
4
- import { promisify } from 'util' ;
5
-
6
- const asyncExec = promisify ( exec ) ;
7
- import cwd from 'cwd' ;
8
4
import { platform } from 'os' ;
9
5
10
6
const debug = getDebug ( 'postgres-local' ) ;
11
- const FILEPATH_PREFIX = `${ cwd ( ) } /node_modules/.cache/@shelf/ postgres-local` ;
7
+ const PD_TEMP_DATA_PATH = `/tmp/ postgres-local- ${ Date . now ( ) } ` ;
12
8
13
- type StartESOptions = {
9
+ export async function start ( options : {
14
10
seedPath ?: string ;
15
11
version ?: number ;
16
- } ;
17
-
18
- export async function start ( options : StartESOptions ) : Promise < string > {
19
- const { seedPath, version = 14 } = options ;
12
+ port ?: number ;
13
+ includeInstallation ?: boolean ;
14
+ } ) : Promise < string > {
15
+ const { seedPath, version = 14 , port = 5555 , includeInstallation = false } = options ;
20
16
21
- const url = ' postgres://localhost:5432 /postgres' ;
17
+ const url = ` postgres://localhost:${ port } /postgres` ;
22
18
23
19
try {
24
- await asyncExec ( getInstallationScript ( version ) ) ;
20
+ spawnSync ( getInstallationScript ( { version, port, includeInstallation} ) , {
21
+ stdio : 'inherit' ,
22
+ shell : true ,
23
+ env : {
24
+ ...process . env ,
25
+ HOME : '/root' ,
26
+ } ,
27
+ } ) ;
25
28
26
29
debug ( 'Connecting to postgres...' ) ;
27
30
const sql = postgres ( url ) ;
@@ -41,47 +44,60 @@ export async function start(options: StartESOptions): Promise<string> {
41
44
}
42
45
}
43
46
44
- export async function stop ( version ?: string ) : Promise < { stdout : string ; stderr : string } > {
45
- return asyncExec ( getStopScript ( version ) ) ;
47
+ export function stop ( { version = 14 } : { version ?: number } ) : void {
48
+ spawnSync ( getStopScript ( { version} ) , {
49
+ stdio : 'inherit' ,
50
+ shell : true ,
51
+ } ) ;
46
52
}
47
53
48
- export function getInstallationScript ( version : number ) : string {
54
+ export function getInstallationScript ( {
55
+ version = 14 ,
56
+ port = 5555 ,
57
+ includeInstallation : includeInstallation = false ,
58
+ } ) : string {
49
59
switch ( platform ( ) ) {
50
60
case 'darwin' : {
61
+ const installation = includeInstallation ? `brew install postgresql@${ version } ;` : '' ;
62
+
51
63
return `
52
- brew install postgresql@ ${ version }
53
- mkdir -p ${ FILEPATH_PREFIX } /data;
54
- initdb -D ${ FILEPATH_PREFIX } /data;
55
- pg_ctl -D ${ FILEPATH_PREFIX } /data -l ${ FILEPATH_PREFIX } /logfile start;
64
+ ${ installation }
65
+ mkdir -p ${ PD_TEMP_DATA_PATH } /data;
66
+ initdb -D ${ PD_TEMP_DATA_PATH } /data;
67
+ pg_ctl -D ${ PD_TEMP_DATA_PATH } /data -o "-F -p ${ port } " - l ${ PD_TEMP_DATA_PATH } /logfile start;
56
68
` ;
57
69
}
58
70
case 'win32' : {
59
71
throw new Error ( 'Unsupported OS, try run on OS X or Linux' ) ;
60
72
}
61
73
default : {
74
+ // eslint-disable-next-line
75
+ const installation = includeInstallation ? `sudo apt update; sudo apt install postgresql-${ version } ;` : '' ;
76
+
62
77
return `
63
- apt-get update
64
- apt-get install -y postgresql-${ version }
65
- mkdir -p ${ FILEPATH_PREFIX } /data;
66
- /usr/lib/postgresql/${ version } /bin/initdb -D ${ FILEPATH_PREFIX } /data;
67
- /usr/lib/postgresql/${ version } /bin/pg_ctl -D ${ FILEPATH_PREFIX } /data -l ${ FILEPATH_PREFIX } logfile start;
78
+ ${ installation }
79
+ sudo -u postgres mkdir -p ${ PD_TEMP_DATA_PATH } /data;
80
+ sudo -u postgres /usr/lib/postgresql/${ version } /bin/initdb -D ${ PD_TEMP_DATA_PATH } /data;
81
+ 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;
82
+ sudo -u postgres createuser -p ${ port } -s $(whoami);
83
+ sudo -u postgres createdb -p ${ port } $(whoami);
68
84
` ;
69
85
}
70
86
}
71
87
}
72
88
73
- export function getStopScript ( version ?: string ) : string {
89
+ export function getStopScript ( { version = 14 } ) : string {
74
90
switch ( platform ( ) ) {
75
91
case 'darwin' : {
76
92
return `
77
- pg_ctl stop -D ${ FILEPATH_PREFIX } /data
78
- rm -rf ${ FILEPATH_PREFIX }
93
+ pg_ctl stop -D ${ PD_TEMP_DATA_PATH } /data
94
+ rm -rf ${ PD_TEMP_DATA_PATH }
79
95
` ;
80
96
}
81
97
default : {
82
98
return `
83
- /usr/lib/postgresql/${ version } /bin/pg_ctl -D ${ FILEPATH_PREFIX } /data
84
- rm -rf ${ FILEPATH_PREFIX }
99
+ sudo -u postgres /usr/lib/postgresql/${ version } /bin/pg_ctl stop -D ${ PD_TEMP_DATA_PATH } /data
100
+ sudo -u postgres rm -rf ${ PD_TEMP_DATA_PATH }
85
101
` ;
86
102
}
87
103
}
0 commit comments