Skip to content

Instantly share code, notes, and snippets.

@yuanliwei
Created November 20, 2018 16:16
Show Gist options
  • Save yuanliwei/94d30cb33aab2cc49481a089ea243029 to your computer and use it in GitHub Desktop.
Save yuanliwei/94d30cb33aab2cc49481a089ea243029 to your computer and use it in GitHub Desktop.
node watch file change.js
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
var ignores = '.git,doc,tool'.split(',')
var watchDir = '.'
function onFileUpdate() {
console.log('onFileUpdate....................');
// exec(`adb push `)
}
var watchMap = {}
var timer = 0
function watch(dir) {
if (!fs.existsSync(dir)) { return }
if (watchMap[dir]) { return }
if (!fs.statSync(dir).isDirectory()) { return }
watchMap[dir] = true
fs.watch(dir, (event, filename)=> {
if (ignores.includes(filename)) return
if (event == 'rename') { watch(path.join(dir, filename)) }
console.log('event: ' + event + ' filename:' + filename + ' time:' + Date.now());
clearTimeout(timer)
timer = setTimeout(()=>{onFileUpdate()}, 800)
})
fs.readdirSync(dir).forEach((file)=>{
if (ignores.includes(file)) return
watch(path.join(dir, file));
})
}
watch(watchDir)
console.log('start watch...');
onFileUpdate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment