diff --git a/tools/cli/bin/modus b/tools/cli/bin/modus new file mode 100755 index 00000000..189bcb8f --- /dev/null +++ b/tools/cli/bin/modus @@ -0,0 +1,94 @@ +#!/usr/bin/env bash + +get_script_dir() { + SOURCE="${BASH_SOURCE[0]}" + DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)" + echo "$DIR" +} + +# Config +NODE_WANTED=20.18.0 +NODE_REQUIRED=18 +NODE_PATH="$(which node)" +NODE_INSTALL_PATH="node-bin" + +# Properties +ARCH="$(uname -m)" +OS="$(uname -s | tr '[:upper:]' '[:lower:]')" +DIR=$(get_script_dir) + +PATH_FILE="$DIR/scripts/modus-path" +LOCAL_FILE="$DIR/scripts/modus-local" + +case $ARCH in +aarch64) ARCH="arm64" ;; +x86_64) ARCH="x64" ;; +armv6l) ARCH="arm" ;; +*) ;; +esac + +echo_fexists() { + [ -f "$1" ] && echo "$1" +} + +install_node() { + mkdir -p "$DIR/$NODE_INSTALL_PATH" + + download_file="node-v$NODE_WANTED-$OS-$ARCH.tar.gz" + download_url="https://nodejs.org/dist/v$NODE_WANTED/$download_file" + temp_dir="$DIR/.temp" + + rm -rf $temp_dir + mkdir $temp_dir + curl --silent --show-error --location --fail "$download_url" \ + --output "$temp_dir/$download_file" + + tar -xf "$temp_dir/$download_file" -C "$temp_dir" + rm -rf $download_file + rm -rf "$DIR/$NODE_INSTALL_PATH" + mkdir "$DIR/$NODE_INSTALL_PATH" + mv "$temp_dir/node-v$NODE_WANTED-$OS-$ARCH/"* "$DIR/$NODE_INSTALL_PATH/" + chmod +x "$DIR/$NODE_INSTALL_PATH/bin/node" + chmod +x "$DIR/$NODE_INSTALL_PATH/bin/npm" + rm -rf $temp_dir + NODE_PATH="$DIR/$NODE_INSTALL_PATH/bin/node" +} + +check_node() { + if ! echo_fexists "./$NODE_INSTALL_PATH/bin/node"; then + NODE_VERSION=$($NODE_PATH -v 2>/dev/null) + + if [ $? -ne 0 ]; then + install_node + fi + + NODE_VERSION=${NODE_VERSION#v} + MAJOR_VERSION=${NODE_VERSION%%.*} + + if [ "$MAJOR_VERSION" -le $NODE_REQUIRED ]; then + install_node + fi + fi +} + +if ! echo_fexists $LOCAL_FILE; then + mkdir -p "$DIR/scripts/" + echo "#!$DIR/node-bin/bin/node +import { execute } from \"@oclif/core\"; +await execute({ dir: import.meta.url });" >$LOCAL_FILE + chmod +x $LOCAL_FILE +else + $LOCAL_FILE + exit 0 +fi + +if ! echo_fexists $PATH_FILE; then + mkdir -p "$DIR/scripts/" + echo "#!/usr/bin/env node +import { execute } from \"@oclif/core\"; +await execute({ dir: import.meta.url });" >$PATH_FILE + chmod +x $PATH_FILE +else + check_node + $PATH_FILE +fi diff --git a/tools/cli/bin/run.cmd b/tools/cli/bin/modus.cmd similarity index 96% rename from tools/cli/bin/run.cmd rename to tools/cli/bin/modus.cmd index 968fc307..8c083b0b 100644 --- a/tools/cli/bin/run.cmd +++ b/tools/cli/bin/modus.cmd @@ -1,3 +1,2 @@ @echo off - node "%~dp0\run" %* diff --git a/tools/cli/bin/run.js b/tools/cli/bin/run.js deleted file mode 100755 index 94a0d6f8..00000000 --- a/tools/cli/bin/run.js +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env node - -import { execute } from "@oclif/core"; - -await execute({ dir: import.meta.url }); diff --git a/tools/cli/install.sh b/tools/cli/install.sh index 66ef4698..f553d2a8 100755 --- a/tools/cli/install.sh +++ b/tools/cli/install.sh @@ -90,7 +90,7 @@ build_path_str() { if [[ $profile == *.fish ]]; then echo -e "set -gx MODUS_CLI \"$install_dir\"\nstring match -r \".modus\" \"\$PATH\" > /dev/null; or set -gx PATH \"\$MODUS_CLI/bin\" \$PATH" else - echo -e "# Modus CLI\nexport MODUS_CLI=\"$install_dir\"\nexport PATH=\"\$MODUS_CLI/bin:\$PATH\"" + echo -e "\n# Modus CLI\nexport MODUS_CLI=\"$install_dir\"\nexport PATH=\"\$MODUS_CLI/bin:\$PATH\"" fi } @@ -183,7 +183,7 @@ install_from_file() { rm -f "$archive" mv "$extract_to/modus/"* "$INSTALL_DIR" rm -rf "$extract_to" - + clear_line echo "[2/5] Unpacked archive" } @@ -210,10 +210,10 @@ restart_shell() { echo -e "[5/5] Restarted shell ${DIM}($shell_name)${RESET}\n\nThe Modus CLI has been installed! 🎉\nRun ${DIM}modus${RESET} to get started" case "$shell_name" in - bash|zsh|fish) exec "$shell_name" ;; - *) - echo -e "[5/5] Clean up\n\nPlease restart your shell for changes to take effect" - ;; + bash | zsh | fish) exec "$shell_name" ;; + *) + echo -e "[5/5] Clean up\n\nPlease restart your shell for changes to take effect" + ;; esac }