@@ -10,123 +10,150 @@ import { logger } from "./logger";
1010import { 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
77+ /**
78+ * Feature: If no command was specified, try to do the 'right thing'
79+ *
80+ * If paths were given, start the server
81+ * eg: browser-sync app/code app/design
82+ * is equal to: browser-sync start --server app/code app/design
83+ *
84+ * eg: browser-sync http://example.com
85+ * is equal to: browser-sync start --proxy http://example.com
86+ *
87+ * eg: browser-sync http://example.com themes/example
88+ * is equal to: browser-sync start --proxy http://example.com --ss themes/example
89+ *
90+ * @param argv
91+ * @param input
92+ * @returns {any }
93+ */
7794function 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- } ;
87- } ) ;
8895
89- const withErrors = paths . filter ( item => item . errors . length ) ;
96+ const paths = input . map ( path => {
97+ const resolved = resolve ( path ) ;
98+ const isUrl = / ^ h t t p s ? : \/ \/ / . test ( path ) ;
99+ return {
100+ isUrl,
101+ userInput : path ,
102+ resolved,
103+ errors : isUrl ? [ ] : pathErrors ( path , resolved )
104+ } ;
105+ } ) ;
90106
91- const withoutErrors = paths . filter ( item => item . errors . length === 0 ) ;
107+ const withErrors = paths . filter ( item => item . errors . length ) ;
108+ const withoutErrors = paths . filter ( item => item . errors . length === 0 ) ;
92109
93- if ( withErrors . length ) {
94- withErrors . forEach ( item => {
95- logger . unprefixed ( "error" , printErrors ( item . errors ) ) ;
96- } ) ;
97- process . exit ( 1 ) ;
98- } else {
99- const ssPaths = withoutErrors
100- . filter ( item => item . isUrl === false )
101- . map ( item => item . resolved ) ;
110+ if ( withErrors . length ) {
111+ withErrors . forEach ( item => {
112+ logger . unprefixed ( "error" , printErrors ( item . errors ) ) ;
113+ } ) ;
114+ return process . exit ( 1 ) ;
115+ }
116+
117+ const serveStaticPaths = withoutErrors
118+ . filter ( item => item . isUrl === false )
119+ . map ( item => item . resolved ) ;
102120
103121 const urls = withoutErrors
104- . filter ( item => item . isUrl === true )
105- . map ( item => item . userInput ) ;
122+ . filter ( item => item . isUrl === true )
123+ . map ( item => item . userInput ) ;
106124
125+ /**
126+ * If a URL was given, switch to proxy mode and use
127+ * any other paths as serveStatic options
128+ */
107129 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" ] } } ) ;
130+ const proxy = urls [ 0 ] ;
131+ const config = {
132+ ...argv ,
133+ proxy,
134+ serveStatic : serveStaticPaths
135+ } ;
136+ return handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
119137 }
120- }
138+
139+ /**
140+ * if NO urls were given switch directly to server mode
141+ * @type {{server: {baseDir: any}} }
142+ */
143+ const config = {
144+ ...argv ,
145+ server : { baseDir : serveStaticPaths }
146+ } ;
147+ handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
121148}
122149
123150/**
124151 * @param {{cli: object, [whitelist]: array, [cb]: function} } opts
125152 * @returns {* }
126153 */
127154function handleCli ( opts ) {
128- opts . cb = opts . cb || utils . defaultCallback ;
129- return require ( `./cli/command.${ opts . cli . input [ 0 ] } ` ) ( opts ) ;
155+ opts . cb = opts . cb || utils . defaultCallback ;
156+ return require ( `./cli/command.${ opts . cli . input [ 0 ] } ` ) ( opts ) ;
130157}
131158
132159export default handleCli ;
@@ -136,84 +163,87 @@ export default handleCli;
136163 * @param {object } yargs
137164 */
138165function handleIncoming ( command , yargs ) {
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 . _ } } ) ;
166+ let out ;
167+ if ( command === "start" ) {
168+ out = yargs
169+ . usage ( "Usage: $0 start [options]" )
170+ . options ( startOpts )
171+ . example (
172+ "$0 start -s app" ,
173+ "- Use the App directory to serve files"
174+ )
175+ . example ( "$0 start -p www.bbc.co.uk" , "- Proxy an existing website" )
176+ . help ( ) . argv ;
177+ }
178+ if ( command === "init" ) {
179+ out = yargs
180+ . usage ( "Usage: $0 init" )
181+ . example ( "$0 init" )
182+ . help ( ) . argv ;
183+ }
184+ if ( command === "reload" ) {
185+ out = yargs
186+ . usage ( "Usage: $0 reload" )
187+ . options ( reloadOpts )
188+ . example ( "$0 reload" )
189+ . example ( "$0 reload --port 4000" )
190+ . help ( ) . argv ;
191+ }
192+ if ( command === "recipe" ) {
193+ out = yargs
194+ . usage ( "Usage: $0 recipe <recipe-name>" )
195+ . option ( recipeOpts )
196+ . example ( "$0 recipe ls" , "list the recipes" )
197+ . example ( "$0 recipe gulp.sass" , "use the gulp.sass recipe" )
198+ . help ( ) . argv ;
199+ }
200+
201+ if ( out . help ) {
202+ return yargs . showHelp ( ) ;
203+ }
204+
205+ handleCli ( { cli : { flags : out , input : out . _ } } ) ;
176206}
177207
178208function pathErrors ( input , resolved ) {
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- ] ;
209+ if ( ! existsSync ( resolved ) ) {
210+ return [
211+ {
212+ type : BsErrorTypes . PathNotFound ,
213+ level : BsErrorLevels . Fatal ,
214+ errors : [
215+ {
216+ error : new Error ( `Path not found: ${ input } ` ) ,
217+ meta ( ) {
218+ return [
219+ `Your Input: {yellow:${ input } }` ,
220+ `CWD: {yellow:${ process . cwd ( ) } }` ,
221+ `Resolved to: {yellow:${ resolved } }`
222+ ] ;
223+ }
224+ }
225+ ]
193226 }
194- }
195- ]
196- }
197- ] ;
198- }
199- return [ ] ;
227+ ] ;
228+ }
229+ return [ ] ;
200230}
201231
202232function printErrors ( errors ) {
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" )
233+ return errors
234+ . map ( error =>
235+ [
236+ `Error Type: {bold:${ error . type } }` ,
237+ `Error Level: {bold:${ error . level } }` ,
238+ error . errors . map ( item =>
239+ [
240+ `Error Message: ${ item . error . message } ` ,
241+ item . meta ? item . meta ( ) . join ( "\n" ) : ""
242+ ]
243+ . filter ( Boolean )
244+ . join ( "\n" )
245+ )
246+ ] . join ( "\n" )
215247 )
216- ] . join ( "\n" )
217- )
218- . join ( "\n\n" ) ;
248+ . join ( "\n\n" ) ;
219249}
0 commit comments