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