diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index adb6a48..944a717 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,12 +15,8 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: 1 - - name: Get DN Version - run: | - source dn.sh - echo "DN_VERSION=$DN_SCRIPT_VERSION" >> $GITHUB_ENV - + run: echo "DN_VERSION=$(. dn.sh -v)" >> $GITHUB_ENV - name: Create Release id: create_release uses: actions/create-release@v1 diff --git a/dn.sh b/dn.sh index 47c8fa4..e72f8fd 100755 --- a/dn.sh +++ b/dn.sh @@ -5,27 +5,46 @@ set -e ## useful resource: https://hackernoon.com/inspecting-docker-images-without-pulling-them-4de53d34a604 # "inspecting docker image without pulling" -DN_SCRIPT_VERSION=0.1.0 -DN_INSTALL_NODE_VERSION=14 +DN_VERSION=0.2.0 +DN_REPO_URL='https://github.com/sfertman/dn' + DN_PREFIX=/usr/local +DN_DEFAULT_NODE_VERSION=14 +DN_DISABLED=false + +DN_CONFIG_DIR=${HOME}/.dn +config_file="${DN_CONFIG_DIR}/config.env" + +if [ -f "${config_file}" ]; then + source "${config_file}"; +fi + DN_INSTALL_DIR=${DN_PREFIX}/lib/dn DN_BIN_DIR=${DN_PREFIX}/bin -DN_REPO_URL='https://github.com/sfertman/dn' OS_TYPE=$(uname) -dn_version() { - local dn_file="${DN_INSTALL_DIR}/dn.sh"; - if [ -f "${dn_file}" ]; then - grep '^DN_SCRIPT_VERSION=' "${dn_file}" \ - | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+'; - else - echo 'N/A' - fi -} - echoerr() { echo "$@" 1>&2; } ERRNIMPL() { echoerr "ERROR: Not implemented! 🤖"; } +delete_disabled_config() { + case "${OS_TYPE}" in + Linux) sed -i '/DN_DISABLED/d' "${config_file}" ;; + Darwin) sed -i '' '/DN_DISABLED/d' "${config_file}" ;; + *) echoerr "Shell ${OS_TYPE} is not supported!"; return 1 ;; + esac +} + +dn_on() { + delete_disabled_config; + echo "DN is enabled."; +} + +dn_off() { + delete_disabled_config; # to delete whatever is there right now + echo "DN_DISABLED=true" >> ${config_file}; + echo "DN has been disabled. Run 'dn on' to enable." +} + dn_install() { ### TODO: consider adding an install option for alias prefix and/or a switch to enable/disable aliasing echo "Installing DN..."; @@ -37,13 +56,13 @@ dn_install() { script_path="${0}" local rc_file="${HOME}/.zshrc" echo "###GENERATED BY DN.SH--DO NOT MODIFY" >> ${rc_file} - echo "source ${DN_INSTALL_DIR}/aliases.sh" >> ${rc_file} + echo "source ${DN_CONFIG_DIR}/alias.sh" >> ${rc_file} ;; */bash|*/sh) script_path="$(realpath "${0}")" local rc_file="${HOME}/.bashrc" echo "###GENERATED BY DN.SH--DO NOT MODIFY" >> ${rc_file} - echo "source ${DN_INSTALL_DIR}/aliases.sh" >> ${rc_file} + echo "source ${DN_CONFIG_DIR}/alias.sh" >> ${rc_file} ;; *) echoerr "Shell ${SHELL} is not supported! Installation FAILED 😱" @@ -64,16 +83,31 @@ dn_install() { # link executable into bin dir so that we can invoke as a command ln -sf ${DN_INSTALL_DIR}/dn.sh ${DN_BIN_DIR}/dn; # add and switch to default version - dn_add_and_switch -g $DN_INSTALL_NODE_VERSION - - cat < "${DN_INSTALL_DIR}/aliases.sh" -alias ${prefix}node='dn run node' -alias ${prefix}npm='dn run npm' -alias ${prefix}npx='dn run npx' -alias ${prefix}yarn='dn run yarn' -alias ${prefix}yarnpkg='dn run yarnpkg' + + dn_add_and_switch -g $DN_DEFAULT_NODE_VERSION + + cat > "${DN_CONFIG_DIR}/alias.sh" < "${DN_CONFIG_DIR}/unalias.sh" <> "${config_file}"; + echo "DN_DEFAULT_NODE_VERSION=${DN_DEFAULT_NODE_VERSION}" >> "${config_file}"; + echo "DN_DISABLED=${DN_DISABLED}" >> "${config_file}"; + fi + echo echo "DN has been successfully INSTALLED. 🎉" dn_info @@ -92,8 +126,9 @@ dn_uninstall() { echo "Uninstalling DN..."; echo ""; - rm -rf ${DN_INSTALL_DIR}; + # dn_off; rm -f ${DN_BIN_DIR}/dn; + rm -rf ${DN_INSTALL_DIR}; local rc_file; case "${SHELL}" in @@ -184,6 +219,12 @@ Uninstall one or more node versions from your system.";} fi } +validate_enabled() { + if [ $DN_DISABLED = 'true' ]; then + echo 'DN_DISABLED'; + fi +} + dn_run() { _help() { echoerr " Usage: dn run COMMAND [ARG...] @@ -203,25 +244,28 @@ Commands: if [[ "$#" -le 0 || "$1" = '-h' || "$1" = '--help' ]]; then _help; else - local node_version=$(get_active_version); - local node_major_version=$(echo "${node_version}" | grep --color=never -Eo ^[0-9]+) - local docker_vol="dnode_modules_${node_major_version}"; - docker volume create "${docker_vol}" > /dev/null; - local CMD=(docker run -it --rm); - ## TODO: figure out how to create a container once and then just keep running stuff in it - ## figure out if it ha any performance advantages - CMD=(${CMD[*]} -it); - CMD=(${CMD[*]} -w "/.dnode${PWD}"); - CMD=(${CMD[*]} -v "${PWD}:/.dnode${PWD}"); - CMD=(${CMD[*]} -v "${docker_vol}:/usr/local/lib/node_modules"); - CMD=(${CMD[*]} "node:${node_version}-alpine"); - if [ -z $(validate_command $1) ]; then - CMD=(${CMD[*]} $@); - else + if [ ! -z $(validate_command $1) ]; then echoerr "Unknown command $1"; return 1; + elif [ ! -z $(validate_enabled) ]; then + $@ + else + local node_version=$(get_active_version); + local node_major_version=$(echo "${node_version}" | grep --color=never -Eo ^[0-9]+) + local docker_vol="dnode_modules_${node_major_version}"; + docker volume create "${docker_vol}" > /dev/null; + local CMD=(docker run -it --rm); + ## TODO: figure out how to create a container once and then just keep running stuff in it + ## figure out if it ha any performance advantages + CMD=(${CMD[*]} -it); + CMD=(${CMD[*]} --network host); + CMD=(${CMD[*]} -w "/.dnode${PWD}"); + CMD=(${CMD[*]} -v "${PWD}:/.dnode${PWD}"); + CMD=(${CMD[*]} -v "${docker_vol}:/usr/local/lib/node_modules"); + CMD=(${CMD[*]} "node:${node_version}-alpine"); + CMD=(${CMD[*]} $@); + ${CMD[*]}; fi - ${CMD[*]}; fi } @@ -330,8 +374,8 @@ dn_info() { echoerr " DN: Docker Powered Node Version Manager 🐳 + ⬢ -Script version: $DN_SCRIPT_VERSION -Installed version: $(dn_version) +DN version: $DN_VERSION +Status: $([ -z $(validate_enabled) ] && echo ENABLED || echo DISABLED) Node version: $node_version ($node_version_type) Source: ${DN_REPO_URL}" } @@ -568,6 +612,8 @@ Commands: inspect Display detailed information about active node version install Install dn on your system ls List installed versions + off Disable dn + on Enable dn rm Delete a version search Search available versions show Show active Node version @@ -595,7 +641,7 @@ Run 'dn COMMAND --help' for more information on a command."; } if [ ! -z $is_help ]; then _help; elif [ ! -z $is_version ]; then - dn_version; + echo $DN_VERSION; else local args=("$@"); local rest_args=${args[*]:1}; @@ -613,6 +659,8 @@ Run 'dn COMMAND --help' for more information on a command."; } inspect) dn_inspect ;; install) dn_install ;; ls|ll|list) dn_ls ${rest_args[*]} ;; + off) dn_off ;; + on) dn_on ;; rm|remove) dn_rm ${rest_args[*]} ;; run) dn_run ${rest_args[*]} ;; search) dn_search ${rest_args[*]} ;;