commit d4f807fe918f9adc745373c5817ddd63ef82f102
parent a0edcab849ae52b77217516d370b6966e3964dbc
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Tue, 28 Nov 2023 12:18:37 +0000
add some basic error checking after all
Diffstat:
2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
@@ -21,7 +21,7 @@ Write a stupidly simple node version manager which is significantly simpler, hac
## Usage
-Use this software at your own risk. In particular, strongly consider reading the 20 lines of source code to understand what it does.
+Use this software at your own risk. In particular, strongly consider reading the 34 lines of source code to understand what it does.
Read the `nsnvm.sh` file, then execute it like:
diff --git a/nsnvm.sh b/nsnvm.sh
@@ -5,17 +5,31 @@ version="$1"
referent="node-v$version-linux-x64"
nodejs_url="https://nodejs.org/dist/v$version/$referent.tar.xz"
+error() {
+ echo "Error: $1" >&2
+ exit 1
+}
+
+if [ -z "$version" ]; then
+ error "No version number was provided"
+fi
+
mkdir -p "$dir"
cd "$dir"
rm -f "$dir/$referent.tar.xz"
rm -rf "$dir/$referent"
-wget "$nodejs_url"
-tar xf "$referent.tar.xz"
+
+wget "$nodejs_url" || error "Failed to download $nodejs_url"
+tar xf "$referent.tar.xz" || error "Failed to extract $referent.tar.xz"
sudo rm -f /usr/bin/node
sudo rm -f /usr/bin/npm
sudo rm -f /usr/bin/npx
-sudo ln -s "$dir/$referent/bin/node" "/usr/bin"
-sudo ln -s "$dir/$referent/bin/npm" "/usr/bin"
-sudo ln -s "$dir/$referent/bin/npx" "/usr/bin"
+sudo ln -s "$dir/$referent/bin/node" "/usr/bin" || error "Failed to link node binary"
+sudo ln -s "$dir/$referent/bin/npm" "/usr/bin" || error "Failed to link npm binary"
+sudo ln -s "$dir/$referent/bin/npx" "/usr/bin" || error "Failed to link npx binary"
+
+echo "Node.js $version has been installed."
+
+exit 0