-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
146 lines (125 loc) · 3.21 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
#!/usr/bin/env node
'use strict';
const { EOL } = require('os');
const path = require('path');
// Requiring npm modules
const program = require('commander');
const chalk = require('chalk');
// Require API
const up = require('./lib');
const killAllConfirm = require('./actions/killAllConfirm');
// Requiring utils
const requirements = require('./utils/requirements');
const currentPath = path.normalize(process.cwd());
let cmdValue = '';
// Check for requirements such as OS version and nginx install.
//TODO: Add ability to satisfy any possible requirements.
//TODO: Ask for requirements only when needed (exclude for `up list`)
requirements();
// Comment in development and uncomment this line in production.
program
.version(up.version())
.arguments('<cmd>')
.action((cmd) => cmdValue = cmd);
const tryCatch = ((test, name) => {
try {
const msg = test();
if(msg) console.log(msg);
} catch (err) {
err.message = EOL + `[${name}]: ` + err.message;
console.log (err.message);
process.exit(1);
}
});
program
.command('serve <domain> [outPort]')
.description('Create a server at this folder.')
.action((domain, outPort) =>
tryCatch(
() => up.server({
domain: domain,
path: currentPath,
outPort: outPort
}),
'new-server'
));
program
.command('static <domain> [outPort]')
.description(`DEPRECATED! Use 'up serve' instead!
Create a static server at this folder.`)
.action((domain, outPort) =>
tryCatch(
() => up.server({
domain: domain,
path: currentPath,
outPort: outPort
}),
'new-server'
));
program
.command('proxy <domain> <inPort> [outPort]')
.description('Create a proxy server, listening at port number.')
.action((domain, inPort, outPort) =>
tryCatch(
() => up.proxy({
domain: domain,
inPort: inPort,
outPort: outPort
}),
'new-proxy'
));
program
.command('list')
.description('List all available servers.')
.action(() =>
tryCatch(
() => up.list(),
'list'
));
program
.command('kill <domain> [ourPort]')
.description('Kill a server.')
.action((domain, outPort) =>
tryCatch (
() => up.kill({
domain: domain,
outPort: outPort
}),
'kill-server'
));
program
.command('kill-all')
.description('Warning! Will completely kill all servers and reset nginx')
.action(() => killAllConfirm());
program
.command('*') // This should pick invalid commands, but it doesn't, yet.
.action(() => {
console.log(EOL + "Invalid command. Type " +
chalk.cyan('up --help') + " for help." + EOL);
});
// Adds custom help text to the automatically generated help.
program.on('--help', () => {
console.log(EOL
+ ' Usage:'
+ EOL
+ EOL
+ ' ' + chalk.yellow('$ up ')
+ chalk.cyan('static')
+ chalk.blue('[domain-name]')
+ EOL
+ ' Set up a static server at domain-name'
+ EOL
+ EOL
+ ' ' + chalk.yellow('$ up ')
+ chalk.cyan('proxy')
+ chalk.blue('[domain-name] <port-number>')
+ EOL
+ ' Set up a proxy server listening at port-number'
+ EOL);
});
// Parses commands passed to `up` and chooses one of the above commands.
program.parse(process.argv);
if (typeof cmdValue === 'undefined') {
console.log(EOL + "No command was given. `up --help` for help info.");
process.exit(1);
}