@@ -4,129 +4,129 @@ const reloadOpts = require("../cli-options/opts.reload.json");
44const recipeOpts = require ( "../cli-options/opts.recipe.json" ) ;
55const pkg = require ( "../package.json" ) ;
66import * as utils from "./utils" ;
7- import { resolve } from "path" ;
8- import { existsSync } from "fs" ;
9- import { logger } from "./logger" ;
10- import { compile } from "eazy-logger" ;
7+ import { resolve } from "path" ;
8+ import { existsSync } from "fs" ;
9+ import { logger } from "./logger" ;
10+ import { compile } from "eazy-logger" ;
1111
1212export enum BsErrorLevels {
13- Fatal = "Fatal"
13+ Fatal = "Fatal"
1414}
1515
1616export enum BsErrorTypes {
17- PathNotFound = "PathNotFound"
17+ PathNotFound = "PathNotFound"
1818}
1919
2020/**
2121 * Handle cli input
2222 */
2323if ( ! module . parent ) {
24- runFromCli ( ) ;
24+ runFromCli ( ) ;
2525}
2626
2727function runFromCli ( ) {
28- const yargs = require ( "yargs" )
29- . command ( "start" , "Start the server" )
30- . command ( "init" , "Create a configuration file" )
31- . command ( "reload" , "Send a reload event over HTTP protocol" )
32- . command ( "recipe" , "Generate the files for a recipe" )
33- . version ( ( ) => pkg . version )
34- . epilogue (
35- [
36- "For help running a certain command, type <command> --help" ,
37- " $0 start --help" ,
38- "" ,
39- "You can run a static server by providing a path(s) directly" ,
40- " $0 app/src app/tmp" ,
41- "" ,
42- "If the directory contains a 'index.html' file, you can omit any input" ,
43- " $0" ,
44- "" ,
45- "You can run the proxy in this manner too" ,
46- " $0 https://example.com" ,
47- "" ,
48- "To run a proxy, whilst also serving static files" ,
49- compile ( " $0 https://example.com htdocs/themes/example" )
50- ] . join ( "\n" )
51- ) ;
52-
53- const argv = yargs . argv ;
54- const input = argv . _ ;
55- const command = input [ 0 ] ;
56- const valid = [ "start" , "init" , "reload" , "recipe" ] ;
57-
58- if ( argv . help ) {
59- return yargs . showHelp ( ) ;
60- }
61-
62- if ( valid . indexOf ( command ) > - 1 ) {
63- return handleIncoming ( command , yargs . reset ( ) ) ;
64- }
65-
66- if ( input . length ) {
67- return handleNoCommand ( argv , input ) ;
68- }
69-
70- if ( existsSync ( "index.html" ) ) {
71- return handleNoCommand ( argv , [ "." ] ) ;
72- }
73-
74- yargs . showHelp ( ) ;
28+ const yargs = require ( "yargs" )
29+ . command ( "start" , "Start the server" )
30+ . command ( "init" , "Create a configuration file" )
31+ . command ( "reload" , "Send a reload event over HTTP protocol" )
32+ . command ( "recipe" , "Generate the files for a recipe" )
33+ . version ( ( ) => pkg . version )
34+ . epilogue (
35+ [
36+ "For help running a certain command, type <command> --help" ,
37+ " $0 start --help" ,
38+ "" ,
39+ "You can run a static server by providing a path(s) directly" ,
40+ " $0 app/src app/tmp" ,
41+ "" ,
42+ "If the directory contains a 'index.html' file, you can omit any input" ,
43+ " $0" ,
44+ "" ,
45+ "You can run the proxy in this manner too" ,
46+ " $0 https://example.com" ,
47+ "" ,
48+ "To run a proxy, whilst also serving static files" ,
49+ compile ( " $0 https://example.com htdocs/themes/example" )
50+ ] . join ( "\n" )
51+ ) ;
52+
53+ const argv = yargs . argv ;
54+ const input = argv . _ ;
55+ const command = input [ 0 ] ;
56+ const valid = [ "start" , "init" , "reload" , "recipe" ] ;
57+
58+ if ( argv . help ) {
59+ return yargs . showHelp ( ) ;
60+ }
61+
62+ if ( valid . indexOf ( command ) > - 1 ) {
63+ return handleIncoming ( command , yargs . reset ( ) ) ;
64+ }
65+
66+ if ( input . length ) {
67+ return handleNoCommand ( argv , input ) ;
68+ }
69+
70+ if ( existsSync ( "index.html" ) ) {
71+ return handleNoCommand ( argv , [ "." ] ) ;
72+ }
73+
74+ yargs . showHelp ( ) ;
7575}
7676
7777function handleNoCommand ( argv , input ) {
78- const paths = input . map ( path => {
79- const resolved = resolve ( path ) ;
80- const isUrl = / ^ h t t p s ? : \/ \/ / . test ( path ) ;
81- return {
82- isUrl,
83- userInput : path ,
84- resolved,
85- errors : isUrl ? [ ] : pathErrors ( path , resolved )
86- } ;
78+ const paths = input . map ( path => {
79+ const resolved = resolve ( path ) ;
80+ const isUrl = / ^ h t t p s ? : \/ \/ / . test ( path ) ;
81+ return {
82+ isUrl,
83+ userInput : path ,
84+ resolved,
85+ errors : isUrl ? [ ] : pathErrors ( path , resolved )
86+ } ;
87+ } ) ;
88+
89+ const withErrors = paths . filter ( item => item . errors . length ) ;
90+
91+ const withoutErrors = paths . filter ( item => item . errors . length === 0 ) ;
92+
93+ if ( withErrors . length ) {
94+ withErrors . forEach ( item => {
95+ logger . unprefixed ( "error" , printErrors ( item . errors ) ) ;
8796 } ) ;
88-
89- const withErrors = paths . filter ( item => item . errors . length ) ;
90-
91- const withoutErrors = paths . filter ( item => item . errors . length === 0 ) ;
92-
93- if ( withErrors . length ) {
94- withErrors . forEach ( item => {
95- logger . unprefixed ( "error" , printErrors ( item . errors ) ) ;
96- } ) ;
97- process . exit ( 1 ) ;
97+ process . exit ( 1 ) ;
98+ } else {
99+ const ssPaths = withoutErrors
100+ . filter ( item => item . isUrl === false )
101+ . map ( item => item . resolved ) ;
102+
103+ const urls = withoutErrors
104+ . filter ( item => item . isUrl === true )
105+ . map ( item => item . userInput ) ;
106+
107+ if ( urls . length ) {
108+ const proxy = urls [ 0 ] ;
109+ var config = Object . assign ( { } , argv , {
110+ proxy,
111+ serveStatic : ssPaths
112+ } ) ;
113+ handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
98114 } else {
99- const ssPaths = withoutErrors
100- . filter ( item => item . isUrl === false )
101- . map ( item => item . resolved ) ;
102-
103- const urls = withoutErrors
104- . filter ( item => item . isUrl === true )
105- . map ( item => item . userInput ) ;
106-
107- if ( urls . length ) {
108- const proxy = urls [ 0 ] ;
109- var config = Object . assign ( { } , argv , {
110- proxy,
111- serveStatic : ssPaths
112- } ) ;
113- handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
114- } else {
115- var config = Object . assign ( { } , argv , {
116- server : { baseDir : ssPaths }
117- } ) ;
118- handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
119- }
115+ var config = Object . assign ( { } , argv , {
116+ server : { baseDir : ssPaths }
117+ } ) ;
118+ handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
120119 }
120+ }
121121}
122122
123123/**
124124 * @param {{cli: object, [whitelist]: array, [cb]: function} } opts
125125 * @returns {* }
126126 */
127127function handleCli ( opts ) {
128- opts . cb = opts . cb || utils . defaultCallback ;
129- return require ( `./cli/command.${ opts . cli . input [ 0 ] } ` ) ( opts ) ;
128+ opts . cb = opts . cb || utils . defaultCallback ;
129+ return require ( `./cli/command.${ opts . cli . input [ 0 ] } ` ) ( opts ) ;
130130}
131131
132132export default handleCli ;
@@ -136,83 +136,84 @@ export default handleCli;
136136 * @param {object } yargs
137137 */
138138function handleIncoming ( command , yargs ) {
139- let out ;
140- if ( command === "start" ) {
141- out = yargs
142- . usage ( "Usage: $0 start [options]" )
143- . options ( startOpts )
144- . example (
145- "$0 start -s app" ,
146- "- Use the App directory to serve files"
147- )
148- . example ( "$0 start -p www.bbc.co.uk" , "- Proxy an existing website" )
149- . help ( ) . argv ;
150- }
151- if ( command === "init" ) {
152- out = yargs
153- . usage ( "Usage: $0 init" )
154- . example ( "$0 init" )
155- . help ( ) . argv ;
156- }
157- if ( command === "reload" ) {
158- out = yargs
159- . usage ( "Usage: $0 reload" )
160- . options ( reloadOpts )
161- . example ( "$0 reload" )
162- . example ( "$0 reload --port 4000" )
163- . help ( ) . argv ;
164- }
165- if ( command === "recipe" ) {
166- out = yargs
167- . usage ( "Usage: $0 recipe <recipe-name>" )
168- . option ( recipeOpts )
169- . example ( "$0 recipe ls" , "list the recipes" )
170- . example ( "$0 recipe gulp.sass" , "use the gulp.sass recipe" )
171- . help ( ) . argv ;
172- }
173-
174- if ( out . help ) {
175- return yargs . showHelp ( ) ;
176- }
177-
178- handleCli ( { cli : { flags : out , input : out . _ } } ) ;
139+ let out ;
140+ if ( command === "start" ) {
141+ out = yargs
142+ . usage ( "Usage: $0 start [options]" )
143+ . options ( startOpts )
144+ . example ( "$0 start -s app" , "- Use the App directory to serve files" )
145+ . example ( "$0 start -p www.bbc.co.uk" , "- Proxy an existing website" )
146+ . help ( ) . argv ;
147+ }
148+ if ( command === "init" ) {
149+ out = yargs
150+ . usage ( "Usage: $0 init" )
151+ . example ( "$0 init" )
152+ . help ( ) . argv ;
153+ }
154+ if ( command === "reload" ) {
155+ out = yargs
156+ . usage ( "Usage: $0 reload" )
157+ . options ( reloadOpts )
158+ . example ( "$0 reload" )
159+ . example ( "$0 reload --port 4000" )
160+ . help ( ) . argv ;
161+ }
162+ if ( command === "recipe" ) {
163+ out = yargs
164+ . usage ( "Usage: $0 recipe <recipe-name>" )
165+ . option ( recipeOpts )
166+ . example ( "$0 recipe ls" , "list the recipes" )
167+ . example ( "$0 recipe gulp.sass" , "use the gulp.sass recipe" )
168+ . help ( ) . argv ;
169+ }
170+
171+ if ( out . help ) {
172+ return yargs . showHelp ( ) ;
173+ }
174+
175+ handleCli ( { cli : { flags : out , input : out . _ } } ) ;
179176}
180177
181178function pathErrors ( input , resolved ) {
182- if ( ! existsSync ( resolved ) ) {
183- return [
184- {
185- type : BsErrorTypes . PathNotFound ,
186- level : BsErrorLevels . Fatal ,
187- errors : [
188- {
189- error : new Error ( `Path not found: ${ input } ` ) ,
190- meta ( ) {
191- return [
192- `Your Input: {yellow:${ input } }` ,
193- `CWD: {yellow:${ process . cwd ( ) } }` ,
194- `Resolved to: {yellow:${ resolved } }`
195- ] ;
196- }
197- }
198- ]
179+ if ( ! existsSync ( resolved ) ) {
180+ return [
181+ {
182+ type : BsErrorTypes . PathNotFound ,
183+ level : BsErrorLevels . Fatal ,
184+ errors : [
185+ {
186+ error : new Error ( `Path not found: ${ input } ` ) ,
187+ meta ( ) {
188+ return [
189+ `Your Input: {yellow:${ input } }` ,
190+ `CWD: {yellow:${ process . cwd ( ) } }` ,
191+ `Resolved to: {yellow:${ resolved } }`
192+ ] ;
199193 }
200- ] ;
201- }
202- return [ ] ;
194+ }
195+ ]
196+ }
197+ ] ;
198+ }
199+ return [ ] ;
203200}
204201
205202function printErrors ( errors ) {
206- return errors
207- . map ( error => [
208- `Error Type: {bold:${ error . type } }` ,
209- `Error Level: {bold:${ error . level } }` ,
210- error . errors . map ( item => [
211- `Error Message: ${ item . error . message } ` ,
212- item . meta ? item . meta ( ) . join ( "\n" ) : ""
213- ]
214- . filter ( Boolean )
215- . join ( "\n" ) )
216- ] . join ( "\n" ) )
217- . join ( "\n\n" ) ;
203+ return errors
204+ . map ( error =>
205+ [
206+ `Error Type: {bold:${ error . type } }` ,
207+ `Error Level: {bold:${ error . level } }` ,
208+ error . errors . map ( item =>
209+ [
210+ `Error Message: ${ item . error . message } ` ,
211+ item . meta ? item . meta ( ) . join ( "\n" ) : ""
212+ ]
213+ . filter ( Boolean )
214+ . join ( "\n" )
215+ )
216+ ] . join ( "\n" )
217+ )
218+ . join ( "\n\n" ) ;
218219}
0 commit comments