Debian Codegraph @

tools/node-modules

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/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.shscip-shell source . . `test/common/git-utils.sh`.
15
16 cmd_removescip-shell shell shell-project 0.0.0 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 rmscip-shell system . . rm. -rf node_modules
22 gitscip-shell system . . git. submodule deinit node_modules
23 rmscip-shell system . . rm. -rf -- "$(gitscip-shell system . . git. rev-parse --absolute-git-dir)/modules/node_modules"
24 }
25
26 cmd_checkoutscip-shell shell shell-project 0.0.0 cmd_checkout().() {
27 # we default to check out the node_modules corresponding to the gitlink in the index
28 local force=""
29 if [scip-shell system . . `[`. "${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 [scip-shell system . . `[`. -z "$force" ]; then
42 if ! cmp_from_cache "${sha}" '.package.json' 'package.json'; then
43 catscip-shell system . . 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_removescip-shell shell shell-project 0.0.0 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_installscip-shell shell shell-project 0.0.0 cmd_install().() {
70 testscip-shell system . . 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="$(sha256sumscip-shell system . . sha256sum. package.json | git_cache commit-tree "${tree}")"
75 git_cache tag "sha-${commit}" "${commit}" "--no-sign"
76 cmd_checkoutscip-shell shell shell-project 0.0.0 cmd_checkout(). "${commit}"
77
78 catscip-shell system . . 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_pushscip-shell shell shell-project 0.0.0 cmd_push().() {
88 # push via the cache: the shared history with the remote helps to thin out the pack we send
89 tag="sha-$(gitscip-shell system . . git. -C node_modules rev-parse HEAD)"
90 message PUSH "${GITHUB_REPO} ${tag}"
91 git_cache push "${SSH_REMOTE}" "${tag}"
92 }
93
94 cmd_verifyscip-shell shell shell-project 0.0.0 cmd_verify().() {
95 testscip-shell system . . test. -e bots || test/common/make-bots
96
97 # Verifies that the package.json and node_modules of the given commit match.
98 commit="$(gitscip-shell system . . 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="$(gitscip-shell system . . git. cat-file blob "$1:package.json" | bots/npm download | tar_to_cache)"
103
104 if [scip-shell system . . `[`. "${committed_tree}" != "${expected_tree}" ]; then
105 exec >&2
106 printfscip-shell system . . printf. "\nCommit %s package.json and node_modules aren't in sync!\n\n" "$1"
107 gitscip-shell system . . git. --no-pager show --stat "$1"
108
109 printfscip-shell system . . printf. "\nThe above commit refers to the following node_modules commit:\n\n"
110 git_cache --no-pager show --no-patch "${commit}"
111
112 printfscip-shell system . . 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_tarscip-shell shell shell-project 0.0.0 cmd_runtime_tar().() {
120 if [scip-shell system . . `[`. -z "${1:-}" ]; then
121 echoscip-shell system . . echo. "Usage: $0 runtime-tar <tarball-path>" >&2
122 exit 1
123 fi
124 if [scip-shell system . . `[`. ! -e node_modules/.package-lock.json ]; then
125 echoscip-shell system . . 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="$(jqscip-shell system . . jq. -r '.lockfileVersion' node_modules/.package-lock.json)"
133 if [scip-shell system . . `[`. "$lockfile_version" != "3" ]; then
134 echoscip-shell system . . 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="$(jqscip-shell system . . 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 [scip-shell system . . `[`. -n "$(lsscip-shell system . . ls. -A "$pkg" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" ] && echoscip-shell system . . 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 # react-dropzone 14.4.0 started to include a dist tarball (https://github.com/react-dropzone/react-dropzone/issues/1447)
159 for pkg in $runtime_pkgs; do
160 if findscip-shell system . . find. "$pkg" -type f ! -path '*/dist/react-dropzone*.tgz' -print0 |
161 xargsscip-shell system . . xargs. -0 file --mime |
162 grepscip-shell system . . grep. -Ev \
163 -e 'text/' \
164 -e 'inode/x-empty' \
165 -e 'application/(javascript|json)' \
166 -e 'font/' \
167 -e 'image/' \
168 -e 'sbcs-data-generated.js' \
169 -e 'cup.coffee' \
170 -e '\.woff'; then
171 echoscip-shell system . . echo. "Error: package '$pkg' contains architecture dependent files, cannot include in runtime tarball" >&2
172 exit 1
173 fi
174 done
175
176 # Create tarball
177 {
178 echoscip-shell system . . echo. "node_modules/.package-lock.json"
179 echoscip-shell system . . echo. "$runtime_pkgs"
180 } | tarscip-shell system . . tar. --format=ustar --sort=name --owner=root:0 --group=root:0 --xz -cf "$tar_path" -C . -T -
181 }
182
183 # called from Makefile.am
184 cmd_make_package_lock_jsonscip-shell shell shell-project 0.0.0 cmd_make_package_lock_json().() {
185 # Run from make to ensure package-lock.json is up to date
186
187 # package-lock.json is used as the stamp file for all things that use
188 # node_modules, so this is the main bit of glue that drives the entire process
189
190 # We try our best not to touch package-lock.json unless it actually changes
191
192 # This isn't going to work for a tarball, but as long as
193 # package-lock.json is already there, and newer than package.json,
194 # we're OK
195 if [scip-shell system . . `[`. ! -e .git ]; then
196 if [scip-shell system . . `[`. package-lock.json -nt package.json ]; then
197 exit 0
198 fi
199
200 echoscip-shell system . . echo. "*** Can't update node modules unless running from git" >&2
201 exit 1
202 fi
203
204 # Otherwise, our main goal is to ensure that the node_modules from
205 # the index is the one that we actually have.
206 local sha
207 sha="$(get_index_gitlink node_modules)"
208 if [scip-shell system . . `[`. ! -e node_modules/.git ]; then
209 # nothing there yet...
210 cmd_checkoutscip-shell shell shell-project 0.0.0 cmd_checkout().
211 elif [scip-shell system . . `[`. "$(gitscip-shell system . . git. -C node_modules rev-parse HEAD)" != "${sha}" ]; then
212 # wrong thing there...
213 cmd_checkoutscip-shell shell shell-project 0.0.0 cmd_checkout().
214 fi
215
216 # This check is more about catching local changes to package.json than
217 # about validating something we just checked out:
218 if ! cmpscip-shell system . . cmp. -s node_modules/.package.json package.json; then
219 catscip-shell system . . cat. 2>&1 <<EOF
220 *** package.json is out of sync with node_modules
221 *** If you modified package.json, please run:
222 ***
223 *** tools/node-modules install
224 ***
225 *** and add the result to the index.
226 EOF
227 exit 1
228 fi
229
230 # Only copy the package-lock.json if it differs from the one we have
231 if ! cmpscip-shell system . . cmp. -s node_modules/.package-lock.json package-lock.json; then
232 message COPY package-lock.json
233 cpscip-shell system . . cp. node_modules/.package-lock.json package-lock.json
234 fi
235
236 # We're now in a situation where:
237 # - the checked out node_modules is equal to the gitlink in the index
238 # - the package.json in the tree is equal to the one in node_modules
239 # - ditto package-lock.json
240 exit 0
241 }
242
243 mainscip-shell shell shell-project 0.0.0 main().() {
244 if [scip-shell system . . `[`. $# = 0 ]; then
245 # don't list the "private" ones
246 echoscip-shell system . . echo. 'This command requires a subcommand: remove checkout install push verify runtime-tar'
247 exit 1
248 fi
249
250 local fname
251 fname="$(printfscip-shell system . . printf. 'cmd_%s' "$1" | trscip-shell system . . tr. '-' '_')"
252 if ! type -t "${fname}" | grepscip-shell system . . grep. -q function; then
253 echoscip-shell system . . echo. "Unknown subcommand '$1'"
254 exit 1
255 fi
256
257 shift
258 [scip-shell system . . `[`. -n "${quiet}" ] || set -x
259 "${fname}" "$@"
260 }
261
262 mainscip-shell shell shell-project 0.0.0 main(). "$@"