Skip to content

Commit

Permalink
fix(open-api-gateway): pkg manager support in scripts and debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
sperka authored and cogwirrel committed Feb 14, 2023
1 parent f408985 commit 8f773c1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
72 changes: 36 additions & 36 deletions packages/open-api-gateway/scripts/common/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,61 @@

set -e

##
# log debug messages if OPEN_API_GATEWAY_DEBUG env var set to 1
# this is a helper function for debugging our scripts
log() {
if [ "$OPEN_API_GATEWAY_DEBUG" == 1 ]; then
echo "$@"
fi
}

# Determine which package manager we're using
# Preference: pnpm > yarn > npm
# Use if available, otherwise fall back to npm so as not to require it as a system-wide dependency
# Use pnpm if available, otherwise fall back to npm (it's present if you have node)

# NOTES: removed `yarn` option: yarn v2+ has no backwards compatibility and will
# fail to run some of the commands because peer dependencies not found;
# yarn v1 has no significant performance difference from npm/npx
pkg_manager=pnpm
pkg_install_cmd=install
if ! $pkg_manager -v &> /dev/null; then
pkg_manager=yarn
pkg_install_cmd=add
if ! $pkg_manager -v &> /dev/null; then
pkg_manager=npm
pkg_install_cmd=install
fi
pkg_manager=npm
fi

# yarn classic vs v2+ have different behavior
# check if it's the classic or later
if [ "$pkg_manager" == "yarn" ]; then
yarn_version=$(yarn --version)
yarn_major_version=${yarn_version:0:1}
fi

##
# workaround yarn v2+ needs yarn.lock present for the "add" command
__prepare_tmpdir() {
if [ "$pkg_manager" == "yarn" ]; then
if [ "$yarn_major_version" != "1" ]; then
touch yarn.lock
fi
fi
}
log "package manager :: $pkg_manager"

##
# installs the passed packages with the package manager in use
install_packages() {
__prepare_tmpdir
# yarn2's --silent switch doesn't work, hence >/dev/null
$pkg_manager $pkg_install_cmd --silent "$@" >/dev/null 2>&1
log "installing packages :: $@"

if [ "$pkg_manager" == "pnpm" ]; then
reporter=silent
if [ "$OPEN_API_GATEWAY_DEBUG" == 1 ]; then
reporter=default
fi
$pkg_manager install --reporter=$reporter "$@"
else
silent_switch="--silent"
if [ "$OPEN_API_GATEWAY_DEBUG" == 1 ]; then
silent_switch=""
fi
$pkg_manager install $silent_switch "$@"
fi
}

##
# runs the passed command with the package manager's proper syntax
run_command() {
cmd="$@"

if [ "$pkg_manager" == "yarn" ]; then
if [ "$yarn_major_version" == "1" ]; then
runner="yarn run"
else
runner="yarn dlx"
fi
elif [ "$pkg_manager" == "pnpm" ]; then
runner="pnpx"
if [ "$pkg_manager" == "pnpm" ]; then
# pnpx cli was deprecated in v6 --> use pnpm dlx
runner="pnpm dlx"
else
runner="npx"
fi

log "running command $runner $cmd"

$runner $cmd >/dev/null 2>&1
}
2 changes: 2 additions & 0 deletions packages/open-api-gateway/scripts/custom/docs/html-redoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ working_dir=$(pwd)
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp/}generate-docs-html-redoc.XXXXXXXXX")
cd $tmp_dir

log "html-redoc :: tmp_dir :: $tmp_dir"

# Install dependencies
install_packages redoc-cli@0.13.20

Expand Down
4 changes: 3 additions & 1 deletion packages/open-api-gateway/scripts/generators/generate
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ script_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null &&
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp/}generate-client-$generator_dir.XXXXXXXXX")
cd $tmp_dir

log "generate :: tmp_dir :: $tmp_dir"

# Copy the specific generator directory into the temp directory
cp -r $script_dir/$generator_dir/* .

Expand All @@ -41,7 +43,7 @@ install_packages @openapitools/openapi-generator-cli@2.5.1
sed 's|{{src}}|'"$src_dir"'|g' config.yaml > config.final.yaml

# Generate the client
run_command openapi-generator-cli generate \
run_command @openapitools/openapi-generator-cli generate \
--log-to-stderr \
--generator-name $generator \
--skip-operation-example \
Expand Down
2 changes: 2 additions & 0 deletions packages/open-api-gateway/scripts/parser/parse-openapi-spec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ script_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null &&
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp/}parse-openapi-spec.XXXXXXXXX")
cd $tmp_dir

log "parse-openapi-spec :: tmp_dir :: $tmp_dir"

# Copy the parse script into the temp directory
cp -r $script_dir/* .

Expand Down

0 comments on commit 8f773c1

Please sign in to comment.