Skip to content

Commit

Permalink
fix(ts): ensures patches are preserved during code generation (#166)
Browse files Browse the repository at this point in the history
Co-authored-by: Artur Troian <[email protected]>
  • Loading branch information
ygrishajev and troian authored May 3, 2024
1 parent 43563b8 commit f004622
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ clean:
rm -rf $(AKASH_TS_ROOT)/dist

.PHONY: clean-codegen
clean-codegen:
rm -rf $(AKASH_TS_ROOT)/src/generated
clean-codegen: $(AKASH_DEVCACHE_BASE)/tmp/ts
rm -rf $(AKASH_TS_ROOT)/src/generated

$(AKASH_DEVCACHE_BASE)/tmp/ts:
script/ts-patches.sh preserve
6 changes: 5 additions & 1 deletion script/protocgen-legacy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash

set -eo pipefail

PATH=$(pwd)/.cache/bin/legacy:$PATH
Expand Down Expand Up @@ -98,3 +97,8 @@ cp -rv github.com/akash-network/akash-api/* ./
--doc_out=./docs/proto \
--doc_opt=./docs/protodoc-markdown.tmpl,provider.md \
$(find "./proto/provider" -maxdepth 4 -name '*.proto')

script/ts-patches.sh restore

cd "$AKASH_TS_ROOT" && npm run format

46 changes: 46 additions & 0 deletions script/ts-patches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

generated_dir="$AKASH_TS_ROOT/src/generated"
tmp_dir="$AKASH_DEVCACHE_BASE/tmp/ts"

if [ ! -d "$generated_dir" ]; then
echo "Directory $generated_dir does not exist. Skipping..."
exit 0
fi

preserve_patches() {
echo "Preserving TypeScript patches..."
find "$generated_dir" -type f -name "*.original.ts" | while read -r src_file; do
src_file=${src_file//.original/}
gen_dir=$(dirname "$src_file")
gen_dir=${gen_dir//$generated_dir\//}
file=$(basename "$src_file" .original.ts)
mkdir -p "$tmp_dir/$gen_dir"
tmp_file="$tmp_dir/$gen_dir/$file"

echo "Preserving $src_file to $tmp_file"
cp "$src_file" "$tmp_file"
done
}

restore_patches() {
echo "Restoring TypeScript patches..."
find "$tmp_dir" -type f -name "*.ts" | while read -r src_file; do
original_file_path=${src_file/$tmp_dir\//}
renamed_original_file_path="${original_file_path/.ts/.original.ts}"
echo "Restoring $original_file_path to $generated_dir/$original_file_path"

mv "$generated_dir/$original_file_path" "$generated_dir/$renamed_original_file_path"
mv "$tmp_dir/$original_file_path" "$generated_dir/$original_file_path"
done
rm -rf "$tmp_dir"
}

if [ "$1" = "preserve" ]; then
preserve_patches
elif [ "$1" = "restore" ]; then
restore_patches
else
echo "Invalid argument. Use 'preserve' or 'restore'."
exit 1
fi
2 changes: 2 additions & 0 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ The files in the `src/generated` directory are auto-generated and should not be
export * from '../../../../patch/cosmos/base/v1beta1/coin';
```

NOTE: Naming and paths are important to prevent the original file from being overwritten when the code is regenerated. See `script/preserve-ts-patches.sh` and `script/restore-ts-patches.sh` for more implementation details.

### License
This package is licensed under the Apache-2.0.
4 changes: 2 additions & 2 deletions ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"scripts": {
"build": "rimraf dist && tsc -p tsconfig.build.json && npm run format",
"format": "prettier --write './**/*.{js,jsx,ts,tsx,css,json}' --config ./.prettierrc",
"format": "prettier --write './**/*.{js,jsx,ts,tsx,css,json}' --config ./.prettierrc --log-level silent",
"lint": "eslint src",
"lint:fix": "npm run lint -- --fix",
"postbuild": "script/generate-exports.js",
Expand All @@ -63,4 +63,4 @@
},
"types": "dist/index.d.ts",
"version": "0.0.0"
}
}

0 comments on commit f004622

Please sign in to comment.