Skip to content

Commit 4253057

Browse files
authored
Add a --poll option (#390)
Partially addresses #264.
1 parent cd0211c commit 4253057

File tree

10 files changed

+461
-363
lines changed

10 files changed

+461
-363
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.8.0
2+
3+
### Command-Line Interface
4+
5+
* Add a `--poll` flag to make `--watch` mode repeatedly check the filesystem for
6+
updates rather than relying on native filesystem notifications.
7+
18
## 1.7.3
29

310
* No user-visible changes.

lib/src/executable/options.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class ExecutableOptions {
7575
..addFlag('watch',
7676
help: 'Watch stylesheets and recompile when they change.',
7777
negatable: false)
78+
..addFlag('poll',
79+
help: 'Manually check for changes rather than using a native '
80+
'watcher.\n'
81+
'Only valid with --watch.')
7882
..addFlag('interactive',
7983
abbr: 'i',
8084
help: 'Run an interactive SassScript shell.',
@@ -170,6 +174,9 @@ class ExecutableOptions {
170174
/// Whether to continuously watch the filesystem for changes.
171175
bool get watch => _options['watch'] as bool;
172176

177+
/// Whether to manually poll for changes when watching.
178+
bool get poll => _options['poll'] as bool;
179+
173180
/// A map from source paths to the destination paths where the compiled CSS
174181
/// should be written.
175182
///
@@ -380,7 +387,11 @@ class ExecutableOptions {
380387
}
381388
}
382389

383-
ExecutableOptions._(this._options);
390+
ExecutableOptions._(this._options) {
391+
if (_options.wasParsed('poll') && !watch) {
392+
_fail("--poll may not be passed without --watch.");
393+
}
394+
}
384395

385396
/// Makes [url] absolute or relative (to the directory containing
386397
/// [destination]) according to the `source-map-urls` option.

lib/src/executable/watch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Future watch(ExecutableOptions options, StylesheetGraph graph) async {
2525
..addAll(options.sourcesToDestinations.keys.map(p.dirname))
2626
..addAll(options.loadPaths);
2727

28-
var dirWatcher = new MultiDirWatcher();
28+
var dirWatcher = new MultiDirWatcher(poll: options.poll);
2929
await Future.wait(directoriesToWatch.map((dir) {
3030
// If a directory doesn't exist, watch its parent directory so that we're
3131
// notified once it starts existing.

lib/src/io/interface.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,7 @@ int exitCode;
8787
/// Returns a future that completes with a single-subscription stream once the
8888
/// directory has been scanned initially. The watch is canceled when the stream
8989
/// is closed.
90-
Future<Stream<WatchEvent>> watchDir(String path) => null;
90+
///
91+
/// If [poll] is `true`, this manually checks the filesystem for changes
92+
/// periodically rather than using a native filesystem monitoring API.
93+
Future<Stream<WatchEvent>> watchDir(String path, {bool poll: false}) => null;

lib/src/io/node.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ external int get exitCode;
239239
@JS("process.exitCode")
240240
external set exitCode(int code);
241241

242-
Future<Stream<WatchEvent>> watchDir(String path) {
243-
var watcher =
244-
chokidar.watch(path, new ChokidarOptions(disableGlobbing: true));
242+
Future<Stream<WatchEvent>> watchDir(String path, {bool poll: false}) {
243+
var watcher = chokidar.watch(
244+
path, new ChokidarOptions(disableGlobbing: true, usePolling: poll));
245245

246246
// Don't assign the controller until after the ready event fires. Otherwise,
247247
// Chokidar will give us a bunch of add events for files that already exist.

lib/src/io/vm.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ DateTime modificationTime(String path) {
7474
return stat.modified;
7575
}
7676

77-
Future<Stream<WatchEvent>> watchDir(String path) async {
78-
var watcher = new DirectoryWatcher(path);
77+
Future<Stream<WatchEvent>> watchDir(String path, {bool poll: false}) async {
78+
var watcher =
79+
poll ? new PollingDirectoryWatcher(path) : new DirectoryWatcher(path);
7980

8081
// Wrap [stream] in a [SubscriptionStream] so that its `onListen` event
8182
// triggers but the caller can still listen at their leisure.

lib/src/node/chokidar.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class Chokidar {
1616
@anonymous
1717
class ChokidarOptions {
1818
external bool get disableGlobbing;
19+
external bool get usePolling;
1920

20-
external factory ChokidarOptions({bool disableGlobbing});
21+
external factory ChokidarOptions({bool disableGlobbing, bool usePolling});
2122
}
2223

2324
@JS()

lib/src/util/multi_dir_watcher.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ class MultiDirWatcher {
2424
Stream<WatchEvent> get events => _group.stream;
2525
final _group = new StreamGroup<WatchEvent>();
2626

27+
/// Whether to manually check the filesystem for changes periodically.
28+
final bool _poll;
29+
30+
/// Creates a [MultiDirWatcher].
31+
///
32+
/// If [poll] is `true`, this manually checks the filesystem for changes
33+
/// periodically rather than using a native filesystem monitoring API.
34+
MultiDirWatcher({bool poll: false}) : _poll = poll;
35+
2736
/// Watches [directory] for changes.
2837
///
2938
/// Returns a [Future] that completes when [events] is ready to emit events
@@ -43,7 +52,7 @@ class MultiDirWatcher {
4352
}
4453
}
4554

46-
var future = watchDir(directory);
55+
var future = watchDir(directory, poll: _poll);
4756
var stream = StreamCompleter.fromFuture(future);
4857
_watchers[directory] = stream;
4958
_group.add(stream);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.7.3
2+
version: 1.8.0-dev
33
description: A Sass implementation in Dart.
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/sass/dart-sass

0 commit comments

Comments
 (0)