0
0
mirror of https://github.com/tj/n.git synced 2024-11-21 18:48:57 +01:00

Add auto label to read version from .node-version (#616)

* Add support for auto, reading version from .node-version

* Make version unique in tests

* Add support for auto doing a lookup for unresolved version

* Make auto description more consistent in style with other labels.
This commit is contained in:
John Gee 2020-04-11 14:10:29 +12:00 committed by GitHub
parent 3a1e96a4c0
commit 59adcab8b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 8 deletions

View File

@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased] (date goes here)
## [6.5.0] (2020-04-11)
### Added
- specify `auto` to read the target version from a ` .node-version` file
## [6.4.0] (2020-03-10)
### Added

View File

@ -104,6 +104,10 @@ There are labels for two especially useful versions:
- `lts`: newest Long Term Support official release
- `latest`, `current`: newest official release
There is a label to read the target version from a file, on the first line:
- `auto`: read version from `.node-version` file
There is support for release streams:

34
bin/n
View File

@ -40,7 +40,7 @@ function echo_red() {
# Setup and state
#
VERSION="6.4.1-0"
VERSION="6.5.0"
N_PREFIX="${N_PREFIX-/usr/local}"
N_PREFIX=${N_PREFIX%/}
@ -334,6 +334,7 @@ Versions:
4.9.1, 8, v6.1 Numeric versions
lts Newest Long Term Support official release
latest, current Newest official release
auto Read version from .node-version
boron, carbon Codenames for release streams
and nightly, chakracore-release/latest, rc/10 et al
@ -890,19 +891,37 @@ function tarball_url() {
echo "${g_mirror_url}/v${version}/node-v${version}-$(display_tarball_platform).tar.${ext}"
}
#
# Synopsis: display_auto_version
#
function display_auto_version() {
local filename=".node-version"
[[ -e "${filename}" ]] || abort "auto version file not found (${filename})"
# read returns a non-zero status but does still work if there is no line ending
<"${filename}" read -r version
# trim possible trailing \d from a Windows created file
version="${version%%[[:space:]]}"
echo "${version}"
}
#
# Synopsis: display_latest_resolved_version version
#
function display_latest_resolved_version() {
local version=${1#node/}
if is_exact_numeric_version "${version}"; then
local version=${1}
if [[ "${version}" = "auto" ]]; then
version="$(display_auto_version)" || return 2
fi
simple_version=${version#node/} # Only place supporting node/ [sic]
if is_exact_numeric_version "${simple_version}"; then
# Just numbers, already resolved, no need to lookup first.
version="${version#v}"
echo "${version}"
simple_version="${simple_version#v}"
echo "${simple_version}"
else
# Complicated recognising exact version, KISS and awlays lookup for now.
N_MAX_REMOTE_MATCHES=1 display_remote_versions "$1"
# Complicated recognising exact version, KISS and lookup.
N_MAX_REMOTE_MATCHES=1 display_remote_versions "$version"
fi
}
@ -1295,6 +1314,7 @@ else
lsr|ls-remote|list-remote) shift; display_remote_versions "$1"; exit ;;
uninstall) uninstall_installed; exit ;;
i|install) shift; install "$1"; exit ;;
N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION) shift; display_latest_resolved_version "$1"; exit ;;
*) install "$1"; exit ;;
esac
shift

View File

@ -5,7 +5,8 @@ load shared-functions
function setup() {
unset_n_env
# fixed directory so can reuse the two installs
export N_PREFIX="${TMPDIR}/n/test/run-which"
tmpdir="${TMPDIR:-/tmp}"
export N_PREFIX="${tmpdir}/n/test/run-which"
# beforeAll
# See https://github.com/bats-core/bats-core/issues/39
if [[ "${BATS_TEST_NUMBER}" -eq 1 ]] ; then

View File

@ -0,0 +1,69 @@
#!/usr/bin/env bats
# Note: full semver is resolved without lookup, so can use arbitrary versions for testing like 999.999.999
load shared-functions
function setup() {
unset_n_env
tmpdir="${TMPDIR:-/tmp}"
export MY_DIR="${tmpdir}/n/test/run-version-resolve"
mkdir -p "${MY_DIR}"
}
@test "auto, missing file" {
cd "${MY_DIR}"
rm -f .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -ne 0 ]
}
@test "auto, no eol" {
cd "${MY_DIR}"
printf "101.0.1" > .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -eq 0 ]
[ "$output" = "101.0.1" ]
}
@test "auto, unix eol" {
cd "${MY_DIR}"
printf "101.0.2\n" > .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -eq 0 ]
[ "$output" = "101.0.2" ]
}
@test "auto, Windows eol" {
cd "${MY_DIR}"
printf "101.0.3\r\n" > .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -eq 0 ]
[ "$output" = "101.0.3" ]
}
@test "auto, leading v" {
cd "${MY_DIR}"
printf "v101.0.4\n" > .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -eq 0 ]
[ "$output" = "101.0.4" ]
}
@test "auto, first line only" {
cd "${MY_DIR}"
printf "101.0.5\nmore text\n" > .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -eq 0 ]
[ "$output" = "101.0.5" ]
}
@test "auto, lookup" {
# Check normal resolving, which is allowed but not required for MVP .node-version
cd "${MY_DIR}"
printf "4.9\n" > .node-version
run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto
[ "$status" -eq 0 ]
[ "$output" = "4.9.1" ]
}