Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save scripting/b01592081511696596ff to your computer and use it in GitHub Desktop.

Select an option

Save scripting/b01592081511696596ff to your computer and use it in GitHub Desktop.
I run this JavaScript every minute to copy files from a private folder to a public-facing server.
//By Dave Winer, May 12, 2015.
//I use this script to copy the only files that River4 produces that have to be publicly accessible.
//It only copies if the dest file doesn't exist or if the content of the files has changed.
//I use Noderunner to run it once every minute: https://github.com/scripting/noderunner
var sourcefolder = "/root/river4data/rivers/";
var destfolder = "../pagepark/domains/hello.com/rivers/";
var urldestfoler = "http://hello.com/rivers/";
console.log ("Hello from copyriverfiles.js");
function copyOneFile (fname) {
var fsource = sourcefolder + fname, fdest = destfolder + fname;
console.log ("copyRiverFiles.js: fname == " + fname + ", list [i] == " + list [i]);
fs.readFile (fsource, function (err, jsontext) {
if (err) {
console.log ("copyriverfiles.js: error reading file == " + fsource + ", message == " + err.message);
}
else {
fs.readFile (fdest, function (err, jsontextFromDest) {
if (err || (jsontext.toString () != jsontextFromDest.toString ())) {
fs.writeFile (fdest, jsontext, function (err) {
if (err) {
console.log ("copyriverfiles.js: error writing file == " + fdest + ", message == " + err.message);
}
else {
console.log ("copyriverfiles.js: copied == " + urldestfoler + fname);
}
});
}
});
}
});
}
fs.readdir (sourcefolder, function (err, list) {
if (err) {
console.log ("copyRiverFiles.js: error == " + err.message);
}
else {
for (var i = 0; i < list.length; i++) {
copyOneFile (list [i]);
}
}
});
@stockholmux
Copy link

Useful! If you pipe the files, it'll be a bit faster and memory efficient. Also, is this on a cron job? If it is you could use a simple setInterval to run every minute and not have to worry about V8 spinning up.

@scripting
Copy link
Author

I use Noderunner to run it once a minute.

https://github.com/scripting/noderunner

@BrunoWinck
Copy link

grunt --watch
would do it at once and only those changed
noderunner would still be a node script (grunt also)

@scripting
Copy link
Author

Bruno, I don't understand what you're saying. I'm sure if I typed grunt --watch at the command line it would not do all that this script does. Maybe somehow magically it can read my mind? ;-)

This script only copies files that changed.

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