Skip to content
Open
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions scripts/generate-nestjs-sdk.bash
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
#!/bin/bash
#
#

# Note: This script is meant to be run from the project root, e.g.
# ./scripts/generate-nestjs-sdk.bash
if [ ! -d "node_modules" ]; then
echo "Error: No node_modules folder found. This script is means to be run from the project root."
exit 1
fi

# This default is for when developing with a backend running directly on localhost
SWAGGER_API_URL="http://localhost:3000/explorer-json"
while [[ "$#" -gt 0 ]]; do
case "$1" in
--swagger-url)
shift
SWAGGER_API_URL=$1
shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done

USER=`who am i | cut -d\ -f1`
echo -e "\nUser running the script: ${USER}"

echo -e "\nCleanup old files..."
rm -rf node_modules/@scicatproject/scicat-sdk-ts-angular
rm -rf @scicatproject/scicat-sdk-ts-angular
rm local-api-for-generator.json

echo -e "\nFetching the Swagger API from the back end..."
curl ${SWAGGER_API_URL} > local-api-for-generator.json

echo -e "\nGenerating the new sdk..."

Expand All @@ -17,14 +43,25 @@ echo -e "\nGenerating the new sdk..."
##
docker run \
--rm \
--add-host host.docker.internal:host-gateway \
-v "`pwd`:/local" \
openapitools/openapi-generator-cli:v7.13.0 generate \
-i http://host.docker.internal:3000/explorer-json \
openapitools/openapi-generator-cli:v7.14.0 generate \
-i /local/local-api-for-generator.json \
-g typescript-angular \
-o local/@scicatproject/scicat-sdk-ts-angular \
--additional-properties=ngVersion=19.0.0,npmName=@scicatproject/scicat-sdk-ts-angular,supportsES6=true,withInterfaces=true --skip-validate-spec

rm local-api-for-generator.json

# Check if the docker command resulted in any output.
# If we don't do this, we'll try to cd into a missing folder,
# and then we'd be invoking 'npm run build' as root in the main project folder,
# which would create a bunch of stuff in ./dist belonging to root,
# causing problems for things like SciCat Live.
if [ ! -d "@scicatproject/scicat-sdk-ts-angular" ]; then
echo "Error: OpenApi output not found."
exit 1
fi

REMOVE_NPM_LINK=0
if ! command -v npm 2>&1 1>/dev/null
then
Expand All @@ -44,19 +81,26 @@ fi

echo -e "\nInstalling dependencies and building the sdk..."
cd @scicatproject/scicat-sdk-ts-angular
# If this fails mysteriously you may need to run 'npm cache verify'.
npm install
npm run build

echo -e "\nCopying the build files in node_modules..."
cd ../..
cp -rv @scicatproject/scicat-sdk-ts-angular/dist node_modules/@scicatproject/scicat-sdk-ts-angular

if [ ! -d "@scicatproject/scicat-sdk-ts-angular/dist" ]; then
echo "Error: Build ouput not found."
exit 1
fi

echo -e "\nCopying the build files into node_modules..."
mkdir -p node_modules/@scicatproject/scicat-sdk-ts-angular
cp -rv @scicatproject/scicat-sdk-ts-angular/dist/ node_modules/@scicatproject/scicat-sdk-ts-angular/

echo -e "\nAdjusting ownership to user ${USER}"
chown -Rv ${USER} node_modules/@scicatproject/scicat-sdk-ts-angular

echo -e "\nFinal cleanup..."
echo -e "Removing sdk folder"
rm -rfv @scicatproject
rm -rf @scicatproject

if [ $REMOVE_NPM_LINK -eq 1 ];
then
Expand All @@ -65,3 +109,4 @@ then
rm -fv "/usr/local/bin/node"
fi

echo -e "\nDone."
Loading