-
Notifications
You must be signed in to change notification settings - Fork 32
/
npm-pack-and-replace.sh
executable file
·80 lines (64 loc) · 2.7 KB
/
npm-pack-and-replace.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
75
76
77
78
79
80
#!/usr/bin/env bash
#
# Copyright (c) Gala Games Inc. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -eu
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
current_dir="$(pwd)"
skip_confirmation_flag="${1:-}"
echo "The script (1) packs all npm packages from source, (2) puts the tar archives in current directory, and (3) replaces dependencies in current package.json to packed packages.
- source directory: $script_dir
- current directory: $current_dir"
if [ "$skip_confirmation_flag" != "--skipConfirmation" ]; then
echo "Do you want to proceed? (y/n)"
read -r proceed
if [ "$proceed" != "y" ]; then
echo "Aborting."
exit 1
fi
fi
to_install=()
replace_in_pwd_package_json() {
path="$1"
package="$2"
current_package_json_path="$current_dir/package.json"
if ! grep -q "\"$package\":" "$current_package_json_path"; then
echo "Package $package not found in $current_package_json_path. Skipping."
return
fi
echo "Packing and replacing $package..."
# pack the package
tar_file="$( (cd "$path" && npm run build && npm pack --pack-destination="$current_dir") | tail -n 1)"
# update package json with reference to the file
current_package_json="$(cat "$current_package_json_path")"
package_escaped="${package//\//\\/}"
updated_package_json="$(echo "$current_package_json" | sed "s/\"$package_escaped\": \"[^\"]*\"/\"$package_escaped\": \"file:$tar_file\"/g")"
echo "$updated_package_json" > "$current_package_json_path"
to_install+=("file:$tar_file")
}
# note: the order of packages is important
replace_in_pwd_package_json "$script_dir/chain-api" "@gala-chain/api"
replace_in_pwd_package_json "$script_dir/chain-connect" "@gala-chain/connect"
replace_in_pwd_package_json "$script_dir/chain-client" "@gala-chain/client"
replace_in_pwd_package_json "$script_dir/chain-test" "@gala-chain/test"
replace_in_pwd_package_json "$script_dir/chaincode" "@gala-chain/chaincode"
replace_in_pwd_package_json "$script_dir/chain-cli" "@gala-chain/cli"
# we need to install it at the end, because they are cross-dependent
npm i --no-cache "${to_install[@]}"
echo ""
echo "Done. Installed packages:"
for package in "${to_install[@]}"; do
echo " - $package"
done
echo ""