+52
| 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) | ||
| } | ||
| } |
+8
-2
| { | ||
| "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" | ||
| } | ||
| } |
+19
-5
| # 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 |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
3363
71.15%5
25%86
95.45%1
-50%35
66.67%4
Infinity%2
100%1
Infinity%