| 
									
										
										
										
											2014-02-05 15:28:23 +01:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							| 
									
										
										
										
											2015-10-30 11:31:59 +00:00
										 |  |  | 'use strict' | 
					
						
							| 
									
										
										
										
											2015-11-15 14:53:25 +00:00
										 |  |  | const s = Date.now() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 22:09:07 +00:00
										 |  |  | const localWebServer = require('../') | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | const commandLineArgs = require('command-line-args') | 
					
						
							|  |  |  | const ansi = require('ansi-escape-sequences') | 
					
						
							|  |  |  | const cliOptions = require('../lib/cli-options') | 
					
						
							|  |  |  | const loadConfig = require('config-master') | 
					
						
							|  |  |  | const path = require('path') | 
					
						
							| 
									
										
										
										
											2014-06-17 00:40:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | const cli = commandLineArgs(cliOptions.definitions) | 
					
						
							|  |  |  | const usage = cli.getUsage(cliOptions.usageData) | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  | const stored = loadConfig('local-web-server') | 
					
						
							|  |  |  | const options = collectOptions() | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  | if (options.misc.help) { | 
					
						
							|  |  |  |   console.log(usage) | 
					
						
							|  |  |  |   process.exit(0) | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  | if (options.misc.config) { | 
					
						
							|  |  |  |   console.log(JSON.stringify(stored, null, '  ')) | 
					
						
							|  |  |  |   process.exit(0) | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | localWebServer({ | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  |   static: { | 
					
						
							|  |  |  |     root: options.server.directory, | 
					
						
							|  |  |  |     options: { | 
					
						
							|  |  |  |       hidden: true | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   serveIndex: { | 
					
						
							|  |  |  |     path: options.server.directory, | 
					
						
							|  |  |  |     options: { | 
					
						
							|  |  |  |       icons: true, | 
					
						
							|  |  |  |       hidden: true | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   log: { format: options.server['log-format'] }, | 
					
						
							|  |  |  |   compress: options.server.compress, | 
					
						
							|  |  |  |   mime: options.server.mime, | 
					
						
							| 
									
										
										
										
											2015-11-15 23:00:17 +00:00
										 |  |  |   forbid: options.server.forbid.map(regexp => RegExp(regexp, "i")), | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  |   proxyRoutes: options.server.proxyRoutes, | 
					
						
							|  |  |  |   spa: options.server.spa, | 
					
						
							|  |  |  |   'no-cache': options.server['no-cache'] | 
					
						
							|  |  |  | }).listen(options.server.port, onServerUp) | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  | function halt (err) { | 
					
						
							| 
									
										
										
										
											2015-11-15 23:00:17 +00:00
										 |  |  |   console.log(ansi.format(`Error: ${err.message}`, 'red')) | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  |   console.log(usage) | 
					
						
							|  |  |  |   process.exit(1) | 
					
						
							| 
									
										
										
										
											2015-11-10 21:50:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-13 11:26:02 +00:00
										 |  |  | function onServerUp () { | 
					
						
							| 
									
										
										
										
											2015-11-15 14:53:25 +00:00
										 |  |  |   const e = Date.now() | 
					
						
							|  |  |  |   const time = `${e-s}ms` | 
					
						
							| 
									
										
										
										
											2015-11-13 11:26:02 +00:00
										 |  |  |   console.error(ansi.format( | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  |     path.resolve(options.server.directory) === process.cwd() | 
					
						
							|  |  |  |       ? `serving at [underline]{http://localhost:${options.server.port}} ${time}` | 
					
						
							|  |  |  |       : `serving [underline]{${options.server.directory}} at [underline]{http://localhost:${options.server.port}} ${time}` | 
					
						
							| 
									
										
										
										
											2015-11-13 11:26:02 +00:00
										 |  |  |   )) | 
					
						
							| 
									
										
										
										
											2015-11-11 17:53:01 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function collectOptions () { | 
					
						
							|  |  |  |   let options = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* parse command line args */ | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     options = cli.parse() | 
					
						
							|  |  |  |   } catch (err) { | 
					
						
							|  |  |  |     halt(err) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const builtIn = { | 
					
						
							|  |  |  |     port: 8000, | 
					
						
							|  |  |  |     root: process.cwd(), // root dir when using multiple static dirs
 | 
					
						
							|  |  |  |     directory: process.cwd(), | 
					
						
							|  |  |  |     proxyRoutes: [], | 
					
						
							| 
									
										
										
										
											2015-11-15 23:00:17 +00:00
										 |  |  |     forbid: [] | 
					
						
							| 
									
										
										
										
											2015-11-15 21:15:25 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* override built-in defaults with stored config and then command line args */ | 
					
						
							|  |  |  |   options.server = Object.assign(builtIn, stored, options.server) | 
					
						
							|  |  |  |   return options | 
					
						
							|  |  |  | } |