0d1f24df08866415f1708b3b61eb02bc01871232
[webi-installers/.git] / node / install.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 #
10 #   ### Hello World
11 #
12 #   ```bash
13 #   node -e 'console.log("Hello, World!")'
14 #   > Hello, World!
15 #   ```
16 #
17 #   <br/>
18 #
19 #   ### A Simple Web Server
20 #
21 #   `server.js`:
22 #
23 #   ```bash
24 #   var http = require('http');
25 #   var app = function (req, res) {
26 #     res.end('Hello, World!');
27 #   };
28 #   http.createServer(app).listen(8080, function () {
29 #     console.info('Listening on', this.address());
30 #   });
31 #   ```
32 #
33 #   <br/>
34 #
35 #   ```bash
36 #   node server.js
37 #   ```
38 #
39 #   ### An Express App
40 #
41 #   ```bash
42 #   mkdir my-server
43 #   pushd my-server
44 #   npm init
45 #   npm install --save express
46 #   ```
47 #
48 #   <br/>
49 #
50 #   `app.js`:
51 #
52 #   ```js
53 #   'use strict';
54 #
55 #   var express = require('express');
56 #   var app = express();
57 #
58 #   app.use('/', function (req, res, next) {
59 #     res.end("Hello, World!");
60 #   });
61 #
62 #   module.exports = app;</code></pre>
63 #   ```
64 #
65 #   <br/>
66 #
67 #   `server.js`:
68 #
69 #   ```js
70 #   'use strict';
71 #
72 #   var http = require('http');
73 #   var app = require('./app.js');
74 #
75 #   http.createServer(app).listen(8080, function () {
76 #     console.info('Listening on', this.address());
77 #   });
78 #   ```
79 #
80 #   <br/>
81 #
82 #   ```bash
83 #   npm start
84 #   ```
85 #
86
87 set -e
88 set -u
89
90 ##################
91 #  Install node  #
92 ##################
93
94 new_node_home="${HOME}/.local/opt/node-v${WEBI_VERSION}"
95 new_node="${HOME}/.local/opt/node-v${WEBI_VERSION}/bin/node"
96
97 # Test for existing version
98 set +e
99 cur_node="$(command -v node)"
100 set -e
101 if [ -e "$new_node_home/bin/node" ]; then
102     # node of some version is already installed
103     if [ "v${WEBI_VERSION}" == "$("$new_node_home/bin/node" -v 2>/dev/null)" ]; then
104         echo node v${WEBI_VERSION} already installed at $new_node_home
105         exit 0
106     fi
107 fi
108 if [ -n "$cur_node" ] && [ "$cur_node" != "$new_node" ]; then
109     echo "WARN: possible conflict with node v$WEBI_VERSION at $cur_node"
110 fi
111
112 # Note: this file is `source`d by the true installer and hence will have the webi functions
113
114 # because we created releases.js we can use webi_download()
115 # downloads node to ~/Downloads
116 webi_download
117
118 # because this is tar or zip, we can webi_extract()
119 # extracts to the WEBI_TMP directory, raw (no --strip-prefix)
120 webi_extract
121
122 pushd "$WEBI_TMP" 2>&1 >/dev/null
123     echo Installing node v${WEBI_VERSION} as "$new_node"
124
125     # simpler for single-binary commands
126     #mv ./example*/bin/example "$HOME/.local/bin"
127
128     # best for packages and toolchains
129     rm -rf "$new_node_home"
130     if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
131       rsync -Krl ./node*/ "$new_node_home/" 2>/dev/null
132     else
133       cp -Hr ./node*/* "$new_node_home/" 2>/dev/null
134       cp -Hr ./node*/.* "$new_node_home/" 2>/dev/null
135     fi
136 popd 2>&1 >/dev/null
137
138 ###################
139 #   Update PATH   #
140 ###################
141
142 # TODO get better output from pathman / output the path to add as return to webi bootstrap
143 webi_path_add "$new_node_home/bin"
144
145 echo "Installed 'node' and 'npm'"
146 echo ""