Code files
This commit is contained in:
@@ -0,0 +1 @@
|
||||
back again
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "watching-files-and-directories",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "watcher.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"human-time": "0.0.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const human = require('human-time')
|
||||
|
||||
const interval = 5007
|
||||
const file = process.argv[2]
|
||||
let exists = false
|
||||
|
||||
if (!file) {
|
||||
console.error('supply a file')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const created = ({birthtime}) =>
|
||||
!exists && (Date.now() - birthtime) < interval
|
||||
|
||||
const missing = ({birthtime, mtime, atime, ctime}) =>
|
||||
!(birthtime|mtime|atime|ctime)
|
||||
|
||||
const updated = (cur, prv) => cur.mtime !== prv.mtime
|
||||
|
||||
fs.watchFile(file, {interval}, (cur, prv) => {
|
||||
if (missing(cur)) {
|
||||
const msg = exists ? 'removed' : 'doesn\'t exist'
|
||||
exists = false
|
||||
return console.log(`${file} ${msg}`)
|
||||
}
|
||||
|
||||
if (created(cur)) {
|
||||
exists = true
|
||||
return console.log(`${file} created ${human((cur.birthtime))}`)
|
||||
}
|
||||
|
||||
exists = true
|
||||
|
||||
if (updated(cur, prv)) {
|
||||
return console.log(`${file} updated ${human((cur.mtime))}`)
|
||||
}
|
||||
|
||||
console.log(`${file} modified ${human((cur.mtime))}`)
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "watching-with-chokidar",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"chokidar": "^1.6.0",
|
||||
"human-time": "0.0.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
'use strict'
|
||||
|
||||
const chokidar = require('chokidar')
|
||||
const human = require('human-time')
|
||||
const watcher = chokidar.watch(process.argv[2] || '.', {
|
||||
alwaysStat: true
|
||||
})
|
||||
|
||||
watcher.on('ready', () => {
|
||||
watcher
|
||||
.on('add', (file, stat) => {
|
||||
console.log(`${file} created ${human((stat.birthtime))}`)
|
||||
})
|
||||
.on('unlink', (file) => {
|
||||
console.log(`${file} removed`)
|
||||
})
|
||||
.on('change', (file, stat) => {
|
||||
const msg = (+stat.ctime === +stat.mtime) ? 'updated' : 'modified'
|
||||
console.log(`${file} ${msg} ${human((stat.ctime))}`)
|
||||
})
|
||||
.on('addDir', (dir, stat) => {
|
||||
console.log(`${dir} folder created ${human((stat.birthtime))}`)
|
||||
})
|
||||
.on('unlinkDir', (dir) => {
|
||||
console.log(`${dir} folder removed`)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user