Skip to content

Commit

Permalink
feat: indexer and core proto definitions gen and publish
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Sep 20, 2024
1 parent 70931a9 commit 49472ec
Show file tree
Hide file tree
Showing 24 changed files with 2,537 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"scripts": {
"postinstall": "node etc/bootstrapEnv",
"build": "node etc/bootstrapEnv && lerna run build",
"build:core-proto-ts": "./proto/core/gen.sh",
"publish:core-proto-ts": "./proto/core/publish.sh",
"build:indexer-proto-ts": "./proto/indexer/gen.sh",
"publish:indexer-proto-ts": "./proto/indexer/publish.sh",
"build:docs": "yarn typedoc",
"lerna:publish:prerelease": "lerna publish --conventional-commits --conventional-prerelease",
"lerna:publish": "lerna publish patch",
Expand Down
6 changes: 6 additions & 0 deletions proto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
indexer/proto/
indexer/gen/
indexer/proto-ts/
core/gen/
core/proto/
core/proto-ts
145 changes: 145 additions & 0 deletions proto/core/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env bash
set -eo pipefail

ROOT_DIR=./proto/core
BUILD_DIR=$ROOT_DIR/gen
PROTO_DIR=$ROOT_DIR/proto
TS_OUTPUT_DIR=$ROOT_DIR/proto-ts
TS_STUB_DIR=$ROOT_DIR/stub
TS_PROTO_TEMPLATE=$PROTO_DIR/buf.gen.ts.yaml

# remote branches/tags
injective_core_branch=master
cosmos_sdk_branch=v0.50.x-inj
wasmd_branch=v0.50.x-inj

# remove old gen
rm -rf $BUILD_DIR
rm -rf $PROTO_DIR
rm -rf $TS_OUTPUT_DIR

# BSD rate limit
counter=0

########################################
######## TS PROTO GENERATION ###########
########################################
echo "Generating TS proto code..."

## Remove/setup old build dir
mkdir -p $BUILD_DIR
mkdir -p $PROTO_DIR
mkdir -p $PROTO_DIR/proto
mkdir -p $TS_OUTPUT_DIR
mkdir -p $TS_OUTPUT_DIR/cjs
mkdir -p $TS_OUTPUT_DIR/esm
mkdir -p $TS_OUTPUT_DIR/proto

########################################
###### REMOTE PROTO DEFINITIONS ########
########################################

## Clone current proto definitions from core
git clone https://github.com/InjectiveLabs/injective-core.git $BUILD_DIR/injective-core -b $injective_core_branch --depth 1 --single-branch > /dev/null
cp -r $BUILD_DIR/injective-core/proto/injective $PROTO_DIR

## Third Party proto definitions
git clone https://github.com/InjectiveLabs/cosmos-sdk.git $BUILD_DIR/cosmos-sdk -b $cosmos_sdk_branch --depth 1 --single-branch > /dev/null
git clone https://github.com/InjectiveLabs/wasmd $BUILD_DIR/wasmd -b $wasmd_branch --depth 1 --single-branch > /dev/null

buf export $BUILD_DIR/cosmos-sdk --output=$PROTO_DIR
buf export $BUILD_DIR/wasmd --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/ibc-go.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/gogoproto.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cometbft/cometbft.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/ics23.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/ibc-apps.git --exclude-imports --output=$PROTO_DIR --path=middleware/packet-forward-middleware/proto/packetforward/v1

# generate TS proto
proto_dirs=$(find $PROTO_DIR -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)

# gen using ts-proto
npm --prefix $ROOT_DIR install
for dir in $proto_dirs; do
protoc \
--plugin="$ROOT_DIR/node_modules/.bin/protoc-gen-ts_proto" \
--ts_proto_opt="esModuleInterop=true" \
--ts_proto_opt="forceLong=string" \
--ts_proto_opt="env=both" \
--ts_proto_opt="useExactTypes=false" \
--ts_proto_opt="outputClientImpl=grpc-web" \
--ts_proto_out="$TS_OUTPUT_DIR/proto" \
-I "$PROTO_DIR" \
$(find "${dir}" -maxdepth 1 -name '*.proto')
done


#######################################
###### NPM PACKAGES GENERATION #######
#######################################

echo "Compiling npm packages..."

## 1. Replace package with our own fork
search1="@improbable-eng/grpc-web"
replace1="@injectivelabs/grpc-web"

FILES=$( find $TS_OUTPUT_DIR -type f )

for file in $FILES
do
sed -ie "s/${search1//\//\\/}/${replace1//\//\\/}/g" "$file"
done

## 2. Replace extension type to ignore on compile time
search1="getExtension():"
replace1="// @ts-ignore \n getExtension():"
search2="setExtension("
replace2="// @ts-ignore \n setExtension("

FILES=$( find $TS_OUTPUT_DIR -type f -name '*.d.ts' )

for file in $FILES
do
sed -ie "s/${search1//\//\\/}/${replace1//\//\\/}/g" "$file"
sed -ie "s/${search2//\//\\/}/${replace2//\//\\/}/g" "$file"
done

ESM_PKG_TEMPLATE=$TS_STUB_DIR/package.json.esm.template
ESM_CFG_TEMPLATE=$TS_STUB_DIR/tsconfig.json.esm.template
CJS_PKG_TEMPLATE=$TS_STUB_DIR/package.json.cjs.template
CJS_CFG_TEMPLATE=$TS_STUB_DIR/tsconfig.json.cjs.template

## 3. Compile TypeScript for ESM package
cp $TS_STUB_DIR/index.ts.template $TS_OUTPUT_DIR/proto/index.ts

### ESM
cp $ESM_PKG_TEMPLATE $TS_OUTPUT_DIR/proto/package.json
cp $ESM_CFG_TEMPLATE $TS_OUTPUT_DIR/proto/tsconfig.json
npm --prefix $TS_OUTPUT_DIR/proto install
npm --prefix $TS_OUTPUT_DIR/proto run gen
cp $ESM_PKG_TEMPLATE $TS_OUTPUT_DIR/esm/package.json

## CJS
cp $CJS_PKG_TEMPLATE $TS_OUTPUT_DIR/proto/package.json
cp $CJS_CFG_TEMPLATE $TS_OUTPUT_DIR/proto/tsconfig.json
npm --prefix $TS_OUTPUT_DIR/proto install
npm --prefix $TS_OUTPUT_DIR/proto run gen
cp $CJS_PKG_TEMPLATE $TS_OUTPUT_DIR/cjs/package.json

## 4. Setup proper package.json for core-proto-ts packages
cp $TS_STUB_DIR/package.json.core-proto-ts.template $TS_OUTPUT_DIR/package.json

## CJS DEPRECATED
# mkdir -p $TS_OUTPUT_DIR/chain-api
# cp $TS_STUB_DIR/package.json.chain-api.template $TS_OUTPUT_DIR/chain-api/package.json

# 5. Clean up folders
rm -rf $BUILD_DIR
rm -rf $PROTO_DIR
rm -rf $TS_OUTPUT_DIR/proto
find $TS_OUTPUT_DIR -name "*.jse" -type f -delete
find $TS_OUTPUT_DIR -name "*.tse" -type f -delete
find $TS_OUTPUT_DIR -name "*.jsone" -type f -delete

echo "Done!"
Loading

0 comments on commit 49472ec

Please sign in to comment.