Last active
August 29, 2015 14:05
-
-
Save qbit/5975a07c0ca27e26a2d5 to your computer and use it in GitHub Desktop.
Script to monitor a directory for new STL files and run slic3r when new ones are added.
This file contains hidden or 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
| var fs = require('fs'), | |
| spawn = require('child_process').spawn, | |
| dir = process.argv[2], | |
| cmd = process.argv[3], | |
| timer = process.argv[4] || 10000, | |
| running_file = ""; | |
| function run_update(file, rcmd) { | |
| return function() { | |
| if (running_file === "") { | |
| running_file = file; | |
| rcmd = rcmd.replace("%f", file); | |
| var s = spawn('slic3r', rcmd.split(' ')); | |
| s.stdout.on('data', function(d) { | |
| console.log(d.toString()); | |
| }); | |
| s.stderr.on('data', function(d) { | |
| console.log(d.toString()); | |
| }); | |
| s.on('close', function(code) { | |
| console.log("finished '%s' with code '%d'", rcmd, code); | |
| running_file = ""; | |
| }); | |
| } else { | |
| return; | |
| } | |
| }; | |
| } | |
| fs.exists(dir, function(exists) { | |
| if (exists) { | |
| console.log("Watching '%s'", dir); | |
| fs.watch(dir, function(e, fname) { | |
| if (fname.match(/\.stl$/i)) { | |
| setTimeout(run_update(dir + '/' + fname, cmd), timer); | |
| } | |
| }); | |
| } else { | |
| console.log("'%s' does not exist.", dir); | |
| process.exit(); | |
| } | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use in the following way:
%fwill be replaced with the file that is being added.First argument is the directory you want to monitor.
Second argument is the command line options to be passed to slic3r.
A third and optional argument can be supplied as the time to wait prior to running Slic3r (default is 10 seconds)