babel-watch
Advanced tools
Comparing version 2.0.7 to 2.0.8
@@ -14,4 +14,6 @@ #!/usr/bin/env node | ||
const commander = require('commander'); | ||
const debounce = require('lodash.debounce'); | ||
const RESTART_COMMAND = 'rs'; | ||
const DEBOUNCE_DURATION = 100; //milliseconds | ||
@@ -121,9 +123,12 @@ const program = new commander.Command("babel-watch"); | ||
watcher.close(); | ||
process.exit(1); | ||
killApp(); | ||
process.exit(0); | ||
}); | ||
watcher.on('change', handleChange); | ||
watcher.on('add', handleChange); | ||
watcher.on('unlink', handleChange); | ||
const debouncedHandleChange = debounce(handleChange, DEBOUNCE_DURATION); | ||
watcher.on('change', debouncedHandleChange); | ||
watcher.on('add', debouncedHandleChange); | ||
watcher.on('unlink', debouncedHandleChange); | ||
watcher.on('ready', () => { | ||
@@ -204,19 +209,40 @@ if (!watcherInitialized) { | ||
const currentPipeFilename = pipeFilename; | ||
childApp.on('exit', () => { | ||
let hasRestarted = false; | ||
const restartOnce = () => { | ||
if (hasRestarted) return; | ||
hasRestarted = true; | ||
if (currentPipeFd) { | ||
fs.closeSync(currentPipeFd); // silently close pipe fd | ||
} | ||
if (currentPipeFilename) { | ||
fs.unlinkSync(currentPipeFilename); // silently remove old pipe file | ||
if (pipeFilename) { | ||
fs.unlinkSync(pipeFilename); // silently remove old pipe file | ||
} | ||
pipeFd = undefined; | ||
childApp = undefined; | ||
pipeFilename = undefined; | ||
restartAppInternal(); | ||
}); | ||
}; | ||
childApp.on('exit', restartOnce); | ||
let isRunning = true; | ||
try { | ||
childApp.kill('SIGHUP'); | ||
} catch (error) { | ||
childApp.kill('SIGKILL'); | ||
process.kill(childApp.pid, 0); | ||
} catch (e) { | ||
isRunning = false; | ||
} | ||
pipeFd = undefined; | ||
pipeFilename = undefined; | ||
childApp = undefined; | ||
if (isRunning) { | ||
try { | ||
childApp.kill('SIGHUP'); | ||
} catch (error) { | ||
childApp.kill('SIGKILL'); | ||
} | ||
pipeFd = undefined; | ||
pipeFilename = undefined; | ||
childApp = undefined; | ||
} else { | ||
pipeFd = undefined; | ||
pipeFilename = undefined; | ||
childApp = undefined; | ||
restartOnce(); | ||
} | ||
} | ||
@@ -282,2 +308,3 @@ } | ||
app.on('message', (data) => { | ||
if (!data || data.event !== 'babel-watch-filename') return; | ||
const filename = data.filename; | ||
@@ -293,14 +320,16 @@ if (!program.disableAutowatch) { | ||
const lenBuf = new Buffer(4); | ||
try { | ||
lenBuf.writeUInt32BE(sourceBuf.length, 0); | ||
fs.writeSync(pipeFd, lenBuf, 0, 4); | ||
sourceBuf.length && fs.writeSync(pipeFd, sourceBuf, 0, sourceBuf.length); | ||
if (pipeFd) { | ||
try { | ||
lenBuf.writeUInt32BE(sourceBuf.length, 0); | ||
fs.writeSync(pipeFd, lenBuf, 0, 4); | ||
sourceBuf.length && fs.writeSync(pipeFd, sourceBuf, 0, sourceBuf.length); | ||
lenBuf.writeUInt32BE(mapBuf.length, 0); | ||
fs.writeSync(pipeFd, lenBuf, 0, 4); | ||
mapBuf.length && fs.writeSync(pipeFd, mapBuf, 0, mapBuf.length); | ||
} catch (error) { | ||
// EPIPE means `pipeFd` has been closed. We can ignore this | ||
if (error.code !== 'EPIPE') { | ||
throw error; | ||
lenBuf.writeUInt32BE(mapBuf.length, 0); | ||
fs.writeSync(pipeFd, lenBuf, 0, 4); | ||
mapBuf.length && fs.writeSync(pipeFd, mapBuf, 0, mapBuf.length); | ||
} catch (error) { | ||
// EPIPE means `pipeFd` has been closed. We can ignore this | ||
if (error.code !== 'EPIPE') { | ||
throw error; | ||
} | ||
} | ||
@@ -312,2 +341,3 @@ } | ||
app.send({ | ||
event: 'babel-watch-start', | ||
pipe: pipeFilename, | ||
@@ -314,0 +344,0 @@ args: program.args, |
{ | ||
"name": "babel-watch", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"description": "Reload your babel-node app on JS source file changes. And do it *fast*.", | ||
"main": "babel-watch.js", | ||
"scripts": { | ||
@@ -31,2 +32,3 @@ "test": "echo \"Error: no test specified\" && exit 1" | ||
"commander": "^2.9.0", | ||
"lodash.debounce": "^4.0.8", | ||
"source-map-support": "^0.4.0" | ||
@@ -33,0 +35,0 @@ }, |
@@ -44,2 +44,3 @@ 'use strict'; | ||
process.send({ | ||
event: 'babel-watch-filename', | ||
filename: filename, | ||
@@ -89,2 +90,3 @@ }); | ||
process.on('message', (options) => { | ||
if (!options || options.event !== 'babel-watch-start') return; | ||
replaceExtensionHooks(options.transpileExtensions); | ||
@@ -91,0 +93,0 @@ sourceMapSupport.install({ |
30402
548
5
15
+ Addedlodash.debounce@^4.0.8
+ Addedlodash.debounce@4.0.8(transitive)