|
1 | 1 | #!/bin/sh
|
2 | 2 |
|
3 | 3 | set -e
|
4 |
| -# node ./dist/init.js |
5 | 4 |
|
6 | 5 | # skip init if $DEPENDENCIES is empty
|
7 | 6 | if [ -z "$DEPENDENCIES" ]; then
|
8 | 7 | echo "No dependencies to install."
|
9 |
| - cp -r /app/* /tmp/app |
10 | 8 | exit 0
|
11 | 9 | fi
|
12 | 10 |
|
| 11 | +# if $NODE_MODULES_URL is not empty |
| 12 | +if [ -n "$NODE_MODULES_PULL_URL" ]; then |
| 13 | + echo "Downloading node_modules from $NODE_MODULES_PULL_URL" |
| 14 | + |
| 15 | + # get start time |
| 16 | + start_time=$(date +%s) |
| 17 | + |
| 18 | + # temporarily disable set -e |
| 19 | + set +e |
| 20 | + |
| 21 | + # download node_modules.tar and untar to `node_modules` |
| 22 | + curl -sSfL $NODE_MODULES_PULL_URL -o node_modules.tar |
| 23 | + |
| 24 | + end_time=$(date +%s) |
| 25 | + |
| 26 | + # if error |
| 27 | + if [ $? -ne 0 ]; then |
| 28 | + echo "Failed to download node_modules cache." |
| 29 | + else |
| 30 | + elapsed_time=$(expr $end_time - $start_time) |
| 31 | + echo "Downloaded node_modules.tar in $elapsed_time seconds." |
| 32 | + fi |
| 33 | + |
| 34 | + # untar node_modules.tar |
| 35 | + tar -xf node_modules.tar -C . |
| 36 | + |
| 37 | + # check tar exit code |
| 38 | + if [ $? -ne 0 ]; then |
| 39 | + echo "Failed to extract node_modules cache." |
| 40 | + else |
| 41 | + end_time_2=$(date +%s) |
| 42 | + elapsed_time_2=$(expr $end_time_2 - $end_time) |
| 43 | + echo "Extracted node_modules cache in $elapsed_time_2 seconds." |
| 44 | + fi |
| 45 | + |
| 46 | + # re-enable set -e |
| 47 | + set -e |
| 48 | +else |
| 49 | + echo "No node_modules cache found, continuing installation." |
| 50 | +fi |
| 51 | + |
| 52 | +CACHED_DEPENDENCIES="" |
| 53 | +# if node_modules/.dependencies exists |
| 54 | +if [ -f "node_modules/.dependencies" ]; then |
| 55 | + CACHED_DEPENDENCIES=`cat node_modules/.dependencies` |
| 56 | +fi |
| 57 | + |
| 58 | +echo "Cached dependencies: $CACHED_DEPENDENCIES" |
| 59 | +echo "Dependencies to install: $DEPENDENCIES" |
| 60 | + |
| 61 | +# if $CACHED_DEPENDENCIES is equal to $DEPENDENCIES |
| 62 | +if [ "$CACHED_DEPENDENCIES" = "$DEPENDENCIES" ]; then |
| 63 | + echo "No dependencies changed since last cache build." |
| 64 | + exit 0 |
| 65 | +else |
| 66 | + echo "Dependencies changed since last cache build." |
| 67 | +fi |
| 68 | + |
| 69 | + |
| 70 | +# npm install $DEPENDENCIES |
| 71 | +start_time=$(date +%s) |
13 | 72 | echo "npm install $DEPENDENCIES $NPM_INSTALL_FLAGS"
|
14 | 73 | npm install $DEPENDENCIES $NPM_INSTALL_FLAGS
|
15 |
| -cp -r /app/* /tmp/app |
| 74 | +end_time=$(date +%s) |
| 75 | +elapsed_time=$(expr $end_time - $start_time) |
| 76 | +echo "Installed dependencies in $elapsed_time seconds." |
| 77 | + |
| 78 | +# if $NODE_MODULES_PUSH_URL is not empty |
| 79 | +if [ -n "$NODE_MODULES_PUSH_URL" ]; then |
| 80 | + # temporarily disable set -e |
| 81 | + set +e |
| 82 | + |
| 83 | + start_time=$(date +%s) |
| 84 | + echo $DEPENDENCIES > node_modules/.dependencies |
| 85 | + echo "Uploading node_modules to $NODE_MODULES_PUSH_URL" |
| 86 | + |
| 87 | + # tar `node_modules` to node_modules.tar |
| 88 | + tar -cf node_modules.tar ./node_modules |
| 89 | + |
| 90 | + end_time_1=$(date +%s) |
| 91 | + elapsed_time=$(expr $end_time_1 - $start_time) |
| 92 | + echo "Compressed node_modules in $elapsed_time seconds." |
| 93 | + |
| 94 | + # upload node_modules.tar to $NODE_MODULES_PUSH_URL |
| 95 | + curl -sSfL -X PUT -T node_modules.tar $NODE_MODULES_PUSH_URL |
| 96 | + |
| 97 | + |
| 98 | + if [ $? -ne 0 ]; then |
| 99 | + echo "Failed to upload node_modules cache." |
| 100 | + else |
| 101 | + end_time_2=$(date +%s) |
| 102 | + elapsed_time_2=$(expr $end_time_2 - $end_time) |
| 103 | + echo "Uploaded node_modules.tar in $elapsed_time_2 seconds." |
| 104 | + fi |
| 105 | + |
| 106 | + # re-enable set -e |
| 107 | + set -e |
| 108 | +fi |
0 commit comments