dat.gui/saver/saver.js

65 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2014-09-27 19:02:25 +00:00
#!/usr/bin/env node
2014-09-27 20:25:12 +00:00
var http = require( 'http' );
var fs = require( 'fs' );
var path = require( 'path' );
var colors = require( 'colors' );
var argv = require( 'yargs' ).argv;
var PORT = 7999;
var BASEPATH = process.argv[ 2 ] || './' ;
var OUTFILE = path.join( BASEPATH, argv.o || 'dat-gui.json' );
http.createServer( function ( req, res ) {
switch ( req.method ) {
2014-09-27 19:02:25 +00:00
case 'POST':
2014-09-27 20:25:12 +00:00
2014-09-27 19:02:25 +00:00
var data = '';
2014-09-27 20:25:12 +00:00
req.on( 'data', function( d ) {
data += d;
} );
req.on( 'end', function() {
res.writeHead( 200, { 'Access-Control-Allow-Origin': 'http://localhost' } );
fs.writeFileSync( OUTFILE, data );
var json = JSON.parse( data );
var propertyCount = 0;
for ( var i in json.values ) {
propertyCount += Object.keys( json.values[ i ] ).length;
}
log( 'Saved ' + propertyCount + ' properties.' );
2014-09-27 19:02:25 +00:00
res.end();
2014-09-27 20:25:12 +00:00
} );
2014-09-27 19:02:25 +00:00
break;
2014-09-27 20:25:12 +00:00
2014-09-27 19:02:25 +00:00
default:
2014-09-27 20:25:12 +00:00
res.writeHead( 405 );
2014-09-27 19:02:25 +00:00
res.end();
2014-09-27 20:25:12 +00:00
2014-09-27 19:02:25 +00:00
}
2014-09-27 20:25:12 +00:00
} ).listen( PORT );
log( 'Running at ' + ( 'http://localhost:' + PORT ).magenta + '.' );
log( 'Writing to ' + OUTFILE.green + '.' );
function log( message ) {
var d = new Date();
var h = d.getHours() % 12;
var m = d.getMinutes();
if ( h < 10 ) h = '0'+h;
if ( m < 10 ) m = '0'+m;
var d = h + ':' + m;
console.log( d.green + ' [ dat-gui-saver ] '.blue + message );
}