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

Add option to watch all files in a directory. #227

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
2 changes: 1 addition & 1 deletion License.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright © 2018 William King, 2018 Alex Korban, 2015-2018 Tomek Wiszniewski, 2016 Brian Dukes
Copyright © 2018 William King, 2018 Alex Korban, 2015-2018 Tomek Wiszniewski, 2016 Brian Dukes, 2020 David Muhr

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ Proxy requests to another server running at `HOST`. Requires `--proxy-prefix` an
#### `-d, --dir=PATH`
The base for static content. Default: `.`.

#### `-w, --watch`
Watches all files in the specified or default directory and triggers a reload if files change.

#### `-s, --start-page=PATH`
A custom html file to serve other than the default `index.html`.

Expand Down
1 change: 1 addition & 0 deletions bin/elm-live.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ program

// Booleans
.option('-u, --pushstate [pushstate]', `Forces the index.html or whatever filename you have passed to the --start-page flag to always be served. Must be used when building with ${chalk.cyan.underline('Browser.application')}.`, false)
.option('-w, --watch [watch]', 'Watches all files in the specified or default directory and triggers a reload if files change.', false)
.option('-H, --hot [hot]', 'Turn on hot module reloading.', false)
.option('-o, --open [open]', 'Open in browser when server starts.', false)
.option('-v, --verbose [verbose]', 'Will log more steps as your server starts up.', 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 @@ -30,6 +30,7 @@ const init = program => ({
build: false,
cwd: process.cwd(),
dir: program.dir || process.cwd(),
watch: program.watch || false,
elm: program.pathToElm || 'elm',
elmArgs: program.args || [],
getAction: key => ({
Expand Down
12 changes: 8 additions & 4 deletions lib/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,22 @@ const websocketWatcher = model => Async((reject, resolve) => {
const watch = model => Async((reject, _) => {
const sources = getSources()

model.log(watchingDirs(sources))

if (model.useServer) {
websocketWatcher(model)
.map(maybeUpdateToWatching)
.fork(model.log, noop)
}

const watcher = chokidar.watch([...sources, ...packageFileNames], {
const watchTargets = model.watch
? [model.dir, ...sources, ...packageFileNames]
: [...sources, ...packageFileNames]

model.log(watchingDirs(watchTargets))

const watcher = chokidar.watch(watchTargets, {
ignoreInitial: true,
followSymlinks: false,
ignored: ['elm-stuff/generated-code/*', /ElmjutsuDumMyM0DuL3.elm/, /\/.#[^/]+\.elm/]
ignored: [new RegExp(model.target), /.git/, /.vscode/, /.idea/, /node_modules/, /elm-stuff/, /ElmjutsuDumMyM0DuL3.elm/, /\/.#[^/]+\.elm/]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these new folders are "personal" to the developer, especially .vscode and .idea. For example, I might be using a different IDE that creates a different temp folder for caching, and the watcher will still listen to those files.
A solution would be check if there's a .gitignore file, read it, and filter the folders

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading .gitignore might lead to other problems, for example when you are generating ignored source files that should still trigger a reload.

We could just force --dir when using --watch or are there use cases where you have to watch the current working directory?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true. Forcing the dist might help if you target another directory other than root, but if you still target the root then the problem will persist.
Maybe we can say "any path (file or folder) that starts with . + /node_modules/ etc, and a flag can be added, like --watchIgnore in case the user wants to ignore additional folders.

})

watcher.on(
Expand Down