Skip to content

Commit 8927d25

Browse files
committed
cli: implement ntt cli
1 parent c371e7d commit 8927d25

18 files changed

+3019
-77
lines changed

Dockerfile.cli

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
FROM ubuntu:latest as base
2+
3+
RUN apt update
4+
5+
RUN apt install -y python3
6+
RUN apt install -y build-essential
7+
RUN apt install -y git
8+
RUN apt install -y curl
9+
RUN apt install -y unzip
10+
11+
RUN curl -fsSL https://bun.sh/install | bash
12+
13+
RUN curl -L https://foundry.paradigm.xyz | bash
14+
RUN bash -ci "foundryup"
15+
16+
RUN apt install -y jq
17+
18+
FROM base as cli-remote
19+
# NOTE: when invoking the installer outside of the source tree, it clones the
20+
# repo and installs that way.
21+
# This build stage tests that path.
22+
COPY cli/install.sh cli/install.sh
23+
RUN bash -ci "./cli/install.sh"
24+
RUN bash -ci "which ntt"
25+
26+
FROM base as cli-local
27+
# NOTE: when invoking the installer inside of the source tree, it installs from
28+
# the local source tree.
29+
# This build stage tests that path.
30+
WORKDIR /app
31+
COPY tsconfig.json tsconfig.json
32+
COPY tsconfig.esm.json tsconfig.esm.json
33+
COPY tsconfig.cjs.json tsconfig.cjs.json
34+
COPY package.json package.json
35+
COPY package-lock.json package-lock.json
36+
COPY sdk sdk
37+
COPY solana/package.json solana/package.json
38+
COPY solana/ts solana/ts
39+
COPY evm/ts evm/ts
40+
COPY solana/tsconfig.*.json solana/
41+
COPY cli/package.json cli/package.json
42+
COPY cli/package-lock.json cli/package-lock.json
43+
COPY cli/src cli/src
44+
COPY cli/install.sh cli/install.sh
45+
RUN bash -ci "./cli/install.sh"
46+
RUN bash -ci "which ntt"
47+
48+
FROM cli-local as cli-local-test
49+
COPY cli/test cli/test
50+
COPY evm evm
51+
RUN bash -ci "./cli/test/sepolia-bsc.sh"

cli/example-overrides.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"chains": {
3+
"Bsc": {
4+
"rpc": "http://127.0.0.1:8545"
5+
},
6+
"Sepolia": {
7+
"rpc": "http://127.0.0.1:8546"
8+
},
9+
"Solana": {
10+
"rpc": "http://127.0.0.1:8899"
11+
}
12+
}
13+
}

cli/install.sh

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# check that 'bun' is installed
6+
7+
if ! command -v bun > /dev/null; then
8+
echo "bun is not installed. Follow the instructions at https://bun.sh/docs/installation"
9+
exit 1
10+
fi
11+
12+
REPO="https://github.com/wormhole-foundation/example-native-token-transfers.git"
13+
14+
function main {
15+
branch=""
16+
17+
while [[ $# -gt 0 ]]; do
18+
key="$1"
19+
20+
case $key in
21+
-b|--branch)
22+
branch="$2"
23+
shift
24+
shift
25+
;;
26+
-r|--repo)
27+
REPO="$2"
28+
shift
29+
shift
30+
;;
31+
*)
32+
echo "Unknown option $key"
33+
exit 1
34+
;;
35+
esac
36+
done
37+
38+
path=""
39+
mkdir -p "$HOME/.ntt-cli"
40+
41+
# check if there's a package.json in the parent directory, with "name": "@wormhole-foundation/ntt-cli"
42+
if [ -f "$(dirname $0)/package.json" ] && grep -q '"name": "@wormhole-foundation/ntt-cli"' "$(dirname $0)/package.json"; then
43+
path="$(dirname $0)/.."
44+
version=$(git -C "$path" rev-parse HEAD)
45+
dirty=$(git -C "$path" diff --quiet || echo "-dirty")
46+
echo "$version$dirty" > "$HOME/.ntt-cli/version"
47+
else
48+
# if branch is set, use it. otherwise use the latest tag of the form "vX.Y.Z+cli" or the 'cli' branch
49+
if [ -z "$branch" ]; then
50+
branch="$(select_branch)"
51+
fi
52+
53+
# clone to $HOME/.ntt-cli if it doesn't exist, otherwise update it
54+
echo "Cloning $REPO $branch"
55+
56+
path="$HOME/.ntt-cli/.checkout"
57+
58+
if [ ! -d "$path" ]; then
59+
git clone --branch "$branch" "$REPO" "$path"
60+
else
61+
pushd "$path"
62+
# update origin url to REPO
63+
git remote set-url origin "$REPO"
64+
git fetch origin
65+
# reset hard
66+
git reset --hard "origin/$branch"
67+
version=$(git rev-parse HEAD)
68+
dirty=$(git diff --quiet || echo "-dirty")
69+
echo "$version$dirty" > "$HOME/.ntt-cli/version"
70+
popd
71+
fi
72+
fi
73+
74+
absolute_path="$(cd $path && pwd)"
75+
echo $absolute_path >> "$HOME/.ntt-cli/version"
76+
77+
# jq would be nicer but it's not portable
78+
# here we make the assumption that the file uses 2 spaces for indentation.
79+
# this is a bit fragile, but we don't want to catch further nested objects
80+
# (there might be a "version" in the scripts section, for example)
81+
version=$(cat "$path/cli/package.json" | grep '^ "version":' | cut -d '"' -f 4)
82+
echo "$version" >> "$HOME/.ntt-cli/version"
83+
84+
remote_url=$(git -C "$path" remote get-url origin)
85+
echo "$remote_url" >> "$HOME/.ntt-cli/version"
86+
87+
echo "Installing ntt CLI version $version"
88+
install_cli "$path"
89+
}
90+
91+
# function that determines which branch/tag to clone
92+
function select_branch {
93+
# if the repo has a tag of the form "vX.Y.Z+cli", use that (the latest one)
94+
# otherwise we'll use the 'cli' branch
95+
branch=""
96+
regex="refs/tags/v[0-9]*\.[0-9]*\.[0-9]*+cli"
97+
if git ls-remote --tags "$REPO" | grep -q "$regex"; then
98+
branch="$(git ls-remote --tags "$REPO" | grep "$regex" | sort -V | tail -n 1 | awk '{print $2}')"
99+
else
100+
branch="cli"
101+
fi
102+
103+
echo "$branch"
104+
}
105+
106+
function install_cli {
107+
cd "$1"
108+
109+
# if 'ntt' is already installed, uninstall it
110+
# just check with 'which'
111+
if which ntt > /dev/null; then
112+
echo "Removing existing ntt CLI"
113+
rm $(which ntt)
114+
fi
115+
116+
# swallow the output of the first install
117+
# TODO: figure out why it fails the first time.
118+
bun install > /dev/null 2>&1 || true
119+
bun install
120+
121+
# make a temporary directory
122+
123+
tmpdir="$(mktemp -d)"
124+
125+
# create a temporary symlink 'npm' to 'bun'
126+
127+
ln -s "$(command -v bun)" "$tmpdir/npm"
128+
129+
# add the temporary directory to the PATH
130+
131+
export PATH="$tmpdir:$PATH"
132+
133+
# swallow the output of the first build
134+
# TODO: figure out why it fails the first time.
135+
bun --bun run --filter '*' build > /dev/null 2>&1 || true
136+
bun --bun run --filter '*' build
137+
138+
# remove the temporary directory
139+
140+
rm -r "$tmpdir"
141+
142+
# now link the CLI
143+
144+
cd cli
145+
146+
bun link
147+
148+
bun link @wormhole-foundation/ntt-cli
149+
}
150+
151+
main "$@"

cli/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"name": "cli",
2+
"name": "@wormhole-foundation/ntt-cli",
3+
"version": "1.0.0-beta",
34
"module": "src/index.ts",
45
"type": "module",
56
"devDependencies": {
@@ -13,7 +14,8 @@
1314
"ntt": "src/index.ts"
1415
},
1516
"dependencies": {
17+
"chalk": "^5.3.0",
1618
"yargs": "^17.7.2"
1719
},
18-
"version": "0.2.0"
19-
}
20+
"version": "1.0.0"
21+
}

0 commit comments

Comments
 (0)