11#!/usr/bin/env node
2- var startOpts = require ( "../cli-options/opts.start.json" ) ;
3- var reloadOpts = require ( "../cli-options/opts.reload.json" ) ;
4- var recipeOpts = require ( "../cli-options/opts.recipe.json" ) ;
5- var pkg = require ( "../package.json" ) ;
6- var utils = require ( "./utils" ) ;
7- var resolve = require ( "path" ) . resolve ;
8- var existsSync = require ( "fs" ) . existsSync ;
9- var logger = require ( "./logger" ) . logger ;
10- var compile = require ( "eazy-logger" ) . compile ;
11-
12- var BsErrorLevels = {
13- Fatal : "Fatal"
14- } ;
15-
16- var BsErrorTypes = {
17- PathNotFound : "PathNotFound"
18- } ;
2+ const startOpts = require ( "../cli-options/opts.start.json" ) ;
3+ const reloadOpts = require ( "../cli-options/opts.reload.json" ) ;
4+ const recipeOpts = require ( "../cli-options/opts.recipe.json" ) ;
5+ const pkg = require ( "../package.json" ) ;
6+ import * 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" ;
11+
12+ export enum BsErrorLevels {
13+ Fatal = "Fatal"
14+ }
15+
16+ export enum BsErrorTypes {
17+ PathNotFound = "PathNotFound"
18+ }
1919
2020/**
2121 * Handle cli input
2222 */
2323if ( ! module . parent ) {
24- var yargs = require ( "yargs" )
24+ runFromCli ( ) ;
25+ }
26+
27+ function runFromCli ( ) {
28+ const yargs = require ( "yargs" )
2529 . command ( "start" , "Start the server" )
2630 . command ( "init" , "Create a configuration file" )
2731 . command ( "reload" , "Send a reload event over HTTP protocol" )
2832 . command ( "recipe" , "Generate the files for a recipe" )
29- . version ( function ( ) {
30- return pkg . version ;
31- } )
33+ . version ( ( ) => pkg . version )
3234 . epilogue (
3335 [
3436 "For help running a certain command, type <command> --help" ,
@@ -48,10 +50,10 @@ if (!module.parent) {
4850 ] . join ( "\n" )
4951 ) ;
5052
51- var argv = yargs . argv ;
52- var input = argv . _ ;
53- var command = input [ 0 ] ;
54- var valid = [ "start" , "init" , "reload" , "recipe" ] ;
53+ const argv = yargs . argv ;
54+ const input = argv . _ ;
55+ const command = input [ 0 ] ;
56+ const valid = [ "start" , "init" , "reload" , "recipe" ] ;
5557
5658 if ( argv . help ) {
5759 return yargs . showHelp ( ) ;
@@ -73,51 +75,39 @@ if (!module.parent) {
7375}
7476
7577function handleNoCommand ( argv , input ) {
76- var paths = input . map ( function ( path ) {
77- var resolved = resolve ( path ) ;
78- var isUrl = / ^ h t t p s ? : \/ \/ / . test ( path ) ;
78+ const paths = input . map ( path => {
79+ const resolved = resolve ( path ) ;
80+ const isUrl = / ^ h t t p s ? : \/ \/ / . test ( path ) ;
7981 return {
80- isUrl : isUrl ,
82+ isUrl,
8183 userInput : path ,
82- resolved : resolved ,
84+ resolved,
8385 errors : isUrl ? [ ] : pathErrors ( path , resolved )
8486 } ;
8587 } ) ;
8688
87- var withErrors = paths . filter ( function ( item ) {
88- return item . errors . length ;
89- } ) ;
89+ const withErrors = paths . filter ( item => item . errors . length ) ;
9090
91- var withoutErrors = paths . filter ( function ( item ) {
92- return item . errors . length === 0 ;
93- } ) ;
91+ const withoutErrors = paths . filter ( item => item . errors . length === 0 ) ;
9492
9593 if ( withErrors . length ) {
96- withErrors . forEach ( function ( item ) {
94+ withErrors . forEach ( item => {
9795 logger . unprefixed ( "error" , printErrors ( item . errors ) ) ;
9896 } ) ;
9997 process . exit ( 1 ) ;
10098 } else {
101- var ssPaths = withoutErrors
102- . filter ( function ( item ) {
103- return item . isUrl === false ;
104- } )
105- . map ( function ( item ) {
106- return item . resolved ;
107- } ) ;
99+ const ssPaths = withoutErrors
100+ . filter ( item => item . isUrl === false )
101+ . map ( item => item . resolved ) ;
108102
109- var urls = withoutErrors
110- . filter ( function ( item ) {
111- return item . isUrl === true ;
112- } )
113- . map ( function ( item ) {
114- return item . userInput ;
115- } ) ;
103+ const urls = withoutErrors
104+ . filter ( item => item . isUrl === true )
105+ . map ( item => item . userInput ) ;
116106
117107 if ( urls . length ) {
118- var proxy = urls [ 0 ] ;
108+ const proxy = urls [ 0 ] ;
119109 var config = Object . assign ( { } , argv , {
120- proxy : proxy ,
110+ proxy,
121111 serveStatic : ssPaths
122112 } ) ;
123113 handleCli ( { cli : { flags : config , input : [ "start" ] } } ) ;
@@ -136,17 +126,17 @@ function handleNoCommand(argv, input) {
136126 */
137127function handleCli ( opts ) {
138128 opts . cb = opts . cb || utils . defaultCallback ;
139- return require ( " ./cli/command." + opts . cli . input [ 0 ] ) ( opts ) ;
129+ return require ( ` ./cli/command.${ opts . cli . input [ 0 ] } ` ) ( opts ) ;
140130}
141131
142- module . exports = handleCli ;
132+ export default handleCli ;
143133
144134/**
145135 * @param {string } command
146136 * @param {object } yargs
147137 */
148138function handleIncoming ( command , yargs ) {
149- var out ;
139+ let out ;
150140 if ( command === "start" ) {
151141 out = yargs
152142 . usage ( "Usage: $0 start [options]" )
@@ -196,12 +186,12 @@ function pathErrors(input, resolved) {
196186 level : BsErrorLevels . Fatal ,
197187 errors : [
198188 {
199- error : new Error ( " Path not found: " + input ) ,
200- meta : function ( ) {
189+ error : new Error ( ` Path not found: ${ input } ` ) ,
190+ meta ( ) {
201191 return [
202- " Your Input: {yellow:" + input + "}" ,
203- " CWD: {yellow:" + process . cwd ( ) + "}" ,
204- " Resolved to: {yellow:" + resolved + "}"
192+ ` Your Input: {yellow:${ input } }` ,
193+ ` CWD: {yellow:${ process . cwd ( ) } }` ,
194+ ` Resolved to: {yellow:${ resolved } }`
205195 ] ;
206196 }
207197 }
@@ -214,19 +204,15 @@ function pathErrors(input, resolved) {
214204
215205function printErrors ( errors ) {
216206 return errors
217- . map ( function ( error ) {
218- return [
219- "Error Type: {bold:" + error . type + "}" ,
220- "Error Level: {bold:" + error . level + "}" ,
221- error . errors . map ( function ( item ) {
222- return [
223- "Error Message: " + item . error . message ,
224- item . meta ? item . meta ( ) . join ( "\n" ) : ""
225- ]
226- . filter ( Boolean )
227- . join ( "\n" ) ;
228- } )
229- ] . join ( "\n" ) ;
230- } )
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" ) )
231217 . join ( "\n\n" ) ;
232218}
0 commit comments