diff --git a/.gitignore b/.gitignore
index f5c42c0..9b407c3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
### Project ###
-config.conf
+versions.txt
### Linux ###
*~
diff --git a/README.md b/README.md
index 191202a..7b785c1 100644
--- a/README.md
+++ b/README.md
@@ -6,16 +6,16 @@
-
_final-REMASTERED
+ what-node-version
- Script for finding the final name
+ Script for automatically switch node versions based on the configured engine the the package.json
·
- Report Bug
+ Report Bug
·
- Request Feature
+ Request Feature
·
@@ -24,36 +24,48 @@
## About The Project
-Helps you find the perfect final name for your file
+It tries to parse the specified engine in `package.json`. It used [nvm](https://github.com/nvm-sh/nvm) to switch to the determined version.
## Usage
-```sh
-_final HelloWorld.js
+```bash
+wnv run start:dev
+```
+
+or evan better: with an alias
+
+```bash
+echo 'alias npm="wnv"' >> ~/.bashrc
+npm run start:dev
```
## Installation
-```sh
-brew install beuluis/homebrew-tap/_final
+```bash
+brew install beuluis/homebrew-tap/wnv
```
or without brew
```bash
-# installs to /usr/local/bin/_final
-curl -L https://raw.githubusercontent.com/beuluis/_final-REMASTERED/master/_final >/usr/local/bin/_final
-chmod 0755 /usr/local/bin/_final
+# installs to /usr/local/bin/wnv
+curl -L https://raw.githubusercontent.com/beuluis/what-node-version/master/wnv >/usr/local/bin/wnv
+chmod 0755 /usr/local/bin/wnv
```
## Parameters
-| Parameter | Description | type |
-| ------------------ | ----------------------------------- | --------- |
-| `-h --help` | Show this usage summary and exit | - |
-| `-e` | Only echos back the generated names | - |
-| `--use_separators` | Use separators (e.g. . , - \_) | `boolean` |
-| `--modifiers_max` | Maximum modifiers to be generated | `number` |
+| Parameter | Description |
+| ----------- | ------------------------------- |
+| `-h --help` | Show the usage summary and exit |
+
+### Force reload of node versions cache
+
+Run command with the `NO_CACHE` environment variable set
+
+```bash
+NO_CACHE=1 wnv run some_command
+```
@@ -67,12 +79,6 @@ Contributions are what make the open source community such an amazing place to l
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
-### Add more modifiers and separators
-
-Add them at the top of the script. Each line is a modifiers/separators.
-
-Modifiers and separators may only contain characters that are allowed in filenames.
-
## Contact
@@ -82,12 +88,12 @@ Luis Beu - me@luisbeu.de
-[contributors-shield]: https://img.shields.io/github/contributors/beuluis/_final-REMASTERED.svg?style=flat-square
-[contributors-url]: https://github.com/beuluis/_final-REMASTERED/graphs/contributors
-[forks-shield]: https://img.shields.io/github/forks/beuluis/_final-REMASTERED.svg?style=flat-square
-[forks-url]: https://github.com/beuluis/_final-REMASTERED/network/members
-[stars-shield]: https://img.shields.io/github/stars/beuluis/_final-REMASTERED.svg?style=flat-square
-[stars-url]: https://github.com/beuluis/_final-REMASTERED/stargazers
-[issues-shield]: https://img.shields.io/github/issues/beuluis/_final-REMASTERED.svg?style=flat-square
-[issues-url]: https://github.com/beuluis/_final-REMASTERED/issues
-[license-shield]: https://img.shields.io/github/license/beuluis/_final-REMASTERED.svg?style=flat-square
+[contributors-shield]: https://img.shields.io/github/contributors/beuluis/what-node-version.svg?style=flat-square
+[contributors-url]: https://github.com/beuluis/what-node-version/graphs/contributors
+[forks-shield]: https://img.shields.io/github/forks/beuluis/what-node-version.svg?style=flat-square
+[forks-url]: https://github.com/beuluis/what-node-version/network/members
+[stars-shield]: https://img.shields.io/github/stars/beuluis/what-node-version.svg?style=flat-square
+[stars-url]: https://github.com/beuluis/what-node-version/stargazers
+[issues-shield]: https://img.shields.io/github/issues/beuluis/what-node-version.svg?style=flat-square
+[issues-url]: https://github.com/beuluis/what-node-version/issues
+[license-shield]: https://img.shields.io/github/license/beuluis/what-node-version.svg?style=flat-square
diff --git a/_final b/_final
deleted file mode 100644
index ae62d8d..0000000
--- a/_final
+++ /dev/null
@@ -1,163 +0,0 @@
-#!/bin/bash
-
-########### _final-REMASTERED #################
-#
-# Script for finding the final name
-# Author: Luis Beu
-# Modifiers and separators: Luis Beu, Anne Engnath
-#
-####################################################
-
-# all possible modifiers
-modifiers=(
- final
- 2.1
- 2.0
- forSure
- 1.1
- remastered
- test
- new
- NoKidding
- HereWeGo
- thisOne
- thiswilldo
- updated
- 100%
- real
- actually
- this
- fix
-)
-
-# all possible separators
-separators=(
- _
- -
- .
- __
- ___
-)
-
-# print usage page
-usage() {
- cat < Use separators (e.g. . , - _)
- --modifiers_max= Maximum modifiers to be generated
-EOF
- exit 0
-}
-
-if [ $# -eq 0 ]; then
- usage
-fi
-
-# set default values
-echo_mode=false
-use_separators=true
-modifiers_max=5
-
-# loop over flags
-while test $# -gt 0; do
- case "$1" in
- -h | --help)
- usage
- ;;
- -e)
- echo_mode=true
- shift
- ;;
- --use_separators*)
- use_separators=$(echo "$1" | sed -e 's/^[^=]*=//g')
- # test if empty or not boolean
- if [ "$use_separators" != false ] && [ "$use_separators" != true ]; then
- echo "use_separators to be a boolean"
- exit 128
- fi
- shift
- ;;
- --modifiers_max*)
- modifiers_max=$(echo "$1" | sed -e 's/^[^=]*=//g')
- # check if empty or not number
- if [ -z "$modifiers_max" ] || ((modifiers_max <= 0)); then
- echo "modifiers_max needs to be a number greater then 0"
- exit 128
- fi
- shift
- ;;
- *)
- break
- ;;
- esac
-done
-
-# loop over params
-for fullpath in "$@"; do
- final_modifier=""
-
- # seperate file into path, name and extension
- filename="${fullpath##*/}"
- dir="${fullpath:0:${#fullpath}-${#filename}}"
- base="${filename%.[^.]*}"
- ext="${filename:${#base}+1}"
-
- # catch no extension and .name
- if [[ -z "${base}" && -n "$ext" ]]; then
- base=".$ext"
- ext=""
- elif [[ -n "$ext" ]]; then
- ext=".$ext"
- fi
-
- # check if directory exists
- if ! $echo_mode && [ -n "$dir" ] && ! [ -d "$dir" ]; then
- echo "$dir: No such file or directory"
- exit 128
- fi
-
- # get random amout of modifiers
- for ((n = 0; n < $((RANDOM % (modifiers_max - 1 + 1) + 1)); n++)); do
- # check if seperators should be generated
- if "$use_separators"; then
- separator=${separators[$RANDOM % ${#separators[@]}]}
- final_modifier="$final_modifier$separator"
- fi
-
- # generate random based on modifiers lenght
- rand=$((RANDOM % ${#modifiers[@]}))
-
- # perform random uppercase modification
- if (((RANDOM % 10 % 5) == 0)); then
- # pick random modifier and transform to uppercase
- modifier=$(echo "${modifiers[$rand]}" | tr '[:lower:]' '[:upper:]')
- else
- # pick random modifier
- modifier=${modifiers[$rand]}
- fi
-
- # stitch it together
- final_modifier="$final_modifier$modifier"
- done
-
- # stitch everything together
- generated="$dir$base$final_modifier$ext"
- if $echo_mode; then
- # only echo in echo mode
- echo "$generated"
- else
- # for everything else touch the file
- touch "$generated"
- fi
- # catch error code produced by echo or touch
- retval=$?
- if [ $retval -ne 0 ]; then
- # print error with code
- echo "exited with error code $retval"
- fi
-done
-
-# exit script
-exit 0
diff --git a/wnv b/wnv
new file mode 100755
index 0000000..371a5d8
--- /dev/null
+++ b/wnv
@@ -0,0 +1,157 @@
+#!/bin/bash
+
+########### what-node-version #################
+#
+# Script for automatically switch node versions based on the configured engine the the package.json
+# Author: Luis Beu
+#
+####################################################
+
+# print usage page
+usage() {
+ cat </dev/null && pwd)
+version_dir="$script_dir/versions.txt"
+engines=$(jq '.engines.node' -r package.json)
+
+# no configuration found dont switch version
+if [ ! -n "$engines" ]; then
+ npm $@
+ exit 0
+fi
+
+# get engine config from package.json
+node_version=($(grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' <<<"$engines"))
+
+# read versions new if file is older then 24h or no_cache is set
+if [[ ! -z "${NO_CACHE}" ]] || test $(find $version_dir -mmin +1440); then
+ # curl version list from nodejs dist index
+ version_list=($(curl --silent https://nodejs.org/dist/ | grep -o 'href=".*">' | sed 's/href="//;s/v//;s/\///;s/">//'))
+
+ for index in "${!version_list[@]}"; do
+ # test if it is version
+ if [[ "${version_list[index]}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
+ # split version into subversions
+ read -a split <<<$(
+ IFS="."
+ echo ${version_list[index]}
+ )
+ # add padding for right sorting
+ for i in "${!split[@]}"; do
+ split[$i]=$((${split[$i]} + $padding))
+ done
+
+ # put array back together
+ version_list[$index]=$(printf "%s." "${split[@]}" | cut -d "." -f 1-${#split[@]})
+ else
+ # unset all that is not considered a version
+ unset -v 'version_list[$index]'
+ fi
+ done
+
+ # sort array
+ version_list=("${version_list[@]}")
+ IFS=$'\n' version_list=($(sort <<<"${version_list[*]}"))
+ unset IFS
+
+ for index in "${!version_list[@]}"; do
+ # split version into subversions
+ read -a split <<<$(
+ IFS="."
+ echo ${version_list[index]}
+ )
+ # remove padding
+ for i in "${!split[@]}"; do
+ split[$i]=$((${split[$i]} - $padding))
+ done
+ # put array back together
+ version_list[$index]=$(printf "%s." "${split[@]}" | cut -d "." -f 1-${#split[@]})
+ done
+
+ # write to file to be a little faster
+ printf "%s\n" "${version_list[@]}" >$version_dir
+else
+ # usign cached version
+ version_list=($(cat $version_dir | tr '\n' ' '))
+fi
+
+echo $version_list
+
+# check if specified version is a legit version
+if [[ ! " ${version_list[*]} " =~ " ${node_version} " ]]; then
+ echo "Unkonw node version specefied in package.json"
+ exit 1
+fi
+
+# check what to do and which version to use/install
+case $engines in
+
+*">"*)
+ node_versio_majorn=($(grep -Eo '^\d+' <<<"$node_version"))
+ nvm install $node_versio_majorn
+ npm $@
+ ;;
+
+*"<"*)
+ if [[ $string == *"="* ]]; then
+ nvm install $node_version
+ npm $@
+ exit 0
+ fi
+ # get index from version
+ for index in "${!version_list[@]}"; do
+ [[ "${version_list[$index]}" = "${node_version}" ]] && break
+ done
+
+ # use the version previous to specified
+ nvm install ${version_list[$index - 1]}
+ npm $@
+ ;;
+
+*"="*)
+ nvm install $node_version
+ npm $@
+ ;;
+
+*)
+ nvm install $node_version
+ npm $@
+ ;;
+esac
+
+# exit script successful
+exit 0