Navigation Menu

Skip to content

Commit

Permalink
Added --cold, --watch and --poll flags (#11)
Browse files Browse the repository at this point in the history
* Add chokidar options

This adds --watch, --nowatch, and --poll to the options that
micro-dev can accept.

* --watch allows passing directories to be watched, ignoring others
    In case you have only one or a few directories to be watched,
    and churn in others, you can do, e.g. `-w lib -w test`
* --nowatch disables watching entirely
    In case you do not want to watch the file system for changes at
    all, as in #10
* --poll uses chokidar's polling method rather than filesystem events
    In case you are doing local development across an NFS mount or in
    a shared volume from a docker container, using -L will enable
    watching to work anyway.  The -L is used as the short form because
    nodemon uses it for the same purpose, and we can't use -p as that
    is already in use for --port.

* fix travis warnings

* typo fix to actually allow nowatch

* Improved help

* Prevent prettier from breaking help

* Made usage description even shorter

* Use --cold instead of --nowatch

* Error definition added
  • Loading branch information
randallsquared authored and leo committed Aug 3, 2017
1 parent cbd1fcd commit 33ecb09
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
11 changes: 11 additions & 0 deletions bin/micro-dev.js
Expand Up @@ -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'
},
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions 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).
4 changes: 3 additions & 1 deletion 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}`)
}
15 changes: 9 additions & 6 deletions 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 <n>')} 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 <n>')} 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 <dir>')} 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
Expand Down
57 changes: 34 additions & 23 deletions lib/listening.js
Expand Up @@ -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!'))
Expand Down

0 comments on commit 33ecb09

Please sign in to comment.