| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# shellcheck disable=SC3043 # local is not POSIX, but every shell has it |
| 4 |
# shellcheck disable=SC3013,SC3045 # ditto for test {-nt,-t} |
| 5 |
|
| 6 |
GITHUB_REPO='node-cache' |
| 7 |
SUBDIR='node_modules' |
| 8 |
|
| 9 |
V="${V-0}" # default to friendly messages |
| 10 |
|
| 11 |
set -eu |
| 12 |
cd "${0%/*}/.." |
| 13 |
# shellcheck source-path=SCRIPTDIR/.. |
| 14 |
. test/common/git-utils.sh |
| 15 |
|
| 16 |
cmd_remove() { |
| 17 |
# if we did this for ourselves the rm is enough, but it might be the case |
| 18 |
# that someone actually used git-submodule to fetch this, so clean up after |
| 19 |
# that as well. NB: deinit nicely recreates the empty directory for us. |
| 20 |
message REMOVE node_modules |
| 21 |
rm -rf node_modules |
| 22 |
git submodule deinit node_modules |
| 23 |
rm -rf -- "$(git rev-parse --absolute-git-dir)/modules/node_modules" |
| 24 |
} |
| 25 |
|
| 26 |
cmd_checkout() { |
| 27 |
# we default to check out the node_modules corresponding to the gitlink in the index |
| 28 |
local force="" |
| 29 |
if [ "${1-}" = "--force" ]; then |
| 30 |
force="1" |
| 31 |
shift |
| 32 |
fi |
| 33 |
|
| 34 |
local sha="${1-$(get_index_gitlink node_modules)}" |
| 35 |
|
| 36 |
# fetch by sha to prevent us from downloading something we don't want |
| 37 |
fetch_sha_to_cache "${sha}" |
| 38 |
|
| 39 |
# verify that our package.json is equal to the one the cached node_modules |
| 40 |
# was created with, unless --force is given |
| 41 |
if [ -z "$force" ]; then |
| 42 |
if ! cmp_from_cache "${sha}" '.package.json' 'package.json'; then |
| 43 |
cat >&2 <<EOF |
| 44 |
|
| 45 |
*** node_modules ${sha} doesn't match our package.json |
| 46 |
*** refusing to automatically check out node_modules |
| 47 |
|
| 48 |
Options: |
| 49 |
|
| 50 |
- tools/node-modules checkout --force # disable this check |
| 51 |
|
| 52 |
- tools/node-modules install # npm install with our package.json |
| 53 |
|
| 54 |
$0: *** aborting |
| 55 |
|
| 56 |
EOF |
| 57 |
exit 1 |
| 58 |
fi |
| 59 |
fi |
| 60 |
|
| 61 |
# we're actually going to do this; let's remove the old one |
| 62 |
cmd_remove |
| 63 |
|
| 64 |
# and check out the new one |
| 65 |
# we need to use the tag name here, unfortunately |
| 66 |
clone_from_cache "${sha}" |
| 67 |
} |
| 68 |
|
| 69 |
cmd_install() { |
| 70 |
test -e bots || test/common/make-bots |
| 71 |
|
| 72 |
# We first read the result directly into the cache, then we unpack it. |
| 73 |
tree="$(bots/npm download < package.json | tar_to_cache)" |
| 74 |
commit="$(sha256sum package.json | git_cache commit-tree "${tree}")" |
| 75 |
git_cache tag "sha-${commit}" "${commit}" "--no-sign" |
| 76 |
cmd_checkout "${commit}" |
| 77 |
|
| 78 |
cat <<EOF |
| 79 |
Next steps: |
| 80 |
|
| 81 |
- git add node_modules && git commit |
| 82 |
- tools/node-modules push |
| 83 |
|
| 84 |
EOF |
| 85 |
} |
| 86 |
|
| 87 |
cmd_push() { |
| 88 |
# push via the cache: the shared history with the remote helps to thin out the pack we send |
| 89 |
tag="sha-$(git -C node_modules rev-parse HEAD)" |
| 90 |
message PUSH "${GITHUB_REPO} ${tag}" |
| 91 |
git_cache push "${SSH_REMOTE}" "${tag}" |
| 92 |
} |
| 93 |
|
| 94 |
cmd_verify() { |
| 95 |
test -e bots || test/common/make-bots |
| 96 |
|
| 97 |
# Verifies that the package.json and node_modules of the given commit match. |
| 98 |
commit="$(git rev-parse "$1:node_modules")" |
| 99 |
fetch_sha_to_cache "${commit}" |
| 100 |
|
| 101 |
committed_tree="$(git_cache rev-parse "${commit}^{tree}")" |
| 102 |
expected_tree="$(git cat-file blob "$1:package.json" | bots/npm download | tar_to_cache)" |
| 103 |
|
| 104 |
if [ "${committed_tree}" != "${expected_tree}" ]; then |
| 105 |
exec >&2 |
| 106 |
printf "\nCommit %s package.json and node_modules aren't in sync!\n\n" "$1" |
| 107 |
git --no-pager show --stat "$1" |
| 108 |
|
| 109 |
printf "\nThe above commit refers to the following node_modules commit:\n\n" |
| 110 |
git_cache --no-pager show --no-patch "${commit}" |
| 111 |
|
| 112 |
printf "\nOur attempt to recreate that commit differs as follows:\n\n" |
| 113 |
git_cache --no-pager diff --stat "${commit}" "${expected_tree}" -- |
| 114 |
git_cache --no-pager diff "${commit}" "${expected_tree}" -- .package-lock.json |
| 115 |
exit 1 |
| 116 |
fi |
| 117 |
} |
| 118 |
|
| 119 |
cmd_runtime_tar() { |
| 120 |
if [ -z "${1:-}" ]; then |
| 121 |
echo "Usage: $0 runtime-tar <tarball-path>" >&2 |
| 122 |
exit 1 |
| 123 |
fi |
| 124 |
if [ ! -e node_modules/.package-lock.json ]; then |
| 125 |
echo "Error: This requires a checked out node_modules, run 'tools/node-modules checkout' first" >&2 |
| 126 |
exit 1 |
| 127 |
fi |
| 128 |
local tar_path="$1" |
| 129 |
|
| 130 |
# Verify lockfile version |
| 131 |
local lockfile_version |
| 132 |
lockfile_version="$(jq -r '.lockfileVersion' node_modules/.package-lock.json)" |
| 133 |
if [ "$lockfile_version" != "3" ]; then |
| 134 |
echo "Error: this code is only certified for lockfileVersion 3, got $lockfile_version" >&2 |
| 135 |
exit 1 |
| 136 |
fi |
| 137 |
|
| 138 |
# Collect runtime packages using jq, excluding: |
| 139 |
# - devDependencies |
| 140 |
# - esbuild (arch dependent, must use distribution packages) |
| 141 |
# - unnecessary binary packages (use JavaScript sass, don't need watcher) |
| 142 |
local runtime_pkgs |
| 143 |
runtime_pkgs="$(jq -r ' |
| 144 |
.packages | to_entries[] | |
| 145 |
select(.value.dev != true) | |
| 146 |
.key | |
| 147 |
select(test(".*/@?esbuild(/|$)") | not) | |
| 148 |
select(test("watcher-linux|sass-embedded") | not) |
| 149 |
' node_modules/.package-lock.json | while read -r pkg; do |
| 150 |
# Only include if exists and is not empty |
| 151 |
[ -n "$(ls -A "$pkg" 2>/dev/null)" ] && echo "$pkg" |
| 152 |
done)" |
| 153 |
|
| 154 |
# Validate that we only have architecture independent files |
| 155 |
# special-case node_modules/iconv-lite/encodings/sbcs-data-generated.js, it's JS with binary strings in it |
| 156 |
# and resolve/test/resolver/cup.coffee which is a single-byte test file |
| 157 |
# Ubuntu 24.04's file (or at least not that in the GitHub actions runner) does not detect *.woff2 file type properly |
| 158 |
for pkg in $runtime_pkgs; do |
| 159 |
if find "$pkg" -type f -print0 | |
| 160 |
xargs -0 file --mime | |
| 161 |
grep -Ev \ |
| 162 |
-e 'text/' \ |
| 163 |
-e 'inode/x-empty' \ |
| 164 |
-e 'application/(javascript|json)' \ |
| 165 |
-e 'font/' \ |
| 166 |
-e 'image/' \ |
| 167 |
-e 'sbcs-data-generated.js' \ |
| 168 |
-e 'cup.coffee' \ |
| 169 |
-e '\.woff'; then |
| 170 |
echo "Error: package '$pkg' contains architecture dependent files, cannot include in runtime tarball" >&2 |
| 171 |
exit 1 |
| 172 |
fi |
| 173 |
done |
| 174 |
|
| 175 |
# Create tarball |
| 176 |
{ |
| 177 |
echo "node_modules/.package-lock.json" |
| 178 |
echo "$runtime_pkgs" |
| 179 |
} | tar --format=ustar --sort=name --owner=root:0 --group=root:0 --xz -cf "$tar_path" -C . -T - |
| 180 |
} |
| 181 |
|
| 182 |
# called from Makefile.am |
| 183 |
cmd_make_package_lock_json() { |
| 184 |
# Run from make to ensure package-lock.json is up to date |
| 185 |
|
| 186 |
# package-lock.json is used as the stamp file for all things that use |
| 187 |
# node_modules, so this is the main bit of glue that drives the entire process |
| 188 |
|
| 189 |
# We try our best not to touch package-lock.json unless it actually changes |
| 190 |
|
| 191 |
# This isn't going to work for a tarball, but as long as |
| 192 |
# package-lock.json is already there, and newer than package.json, |
| 193 |
# we're OK |
| 194 |
if [ ! -e .git ]; then |
| 195 |
if [ package-lock.json -nt package.json ]; then |
| 196 |
exit 0 |
| 197 |
fi |
| 198 |
|
| 199 |
echo "*** Can't update node modules unless running from git" >&2 |
| 200 |
exit 1 |
| 201 |
fi |
| 202 |
|
| 203 |
# Otherwise, our main goal is to ensure that the node_modules from |
| 204 |
# the index is the one that we actually have. |
| 205 |
local sha |
| 206 |
sha="$(get_index_gitlink node_modules)" |
| 207 |
if [ ! -e node_modules/.git ]; then |
| 208 |
# nothing there yet... |
| 209 |
cmd_checkout |
| 210 |
elif [ "$(git -C node_modules rev-parse HEAD)" != "${sha}" ]; then |
| 211 |
# wrong thing there... |
| 212 |
cmd_checkout |
| 213 |
fi |
| 214 |
|
| 215 |
# This check is more about catching local changes to package.json than |
| 216 |
# about validating something we just checked out: |
| 217 |
if ! cmp -s node_modules/.package.json package.json; then |
| 218 |
cat 2>&1 <<EOF |
| 219 |
*** package.json is out of sync with node_modules |
| 220 |
*** If you modified package.json, please run: |
| 221 |
*** |
| 222 |
*** tools/node-modules install |
| 223 |
*** |
| 224 |
*** and add the result to the index. |
| 225 |
EOF |
| 226 |
exit 1 |
| 227 |
fi |
| 228 |
|
| 229 |
# Only copy the package-lock.json if it differs from the one we have |
| 230 |
if ! cmp -s node_modules/.package-lock.json package-lock.json; then |
| 231 |
message COPY package-lock.json |
| 232 |
cp node_modules/.package-lock.json package-lock.json |
| 233 |
fi |
| 234 |
|
| 235 |
# We're now in a situation where: |
| 236 |
# - the checked out node_modules is equal to the gitlink in the index |
| 237 |
# - the package.json in the tree is equal to the one in node_modules |
| 238 |
# - ditto package-lock.json |
| 239 |
exit 0 |
| 240 |
} |
| 241 |
|
| 242 |
main() { |
| 243 |
if [ $# = 0 ]; then |
| 244 |
# don't list the "private" ones |
| 245 |
echo 'This command requires a subcommand: remove checkout install push verify runtime-tar' |
| 246 |
exit 1 |
| 247 |
fi |
| 248 |
|
| 249 |
local fname |
| 250 |
fname="$(printf 'cmd_%s' "$1" | tr '-' '_')" |
| 251 |
if ! type -t "${fname}" | grep -q function; then |
| 252 |
echo "Unknown subcommand '$1'" |
| 253 |
exit 1 |
| 254 |
fi |
| 255 |
|
| 256 |
shift |
| 257 |
[ -n "${quiet}" ] || set -x |
| 258 |
"${fname}" "$@" |
| 259 |
} |
| 260 |
|
| 261 |
main "$@" |