Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented option to watch other non-elm files and auto-reload on changes #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/elm-live.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ program
// Server
.option('-p, --port [port]', 'The port to bind to.', Math.floor, 8000)
.option('-h, --host [host]', 'Set the host interface to attach the server to.', 'localhost')
.option('-w, --watch [to-watch]', 'Specify additional directories or files to watch for changes. (comma separated list)', v => v.split(','))

// SSL
.option('-S, --ssl [ssl]', 'Start an https server instead of http.', false)
Expand Down
1 change: 1 addition & 0 deletions lib/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const init = program => ({
ssl: parseSsl(program),
startPage: program.startPage || 'index.html',
target: parseOutput(program.args),
toWatch: program.watch || [],
verbose: program.verbose || false
})

Expand Down
4 changes: 2 additions & 2 deletions lib/src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ const watchingDirs = sourceDirs =>
.join('\n ')}
`

const eventNotification = (event, path) => (isBrowser = false) => {
const msg = `You’ve ${event} \`${path}\`. Rebuilding!`
const eventNotification = (event, path, action = 'Rebuilding') => (isBrowser = false) => {
const msg = `You’ve ${event} \`${path}\`. ${action}!`
return isBrowser ? msg
: `${header}
${msg}
Expand Down
26 changes: 26 additions & 0 deletions lib/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ const watch = model => Async((reject, _) => {
// Package file changes may result in changes to the set
// of watched files
watcher.close()
if(additionalWatcher) additionalWatcher.close()
watch(model)
}

function closeAndReject (x) {
watcher.close()
if(additionalWatcher) additionalWatcher.close()
reject(x)
}

Expand All @@ -145,6 +147,30 @@ const watch = model => Async((reject, _) => {
.fork(closeAndReject, watch)
}
)

/**
* This watcher only watches the files and directories passed by the --watch flag.
* These files are watched on its own watcher because changes to them shouldn't trigger an rebuild of the project, only a reload of the page.
*/
const additionalWatcher = model.toWatch.length ?
chokidar.watch(model.toWatch, {
ignoreInitial: true,
followSymlinks: false,
}).on(
'all',
debounce((event, filePath) => {
const relativePath = path.relative(process.cwd(), filePath)
const eventName = eventNameMap[event] || event
const eventMsg = eventNotification(eventName, relativePath, 'Reloading')
model.log(eventMsg())

if (getPropOr(true, 'useServer')())
sendMessage(update(model, { build: { action: model.getAction('reload') } }));
maybeUpdateToWatching(model)
}),
100
) : null

})

module.exports = {
Expand Down