UNPKG

2.06 kBPlain TextView Raw
1#!/usr/bin/env node
2
3const program = require('commander')
4const supervisor = require('supervisor')
5const path = require('path')
6const os = require('os')
7const clc = require('cli-color')
8
9program.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
21const options = program.opts()
22
23const runFile = path.join(os.tmpdir(), 'reload-' + Math.random().toString().slice(2))
24const serverFile = path.join(__dirname, '../lib/reload-server.js')
25
26if (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.
31if (typeof options.watchDir === 'undefined') {
32 options.watchDir = options.dir
33}
34
35const args = ['-e', options.exts, '-w', options.watchDir, '-q', '--', serverFile, options.port, options.dir, !!options.browser, options.hostname, runFile, options.startPage, options.fallback, options.verbose]
36supervisor.run(args)
37
38console.log('\nReload web server:')
39console.log('listening on port ' + clc.blue.bold(options.port))
40console.log('monitoring dir ' + clc.green.bold(options.dir))