f4158d86aa0048757f17ad3c6ad5cea402de1ad9
[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 my_tmp=${WEBI_TMP:-$(mktemp -d node-install.XXXXXX)}
55 sudo_cmd=${WEBI_SUDO:-}
56
57 http_get() {
58   if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
59     curl -fsSL $1 -o $2 || echo 'error downloading node'
60   elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
61     wget --quiet $1 -O $2 || echo 'error downloading node'
62   else
63     echo "'wget' and 'curl' are missing. Please run the following command and try again"
64     echo ""
65     echo "    sudo apt-get install --yes curl wget"
66     exit 1
67   fi
68 }
69
70 WEBI_CSV=$(curl -fsSL "https://webinstall.dev/api/releases/node@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1" -H "User-Agent: $(uname -a)")
71 NODEJS_VER=$(echo $WEBI_CSV | cut -d ',' -f 1)
72 NODEJS_REMOTE=$(echo $WEBI_CSV | cut -d ',' -f 9)
73 NODEJS_LOCAL="$my_tmp/$(echo $NODEJS_REMOTE | sed s:.*/::)"
74 NODE_OS="$(echo $WEBI_CSV | cut -d ',' -f 5)"
75
76 #########
77 # BEGIN #
78 #########
79
80 # WEBI_ARCH uses only slightly different names from NODE_ARCH
81 NODE_OS="$(echo $WEBI_CSV | cut -d ',' -f 5)"
82 if [ "macos" == "$NODE_OS" ]; then
83   NODE_OS="darwin"
84 fi
85 NODE_ARCH="$(echo $WEBI_CSV | cut -d ',' -f 6)"
86 if [ "amd64" == "$NODE_ARCH" ]; then
87   NODE_ARCH="x64"
88 fi
89
90 node_install_path=$HOME/.local/opt/node-v${NODEJS_VER}
91 mkdir -p $node_install_path
92
93 if [ -e "$node_install_path/bin/node" ]; then
94   # node of some version is already installed
95   if [ "v${NODEJS_VER}" == "$($node_install_path/bin/node -v 2>/dev/null)" ]; then
96     echo node ${NODEJS_VER} already installed at $node_install_path
97     exit 0
98   fi
99 fi
100
101 # TODO warn if existing node in path my take precedence
102
103 echo "downloading node v${NODEJS_VER}..."
104 http_get ${NODEJS_REMOTE} ${NODEJS_LOCAL} || echo 'error downloading node'
105
106 echo "installing node v${NODEJS_VER}..."
107 tar xf ${NODEJS_LOCAL} -C $my_tmp/
108 # we know how it'll unpack
109 NODEJS_UNTAR=$my_tmp/node-v${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}
110
111 # this funny business is to allow something a non-/opt directory
112 # ( such as /usr/local ) to be an install target
113 rm ${NODEJS_UNTAR}/{LICENSE,CHANGELOG.md,README.md}
114 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
115   echo $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
116   rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
117 else
118   # due to symlink issues on Arch Linux, don't copy the share directory
119   rm -rf ${NODEJS_UNTAR}/share
120   echo $sudo_cmd cp -Hr "${NODEJS_UNTAR}/*" "$node_install_path/"
121   cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/"
122 fi
123 rm -rf "${NODEJS_UNTAR}"
124 rm -rf "${my_tmp}"
125
126 # By default, npm is stupid and uses any version of node in any path. Stop that.
127 # npm config set scripts-prepend-node-path true
128 "$node_install_path"/bin/node "$node_install_path"/bin/npm --scripts-prepend-node-path=true config set scripts-prepend-node-path true
129
130 #######
131 # END #
132 #######
133
134 pathman add $node_install_path/bin