Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
feat: be possible disable watch
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 21, 2020
1 parent a42298b commit bda15a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
10 changes: 8 additions & 2 deletions bin/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ const cli = require('meow')(require('./help'), {
}
}
})

const watchFiles = watch => {
if (watch[0] === false) return false
return Array.isArray(watch) ? watch : [watch]
}

;(async () => {
const { input } = cli
const { cwd, port, watch: watchFiles, ...opts } = cli.flags
const { cwd, port, watch, ...opts } = cli.flags
const { filename, pkg } = getMainFile({ input, cwd })
const filepath = path.resolve(cwd, filename)

Expand All @@ -57,7 +63,7 @@ const cli = require('meow')(require('./help'), {
pkg,
cwd,
port,
watchFiles: Array.isArray(watchFiles) ? watchFiles : [watchFiles],
watchFiles: watchFiles(watch),
...opts
})
})()
31 changes: 13 additions & 18 deletions bin/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const isRestartSignal = str =>
.trim()
.toLowerCase() === RESTART_SIGNAL

let firsTime = false

const doRestart = ({ sockets, server, filename, forcing, ...opts }) => {
const spinner = logRestart({ filename, forcing })
destroySockets(sockets)
Expand All @@ -41,9 +39,6 @@ module.exports = ({ cwd, pkg, watchFiles, ...opts }) => {
pkg
})

const toWatch = [].concat(watchFiles, cwd)
const watcher = watch(toWatch, watchConfig)

const restart = ({ forcing, filename }) =>
doRestart({
...opts,
Expand All @@ -54,19 +49,19 @@ module.exports = ({ cwd, pkg, watchFiles, ...opts }) => {
forcing
})

watcher.once(
'all',
debounce(
(event, filename) => restart({ forcing: false, filename: path.relative(cwd, filename) }),
10
if (watchFiles) {
const toWatch = [].concat(watchFiles, cwd)
const watcher = watch(toWatch, watchConfig)
watcher.once(
'all',
debounce(
(event, filename) => restart({ forcing: false, filename: path.relative(cwd, filename) }),
10
)
)
)

if (!firsTime) {
firsTime = true

process.stdin.on('data', data => {
if (isRestartSignal(data)) restart({ forcing: true })
})
}

process.stdin.once('data', data => {
if (isRestartSignal(data)) restart({ forcing: true })
})
}

0 comments on commit bda15a3

Please sign in to comment.