-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
50 lines (48 loc) · 2.59 KB
/
update.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
#!/usr/bin/env bash
# Get the current values of main, types, dist-main and dist-types
main=$(jq -r '."main" // ""' package.json)
types=$(jq -r '."types" // ""' package.json)
dist_main=$(jq -r '."dist-main" // ""' package.json)
dist_types=$(jq -r '."dist-types" // ""' package.json)
exports=$(jq -r '."exports" // ""' package.json)
dist_exports=$(jq -r '."dist-exports" // ""' package.json)
# Get the first argument passed to the script as the npm lifecycle event
npm_lifecycle_event=$1
# Check if the script is run as prepublish or postpublish
if [ "$npm_lifecycle_event" == "prepublish" ]; then
# Swap the values of main and dist-main in the package.json file if they are not empty
if [ -n "$main" ] && [ -n "$dist_main" ]; then
jq --arg main "$main" --arg dist_main "$dist_main" \
'."main" = $dist_main | ."dist-main" = $main' package.json > tmp.json && mv tmp.json package.json
fi
# Swap the values of types and dist-types in the package.json file if they are not empty
if [ -n "$types" ] && [ -n "$dist_types" ]; then
jq --arg types "$types" --arg dist_types "$dist_types" \
'."types" = $dist_types | ."dist-types" = $types' package.json > tmp.json && mv tmp.json package.json
fi
# Rename the dist-exports field to exports in the package.json file if it is not empty
if [ -n "$dist_exports" ]; then
jq --argjson dist_exports "$dist_exports" \
'."exports" = $dist_exports | del(."dist-exports")' package.json > tmp.json && mv tmp.json package.json
fi
elif [ "$npm_lifecycle_event" == "postpublish" ]; then
# Swap back the values of main and dist-main in the package.json file if they are not empty
if [ -n "$main" ] && [ -n "$dist_main" ]; then
jq --arg main "$main" --arg dist_main "$dist_main" \
'."main" = $dist_main | ."dist-main" = $main' package.json > tmp.json && mv tmp.json package.json
fi
# Swap back the values of types and dist-types in the package.json file if they are not empty
if [ -n "$types" ] && [ -n "$dist_types" ]; then
jq --arg types "$types" --arg dist_types "$dist_types" \
'."types" = $dist_types | ."dist-types" = $types' package.json > tmp.json && mv tmp.json package.json
fi
# Rename the exports field to dist-exports in the package.json file if it is not empty
if [ -n "$exports" ]; then
jq --argjson exports "$exports" \
'."dist-exports" = $exports | del(."exports")' package.json > tmp.json && mv tmp.json package.json
fi
else
# Exit with an error message if neither prepublish nor postpublish or no argument is given
echo "This script should only be run as prepublish or postpublish with an argument."
exit1
fi