1#!/usr/bin/env nix-shell
2#! nix-shell -i bash -p nodejs yarn prefetch-yarn-deps jq rsync common-updater-scripts moreutils
3
4set -exuo pipefail
5
6expr_dir=$(cd "$(dirname "$0")"; pwd)
7tmp=$(mktemp -dt update-meshcentral.XXXXXX)
8
9npm show --json meshcentral > "$tmp/npm.json"
10version=$(<"$tmp/npm.json" jq -r .version)
11tarball=$(<"$tmp/npm.json" jq -r .dist.tarball)
12
13prefetch=$(nix-prefetch-url --unpack --print-path "$tarball" | tr '\n' ' ')
14read -r hash storePath <<<"$prefetch"
15cd "$tmp"
16rsync -r --chmod=u=rwX "$storePath/" package/
17cd package
18
19# Very crude way of discovering optional dependencies. These are
20# fetched at runtime by stock upstream, but we don't allow that kind
21# of thing in nix :)
22awk <meshcentral.js "
23 BEGIN { RS=\"[\n;]\" }
24 match(\$0, /(modules|passport) = (\[.*\])$/, a) { print a[2] }
25 match(\$0, /(modules|passport).push\(('[^)]+')\)/, a) { print \"[\" a[2] \"]\" }
26" |
27 tr \' \" |
28 jq --slurp '[if type == "array" then .[] else . end] | flatten' |
29 # And an equally crude way of adding them to package.json. We
30 # can't use yarn add here, because that will blow up on
31 # dependencies which don't support the current platform. Even with
32 # --optional.
33 jq --slurpfile package package.json \
34 '(. | map(. | capture("(?<name>@?[^@]+)(@(?<version>.+))?") | { key: .name, value: (.version // "*")}) | from_entries) as $optionalDependencies | $package | .[] | .optionalDependencies |= . + $optionalDependencies' |
35 sponge package.json
36
37# Fetch all the optional dependencies, so we have them available in
38# yarn.lock/yarn.nix
39yarn install --ignore-scripts
40
41cp package.json "$expr_dir"
42cp yarn.lock "$expr_dir/yarn.lock"
43
44cd "$expr_dir/../../../.."
45update-source-version meshcentral "$version" "$hash" "$tarball"
46
47new_yarn_hash=$(prefetch-yarn-deps "$expr_dir/yarn.lock")
48new_yarn_hash=$(nix-hash --type sha256 --to-sri "$new_yarn_hash")
49old_yarn_hash=$(nix-instantiate --eval -A meshcentral.offlineCache.outputHash | tr -d '"')
50sed -i "$expr_dir/default.nix" -e "s|\"$old_yarn_hash\"|\"$new_yarn_hash\"|"
51
52# Only clean up if everything worked
53cd /
54rm -rf "$tmp"