-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.sh
executable file
·75 lines (55 loc) · 1.43 KB
/
env.sh
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
#!/bin/bash
# Config
ENVSH_ENV="${ENVSH_ENV:-"./.env"}"
ENVSH_PREFIX="${ENVSH_PREFIX:-"NEXT_PUBLIC_"}"
ENVSH_PREFIX_STRIP="${ENVSH_PREFIX_STRIP:-true}"
# Can be `window.__appenv = {` or `const APPENV = {` or whatever you want
ENVSH_PREPEND="${ENVSH_PREPEND:-"window.__appenv = {"}"
ENVSH_APPEND="${ENVSH_APPEND:-"}"}"
ENVSH_OUTPUT="${ENVSH_OUTPUT:-"./public/__appenv.js"}"
[ -f "$ENVSH_ENV" ] && INPUT="$ENVSH_ENV" || INPUT=/dev/null
# Add assignment
echo "$ENVSH_PREPEND" >"$ENVSH_OUTPUT"
gawk -v PREFIX="$ENVSH_PREFIX" -v STRIP_PREFIX="$ENVSH_PREFIX_STRIP" '
BEGIN {
OFS=": ";
FS="=";
PATTERN="^" PREFIX;
for (v in ENVIRON)
if (v ~ PATTERN)
vars[v] = ENVIRON[v]
}
$0 ~ PATTERN {
v = $2;
for (i = 3; i <= NF; i++)
v = v FS $i;
vars[$1] = (vars[$1] ? vars[$1] : v);
}
END {
for (v in vars) {
val = vars[v];
switch (val) {
case /^true$/:
break;
case /^false$/:
break;
case /^'"'.*'"'$/:
break;
case /^".*"$/:
break;
case /^[[:digit:]]+$/:
break;
default:
val = "\"" val "\"";
break;
}
val = val ","
if (STRIP_PREFIX == "true" || STRIP_PREFIX == "1")
v = gensub(PATTERN, "", 1, v)
print v, val;
}
}
' "$INPUT" >>"$ENVSH_OUTPUT"
echo "$ENVSH_APPEND" >>"$ENVSH_OUTPUT"
# Accepting commands (for Docker)
exec "$@"