Created
March 23, 2016 15:15
-
-
Save dimfeld/306bb2ebce779962a5a8 to your computer and use it in GitHub Desktop.
Convert JS-style objects into proper JSON
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 | |
"use strict"; | |
var fs = require("fs"); | |
var vm = require("vm"); | |
var sandbox = { value: null }; | |
var context = vm.createContext(sandbox); | |
function convert(contents) { | |
vm.runInContext('value = ' + contents, context); | |
return JSON.stringify(sandbox.value); | |
} | |
var args = process.argv.slice(2); | |
if(args.length) { | |
args.forEach(function(name) { | |
var contents = fs.readFileSync(name); | |
console.log(convert(contents)); | |
}); | |
} else { | |
var data = []; | |
process.stdin.on('data', function(d) { data.push(d); }); | |
process.stdin.on('end', function() { | |
var contents = data.join(""); | |
console.log(convert(contents)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment