Skip to content

Commit 2791fa2

Browse files
committed
[pnpm] use oncreate.sh
1 parent 97b5769 commit 2791fa2

File tree

3 files changed

+65
-49
lines changed

3 files changed

+65
-49
lines changed

src/mount-pnpm-store/devcontainer-feature.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
"ghcr.io/devcontainers/features/common-utils",
1818
"ghcr.io/meaningful-ooo/devcontainer-features/fish"
1919
],
20-
"postCreateCommand": "/usr/local/share/mount-pnpm-store-post-create.sh"
20+
"onCreateCommand": {
21+
"mount-pnpm-store-setup": "/usr/local/share/mount-pnpm-store-setup/scripts/oncreate.sh"
22+
}
2123
}

src/mount-pnpm-store/install.sh

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
#!/bin/sh
2-
set -e
3-
4-
FEATURE_ID="mount-pnpm-store"
52

6-
echo "Activating feature '$FEATURE_ID'"
7-
echo "User: ${_REMOTE_USER} User home: ${_REMOTE_USER_HOME}"
8-
9-
if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then
10-
echo "***********************************************************************************"
11-
echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***"
12-
echo "***********************************************************************************"
13-
exit 1
14-
fi
15-
16-
# make /dc/mounted-pnpm-store folder if doesn't exist
17-
mkdir -p "/dc/mounted-pnpm-store"
18-
19-
# as to why we move around the folder, check `github-cli-persistence/install.sh`
20-
if [ -e "$_REMOTE_USER_HOME/.pnpm-store" ]; then
21-
echo "Moving existing .pnpm-store folder to .pnpm-store-old"
22-
mv "$_REMOTE_USER_HOME/.pnpm-store" "$_REMOTE_USER_HOME/.pnpm-store-old"
23-
fi
24-
25-
ln -s /dc/mounted-pnpm-store "$_REMOTE_USER_HOME/.pnpm-store"
26-
chown -R "$_REMOTE_USER:$_REMOTE_USER" "$_REMOTE_USER_HOME/.pnpm-store"
27-
28-
# --- Generate a '$FEATURE_ID-post-create.sh' script to be executed by the 'postCreateCommand' lifecycle hook
29-
# Looks like this is the best way to run a script in lifecycle hooks
30-
# Source: https://github.com/devcontainers/features/blob/562305d37b97d47331d96306ffc2a0a3cce55e64/src/git-lfs/install.sh#L190C1-L190C109
31-
POST_CREATE_SCRIPT_PATH="/usr/local/share/$FEATURE_ID-post-create.sh"
32-
33-
tee "$POST_CREATE_SCRIPT_PATH" >/dev/null \
34-
<<'EOF'
35-
#!/bin/sh
3+
USERNAME=${USERNAME:-${_REMOTE_USER}}
4+
LIFECYCLE_SCRIPTS_DIR="/usr/local/share/${FEATURE_ID}/scripts"
365

376
set -e
387

39-
# set pnpm config (if it's installed)
40-
if type pnpm >/dev/null 2>&1; then
41-
echo "Setting pnpm store location to $_REMOTE_USER_HOME/.pnpm-store"
42-
pnpm config set store-dir ~/.pnpm-store --global
43-
else
44-
echo "WARN: pnpm is not installed! Please ensure pnpm is installed and in your PATH."
45-
echo "WARN: pnpm store location will not be set."
46-
fi
47-
48-
# if the user is not root, chown /dc/mounted-pnpm-store to the user
49-
if [ "$(id -u)" != "0" ]; then
50-
echo "Running post-start.sh for user $USER"
51-
sudo chown -R "$USER:$USER" /dc/mounted-pnpm-store
8+
create_cache_dir() {
9+
if [ -d "$1" ]; then
10+
echo "Cache directory $1 already exists. Skip creation..."
11+
else
12+
echo "Create cache directory $1..."
13+
mkdir -p "$1"
14+
fi
15+
16+
if [ -z "$2" ]; then
17+
echo "No username provided. Skip chown..."
18+
else
19+
echo "Change owner of $1 to $2..."
20+
chown -R "$2:$2" "$1"
21+
fi
22+
}
23+
24+
create_symlink_dir() {
25+
local local_dir=$1
26+
local cache_dir=$2
27+
local username=$3
28+
29+
runuser -u "$username" -- mkdir -p "$(dirname "$local_dir")"
30+
runuser -u "$username" -- mkdir -p "$cache_dir"
31+
32+
# if the folder we want to symlink already exists, the ln -s command will create a folder inside the existing folder
33+
if [ -e "$local_dir" ]; then
34+
echo "Moving existing $local_dir folder to $local_dir-old"
35+
mv "$local_dir" "$local_dir-old"
36+
fi
37+
38+
echo "Symlink $local_dir to $cache_dir for $username..."
39+
runuser -u "$username" -- ln -s "$cache_dir" "$local_dir"
40+
}
41+
42+
create_cache_dir "/dc/mounted-pnpm-store" "${USERNAME}"
43+
create_symlink_dir "$_REMOTE_USER_HOME/.pnpm-store" "/dc/mounted-pnpm-store" "${USERNAME}"
44+
45+
# Set Lifecycle scripts
46+
if [ -f oncreate.sh ]; then
47+
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"
48+
cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh"
5249
fi
53-
EOF
5450

55-
chmod 755 "$POST_CREATE_SCRIPT_PATH"
51+
echo "Finished installing $FEATURE_ID"

src/mount-pnpm-store/oncreate.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# set pnpm config (if it's installed)
6+
if type pnpm >/dev/null 2>&1; then
7+
echo "Setting pnpm store location to $_REMOTE_USER_HOME/.pnpm-store"
8+
pnpm config set store-dir ~/.pnpm-store --global
9+
else
10+
echo "WARN: pnpm is not installed! Please ensure pnpm is installed and in your PATH."
11+
echo "WARN: pnpm store location will not be set."
12+
fi
13+
14+
# if the user is not root, chown /dc/mounted-pnpm-store to the user
15+
if [ "$(id -u)" != "0" ]; then
16+
echo "Running post-start.sh for user $USER"
17+
sudo chown -R "$USER:$USER" /dc/mounted-pnpm-store
18+
fi

0 commit comments

Comments
 (0)