Created
October 28, 2016 18:36
-
-
Save damieng/6196aa4676aad30c5a32e8107b832666 to your computer and use it in GitHub Desktop.
Watch the filesystem using NSFW from the command line
This file contains 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
#!/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 ' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"