use new bootstrap + webinstall approach
[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 ##################
55 #  Install node  #
56 ##################
57
58 new_node_home="${HOME}/.local/opt/node-v${WEBI_VERSION}"
59 new_node="${HOME}/.local/opt/node-v${WEBI_VERSION}/bin/node"
60
61 # Test for existing version
62 set +e
63 cur_node="$(command -v node)"
64 set -e
65 if [ -e "$new_node_home/bin/node" ]; then
66     # node of some version is already installed
67     if [ "v${WEBI_VERSION}" == "$("$new_node_home/bin/node" -v 2>/dev/null)" ]; then
68         echo node v${WEBI_VERSION} already installed at $new_node_home
69         exit 0
70     fi
71 fi
72 if [ "$cur_node" != "$new_node" ]; then
73     echo "WARN: possible conflict with node v$WEBI_VERSION at $cur_node"
74 fi
75
76 # Note: this file is `source`d by the true installer and hence will have the webi functions
77
78 # because we created releases.js we can use webi_download()
79 # downloads node to ~/Downloads
80 webi_download
81
82 # because this is tar or zip, we can webi_extract()
83 # extracts to the WEBI_TMP directory, raw (no --strip-prefix)
84 webi_extract
85
86 pushd "$WEBI_TMP" 2>&1 >/dev/null
87     echo Installing node v${WEBI_VERSION} as "$new_node"
88
89     # simpler for single-binary commands
90     #mv ./example*/bin/example "$HOME/.local/bin"
91
92     # best for packages and toolchains
93     rm -rf "$new_node_home"
94     if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
95       rsync -Krl ./node*/ "$new_node_home/" 2>/dev/null
96     else
97       cp -Hr ./node*/* "$new_node_home/" 2>/dev/null
98       cp -Hr ./node*/.* "$new_node_home/" 2>/dev/null
99     fi
100 popd 2>&1 >/dev/null
101
102 ###################
103 #   Update PATH   #
104 ###################
105
106 # TODO get better output from pathman / output the path to add as return to webi bootstrap
107 webi_path_add "$new_node_home/bin"
108
109 echo "Installed 'node' and 'npm'"
110 echo ""