Skip to content

Commit

Permalink
Abort sass if stdin is closed when watching
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumm committed Aug 4, 2021
1 parent e4f38a7 commit 144cd35
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/sass.dart
Expand Up @@ -54,6 +54,7 @@ Future<void> main(List<String> args) async {
var graph = StylesheetGraph(
ImportCache(loadPaths: options.loadPaths, logger: options.logger));
if (options.watch) {
ensureWatchWillExit();
await watch(options, graph);
return;
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/io/interface.dart
Expand Up @@ -94,6 +94,14 @@ String? getEnvironmentVariable(String name) => throw '';
int get exitCode => throw '';
set exitCode(int value) => throw '';

/// Attaches a listener to exit when stdin closes.
///
/// The listener is *not* attached when stdin is a TTY because it would
/// interfere with the Unix background job system. If we read from stdin and
/// then Ctrl+Z to move the process to the background, it will incorrectly
/// cause the job to stop. See: https://github.com/brunch/brunch/issues/998.
void ensureWatchWillExit() => throw '';

/// Recursively watches the directory at [path] for modifications.
///
/// Returns a future that completes with a single-subscription stream once the
Expand Down
9 changes: 9 additions & 0 deletions lib/src/io/node.dart
Expand Up @@ -196,6 +196,9 @@ final stderr = Stderr(process.stderr);
@JS('process.stdout.isTTY')
external bool? get isTTY;

@JS('process.stdin.isTTY')
external bool? get isStdinTTY;

bool get hasTerminal => isTTY == true;

bool get isWindows => process.platform == 'win32';
Expand All @@ -213,6 +216,12 @@ int get exitCode => process.exitCode;

set exitCode(int code) => process.exitCode = code;

void ensureWatchWillExit() {
if (isStdinTTY == true) {
process.stdin.on('end', allowInterop(() => process.exit(0)));
}
}

Future<Stream<WatchEvent>> watchDir(String path, {bool poll = false}) {
var watcher = chokidar.watch(
path, ChokidarOptions(disableGlobbing: true, usePolling: poll));
Expand Down
4 changes: 4 additions & 0 deletions lib/src/io/vm.dart
Expand Up @@ -87,6 +87,10 @@ DateTime modificationTime(String path) {

String? getEnvironmentVariable(String name) => io.Platform.environment[name];

void ensureWatchWillExit() {
if (!io.stdin.hasTerminal) io.stdin.listen(null, onDone: () => io.exit(0));
}

Future<Stream<WatchEvent>> watchDir(String path, {bool poll = false}) async {
var watcher = poll ? PollingDirectoryWatcher(path) : DirectoryWatcher(path);

Expand Down

0 comments on commit 144cd35

Please sign in to comment.