get all node releases
[webi-installers/.git] / node / node.bash
1 #!/bin/bash
2
3 # title: Node.js
4 # homepage: https://nodejs.org
5 # tagline: JavaScript V8 runtime
6 # description: |
7 #   Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine
8 # examples: |
9 #   ```bash
10 #   node -e 'console.log("Hello, World!")'
11 #   > Hello, World!
12 #   ```
13 #   <br/>
14 #   <br/>
15 #   
16 #   <table>
17 #   <tr>
18 #   <td>Run a webserver</td>
19 #   <td><pre><code class="language-bash">
20 #   mkdir my-server
21 #   pushd my-server
22 #   npm init
23 #   npm install --save express</code></pre>
24 #   <br/>
25 #   <code>app.js:</code>
26 #   <br/>
27 #   <pre><code class="language-javascript">'use strict'
28 #   var express = require('express');
29 #   var app = express();
30 #   
31 #   app.use('/', function (req, res, next) {
32 #     res.end("Hello, World!");
33 #   });
34 #   
35 #   module.exports = app;</code></pre>
36 #   <br/>
37 #   <code>server.js:</code>
38 #   <br/>
39 #   <pre><code class="language-javascript">'use strict'
40 #   var http = require('http');
41 #   var app = require('./app.js');
42 #   http.createServer(app).listen(8080, function () {
43 #     console.log('Listening on', this.address());
44 #   });</code></pre>
45 #   <br/>
46 #   <pre><code class="language-bash">npm start</code></pre>
47 #   </td>
48 #   </tr>
49 #   </table>
50
51 set -e
52 set -u
53
54 NODEJS_VER=${WEBI_VERSION:-}
55 NODEJS_VER="${NODEJS_VER:-v}" # Search for 'v' at the least
56
57 # WEBI_ARCH uses only slightly different names from NODE_ARCH
58 NODE_OS="${WEBI_OS}" # linux or darwin
59 NODE_ARCH="${WEBI_ARCH}"
60 if [ "amd64" == "$NODE_ARCH" ]; then
61   NODE_ARCH="x64"
62 fi
63
64 my_tmp="$WEBI_TMP"
65 sudo_cmd="$WEBI_SUDO"
66
67 #########
68 # BEGIN #
69 #########
70
71 get_node_version() {
72   # sort -rV    # will sort by version number, but it appears these are already sorted
73   # tail -n +2  # starts at line two (1-indexed) and all after (omits the csv header with 'version' and such)
74   # cut -f 1    # gets only the first column
75   # head -n 1   # gets only the most recent version
76   my_char="."
77   my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${NODEJS_VER}")
78   # get the latest version if partial
79   if [ $my_count -ne 2 ]; then
80     if [ "$NODEJS_VER" != "v" ]; then
81       NODEJS_VER="$NODEJS_VER\\."
82     fi
83     if [ -n "$(type -p curl)" ]; then
84       NODEJS_VER=$(curl -fsL "https://nodejs.org/dist/index.tab" | tail -n +2 | cut -f 1 | grep "^$NODEJS_VER" | head -n 1) \
85         || echo 'error automatically determining current node.js version'
86     elif [ -n "$(type -p wget)" ]; then
87       NODEJS_VER=$(wget --quiet "https://nodejs.org/dist/index.tab" -O - | tail -n +2 | cut -f 1 | grep "^$NODEJS_VER" | head -n 1) \
88         || echo 'error automatically determining current node.js version'
89     else
90       echo "Found neither 'curl' nor 'wget'. Can't Continue."
91       exit 1
92     fi
93   fi
94 }
95
96 get_node_version
97
98 #
99 # node
100 #
101 node_install_path=$HOME/.local/opt/node-${NODEJS_VER}
102 mkdir -p $node_install_path
103
104 # TODO warn if existing node in path my take precedence
105 if [ -e "$node_install_path/bin/node" ]; then
106   # node of some version is already installed
107   if [ "${NODEJS_VER}" == "$($node_install_path/bin/node -v 2>/dev/null)" ]; then
108     echo node ${NODEJS_VER} already installed at $node_install_path
109     exit 0
110   fi
111 fi
112
113 NODEJS_REMOTE="https://nodejs.org/dist/${NODEJS_VER}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}.tar.gz"
114 NODEJS_LOCAL="$my_tmp/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}.tar.gz"
115 NODEJS_UNTAR="$my_tmp/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}"
116
117 echo "installing node as node ${NODEJS_VER}..."
118
119 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
120   curl -fsSL ${NODEJS_REMOTE} -o ${NODEJS_LOCAL} || echo 'error downloading node'
121 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
122   wget --quiet ${NODEJS_REMOTE} -O ${NODEJS_LOCAL} || echo 'error downloading node'
123 else
124   echo "'wget' and 'curl' are missing. Please run the following command and try again"
125   echo "    sudo apt-get install --yes curl wget"
126   exit 1
127 fi
128
129 mkdir -p ${NODEJS_UNTAR}/
130 # --strip-components isn't portable, switch to portable version by performing move step after untar
131 tar xf ${NODEJS_LOCAL} -C ${NODEJS_UNTAR}/ #--strip-components=1
132 mv ${NODEJS_UNTAR}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}/* ${NODEJS_UNTAR}/
133 rm -rf ${NODEJS_UNTAR}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH} # clean up the temporary unzip folder
134 rm ${NODEJS_UNTAR}/{LICENSE,CHANGELOG.md,README.md}
135 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
136   echo $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
137   rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
138 else
139   # due to symlink issues on Arch Linux, don't copy the share directory
140   rm -rf ${NODEJS_UNTAR}/share
141   echo $sudo_cmd cp -Hr "${NODEJS_UNTAR}/*" "$node_install_path/"
142   cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/"
143 fi
144 echo ""
145 rm -rf "${NODEJS_UNTAR}"
146
147 chown -R $(whoami) "$node_install_path/lib/node_modules/" 2>/dev/null || $sudo_cmd chown -R $(whoami) "$node_install_path/lib/node_modules/"
148 chown $(whoami) "$node_install_path"/bin/ 2>/dev/null || $sudo_cmd chown $(whoami) "$node_install_path"/bin/
149
150 mkdir -p $node_install_path/lib/node_modules 2> /dev/null || $sudo_cmd mkdir -p $node_install_path/lib/node_modules
151 chown -R $(whoami) $node_install_path/lib/node_modules 2> /dev/null || $sudo_cmd chown -R $(whoami) $node_install_path/lib/node_modules
152
153 # By default, npm is stupid and uses any version of node in any path. Stop that.
154 # npm config set scripts-prepend-node-path true
155 "$node_install_path"/bin/node "$node_install_path"/bin/npm --scripts-prepend-node-path=true config set scripts-prepend-node-path true
156
157 #######
158 # END #
159 #######
160
161 pathman add $node_install_path/bin