diff --git a/index.js b/index.js index edc7a3f..47a6e5a 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,11 @@ let configFile if (argv.env) process.env.NODE_ENV = argv.env if (argv.config) argv.config = path.resolve(argv.config) +if (argv.watch) { + process.stdin.on('end', () => process.exit(0)) + process.stdin.resume() +} + Promise.resolve() .then(() => { if (argv.watch && !(argv.output || argv.replace || argv.dir)) { diff --git a/test/watch.js b/test/watch.js index b5bce40..4a51a11 100644 --- a/test/watch.js +++ b/test/watch.js @@ -3,11 +3,12 @@ const test = require('ava') const fs = require('fs-extra') const path = require('path') -const { exec } = require('child_process') +const { exec, spawn } = require('child_process') const chokidar = require('chokidar') const ENV = require('./helpers/env.js') const read = require('./helpers/read.js') +const tmp = require('./helpers/tmp.js') // XXX: All the tests in this file are skipped on the CI; too flacky there const testCb = process.env.CI ? test.cb.skip : test.cb @@ -285,3 +286,19 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => { // Timeout: setTimeout(() => t.end('test timeout'), 50000) }) + +testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { + t.plan(1) + + const cp = spawn( + `./bin/postcss test/fixtures/a.css -o ${tmp()} -w --no-map`, + { shell: true } + ) + + cp.on('error', t.end) + cp.on('exit', (code) => { + t.is(code, 0) + t.end() + }) + cp.stdin.end() +})