Skip to content

Commit 63f32f5

Browse files
committed
wip
1 parent 2bc25fe commit 63f32f5

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"utils/*"
2323
],
2424
"scripts": {
25-
"start": "node dist/cli.js tests/example --reload --browse --static --module",
25+
"start": "SWC_NODE_PROJECT=./tsconfig.json node -r @swc-node/register src/cli.ts tests/example index.html 8081 --reload --browse",
2626
"build": "tsc",
2727
"cleanup": "rm -f servor.key servor.crt",
2828
"test": "npm run cleanup && cd tests && node index.js"
@@ -40,6 +40,7 @@
4040
},
4141
"dependencies": {
4242
"chokidar": "^3.5.3",
43+
"commander": "^10.0.1",
4344
"http-proxy": "^1.18.1"
4445
},
4546
"packageManager": "[email protected]",

src/cli.ts

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'node:fs';
33
import path from 'node:path';
44
import { servor } from './servor';
55
import openBrowser from './utils/openBrowser';
6+
import { program } from 'commander';
67

78
const readCredentials = () => ({
89
cert: fs.readFileSync(__dirname + '/servor.crt'),
@@ -15,35 +16,54 @@ const certify = () =>
1516
});
1617

1718
(async () => {
18-
const args = process.argv.slice(2).filter((x) => !~x.indexOf('--'));
19+
// Parse arguments from the command line
20+
program
21+
.arguments('[root] [fallback] [port]')
22+
.option('-r, --reload', 'reload on file change')
23+
.option('-m, --module', 'serve javascript modules')
24+
.option('-s, --static', 'serve static files')
25+
.option('--secure', 'use https')
26+
.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('--silent', 'disable console output')
30+
.option('--editor', 'open code editor')
31+
.parse(process.argv);
32+
33+
const opts = program.opts();
34+
const argsRoot = program.args[0];
35+
const fallback = program.args[1];
36+
const argsPort = program.args[2];
37+
38+
// const args = process.argv.slice(2).filter((x) => !~x.indexOf('--'));
1939
const admin = process.getuid && process.getuid() === 0;
2040
let credentials;
2141

22-
if (args[0] && args[0].startsWith('gh:')) {
23-
const repo = args[0].replace('gh:', '');
24-
const dest = repo.split('/')[1];
25-
if (!fs.existsSync(dest)) {
26-
try {
27-
require('child_process').execSync(`git clone https://github.com/${repo}`, { stdio: 'ignore' });
28-
} catch (e) {
29-
console.log(`\n ⚠️ Could not clone from https://github.com/${repo}\n`);
30-
process.exit();
31-
}
32-
}
33-
args[0] = dest;
34-
}
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+
// }
3555

36-
if (~process.argv.indexOf('--editor')) {
56+
if (opts.editor) {
3757
try {
38-
require('child_process').execSync(`code ${args[0] || '.'}`);
58+
require('child_process').execSync(`code ${argsRoot || '.'}`);
3959
} catch (e) {
40-
console.log(`\n ⚠️ Could not open code editor for ${args[0] || '.'}`);
60+
console.log(`\n ⚠️ Could not open code editor for ${argsRoot || '.'}`);
4161
}
4262
}
4363

4464
// Generate ssl certificates
4565

46-
if (~process.argv.indexOf('--secure')) {
66+
if (opts.secure) {
4767
admin && certify();
4868
admin && process.platform === 'darwin' && process.setuid?.(501);
4969
try {
@@ -59,23 +79,21 @@ const certify = () =>
5979
}
6080
}
6181

62-
// Parse arguments from the command line
63-
6482
const { root, protocol, port, ips, url } = await servor({
65-
root: args[0],
66-
fallback: args[1],
67-
port: args[2],
68-
reload: !!~process.argv.indexOf('--reload'),
69-
module: !!~process.argv.indexOf('--module'),
70-
static: !!~process.argv.indexOf('--static'),
71-
host: !!~process.argv.indexOf('--localhost-only') ? '127.0.0.1' : undefined,
72-
noDirListing: !!~process.argv.indexOf('--no-dir-listing'),
83+
root: argsRoot,
84+
fallback,
85+
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,
7391
credentials,
7492
});
7593

7694
// Output server details to the console
7795

78-
!~process.argv.indexOf('--silent') &&
96+
!opts.silent &&
7997
console.log(`
8098
🗂 Serving:\t${root}\n
8199
🏡 Local:\t${url}
@@ -84,5 +102,5 @@ const certify = () =>
84102

85103
// Browser the server index
86104

87-
!!~process.argv.indexOf('--browse') && openBrowser(url);
105+
!!opts.browse && openBrowser(url);
88106
})();

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ __metadata:
2121
"@types/http-proxy": ^1.17.10
2222
"@types/node": ^18.14.6
2323
chokidar: ^3.5.3
24+
commander: ^10.0.1
2425
http-proxy: ^1.18.1
2526
prettier: ^2.8.7
2627
puppeteer: ^3.0.4
@@ -483,6 +484,13 @@ __metadata:
483484
languageName: node
484485
linkType: hard
485486

487+
"commander@npm:^10.0.1":
488+
version: 10.0.1
489+
resolution: "commander@npm:10.0.1"
490+
checksum: 436901d64a818295803c1996cd856621a74f30b9f9e28a588e726b2b1670665bccd7c1a77007ebf328729f0139838a88a19265858a0fa7a8728c4656796db948
491+
languageName: node
492+
linkType: hard
493+
486494
"concat-map@npm:0.0.1":
487495
version: 0.0.1
488496
resolution: "concat-map@npm:0.0.1"

0 commit comments

Comments
 (0)