summary refs log tree commit diff
path: root/pkgs/tools/admin/meshcentral/update.sh
blob: f7f12078372ed3688cf314177ac278fe2433ef5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p nodejs yarn yarn2nix jq rsync common-updater-scripts moreutils

set -exuo pipefail

expr_dir=$(cd "$(dirname "$0")"; pwd)
tmp=$(mktemp -dt update-meshcentral.XXXXXX)

npm show --json meshcentral > "$tmp/npm.json"
version=$(<"$tmp/npm.json" jq -r .version)
tarball=$(<"$tmp/npm.json" jq -r .dist.tarball)

prefetch=$(nix-prefetch-url --unpack --print-path "$tarball" | tr '\n' ' ')
read -r hash storePath <<<"$prefetch"
cd "$tmp"
rsync -r --chmod=u=rwX "$storePath/" package/
cd package

# Very crude way of discovering optional dependencies. These are
# fetched at runtime by stock upstream, but we don't allow that kind
# of thing in nix :)
awk <meshcentral.js "
  BEGIN { RS=\"[\n;]\" }
  match(\$0, /(modules|passport) = (\[.*\])$/, a) { print a[2] }
  match(\$0, /(modules|passport).push\(('[^']+')\)/, a) { print a[2] }
" |
    tr \' \" |
    jq --slurp '[if type == "array" then .[] else . end] | flatten' |
    # And an equally crude way of adding them to package.json. We
    # can't use yarn add here, because that will blow up on
    # dependencies which don't support the current platform. Even with
    # --optional.
    jq --slurpfile package package.json \
       '(. | map(. | capture("(?<name>@?[^@]+)(@(?<version>.+))?") | { key: .name, value: (.version // "*")}) | from_entries) as $optionalDependencies | $package | .[] | .optionalDependencies |= . + $optionalDependencies' |
    sponge package.json

# Fetch all the optional dependencies, so we have them available in
# yarn.lock/yarn.nix
yarn install --ignore-scripts

cp package.json "$expr_dir"
cp yarn.lock "$expr_dir/yarn.lock"
yarn2nix > "$expr_dir/yarn.nix"

cd "$expr_dir/../../../.."
update-source-version meshcentral "$version" "$hash" "$tarball"

# Only clean up if everything worked
cd /
rm -rf "$tmp"