update comments and output
[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 pkg_cmd_name="node"
81
82 pkg_get_current_version() {
83     # 'node --version' has output in this format:
84     #       v12.8.0
85     # This trims it down to just the version number:
86     #       12.8.0
87     echo "$(node --version 2>/dev/null | head -n 1 | cut -d' ' -f1 | sed 's:^v::')"
88 }
89
90 pkg_format_cmd_version() {
91     # 'node v12.8.0' is the canonical version format for node
92     my_version="$1"
93     echo "$pkg_cmd_name v$my_version"
94 }
95
96 pkg_link_new_version() {
97     # 'pkg_common_opt' will default to $HOME/.local/opt/node
98     # 'pkg_new_opt' will be the installed version, such as to $HOME/.local/opt/node-v12.8.0
99     rm -rf "$pkg_common_opt"
100     ln -s "$pkg_new_opt" "$pkg_common_opt"
101 }
102
103 pkg_pre_install() {
104     # web_* are defined in webi/template.bash at https://github.com/webinstall/packages
105
106     # if selected version is installed, re-link it and quit
107     webi_check
108
109     # will save to ~/Downloads/$WEBI_PKG_FILE by default
110     webi_download
111
112     # supported formats (.xz, .tar.*, .zip) will be extracted to $WEBI_TMP
113     webi_extract
114 }
115
116 pkg_install() {
117     pushd "$WEBI_TMP" 2>&1 >/dev/null
118
119         # remove the versioned folder, just in case it's there with junk
120         rm -rf "$pkg_new_opt"
121
122         # rename the entire extracted folder to the new location
123         # (this will be "$HOME/.local/opt/node-v$WEBI_VERSION" by default)
124
125         # ex (full directory): ./node-v13-linux-amd64/bin/node.exe
126         mv ./"$pkg_cmd_name"* "$pkg_new_opt"
127
128         # ex (single file): ./caddy-v13-linux-amd64.exe
129         #mv ./"$pkg_cmd_name"* "$pkg_common_cmd"
130         #chmod a+x "$pkg_common_cmd"
131
132         # ex (single file, nested in directory): ./rg/rg-v13-linux-amd64
133         #mv ./"$pkg_cmd_name"*/"$pkg_cmd_name"* "$pkg_commend_cmd"
134         #chmod a+x "$pkg_common_cmd"
135
136     popd 2>&1 >/dev/null
137 }
138
139 pkg_post_install() {
140     pkg_link_new_version
141
142     # web_path_add is defined in webi/template.bash at https://github.com/webinstall/packages
143     # Adds "$HOME/.local/opt/node/bin" to PATH
144     webi_path_add "$pkg_common_bin"
145 }
146
147 pkg_post_install_message() {
148     echo "Installed 'node' and 'npm'"
149 }