Skip to content

Instantly share code, notes, and snippets.

@damieng
Created October 28, 2016 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damieng/6196aa4676aad30c5a32e8107b832666 to your computer and use it in GitHub Desktop.
Save damieng/6196aa4676aad30c5a32e8107b832666 to your computer and use it in GitHub Desktop.
Watch the filesystem using NSFW from the command line
#!/usr/bin/env node
const nsfw = require('nsfw')
const process = require('process')
const path = require('path')
if (process.argv.length == 0) {
console.log(`Usage: ${process.argv[0]} [path]`)
process.exit(0)
}
var pathParam
if (process.argv.length > 2) {
pathParam = process.argv[2]
} else {
pathParam = '.'
}
watch(pathParam, 100)
function watch(pathParam, howLong, json) {
const absolutePath = path.resolve(pathParam)
console.log(`Watching ${absolutePath} for ${howLong} seconds`)
nsfw(absolutePath,
function(events) {
for(var i = 0; i < events.length; i++) {
const e = events[i]
var output = [ `${actionNames[e.action]}` ]
const file = e.file || e.oldFile
if (file) {
output.push(`${path.join(e.directory, file)}`)
} else {
output.push(e.directory)
}
if (e.newFile) output.push(`to ${path.join(e.directory, e.newFile)}`)
console.log(output.join(' '))
}
})
.then(function(watcher) {
return watcher.start();
})
.then(function() {
setTimeout(function() { process.exit(1) }, howLong * 1000)
})
}
var actionNames = [ 'Created ', 'Deleted ', 'Modified', 'Renamed ' ]
@bhavjot
Copy link

bhavjot commented Jan 29, 2018

Hi thanks for this code, can you please let me know how I can access nsfw when running this from command line. I have done
npm install -g nsfw
but still getting error of
Cannot find module "nsfw"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment