1
1
const { spawnSync : baseSpawnSync } = require ( "child_process" ) ;
2
2
const { basename, dirname, resolve } = require ( "path" ) ;
3
3
const platform = require ( "os" ) . platform ( ) ;
4
+ const { safeRandomString } = require ( "../../scripts/lib/random" ) ;
5
+ const fsp = require ( "fs" ) . promises ;
6
+
7
+ const DOTENV_PATH = `${ __dirname } /../../.env` ;
4
8
5
9
if ( platform !== "win32" && ! process . env . UID ) {
6
10
console . error (
@@ -23,28 +27,65 @@ function spawnSync(command, args, options = {}) {
23
27
}
24
28
}
25
29
26
- // The `docker-compose` project name defaults to the directory name containing
27
- // `docker-compose.yml`, which is the root folder of our project. Let's call
28
- // that 'ROOT'. We're in ROOT/docker/scripts and we want to get the name of
29
- // ROOT:
30
- const projectName = basename ( dirname ( dirname ( resolve ( __dirname ) ) ) ) ;
31
-
32
- // On Windows we must run 'yarn.cmd' rather than 'yarn'
33
- const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn" ;
34
-
35
- spawnSync ( yarnCmd , [ "down" ] ) ;
36
- spawnSync ( yarnCmd , [ "db:up" ] ) ;
37
-
38
- // Fix permissions
39
- spawnSync ( yarnCmd , [
40
- "compose" ,
41
- "run" ,
42
- "server" ,
43
- "sudo" ,
44
- "bash" ,
45
- "-c" ,
46
- "chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules" ,
47
- ] ) ;
48
-
49
- // Run setup as normal
50
- spawnSync ( yarnCmd , [ "compose" , "run" , "server" , "yarn" , "setup" , projectName ] ) ;
30
+ async function main ( ) {
31
+ // POSTGRES_PASSWORD must be set for the Docker Postgres image to boot
32
+ let data ;
33
+ try {
34
+ data = await fsp . readFile ( DOTENV_PATH , "utf8" ) ;
35
+ } catch ( e ) {
36
+ data = "" ;
37
+ }
38
+ if ( ! data . includes ( "POSTGRES_PASSWORD=" ) ) {
39
+ // We cannot use `dotenv` here because we exist outside of Docker, and we
40
+ // don't have the module installed yet.
41
+ const password = safeRandomString ( 30 ) ;
42
+ data += `
43
+ # POSTGRES_PASSWORD is the superuser password for PostgreSQL, it's required to
44
+ # initialize the Postgres docker volume.
45
+ POSTGRES_PASSWORD=${ password }
46
+
47
+ # We're accessing Postgres via Docker:
48
+ DATABASE_HOST=db
49
+ ROOT_DATABASE_URL=postgres://postgres:${ password } @db/template1
50
+ ` ;
51
+ await fsp . writeFile ( DOTENV_PATH , data ) ;
52
+ }
53
+
54
+ // The `docker-compose` project name defaults to the directory name containing
55
+ // `docker-compose.yml`, which is the root folder of our project. Let's call
56
+ // that 'ROOT'. We're in ROOT/docker/scripts and we want to get the name of
57
+ // ROOT:
58
+ const projectName = basename ( dirname ( dirname ( resolve ( __dirname ) ) ) ) ;
59
+
60
+ // On Windows we must run 'yarn.cmd' rather than 'yarn'
61
+ const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn" ;
62
+
63
+ spawnSync ( yarnCmd , [ "down" ] ) ;
64
+ spawnSync ( yarnCmd , [ "db:up" ] ) ;
65
+
66
+ // Fix permissions
67
+ spawnSync ( yarnCmd , [
68
+ "compose" ,
69
+ "run" ,
70
+ "server" ,
71
+ "sudo" ,
72
+ "bash" ,
73
+ "-c" ,
74
+ "chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules" ,
75
+ ] ) ;
76
+
77
+ // Run setup as normal
78
+ spawnSync ( yarnCmd , [
79
+ "compose" ,
80
+ "run" ,
81
+ "server" ,
82
+ "yarn" ,
83
+ "setup" ,
84
+ projectName ,
85
+ ] ) ;
86
+ }
87
+
88
+ main ( ) . catch ( e => {
89
+ console . error ( e ) ;
90
+ process . exit ( 1 ) ;
91
+ } ) ;
0 commit comments