2017-04-13 15:53:35 +01:00
/ * *
* @ file
*
* Watch changes to * . es6 . js files and compile them to ES5 during development .
*
* @ internal This file is part of the core javascript build process and is only
* meant to be used in that context .
* /
'use strict' ;
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
const chokidar = require ( 'chokidar' ) ;
2018-11-23 12:29:20 +00:00
const changeOrAdded = require ( './changeOrAdded' ) ;
const log = require ( './log' ) ;
2017-04-13 15:53:35 +01:00
2018-11-23 12:29:20 +00:00
// Match only on .es6.js files.
2017-04-13 15:53:35 +01:00
const fileMatch = './**/*.es6.js' ;
2018-11-23 12:29:20 +00:00
// Ignore everything in node_modules
2017-04-13 15:53:35 +01:00
const watcher = chokidar . watch ( fileMatch , {
ignoreInitial : true ,
2018-11-23 12:29:20 +00:00
ignored : './node_modules/**'
2017-04-13 15:53:35 +01:00
} ) ;
const unlinkHandler = ( err ) => {
if ( err ) {
log ( err ) ;
}
} ;
2018-11-23 12:29:20 +00:00
// Watch for filesystem changes.
2017-04-13 15:53:35 +01:00
watcher
2018-11-23 12:29:20 +00:00
. on ( 'add' , changeOrAdded )
. on ( 'change' , changeOrAdded )
2017-04-13 15:53:35 +01:00
. on ( 'unlink' , ( filePath ) => {
const fileName = filePath . slice ( 0 , - 7 ) ;
fs . stat ( ` ${ fileName } .js ` , ( ) => {
fs . unlink ( ` ${ fileName } .js ` , unlinkHandler ) ;
} ) ;
} )
. on ( 'ready' , ( ) => log ( ` Watching ' ${ fileMatch } ' for changes. ` ) ) ;