11#!/usr/bin/env node
22
33var  fs  =  require ( "fs" ) ; 
4- var  argv  =  require ( "minimist" ) ( process . argv . slice ( 2 ) ) ; 
5- var  mime  =  require ( "mime" ) ; 
64var  path  =  require ( "path" ) ; 
5+ var  mime  =  require ( "mime" ) ; 
76var  pem  =  require ( "pem" ) ; 
87var  https  =  require ( "https" ) ; 
98var  http  =  require ( "http" ) ; 
109var  opn  =  require ( "opn" ) ; 
1110
12- const   getFilePathFromUrl  =  require ( './lib/get-file-path-from-url' ) ; 
13- 
11+ var   argv  =  require ( "minimist" ) ( process . argv . slice ( 2 ) ) ; 
12+ const   getFilePathFromUrl   =   require ( './get-file-path-from-url' ) ; 
1413
1514const  NO_PATH_FILE_ERROR_MESSAGE  = 
1615    "Error: index.html could not be found in the specified path " ; 
17- const  NO_ROOT_FILE_ERROR_MESSAGE  = 
18-     "Error: Could not find index.html within the working directory." ; 
1916
17+ // if using config file, load that first 
2018if  ( argv . config )  { 
2119    let  configPath ; 
2220    if  ( path . isAbsolute ( argv . config ) )  { 
@@ -32,11 +30,13 @@ if (argv.config) {
3230        config  =  getConfig ; 
3331    } 
3432    // supplement argv with config, but CLI args take precedence 
35-     argv  =  Object . assign ( { } ,   config ,  argv ) ; 
33+     argv  =  Object . assign ( config ,  argv ) ; 
3634} 
3735
36+ // Pre-process arguments 
3837const  useHttps  =  argv . ssl  ||  argv . https ; 
3938const  basePath  =  argv . path  ? path . resolve ( argv . path )  : process . cwd ( ) ; 
39+ const  port  =  getPort ( argv . p ) ; 
4040
4141// As a part of the startup - check to make sure we can access index.html 
4242returnDistFile ( true ) ; 
@@ -71,11 +71,11 @@ if (useHttps) {
7171} 
7272
7373function  start ( )  { 
74-     server . listen ( getPort ( ) ,  function ( )  { 
74+     server . listen ( port ,  function ( )  { 
7575        if  ( argv . open  ==  true  ||  argv . o )  { 
76-             opn ( ( useHttps  ? "https"  : "http" )  +  "://localhost:"  +  getPort ( ) ) ; 
76+             opn ( ( useHttps  ? "https"  : "http" )  +  "://localhost:"  +  port ) ; 
7777        } 
78-         return  console . log ( "Listening on "  +  getPort ( ) ) ; 
78+         return  console . log ( "Listening on "  +  port ) ; 
7979    } ) ; 
8080} 
8181
@@ -119,9 +119,9 @@ function requestListener(req, res) {
119119    } ) ; 
120120} 
121121
122- function  getPort ( )  { 
123-     if  ( argv . p )  { 
124-         var  portNum  =  parseInt ( argv . p ) ; 
122+ function  getPort ( portNo )  { 
123+     if  ( portNo )  { 
124+         var  portNum  =  parseInt ( portNo ) ; 
125125        if  ( ! isNaN ( portNum ) )  { 
126126            return  portNum ; 
127127        }  else  { 
0 commit comments