22
33'use strict' ;
44
5+ function help ( ) {
6+ return `Node-Static CLI - simple, RFC 2616 compliant file streaming module for Node.
7+
8+ USAGE: cli.js [OPTIONS] [-p PORT] [<directory>]
9+
10+ Options:
11+ -p PORT, --port PORT
12+ TCP port at which the files will be served. [default: 8080]
13+ -a ADDRESS, --host-address ADDRESS
14+ The local network interface at which to listen. [default: "127.0.0.1"]
15+ -c SECONDS, --cache SECONDS
16+ "Cache-Control" header setting. [default: 3600]
17+ -v, --version
18+ Node-static version
19+ -H HEADERS, --headers HEADERS
20+ Additional headers in JSON format.
21+ -f FILE, --header-file FILE
22+ JSON file of additional headers.
23+ -z, --gzip
24+ Enable compression (tries to serve file of same name plus ".gz").
25+ --spa
26+ Serve the content as a single page app by redirecting all non-file requests to the index HTML file.
27+ -i FILENAME, --indexFile FILENAME
28+ Specify a custom index file when serving up directories. [default: "index.html"]
29+ -h, --help
30+ Display this help message.
31+ ` ;
32+ }
33+
534const fs = require ( 'fs' ) ,
635 tty = require ( 'tty' ) ,
736 url = require ( 'url' ) ,
8- statik = require ( './../lib/node-static' ) ;
9-
10- const argv = require ( 'optimist' )
11- . usage ( [
12- 'USAGE: $0 [-p <port>] [<directory>]' ,
13- 'simple, rfc 2616 compliant file streaming module for node' ]
14- . join ( '\n\n' ) )
15- . option ( 'port' , {
16- alias : 'p' ,
17- 'default' : 8080 ,
18- description : 'TCP port at which the files will be served'
19- } )
20- . option ( 'host-address' , {
21- alias : 'a' ,
22- 'default' : '127.0.0.1' ,
23- description : 'the local network interface at which to listen'
24- } )
25- . option ( 'cache' , {
26- alias : 'c' ,
27- description : '"Cache-Control" header setting, defaults to 3600'
28- } )
29- . option ( 'version' , {
30- alias : 'v' ,
31- description : '@brettz9/node-static version'
32- } )
33- . option ( 'headers' , {
34- alias : 'H' ,
35- description : 'additional headers (in JSON format)'
36- } )
37- . option ( 'header-file' , {
38- alias : 'f' ,
39- description : 'JSON file of additional headers'
40- } )
41- . option ( 'gzip' , {
42- alias : 'z' ,
43- description : 'enable compression (tries to serve file of same name plus \'.gz\')'
44- } )
45- . option ( 'spa' , {
46- description : 'serve the content as a single page app by redirecting all non-file requests to the index html file'
47- } )
48- . option ( 'indexFile' , {
49- alias : 'i' ,
50- 'default' : 'index.html' ,
51- description : 'specify a custom index file when serving up directories'
52- } )
53- . option ( 'help' , {
54- alias : 'h' ,
55- description : 'display this help message'
56- } )
57- . argv ;
58-
59- const dir = argv . _ [ 0 ] || '.' ;
37+ statik = require ( './../lib/node-static' ) ,
38+ neodoc = require ( 'neodoc' ) ;
39+
40+ const args = neodoc . run ( help ( ) , {
41+ laxPlacement : true ,
42+ helpFlags : [ '-h' , '--help' ]
43+ } ) ;
44+
45+ const dir = args [ '<directory>' ] || '.' ;
6046
6147const log = function ( request , response , statusCode ) {
6248 const d = new Date ( ) ;
@@ -74,37 +60,31 @@ const log = function(request, response, statusCode) {
7460 console . log ( colorized ) ;
7561} ;
7662
77- let options ;
78-
79- if ( argv . help ) {
80- require ( 'optimist' ) . showHelp ( console . log ) ;
81- process . exit ( 0 ) ;
82- }
63+ const options = { } ;
8364
84- if ( argv . version ) {
65+ if ( args [ '-- version' ] ) {
8566 console . log ( '@brettz9/node-static' , statik . version . join ( '.' ) ) ;
8667 process . exit ( 0 ) ;
8768}
8869
89- if ( 'cache' in argv ) {
90- ( options = options || { } ) . cache = argv . cache ;
70+ if ( '-- cache' in args ) {
71+ options . cache = args [ '-- cache' ]
9172}
9273
93- if ( argv . headers ) {
94- ( options = options || { } ) . headers = JSON . parse ( argv . headers ) ;
74+ if ( args [ '-- headers' ] ) {
75+ options . headers = JSON . parse ( args [ '-- headers' ] ) ;
9576}
9677
97- if ( argv [ 'header-file' ] ) {
98- ( options = options || { } ) . headers =
99- JSON . parse ( fs . readFileSync ( argv [ 'header-file' ] ) ) ;
78+ if ( args [ '--header-file' ] ) {
79+ options . headers = JSON . parse ( fs . readFileSync ( args [ '--header-file' ] ) ) ;
10080}
10181
102- if ( argv . gzip ) {
103- ( options = options || { } ) . gzip = true ;
82+ if ( args [ '-- gzip' ] ) {
83+ options . gzip = true ;
10484}
10585
106- if ( argv . indexFile ) {
107- ( options = options || { } ) . indexFile = argv [ 'indexFile '] ;
86+ if ( args [ '--index-file' ] ) {
87+ options . indexFile = args [ '--index-file '] ;
10888}
10989
11090const file = new ( statik . Server ) ( dir , options ) ;
@@ -124,21 +104,21 @@ const server = require('http').createServer(function (request, response) {
124104 // Parsing catches:
125105 // npm start -- --spa --indexFile test/fixtures/there/index.html
126106 // with http://127.0.0.1:8080/test/fixtures/there?email=john.cena
127- if ( argv [ 'spa' ] && ! url . parse ( request . url ) . pathname . includes ( "." ) ) {
128- file . serveFile ( argv [ 'indexFile '] , 200 , { } , request , response ) ;
107+ if ( args [ 'spa' ] && ! url . parse ( request . url ) . pathname . includes ( "." ) ) {
108+ file . serveFile ( args [ '--index-file '] , 200 , { } , request , response ) ;
129109 } else {
130110 file . serve ( request , response , callback ) ;
131111 }
132112 } ) . resume ( ) ;
133113} ) ;
134114
135- if ( argv [ 'host-address' ] === '127.0.0.1' ) {
136- server . listen ( + argv . port ) ;
115+ if ( args [ 'host-address' ] === '127.0.0.1' ) {
116+ server . listen ( + args [ '-- port' ] ) ;
137117} else {
138- server . listen ( + argv . port , argv [ ' host-address'] ) ;
118+ server . listen ( + args [ '-- port' ] , args [ '-- host-address'] ) ;
139119}
140120
141- console . log ( 'serving "' + dir + '" at http://' + argv [ ' host-address'] + ':' + argv . port ) ;
142- if ( argv . spa ) {
143- console . log ( 'serving as a single page app (all non-file requests redirect to ' + argv [ 'indexFile '] + ')' ) ;
121+ console . log ( 'serving "' + dir + '" at http://' + args [ '-- host-address'] + ':' + args [ '-- port' ] ) ;
122+ if ( args [ '-- spa' ] ) {
123+ console . log ( 'serving as a single page app (all non-file requests redirect to ' + args [ '--index-file '] + ')' ) ;
144124}
0 commit comments