Created
November 20, 2018 16:16
-
-
Save yuanliwei/94d30cb33aab2cc49481a089ea243029 to your computer and use it in GitHub Desktop.
node watch file change.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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