Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

yolowatch

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yolowatch - npm Package Compare versions

Comparing version
1.0.0
to
2.1.0
+52
tests.js
var fs = require('fs')
var os = require('os')
var path = require('path')
var test = require('tape')
var rimraf = require('rimraf')
var yoloWatch = require('.')
var dir = path.join(os.tmpdir(), 'yolotest')
var opts = {persistent: false}
var i = 0
var fStart
var watcher
test('prep', function (t) {
rimraf(dir, function () {
fs.mkdirSync(dir)
fStart = createFile()
watcher = yoloWatch(dir, opts)
t.end()
})
})
test('file is updated', function (t) {
watcher.on('changed', function (file, stat) {
t.plan(2)
if (file !== fStart) return false
t.equal(file, fStart)
t.ok(stat.mtime > 0, 'mtime > 0')
})
touch(fStart)
})
test('new file added', function (t) {
var f
f = createFile()
watcher.on('added', function (file, stat) {
if (file !== f) return false
t.equal(file, f)
t.end()
})
})
function createFile () {
var n = path.join(dir, 'tmp00' + (i++))
fs.writeFileSync(n, Date.now())
return n
}
function touch (f) {
setTimeout(function () { fs.writeFileSync(f, Date.now()) }, 1000)
}
+25
-30
var filewatcher = require('filewatcher')
var from = require('from2')
var events = require('events')
var each = require('stream-each')
var path = require('path')
var walker = require('folder-walker')
module.exports = function yoloWatch (root) {
module.exports = function yoloWatch (root, opts) {
var yolo = new events.EventEmitter()
var stats = {}
var dirs = filewatcher()
dirs.on('change', kick)
var dirs = filewatcher(opts)
dirs.on('change', function () {
kick(root, false)
})
dirs.add(root)
kick(root)
kick(root, true)
var watcher = filewatcher()
return from.obj(read)
var watcher = filewatcher(opts)
watcher.on('change', fileChanged)
return yolo
function read (size, cb) {
watcher.on('change', function (filepath, stat) {
fileChanged(filepath, stat, cb)
})
}
function kick (dir) {
function kick (dir, first) {
// find any new files
var fileStream = walker(dir)
each(fileStream, function (data, next) {
if (!stats[data.filepath]) watcher.add(data.filepath)
if (!stats[data.filepath]) {
// new file
if (!first) yolo.emit('added', data.filepath, data)
watcher.add(data.filepath)
}
stats[data.filepath] = data

@@ -34,18 +36,11 @@ next()

function fileChanged (filepath, stat, cb) {
var item = {
root: root,
filepath: filepath,
stat: stat,
relname: root === filepath ? path.basename(name) : path.relative(root, filepath),
basename: path.basename(filepath)
function fileChanged (filepath, stat) {
if (!stat || stat.deleted) {
stats[filepath] = null
yolo.emit('deleted', filepath, stat)
} else {
stats[filepath].stat = stat
yolo.emit('changed', filepath, stat)
}
if (!stat || stat.deleted) item.type = 'deleted'
else if (stat.isFile()) {
item.type = 'file'
}
else if (stat.isDirectory()) item.type = 'directory'
cb(null, item)
}
}
{
"name": "yolowatch",
"version": "1.0.0",
"version": "2.1.0",
"description": "watch filesystem changes yolostyle",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "standard && tape tests.js | tap-spec"
},

@@ -16,3 +16,9 @@ "author": "",

"stream-each": "^1.1.2"
},
"devDependencies": {
"rimraf": "^2.5.2",
"standard": "^7.1.2",
"tap-spec": "^4.1.1",
"tape": "^4.6.0"
}
}
# yolowatch
Watch filesystem changes yolostyle.
Watch changes to subdirectories and subfiles within a given directory.
Uses the `filewatcher` module to fall back on polling the filesystem when ulimit is reached.
```

@@ -9,3 +11,3 @@ npm install yolowatch

### example
### Example

@@ -16,7 +18,19 @@ ```js

var watcher = yolowatch('/path/to/my/dir')
watcher.on('data', function (file) {
console.log(file.filepath, 'was added or modified')
console.log('is a', file.type) //'file', 'directory', or 'deleted'
watcher.on('changed', function (file) {
console.log(file.filepath, 'was changed')
console.log('is a', file.type) //'file', 'directory'
})
watcher.on('deleted', function (file) {
console.log(file.filepath, 'was deleted')
})
watcher.on('added', function (file) {
console.log(file.filepath, 'was deleted')
})
```
### Todo
* expose options to pass to filewatcher module