Skip to content

Commit 144cd35

Browse files
committed
Abort sass if stdin is closed when watching
1 parent e4f38a7 commit 144cd35

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

bin/sass.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Future<void> main(List<String> args) async {
5454
var graph = StylesheetGraph(
5555
ImportCache(loadPaths: options.loadPaths, logger: options.logger));
5656
if (options.watch) {
57+
ensureWatchWillExit();
5758
await watch(options, graph);
5859
return;
5960
}

lib/src/io/interface.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ String? getEnvironmentVariable(String name) => throw '';
9494
int get exitCode => throw '';
9595
set exitCode(int value) => throw '';
9696

97+
/// Attaches a listener to exit when stdin closes.
98+
///
99+
/// The listener is *not* attached when stdin is a TTY because it would
100+
/// interfere with the Unix background job system. If we read from stdin and
101+
/// then Ctrl+Z to move the process to the background, it will incorrectly
102+
/// cause the job to stop. See: https://github.com/brunch/brunch/issues/998.
103+
void ensureWatchWillExit() => throw '';
104+
97105
/// Recursively watches the directory at [path] for modifications.
98106
///
99107
/// Returns a future that completes with a single-subscription stream once the

lib/src/io/node.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ final stderr = Stderr(process.stderr);
196196
@JS('process.stdout.isTTY')
197197
external bool? get isTTY;
198198

199+
@JS('process.stdin.isTTY')
200+
external bool? get isStdinTTY;
201+
199202
bool get hasTerminal => isTTY == true;
200203

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

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

219+
void ensureWatchWillExit() {
220+
if (isStdinTTY == true) {
221+
process.stdin.on('end', allowInterop(() => process.exit(0)));
222+
}
223+
}
224+
216225
Future<Stream<WatchEvent>> watchDir(String path, {bool poll = false}) {
217226
var watcher = chokidar.watch(
218227
path, ChokidarOptions(disableGlobbing: true, usePolling: poll));

lib/src/io/vm.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ DateTime modificationTime(String path) {
8787

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

90+
void ensureWatchWillExit() {
91+
if (!io.stdin.hasTerminal) io.stdin.listen(null, onDone: () => io.exit(0));
92+
}
93+
9094
Future<Stream<WatchEvent>> watchDir(String path, {bool poll = false}) async {
9195
var watcher = poll ? PollingDirectoryWatcher(path) : DirectoryWatcher(path);
9296

0 commit comments

Comments
 (0)