-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathprepare-deps.sh
executable file
·74 lines (59 loc) · 3.26 KB
/
prepare-deps.sh
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Script to manually prepare dependencies for the SDK, examples, or tests
# It will go through each workspace in the provided environment and prepare the dependencies
# The dependencies are prepared by having the script check its dependencies and devDependencies
# and replace the package version with a local tarball version of the package
# The script will then pack the workspace if it is a dependency of the SDK package, or the sdk package itself
# This is much faster than going through all workspaces individually and running `pnpm add @imtbl/*` for each
# workspace package and then package each workspace separately. This script will do it all in one go in parallel.
# Get environment to prepare from the first argument
envToPrepare=$1
# Check if an argument was provided
if [ -z "$envToPrepare" ]; then
echo "Please provide the environment to prepare"
echo "Environments: sdk | examples | tests"
echo "Example: ./prepare-deps.sh sdk"
exit 1
fi
# Set the initial workspace filter to the SDK package and its workspace dependencies recursively
workspaceFilter="@imtbl/sdk..."
# Change the workspace filter based on the provided environment
if [ "$envToPrepare" = "examples" ]; then
workspaceFilter="@examples/**"
elif [ "$envToPrepare" = "tests" ]; then
workspaceFilter="@tests/**"
fi
echo "Preparing $envToPrepare..."
prepare_deps() {
# Get all dependencies and devDependencies that start with @imtbl/ and combine them into a single list
deps=$(pnpm list --json | jq -r ".[0].dependencies | keys[] | select(startswith(\"@imtbl/\"))")
devDeps=$(pnpm list --json | jq -r ".[0].devDependencies | keys[] | select(startswith(\"@imtbl/\"))")
combinedDeps=$(echo -e "$deps\n$devDeps")
# Loop through each dependency and devDependency and replace the package version with a local tarball version matching what the output name will be
while read -r dep; do
# Skip if the dependency is empty
if [ -z "$dep" ]; then
continue
fi
# Skip if the dependency is not in the workspace
if ! pnpm recursive list --depth -1 | grep -q $dep; then
continue
fi
# Get the packed dependency filename. For example, @imtbl/sdk -> imtbl-sdk-0.0.0.tgz
# version=$(pnpm exec jq -r ".version" package.json)
packedDepFilename=$(echo "$dep" | sed "s/@imtbl\///" | sed "s/\//-/" | sed "s/^/imtbl-/" | sed "s/$/-0.0.0.tgz/")
# Check if the dependency is a dependency or a devDependency and replace the version with a local tarball version
if echo "$deps" | grep -q "$dep"; then
pnpm exec jq ".dependencies[\"$dep\"] = \"file:$(dirname $(pnpm root -w))/$packedDepFilename\"" package.json > package.tmp.json
elif echo "$devDeps" | grep -q "$dep"; then
pnpm exec jq ".devDependencies[\"$dep\"] = \"file:$(dirname $(pnpm root -w))/$packedDepFilename\"" package.json > package.tmp.json
fi
mv package.tmp.json package.json
done <<< "$combinedDeps"
# Pack the workspace if it is a dependency of the SDK package or is the SDK package itself
if [ "$1" = "sdk" ]; then
pnpm pack --pack-destination "$(dirname "$(pnpm root -w)")"
fi
}
# Run the below recursively for each workspace in the workspace filter set above
pnpm --parallel --filter $workspaceFilter exec bash -c "$(declare -f prepare_deps); prepare_deps $envToPrepare"