]> git.wincent.com - wincent.git/commitdiff
refactor: extract common functions
authorGreg Hurrell <greg@hurrell.net>
Thu, 19 Mar 2020 20:27:46 +0000 (21:27 +0100)
committerGreg Hurrell <greg@hurrell.net>
Fri, 20 Mar 2020 09:53:24 +0000 (10:53 +0100)
bin/common [new file with mode: 0755]
bin/n
install-next

diff --git a/bin/common b/bin/common
new file mode 100755 (executable)
index 0000000..5316eb5
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+# https://stackoverflow.com/a/2684300
+if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
+  echo "[error] script \"$0\" is intended to be sourced, not executed directly"
+  exit 1
+fi
+
+log_debug() {
+  if [[ -n $DEBUG ]]; then
+    echo "[debug] $*" > /dev/stderr
+  fi
+}
+
+log_error() {
+  echo "[warn] $*" > /dev/stderr
+}
+
+log_info() {
+  echo "[info] $*" > /dev/stderr
+}
+
+log_warn() {
+  echo "[warn] $*" > /dev/stderr
+}
diff --git a/bin/n b/bin/n
index 2ad3b408719d58ecc434dd73452f8b972793a06b..1e08c0d91a7d2a132093fae0d37c0a9d3279b8f6 100755 (executable)
--- a/bin/n
+++ b/bin/n
@@ -3,22 +3,25 @@
 set -e
 
 REPO_ROOT="${BASH_SOURCE%/*}/.."
+BIN_DIR="$REPO_ROOT/bin"
 VENDOR_ROOT="$REPO_ROOT/vendor"
 N_EXE="$VENDOR_ROOT/n/bin/n"
 N_PREFIX="$VENDOR_ROOT/node"
 
 export N_PREFIX
 
+source "$BIN_DIR/common"
+
 if [[ ! -x $N_EXE ]]; then
-  echo "[abort] No executable at $N_EXE; did you forget to \`git submodule init\`?" /dev/stderr
+  log_error "No executable at $N_EXE; did you forget to \`git submodule init\`?"
   exit 1
 fi
 
 if ! "$N_EXE" which lts &> /dev/null; then
-  echo "[status] Installing Node LTS version" > /dev/stderr
+  log_info "Installing Installing Node LTS version"
   "$N_EXE" lts
 else
-  echo "[status] Using Node LTS installation" > /dev/stderr
+  log_debug "Using Node LTS installation"
 fi
 
 "$N_EXE" "$@"
index 6b676e2d7fc77b343504d97484c822b6a272d88e..892974da581f730a254bfffaf6ca4dca259ce258 100755 (executable)
@@ -8,14 +8,16 @@ NODE_EXE="$BIN_DIR/node"
 YARN_EXE="$BIN_DIR/yarn"
 MAIN_EXE="$REPO_ROOT/lib/main.js"
 
-echo "[status] Installing TypeScript"
+source "$BIN_DIR/common"
+
+log_info "Installing TypeScript"
 
 "$YARN_EXE" --frozen-lockfile --no-default-rc --no-progress --silent
 
-echo "[status] Building"
+log_info "Building"
 
 "$YARN_EXE" run --silent tsc
 
-echo "[status] Running main"
+log_info "Running main"
 
 "$NODE_EXE" "$MAIN_EXE" "$@"