Repository navigation
Expand file tree
/
Copy pathproject.sh
More file actions
executable file
·123 lines (85 loc) · 2.66 KB
/
Copy pathproject.sh
File metadata and controls
executable file
·123 lines (85 loc) · 2.66 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
set -euo pipefail
## Variables
#PROJECT_NAME=`echo ${PWD##*/}` ## PROJECT_NAME = parent directory
PROJECT_UID=$(id -u)
PROJECT_GID=$(id -g)
PROJECT_GROUPS=$({ id -G; stat -c %g /dev/dri/* /dev/kvm /dev/snd/* || true; } 2>/dev/null | tr ' ' '\n' | sort -un \
| sed "/^0\?$/d; /^$PROJECT_GID$/d" | sed 's/.*/\\n\\1 - "&"/' | tr -d '\n') ## your groups and those of the GPU, KVM and sound devices, as group_add lines for composehack
USER=node
YARN_VERSION=4.19.0
## Functions
generated() {
find . -mindepth 1 -maxdepth 1 \
| sed "
/Dockerfile/d;
/project.sh/d;
/README.md/d;
"
}
confirm() {
if [[ ! project.sh -ef $0 ]]; then
echo "clean removes the files of the current directory, so run it in the one of project.sh." >&2
return 1
fi
[[ $1 == --force ]] && return
echo "clean removes the containers, images and volumes (with the databases) of this environment,"
echo "and these files in $PWD:"
generated | sort
if [[ ! -t 0 ]]; then
echo "Nothing removed: run ./project.sh clean --force to remove them without being asked." >&2
return 1
fi
read -r -p "Type yes to remove them: "
[[ $REPLY == yes ]] || { echo "Nothing removed." >&2; return 1; }
}
clean() {
confirm "${1:-}" || exit 1
if [[ -f docker-compose.yml ]]; then
docker compose down -v --rmi all --remove-orphans
fi
generated | xargs -I {} rm -rf {}
}
compose() {
if [[ ! -f docker-compose.yml ]]; then
cat << EOF > docker-compose.yml
services:
node:
build: .
working_dir: /home/$USER
volumes:
- .:/home/$USER
environment:
COREPACK_ENABLE_DOWNLOAD_PROMPT: 0
HOME: /home/$USER
NODE_ENV: development
network_mode: host
EOF
fi
}
composehack() {
if ! grep -q "user\:" "docker-compose.yml"; then
echo "Adding user configuration line to docker-compose.yml for GNU/Linux users."
sed -i "/working_dir\:/{s@^\( \+\)@\1user\: $PROJECT_UID\:$PROJECT_GID${PROJECT_GROUPS:+\n\1group_add\:$PROJECT_GROUPS}\n\1@}" docker-compose.yml
fi
}
node() {
if [[ ! -f package.json ]]; then
docker compose run --rm node corepack yarn@$YARN_VERSION init -y
else
docker compose run --rm node yarn install
fi
docker compose run --rm node sh -c "printenv"
}
start() {
compose
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
composehack
fi
node
}
if ! declare -F "${1:-}" > /dev/null; then
echo "Usage: $0 {start|clean}" >&2
exit 1
fi
"$1" "${@:2}"