1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps nodejs
3set -euo pipefail
4
5version=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s https://api.github.com/repos/microsoft/pyright/releases/latest | jq -r '.tag_name | sub("^v"; "")')
6
7update-source-version pyright "$version"
8
9root="$(dirname "$(readlink -f "$0")")"
10FILE_PATH="$root/package.nix"
11REPO_URL_PREFIX="https://github.com/microsoft/pyright/raw"
12TEMP_DIR=$(mktemp -d)
13
14trap 'rm -rf "$TEMP_DIR"' EXIT
15
16# Function to download `package-lock.json` for a given source path and update hash
17update_hash() {
18 local source_root_path="$1"
19 local existing_hash="$2"
20
21 local package_url="${REPO_URL_PREFIX}/${version}${source_root_path}"
22 if [ "$source_root_path" == "" ]; then
23 pushd "${TEMP_DIR}"
24 curl -fsSL "$package_url/package.json" | jq '
25 .devDependencies |= with_entries(select(.key == "glob" or .key == "jsonc-parser"))
26 | .scripts = { }
27 ' > package.json
28 npm install --package-lock
29 cp package-lock.json "$root/package-lock.json"
30 popd
31 else
32 curl -fsSL -o "${TEMP_DIR}/package-lock.json" "$package_url/package-lock.json"
33 fi
34
35 # Calculate the new hash
36 local new_hash
37 new_hash=$(prefetch-npm-deps "${TEMP_DIR}/package-lock.json")
38
39 # Update npmDepsHash in the original file
40 sed -i "s|$existing_hash|${new_hash}|" "$FILE_PATH"
41}
42
43while IFS= read -r source_root_line; do
44 [[ "$source_root_line" =~ sourceRoot ]] || continue
45 source_root_path=$(echo "$source_root_line" | sed -e 's/^.*"${src.name}\(.*\)";.*$/\1/')
46
47 # Extract the current npmDepsHash for this sourceRoot
48 existing_hash=$(grep -A1 "$source_root_line" "$FILE_PATH" | grep 'npmDepsHash' | sed -e 's/^.*npmDepsHash = "\(.*\)";$/\1/')
49
50 # Call the function to download and update the hash
51 update_hash "$source_root_path" "$existing_hash"
52done < "$FILE_PATH"