Created
September 4, 2019 20:00
-
-
Save tingham/d557db25054715e833340001c96259a5 to your computer and use it in GitHub Desktop.
A NodeJS script to generate a new Processing.org Sketch file
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 | |
/* | |
* Execute to vim by running | |
* $vim `./msketch.js` | |
* or | |
* $vim `./msketch.js someName` | |
* | |
* Add this to your vimrc to execute a sketch using F5 | |
* autocmd Filetype java nnoremap <F5> :w<CR> :!cd %:p:h && processing-java --sketch=$PWD/ --run <CR> % | |
*/ | |
var fs = require("fs"), | |
sketchName = process.argv[2], | |
today = new Date(), | |
alphas = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split(","), | |
stream, | |
template; | |
function dateSlug(d) { | |
"use strict"; | |
var year = d.getFullYear().toString().substr(2, 2), | |
month = (d.getMonth() + 1).toString(), | |
day = d.getDate().toString(); | |
if (month.length < 2) { month = "0" + month; } | |
if (day.length < 2) { day = "0" + day; } | |
return year + month + day; | |
} | |
if (sketchName === undefined) { | |
sketchName = "sketch_" + dateSlug(today); | |
while (fs.existsSync(sketchName)) { | |
sketchName = "sketch_" + dateSlug(today) + alphas.shift(); | |
} | |
} | |
template = '// # ' + sketchName + "\n"; | |
template += '// **Created By:** + ' + process.env.USER + "\n"; | |
template += '// **Created On:** + ' + today.toDateString() + "\n"; | |
template += '// ' + "\n\n"; | |
template += "int tick = 0;\n"; | |
template += "boolean save_files = false;\n"; | |
template += 'String outputName = "data/output/" + System.currentTimeMillis() + "/";' + "\n\n"; | |
template += "void setup ()\n"; | |
template += "{\n"; | |
template += " // P3D, P2D \n"; | |
template += " size(512, 512);\n"; | |
template += "}\n\n"; | |
template += "void draw ()\n"; | |
template += "{\n"; | |
template += " long t = System.currentTimeMillis();\n"; | |
template += " if (t % 30 == 0 && save_files) {\n"; | |
template += ' save(outputName + "s-" + t + ".jpg");' + "\n"; | |
template += " }\n"; | |
template += "}\n\n"; | |
fs.mkdirSync("./" + sketchName); | |
stream = fs.createWriteStream("./" + sketchName + "/" + sketchName + ".pde"); | |
stream.write(template); | |
stream.end(); | |
fs.mkdirSync("./" + sketchName + "/data"); | |
fs.mkdirSync("./" + sketchName + "/data/output"); | |
fs.mkdirSync("./" + sketchName + "/data/input"); | |
process.stdout.write("./" + sketchName + "/" + sketchName + ".pde"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment