Skip to content

Commit 4ddbd46

Browse files
committed
wip
1 parent eb43ca7 commit 4ddbd46

File tree

3 files changed

+24
-45
lines changed

3 files changed

+24
-45
lines changed

src/cli.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,27 @@ const certify = () =>
1818
(async () => {
1919
// Parse arguments from the command line
2020
program
21-
.arguments('[root] [fallback] [port]')
21+
.arguments('[root]')
22+
.option('-f, --fallback <fallback>', 'fallback file', 'index.html')
23+
.option('-p, --port <port>', 'port to use', '8080')
2224
.option('-r, --reload', 'reload on file change')
2325
.option('-m, --module', 'serve javascript modules')
2426
.option('-s, --static', 'serve static files')
2527
.option('--secure', 'use https')
2628
.option('-b, --browse', 'open browser on start')
27-
.option('--localhost-only', 'only serve on localhost')
28-
.option('--no-dir-listing', 'disable directory listing')
29+
.option('--host <host>', 'host to listen on')
30+
.option('--dir-listing', 'enable directory listing')
2931
.option('--silent', 'disable console output')
30-
.option('--editor', 'open code editor')
3132
.parse(process.argv);
3233

3334
const opts = program.opts();
3435
const argsRoot = program.args[0];
35-
const fallback = program.args[1];
36-
const argsPort = program.args[2];
36+
const fallback = opts.fallback;
37+
const argsPort = opts.port;
3738

38-
// const args = process.argv.slice(2).filter((x) => !~x.indexOf('--'));
3939
const admin = process.getuid && process.getuid() === 0;
4040
let credentials;
4141

42-
// if (args[0] && args[0].startsWith('gh:')) {
43-
// const repo = args[0].replace('gh:', '');
44-
// const dest = repo.split('/')[1];
45-
// if (!fs.existsSync(dest)) {
46-
// try {
47-
// require('child_process').execSync(`git clone https://github.com/${repo}`, { stdio: 'ignore' });
48-
// } catch (e) {
49-
// console.log(`\n ⚠️ Could not clone from https://github.com/${repo}\n`);
50-
// process.exit();
51-
// }
52-
// }
53-
// args[0] = dest;
54-
// }
55-
56-
if (opts.editor) {
57-
try {
58-
require('child_process').execSync(`code ${argsRoot || '.'}`);
59-
} catch (e) {
60-
console.log(`\n ⚠️ Could not open code editor for ${argsRoot || '.'}`);
61-
}
62-
}
63-
6442
// Generate ssl certificates
6543

6644
if (opts.secure) {
@@ -83,11 +61,11 @@ const certify = () =>
8361
root: argsRoot,
8462
fallback,
8563
port: argsPort,
86-
reload: !!opts.reload,
87-
module: !!opts.module,
88-
static: !!opts.static,
89-
host: !!opts.localhostOnly ? '127.0.0.1' : undefined,
90-
noDirListing: !!opts.noDirListing,
64+
reload: opts.reload,
65+
module: opts.module,
66+
static: opts.static,
67+
host: opts.host,
68+
noDirListing: !opts.dirListing,
9169
credentials,
9270
});
9371

src/servor.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface Options {
2424
static?: boolean;
2525
inject?: string;
2626
credentials?: any;
27-
port?: string | number;
27+
port?: number;
2828
host?: string;
2929
livereloadUrl?: string;
3030
proxy?: Record<string, string>;
@@ -40,22 +40,23 @@ export const servor = async ({
4040
inject = '',
4141
credentials,
4242
port: outerPort,
43-
host = '127.0.0.1',
43+
host,
4444
livereloadUrl = '/livereload',
4545
proxy: proxyConfigObj,
4646
noDirListing = false,
4747
}: Options = {}) => {
4848
// Try start on specified port then fail or find a free port
4949

50-
let port: string;
50+
let port: number;
51+
const envPort = process.env.PORT ? parseInt(process.env.PORT) : undefined;
5152
try {
52-
port = await usePort(outerPort || process.env.PORT || 8080);
53+
port = await usePort(outerPort || envPort || 8080, host);
5354
} catch (e) {
54-
if (outerPort || process.env.PORT) {
55+
if (outerPort || envPort) {
5556
console.log('[ERR] The port you have specified is already in use!');
5657
process.exit();
5758
}
58-
port = await usePort();
59+
port = await usePort(undefined, host);
5960
}
6061

6162
const proxyConfig: Array<[RegExp, string]> = proxyConfigObj
@@ -231,7 +232,7 @@ export const servor = async ({
231232
if (reload && pathname === livereloadUrl) return serveReload(res);
232233
if (!isRouteRequest(pathname) && !(await isDir(pathname))) return await serveStaticFile(res, pathname);
233234
return await serveRoute(req, res, pathname);
234-
}).listen(parseInt(port, 10), host);
235+
}).listen(port, host);
235236

236237
// Notify livereload reloadClients on file change
237238

src/utils/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ export const fileWatch = (
2828
chokidar.watch(x).on('all', cb);
2929
};
3030

31-
export const usePort = (port: number | string = 0) =>
32-
new Promise<string>((ok, x) => {
31+
export const usePort = (port?: number, host?: string) =>
32+
new Promise<number>((ok, x) => {
3333
const s = net.createServer();
3434
s.on('error', x);
35-
s.listen(port, () => {
35+
s.listen(port, host, undefined, () => {
3636
const a = s.address();
37-
s.close(() => (a ? ok(typeof a === 'string' ? a : a.port.toString()) : void 0));
37+
s.close(() => (a && typeof a === 'object' ? ok(a.port) : void 0));
3838
});
3939
});
4040

0 commit comments

Comments
 (0)