Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions scripts/generate-nestjs-sdk.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
#
#

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
rm -rf @scicatproject/scicat-sdk-ts

echo -e "\nGenerating the new sdk..."
docker run \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Add error handling for Docker operations

Add checks to verify Docker is running and host.docker.internal is accessible before attempting operations. Handle potential errors explicitly.

--rm \
--add-host host.docker.internal:host-gateway \
-v "`pwd`:/local" \
openapitools/openapi-generator-cli:v7.9.0 generate \
-i http://host.docker.internal:3000/explorer-json \
-g typescript-angular \
-o local/@scicatproject/scicat-sdk-ts \
--additional-properties=ngVersion=16.2.12,npmName=@scicatproject/scicat-sdk-ts,supportsES6=true,npmVersion=10.8.2,withInterfaces=true

REMOVE_NPM_LINK=0
if ! command -v npm 2>&1 1>/dev/null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider using direct binary path variables instead of creating symlinks in system directories

The current approach of creating/removing symlinks in /usr/local/bin adds unnecessary complexity and risk. Instead, detect and use the correct binary paths directly:

# At the start of the script
if ! command -v npm >/dev/null 2>&1; then
    if [[ -n "${NVM_BIN}" ]]; then
        NPM_CMD="${NVM_BIN}/npm"
        NODE_CMD="${NVM_BIN}/node"
        if [[ ! -x "$NPM_CMD" ]] || [[ ! -x "$NODE_CMD" ]]; then
            echo "Required binaries not found or not executable in NVM_BIN" >&2
            exit 1
        fi
    else
        echo "No npm found and NVM_BIN not set" >&2
        exit 1
    fi
else
    NPM_CMD="npm"
    NODE_CMD="node"
fi

# Then use these variables instead of global npm/node commands
"$NPM_CMD" install
"$NPM_CMD" run build

This approach:

  • Removes the need for symlink creation/cleanup
  • Fails fast if requirements aren't met
  • Uses full paths directly when needed
  • Maintains the same functionality more safely

then
if [ "--`env | grep NVM_BIN`--" != "----" ]
then
echo -e "\nCreating links to npm and node versions"
ln -s "$NVM_BIN/npm" "/usr/local/bin/npm"
whereis npm
ln -s "$NVM_BIN/node" "/usr/local/bin/node"
whereis node
REMOVE_NPM_LINK=1
else
echo -e "\nNo npm found!!!"
exit 1
fi
fi

echo -e "\nInstalling dependencies and building the sdk..."
cd @scicatproject/scicat-sdk-ts
npm install
npm run build

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

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

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

if [ $REMOVE_NPM_LINK -eq 1 ];
then
echo -e "\nRemoving links to npm and node"
rm -fv "/usr/local/bin/npm"
rm -fv "/usr/local/bin/node"
fi

79 changes: 47 additions & 32 deletions scripts/generate-nestjs-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,50 @@ function getCurrentDirectory() {
return "$(pwd)";
}

// NOTE: First do some cleanup before starting the generation
console.log("Cleanup old files...");
execSync(
"rm -rf node_modules/@scicatproject/scicat-sdk-ts && rm -rf @scicatproject/scicat-sdk-ts",
{ encoding: "utf-8" },
);

console.log("Generating the new sdk...");
const generationOutput = execSync(
`docker run --rm --add-host host.docker.internal:host-gateway -v "${getCurrentDirectory()}:/local" openapitools/openapi-generator-cli:v7.9.0 generate -i http://host.docker.internal:3000/explorer-json -g typescript-angular -o local/@scicatproject/scicat-sdk-ts --additional-properties=ngVersion=16.2.12,npmName=@scicatproject/scicat-sdk-ts,supportsES6=true,npmVersion=10.8.2,withInterfaces=true`,
{ encoding: "utf-8" },
);
console.log(generationOutput);

console.log("Installing dependencies and building the sdk...");
const installBuildOutput = execSync(
"cd @scicatproject/scicat-sdk-ts && npm install && npm run build",
{ encoding: "utf-8" },
);
console.log(installBuildOutput);

console.log("Copying the build files in node_modules...");
const copyToNodeModulesOutput = execSync(
"cp -r @scicatproject/scicat-sdk-ts/dist node_modules/@scicatproject/scicat-sdk-ts",
{ encoding: "utf-8" },
);
console.log(copyToNodeModulesOutput);

console.log("Final cleanup...");
execSync("rm -rf @scicatproject", {
encoding: "utf-8",
});
if (isWindows()) {

// NOTE: First do some cleanup before starting the generation
console.log("Cleanup old files...");
execSync(
"rm -rf node_modules/@scicatproject/scicat-sdk-ts && rm -rf @scicatproject/scicat-sdk-ts",
{ encoding: "utf-8" },
);

console.log("Generating the new sdk...");
const generationOutput = execSync(
`docker run --rm --add-host host.docker.internal:host-gateway -v "${getCurrentDirectory()}:/local" openapitools/openapi-generator-cli:v7.9.0 generate -i http://host.docker.internal:3000/explorer-json -g typescript-angular -o local/@scicatproject/scicat-sdk-ts --additional-properties=ngVersion=16.2.12,npmName=@scicatproject/scicat-sdk-ts,supportsES6=true,npmVersion=10.8.2,withInterfaces=true`,
{ encoding: "utf-8" },
);
console.log(generationOutput);

console.log("Installing dependencies and building the sdk...");
const installBuildOutput = execSync(
"cd @scicatproject/scicat-sdk-ts && npm install && npm run build",
{ encoding: "utf-8" },
);
console.log(installBuildOutput);

console.log("Copying the build files in node_modules...");
const copyToNodeModulesOutput = execSync(
"cp -r @scicatproject/scicat-sdk-ts/dist node_modules/@scicatproject/scicat-sdk-ts",
{ encoding: "utf-8" },
);
console.log(copyToNodeModulesOutput);

console.log("Final cleanup...");
execSync("rm -rf @scicatproject", {
encoding: "utf-8",
});

console.log("Local SDK generation completed");

} else {
console.log("Your environment is a linux/unix");
console.log("Please run the following command on your terminal:");
console.log("> sudo -E ./scripts/generate-nestjs-sdk.bash");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nitrosx I am thinking instead of asking people to run the command, can't we just run the command from here directly. Something like:
execSync( "sudo -E ./scripts/generate-nestjs-sdk.bash", { encoding: "utf-8" }, );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it does not work in the current form of the bash script.
It does not pass the correct user and environment in the shell.
If we can find a way to do it from the javascript script, I fine with it

console.log("");
console.log("IMPORTANT: the script runs under sudo. You will be asked your password.");
console.log("");

}