diff --git a/bin/micro-dev.js b/bin/micro-dev.js index 60a664c..7bbebd9 100755 --- a/bin/micro-dev.js +++ b/bin/micro-dev.js @@ -21,6 +21,9 @@ const flags = mri(process.argv.slice(2), { alias: { p: 'port', H: 'host', + c: 'cold', + w: 'watch', + L: 'poll', h: 'help', v: 'version' }, @@ -45,6 +48,14 @@ if (flags.version) { process.exit() } +if (flags.cold && (flags.watch || flags.poll)) { + logError( + 'The --cold flag is not compatible with --watch or --poll!', + 'watch-flags' + ) + process.exit(1) +} + let file = flags._[0] if (!file) { diff --git a/errors/watch-flags.md b/errors/watch-flags.md new file mode 100644 index 0000000..60f9e46 --- /dev/null +++ b/errors/watch-flags.md @@ -0,0 +1,9 @@ +# Too Many Watch Flags + +#### Why This Error Occurred + +When you ran the `micro-dev` command with the `--cold` flag (which disables hot reloading), you've defined more flags that are also related to hot reloading. This is not allowed! + +#### Possible Ways to Fix It + +When using `--cold`, you cannot use `--watch` or `--poll`. To use one of the latter two flags, you need to enable hot reloading (leave the `--cold` flag away). diff --git a/lib/error.js b/lib/error.js index d054627..f9ccd33 100644 --- a/lib/error.js +++ b/lib/error.js @@ -1,4 +1,6 @@ module.exports = (message, errorCode) => { + const repo = errorCode === 'watch-flags' ? 'micro-dev' : 'micro' + console.error(message) - console.error(`Read more here: https://err.sh/micro/${errorCode}`) + console.error(`Read more here: https://err.sh/${repo}/${errorCode}`) } diff --git a/lib/help.js b/lib/help.js index 8acaae0..276e0fa 100644 --- a/lib/help.js +++ b/lib/help.js @@ -1,15 +1,18 @@ // Packages -const { green } = require('chalk') +const { green: g } = require('chalk') module.exports = () => { - const usage = `\n Usage: ${green('micro-dev')} [path] [options] + const usage = `\n Usage: ${g('micro-dev')} [path] [options] Options: - ${green('-p, --port ')} Port to listen on (defaults to 3000) - ${green('-H, --host')} The host on which micro will run - ${green('-v, --version')} Output the version number - ${green('-h, --help')} Show this usage information + ${g('-p, --port ')} Port to listen on (defaults to 3000) + ${g('-H, --host')} The host on which micro will run + ${g('-c, --cold')} Disable hot reloading + ${g('-w, --watch ')} A directory to watch in addition to [path] + ${g('-L, --poll')} Poll for code changes rather than using events + ${g('-v, --version')} Output the version number + ${g('-h, --help')} Show this usage information ` return usage diff --git a/lib/listening.js b/lib/listening.js index 0d96693..d691679 100644 --- a/lib/listening.js +++ b/lib/listening.js @@ -70,36 +70,47 @@ module.exports = async (server, inUse, flags, sockets) => { const { isTTY } = process.stdout const file = flags._[0] - const watchConfig = { - ignoreInitial: true, - ignored: /.git|node_modules|.nyc_output|.sass-cache|coverage/ - } + if (!flags.cold) { + let toWatch = flags.watch || false - const ignoredExtensions = ['swp'] + const watchConfig = { + usePolling: flags.poll, + ignoreInitial: true, + ignored: /.git|node_modules|.nyc_output|.sass-cache|coverage/ + } - // Find out which directory to watch - const closestPkg = await pkgUp(path.dirname(file)) - const toWatch = closestPkg ? path.dirname(closestPkg) : process.cwd() + const ignoredExtensions = ['swp'] - // Start watching the project files - const watcher = watch(toWatch, watchConfig) + if (Array.isArray(toWatch)) { + toWatch.push(file) + } else if (toWatch) { + toWatch = [toWatch, file] + } else { + // Find out which directory to watch + const closestPkg = await pkgUp(path.dirname(file)) + toWatch = [closestPkg ? path.dirname(closestPkg) : process.cwd()] + } - // Ensure that the server gets restarted if a file changes - watcher.on('all', (event, filePath) => { - const location = path.relative(process.cwd(), filePath) - const extension = path.extname(location).split('.')[1] + // Start watching the project files + const watcher = watch(toWatch, watchConfig) - if (ignoredExtensions.includes(extension)) { - return - } + // Ensure that the server gets restarted if a file changes + watcher.on('all', (event, filePath) => { + const location = path.relative(process.cwd(), filePath) + const extension = path.extname(location).split('.')[1] - console.log( - `\n${chalk.blue('File changed:')} ${location} - Restarting server...` - ) + if (ignoredExtensions.includes(extension)) { + return + } - destroySockets(sockets) - server.close(restartServer.bind(this, file, flags, watcher)) - }) + console.log( + `\n${chalk.blue('File changed:')} ${location} - Restarting server...` + ) + + destroySockets(sockets) + server.close(restartServer.bind(this, file, flags, watcher)) + }) + } if (flags.restarted) { console.log(chalk.green('Restarted!'))