@@ -299,3 +299,198 @@ testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {
299
299
} )
300
300
cp . stdin . end ( )
301
301
} )
302
+
303
+ testCb ( '--watch watches dependencies' , ( t ) => {
304
+ let cp
305
+
306
+ t . plan ( 2 )
307
+
308
+ ENV ( '' , [ 's.css' , 'a.css' , 'b.css' ] ) . then ( ( dir ) => {
309
+ fs . writeFile (
310
+ path . join ( dir , 'postcss.config.js' ) ,
311
+ `
312
+ const fs = require('fs')
313
+ module.exports = {
314
+ plugins: [
315
+ (root, result) => {
316
+ const file = '${ path . resolve ( dir , 'a.css' ) } '
317
+ result.messages.push({
318
+ plugin: 'test',
319
+ type: 'dependency',
320
+ file,
321
+ parent: result.opts.from,
322
+ })
323
+ root.nodes = []
324
+ root.append(fs.readFileSync(file, 'utf8'))
325
+ return root
326
+ }
327
+ ]
328
+ }
329
+ `
330
+ )
331
+ . then ( ( ) => {
332
+ // Init watcher:
333
+ const watcher = chokidar . watch ( '.' , {
334
+ cwd : dir ,
335
+ ignoreInitial : true ,
336
+ awaitWriteFinish : true ,
337
+ } )
338
+
339
+ // On the first output:
340
+ watcher . on ( 'add' , ( p ) => {
341
+ // Assert, then change the source file
342
+ if ( p === 'output.css' ) {
343
+ isEqual ( p , 'test/fixtures/a.css' )
344
+ . then ( ( ) => read ( 'test/fixtures/b.css' ) )
345
+ . then ( ( css ) => fs . writeFile ( path . join ( dir , 'a.css' ) , css ) )
346
+ . catch ( done )
347
+ }
348
+ } )
349
+
350
+ // When the change is picked up:
351
+ watcher . on ( 'change' , ( p ) => {
352
+ if ( p === 'output.css' ) {
353
+ isEqual ( p , 'test/fixtures/b.css' )
354
+ . then ( ( ) => done ( ) )
355
+ . catch ( done )
356
+ }
357
+ } )
358
+
359
+ // Start postcss-cli:
360
+ watcher . on ( 'ready' , ( ) => {
361
+ // Using exec() and quoting "*.css" to test watch's glob handling:
362
+ cp = exec (
363
+ `node ${ path . resolve (
364
+ 'bin/postcss'
365
+ ) } "s.css" -o output.css --no-map -w`,
366
+ { cwd : dir }
367
+ )
368
+ cp . on ( 'error' , t . end )
369
+ cp . on ( 'exit' , ( code ) => {
370
+ if ( code ) t . end ( code )
371
+ } )
372
+ } )
373
+
374
+ // Helper functions:
375
+ function isEqual ( p , expected ) {
376
+ return Promise . all ( [ read ( path . join ( dir , p ) ) , read ( expected ) ] ) . then (
377
+ ( [ a , e ] ) => t . is ( a , e )
378
+ )
379
+ }
380
+
381
+ function done ( err ) {
382
+ try {
383
+ cp . kill ( )
384
+ } catch { }
385
+
386
+ t . end ( err )
387
+ }
388
+ } )
389
+ . catch ( t . end )
390
+ } )
391
+
392
+ // Timeout:
393
+ setTimeout ( ( ) => t . end ( 'test timeout' ) , 50000 )
394
+ } )
395
+
396
+ testCb ( '--watch watches directory dependencies' , ( t ) => {
397
+ let cp
398
+
399
+ t . plan ( 2 )
400
+
401
+ ENV ( '' , [ 's.css' , 'base/level-1/b.css' , 'base/level-1/level-2/a.css' ] ) . then (
402
+ ( dir ) => {
403
+ fs . writeFile (
404
+ path . join ( dir , 'postcss.config.js' ) ,
405
+ `
406
+ const fs = require('fs')
407
+ module.exports = {
408
+ plugins: [
409
+ (root, result) => {
410
+ result.messages.push({
411
+ plugin: 'test',
412
+ type: 'dir-dependency',
413
+ dir: '${ path . resolve ( dir , 'base' ) } ',
414
+ parent: result.opts.from,
415
+ })
416
+ root.nodes = []
417
+ root.append(fs.readFileSync('${ path . resolve (
418
+ dir ,
419
+ 'base/level-1/level-2/a.css'
420
+ ) } ', 'utf8'))
421
+ return root
422
+ }
423
+ ]
424
+ }
425
+ `
426
+ )
427
+ . then ( ( ) => {
428
+ // Init watcher:
429
+ const watcher = chokidar . watch ( '.' , {
430
+ cwd : dir ,
431
+ ignoreInitial : true ,
432
+ awaitWriteFinish : true ,
433
+ } )
434
+
435
+ // On the first output:
436
+ watcher . on ( 'add' , ( p ) => {
437
+ // Assert, then change the source file
438
+ if ( p === 'output.css' ) {
439
+ isEqual ( p , 'test/fixtures/base/level-1/level-2/a.css' )
440
+ . then ( ( ) => read ( 'test/fixtures/base/level-1/b.css' ) )
441
+ . then ( ( css ) =>
442
+ fs . writeFile (
443
+ path . join ( dir , 'base/level-1/level-2/a.css' ) ,
444
+ css
445
+ )
446
+ )
447
+ . catch ( done )
448
+ }
449
+ } )
450
+
451
+ // When the change is picked up:
452
+ watcher . on ( 'change' , ( p ) => {
453
+ if ( p === 'output.css' ) {
454
+ isEqual ( p , 'test/fixtures/base/level-1/b.css' )
455
+ . then ( ( ) => done ( ) )
456
+ . catch ( done )
457
+ }
458
+ } )
459
+
460
+ // Start postcss-cli:
461
+ watcher . on ( 'ready' , ( ) => {
462
+ // Using exec() and quoting "*.css" to test watch's glob handling:
463
+ cp = exec (
464
+ `node ${ path . resolve (
465
+ 'bin/postcss'
466
+ ) } "s.css" -o output.css --no-map -w`,
467
+ { cwd : dir }
468
+ )
469
+ cp . on ( 'error' , t . end )
470
+ cp . on ( 'exit' , ( code ) => {
471
+ if ( code ) t . end ( code )
472
+ } )
473
+ } )
474
+
475
+ // Helper functions:
476
+ function isEqual ( p , expected ) {
477
+ return Promise . all ( [ read ( path . join ( dir , p ) ) , read ( expected ) ] ) . then (
478
+ ( [ a , e ] ) => t . is ( a , e )
479
+ )
480
+ }
481
+
482
+ function done ( err ) {
483
+ try {
484
+ cp . kill ( )
485
+ } catch { }
486
+
487
+ t . end ( err )
488
+ }
489
+ } )
490
+ . catch ( t . end )
491
+ }
492
+ )
493
+
494
+ // Timeout:
495
+ setTimeout ( ( ) => t . end ( 'test timeout' ) , 50000 )
496
+ } )
0 commit comments