-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathyarn-setup.mjs
More file actions
90 lines (76 loc) · 2.62 KB
/
yarn-setup.mjs
File metadata and controls
90 lines (76 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env zx
import { $, fs, path } from "zx";
import { safeRandomString } from "../../scripts/lib/random.mjs";
const { basename, dirname, resolve } = path;
const platform = os.platform();
const DOCKER_DOTENV_PATH = `${__dirname}/../.env`;
if (platform !== "win32" && !process.env.UID) {
console.error(
"You should run `export UID` before running 'yarn docker setup' otherwise you may end up with permissions issues."
);
process.exit(1);
}
async function main() {
// Check that docker/.env exists
try {
await fs.access(DOCKER_DOTENV_PATH, fs.constants.F_OK);
} catch (e) {
// Does not exist, write it
const password = safeRandomString(30);
const data = `
# We'd like scripts ran through Docker to pretend they're in a normal
# interactive terminal.
FORCE_COLOR=2
# \`pg_dump\` is run from inside container, which doesn't have pg tools installed
# so it needs a way to still run it. \`docker-compose run\` would start an
# instance inside the current running container which doesn't work with volume
# mappings, so we must use \`docker-compose exec\`. \`-T\` is needed because our
# \`.gmrc\` checks for interactive TTY.
PG_DUMP=docker-compose exec -T db pg_dump
# Drops tables without asking in \`yarn setup\`. Reasoning: 1) docker-compose is
# not tty, 2) it's a dev env anyway.
CONFIRM_DROP=y
# POSTGRES_PASSWORD is the superuser password for PostgreSQL, it's required to
# initialize the Postgres docker volume.
POSTGRES_PASSWORD=${password}
# We're accessing Postgres via Docker, so we must use the db host and the
# relevant password.
DATABASE_HOST=db
ROOT_DATABASE_URL=postgres://postgres:${password}@db/postgres
`;
await fs.writeFile(DOCKER_DOTENV_PATH, data);
}
// The `docker-compose` project name defaults to the directory name containing
// `docker-compose.yml`, which is the root folder of our project. Let's call
// that 'ROOT'. We're in ROOT/docker/scripts and we want to get the name of
// ROOT:
const projectName = basename(dirname(dirname(resolve(__dirname))));
// On Windows we must run 'yarn.cmd' rather than 'yarn'
const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn";
await $`${yarnCmd} down`;
await $`${yarnCmd} db:up`;
// Fix permissions
await $`${yarnCmd} ${[
"compose",
"run",
"server",
"sudo",
"bash",
"-c",
"chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules",
]}`;
// Run setup as normal
await $`${yarnCmd} ${[
"compose",
"run",
"-e",
`PROJECT_NAME=${projectName}`,
"server",
"yarn",
"setup",
]}`;
}
main().catch((e) => {
console.error(e);
process.exit(1);
});