-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
178 lines (144 loc) · 4.85 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
/* eslint no-console: 0 */
const fs = require('fs');
const path = require('path');
const meow = require('meow');
const yaml = require('js-yaml');
const ngrok = require('ngrok');
const init = require('./lib/init');
const interpolate = require('./lib/interpolate');
const sendEmail = require('./lib/send-email');
const sendWebhook = require('./lib/send-webhook');
const updateNotifier = require('update-notifier');
require('dotenv').config();
const pkg = require('./package.json');
const cli = meow(
`
Usage: ngrok-notify PROTO PORT [-n]
ngrok-notify init [-f]
Positional arguments:
PROTO Protocol to use in the ngrok tunnel {http,tcp,tls}
PORT Port number of the localhost service to expose (e.g. 8080)
init Copy starter config files into directory for customizing
Optional arguments:
-e, --email Send an email providing the URL of the ngrok tunnel
-w, --webhook Call a webhook providing the URL of the ngrok tunnel as POST params
-h, --help Show help
-v, --version Display version information
-f, --force Overwrite config files in directory if they exist.
Notes
Email messages are sent using the settings in the config.yml file and the
Gmail password stored in the .env file.
Examples
Create ngrok tunnel to expose localhost web server running on port 8080.
Email is sent with the ngrok URL since "-e" is included.
$ ngrok-notify http 8080 -e
Call a webhook instead of sending an email.
$ ngrok-notify http 8080 -w
You can send an email and call webhook with one command specifying both options.
$ ngrok-notify http 8080 -e -w
`,
{
flags: {
email: {
type: 'boolean',
alias: 'e'
},
webhook: {
type: 'boolean',
alias: 'w'
},
force: {
type: 'boolean',
alias: 'f'
},
version: {
type: 'boolean',
alias: 'v'
},
help: {
type: 'boolean',
alias: 'h'
}
}
}
);
const [command] = cli.input;
if (command === 'init') {
init(cli.flags.f);
process.exit(0);
}
const missingFiles = init.checkIfNeeded();
if (missingFiles) {
console.log(missingFiles);
console.log("Please run 'ngrok-notify init' to copy starter config files to your directory for customizing.")
process.exit(1);
}
const cwd = process.cwd();
const config = yaml.safeLoad(
fs.readFileSync(path.join(cwd, 'config.yml'), 'utf8')
);
const opts = config.ngrok || {};
if (cli.input.length < 2) {
cli.showHelp();
} else {
const [proto, strPort] = cli.input;
opts.proto = proto;
const isIntegerInRange = (i, min, max) =>
Number.isInteger(i) && i >= min && i <= max;
const PORT_MIN = 0;
const PORT_MAX = 65535;
const port = parseInt(strPort, 10);
if (!isIntegerInRange(port, PORT_MIN, PORT_MAX)) {
console.log(
`Expected an integer for 'port' between ${PORT_MIN} and ${PORT_MAX} and received: ${strPort}`
);
process.exit(1);
}
// The ngrok npm package parlance uses "addr" instead of "port".
opts.addr = port;
}
// Graft in secrets from .env file to pass to ngrok, if present.
const auth = process.env.NGROK_AUTH;
if (auth) opts.auth = auth;
const authtoken = process.env.NGROK_AUTHTOKEN;
if (authtoken) opts.authtoken = authtoken;
const emailOpts = config.email;
(async () => {
console.log("Opening connection with ngrok...");
const url = await ngrok.connect(opts);
console.log(`Connected to ngrok: ${url} `);
// Add url so it can be interpolated from the message text containing "{url}"
opts.url = url;
const emailEnabled = cli.flags.email;
let emailTail = '';
if (emailEnabled) {
console.log("Sending Email...");
// Get Gmail password if set in .env file.
if (process.env.GMAIL_PASSWORD)
emailOpts.password = process.env.GMAIL_PASSWORD;
// substitute values like {proto} with their configuration values
// patch in property name of port since it's a more technically correct and known term.
opts.port = opts.addr;
const subject = interpolate(emailOpts.subject, opts);
const message = interpolate(emailOpts.message, opts);
sendEmail(emailOpts, subject, message);
emailTail = ' (email sent)';
console.log(`${message}${emailTail}`);
}
const webhookOpts = config.webhook;
const webhookEnabled = cli.flags.webhook;
if (webhookEnabled) {
console.log("Calling Webhook...");
const webhook_url = webhookOpts.url
const webhook_method = webhookOpts.method === "GET" ? "GET" : "POST";
try {
const response = await sendWebhook(opts, webhook_url, webhook_method);
console.log(`Webhook triggered at ${webhook_url} with method ${webhook_method}`);
} catch (error) {
console.error(`Error calling webhook at ${webhook_url} with method ${webhook_method}`);
console.error(error);
}
}
updateNotifier({pkg}).notify();
})();