@@ -3,6 +3,7 @@ import fs from 'node:fs';
33import path from 'node:path' ;
44import { servor } from './servor' ;
55import openBrowser from './utils/openBrowser' ;
6+ import { program } from 'commander' ;
67
78const 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} ) ( ) ;
0 commit comments