Skip to content

Commit 46ca4de

Browse files
committed
feat(serve): add all flags, improve args parsing
1 parent ce8f284 commit 46ca4de

File tree

9 files changed

+328
-259
lines changed

9 files changed

+328
-259
lines changed

lib/commands/external.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ class ExternalCommand {
6060
if (!pkgLoc) {
6161
pkgLoc = await ExternalCommand.promptInstallation(scopeName, name);
6262
}
63-
// Serve needs to be checked for
64-
// if (name === 'serve') {
65-
// return pkgLoc ? require(pkgLoc).serve(args) : null;
66-
// }
6763
return pkgLoc ? require(pkgLoc).default(args) : null;
6864
}
6965
}

packages/serve/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
*.js.map
2-
index.js

packages/serve/argsToCamelCase.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
* Converts dash-seperated strings to camel case
4+
*
5+
* @param {String} str - the string to convert
6+
*
7+
* @returns {String} - new camel case string
8+
*/
9+
function dashesToCamelCase(str): string {
10+
return str.replace(/-([a-z])/g, (g): string => g[1].toUpperCase());
11+
}
12+
13+
/**
14+
*
15+
* Converts CLI args to camel case from dash-separated words
16+
*
17+
* @param {Object} args - argument object parsed by command-line-args
18+
*
19+
* @returns {Object} - the same args object as passed in, with new keys
20+
*/
21+
export default function argsToCamelCase(args): object {
22+
Object.keys(args).forEach((key) => {
23+
const newKey = dashesToCamelCase(key);
24+
if (key !== newKey) {
25+
const arg = args[key];
26+
delete args[key];
27+
args[newKey] = arg;
28+
}
29+
});
30+
return args;
31+
}

packages/serve/flags.js

Lines changed: 208 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,218 @@ const RESPONSE_GROUP = 'Response options:';
77
const BASIC_GROUP = 'Basic options:';
88

99
module.exports = {
10-
core: [
10+
devServer: [
11+
{
12+
name: 'bonjour',
13+
type: Boolean,
14+
describe: 'Broadcasts the server via ZeroConf networking on start',
15+
},
16+
{
17+
name: 'lazy',
18+
type: Boolean,
19+
describe: 'Lazy',
20+
},
21+
{
22+
name: 'liveReload',
23+
type: Boolean,
24+
defaultValue: true,
25+
describe: 'Enables/Disables live reloading on changing files',
26+
},
27+
{
28+
name: 'serveIndex',
29+
type: Boolean,
30+
describe: 'Enables/Disables serveIndex middleware',
31+
defaultValue: true,
32+
},
1133
{
1234
name: 'inline',
1335
type: Boolean,
1436
defaultValue: true,
15-
description: 'inline',
37+
describe:
38+
'Inline mode (set to false to disable including client scripts like livereload)',
39+
},
40+
{
41+
name: 'profile',
42+
type: Boolean,
43+
describe: 'Print compilation profile data for progress steps',
44+
},
45+
{
46+
name: 'progress',
47+
type: Boolean,
48+
describe: 'Print compilation progress in percentage',
49+
group: BASIC_GROUP,
50+
},
51+
{
52+
name: 'hot-only',
53+
type: Boolean,
54+
describe: 'Do not refresh page if HMR fails',
55+
group: ADVANCED_GROUP,
56+
},
57+
{
58+
name: 'stdin',
59+
type: Boolean,
60+
describe: 'close when stdin ends',
61+
},
62+
{
63+
name: 'open',
64+
type: String,
65+
describe: 'Open the default browser, or optionally specify a browser name',
66+
},
67+
{
68+
name: 'useLocalIp',
69+
type: Boolean,
70+
describe: 'Open default browser with local IP',
71+
},
72+
{
73+
name: 'open-page',
74+
type: String,
75+
describe: 'Open default browser with the specified page',
76+
},
77+
// instead of this option, we should consider retrieving the 'color' option
78+
// from the compiler (since webpack CLI already has 'color' option)
79+
// also, we should reconsider how to use supports-color with command-line-args
80+
// {
81+
// name: 'colors',
82+
// type: Boolean,
83+
// // alias: 'color',
84+
// defaultValue: function supportsColor() {
85+
// // Use `require('supports-color').stdout` for supports-color >= 5.0.0.
86+
// // See https://github.com/webpack/webpack-dev-server/pull/1555.
87+
// return require('supports-color').stdout;
88+
// },
89+
// group: DISPLAY_GROUP,
90+
// describe: 'Enables/Disables colors on the console',
91+
// },
92+
// we need to rethink this option, since it is CLI only at the moment
93+
// {
94+
// name: 'info',
95+
// type: Boolean,
96+
// group: DISPLAY_GROUP,
97+
// defaultValue: true,
98+
// describe: 'Info',
99+
// },
100+
{
101+
name: 'quiet',
102+
type: Boolean,
103+
group: DISPLAY_GROUP,
104+
describe: 'Quiet',
105+
},
106+
{
107+
name: 'client-log-level',
108+
type: String,
109+
group: DISPLAY_GROUP,
110+
defaultValue: 'info',
111+
describe:
112+
'Log level in the browser (trace, debug, info, warn, error or silent)',
113+
},
114+
{
115+
name: 'https',
116+
type: Boolean,
117+
group: SSL_GROUP,
118+
describe: 'HTTPS',
119+
},
120+
{
121+
name: 'http2',
122+
type: Boolean,
123+
group: SSL_GROUP,
124+
describe: 'HTTP/2, must be used with HTTPS',
125+
},
126+
{
127+
name: 'key',
128+
type: String,
129+
describe: 'Path to a SSL key.',
130+
group: SSL_GROUP,
131+
},
132+
{
133+
name: 'cert',
134+
type: String,
135+
describe: 'Path to a SSL certificate.',
136+
group: SSL_GROUP,
137+
},
138+
{
139+
name: 'cacert',
140+
type: String,
141+
describe: 'Path to a SSL CA certificate.',
142+
group: SSL_GROUP,
143+
},
144+
{
145+
name: 'pfx',
146+
type: String,
147+
describe: 'Path to a SSL pfx file.',
148+
group: SSL_GROUP,
149+
},
150+
{
151+
name: 'pfx-passphrase',
152+
type: String,
153+
describe: 'Passphrase for pfx file.',
154+
group: SSL_GROUP,
155+
},
156+
{
157+
name: 'content-base',
158+
type: String,
159+
describe: 'A directory or URL to serve HTML content from.',
160+
group: RESPONSE_GROUP,
161+
},
162+
{
163+
name: 'watch-content-base',
164+
type: Boolean,
165+
describe: 'Enable live-reloading of the content-base.',
166+
group: RESPONSE_GROUP,
167+
},
168+
{
169+
name: 'history-api-fallback',
170+
type: Boolean,
171+
describe: 'Fallback to /index.html for Single Page Applications.',
172+
group: RESPONSE_GROUP,
173+
},
174+
{
175+
name: 'compress',
176+
type: Boolean,
177+
describe: 'Enable gzip compression',
178+
group: RESPONSE_GROUP,
179+
},
180+
// findPort is currently not set up
181+
{
182+
name: 'port',
183+
type: Number,
184+
describe: 'The port',
185+
defaultValue: 8080,
186+
group: CONNECTION_GROUP,
187+
},
188+
{
189+
name: 'disable-host-check',
190+
type: Boolean,
191+
describe: 'Will not check the host',
192+
group: CONNECTION_GROUP,
193+
},
194+
{
195+
name: 'socket',
196+
type: String,
197+
describe: 'Socket to listen',
198+
group: CONNECTION_GROUP,
199+
},
200+
{
201+
name: 'public',
202+
type: String,
203+
describe: 'The public hostname/ip address of the server',
204+
group: CONNECTION_GROUP,
205+
},
206+
{
207+
name: 'host',
208+
type: String,
209+
defaultValue: 'localhost',
210+
describe: 'The hostname/ip address the server will bind to',
211+
group: CONNECTION_GROUP,
212+
},
213+
// use command-line-args "multiple" option, allowing the usage: --allowed-hosts host1 host2 host3
214+
// instead of the old, comma-separated syntax: --allowed-hosts host1,host2,host3
215+
{
216+
name: 'allowed-hosts',
217+
type: String,
218+
describe:
219+
'A list of hosts that are allowed to access the dev server, separated by spaces',
220+
group: CONNECTION_GROUP,
221+
multiple: true,
16222
},
17223
],
18224
};

packages/serve/index.js

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)