1 | #!/usr/bin/env node
|
2 |
|
3 | const program = require('commander')
|
4 | const supervisor = require('supervisor')
|
5 | const path = require('path')
|
6 | const os = require('os')
|
7 | const clc = require('cli-color')
|
8 |
|
9 | program.version(require('../package.json').version)
|
10 | .option('-b, --browser', 'Open in the browser automatically.')
|
11 | .option('-n, --hostname [hostname]', 'If -b flag is being used, this allows for custom hostnames. Defaults to localhost.', 'localhost')
|
12 | .option('-d, --dir [dir]', 'The directory to serve up. Defaults to current dir.', process.cwd())
|
13 | .option('-w, --watch-dir [watch-dir]', 'The directory to watch. Defaults the serving directory.')
|
14 | .option('-e, --exts [extensions]', 'Extensions separated by commas or pipes. Defaults to html,js,css.', 'html|js|css')
|
15 | .option('-p, --port [port]', 'The port to bind to. Can be set with PORT env variable as well. Defaults to 8080', '8080')
|
16 | .option('-s, --start-page [start-page]', 'Specify a start page. Defaults to index.html', 'index.html')
|
17 | .option('-f, --fallback [fallback]', 'Fallback to the start page when route is not found')
|
18 | .option('-v, --verbose [verbose]', 'Turning on logging on the server and client side. Defaults to false', false)
|
19 | .parse(process.argv)
|
20 |
|
21 | const options = program.opts()
|
22 |
|
23 | const runFile = path.join(os.tmpdir(), 'reload-' + Math.random().toString().slice(2))
|
24 | const serverFile = path.join(__dirname, '../lib/reload-server.js')
|
25 |
|
26 | if (options.exts.indexOf(',')) {
|
27 | options.exts = options.exts.replace(/,/g, '|') // replace comma for pipe, that's what supervisor likes
|
28 | }
|
29 |
|
30 | // Fall back to the serving directory.
|
31 | if (typeof options.watchDir === 'undefined') {
|
32 | options.watchDir = options.dir
|
33 | }
|
34 |
|
35 | const args = ['-e', options.exts, '-w', options.watchDir, '-q', '--', serverFile, options.port, options.dir, !!options.browser, options.hostname, runFile, options.startPage, options.fallback, options.verbose]
|
36 | supervisor.run(args)
|
37 |
|
38 | console.log('\nReload web server:')
|
39 | console.log('listening on port ' + clc.blue.bold(options.port))
|
40 | console.log('monitoring dir ' + clc.green.bold(options.dir))
|