coin-parser
v0.0.2
Published
Parse a json file for into a commandline interface.
Downloads
4
Readme
//////////// Node Parser
//
const readline = require('readline');
const fs = require('fs');
const rl = readline.createInterface(process.stdin, process.stdout);
const prompt = 'CoinParser'
rl.setPrompt(''+prompt+'> ');
rl.prompt();
// you can always expect two things
var cache = {
tails:[],heads:{},tools:{map:function(fn){console.log(fn);for(var key=1;arguments[key];key++){fn(arguments[key]);};}}
};
var loadJSON = function(file_location, callback){
// console.log("loadJSON");
fs.readFile(file_location, function (err, data) {
if (err) {
console.log("error");
return console.error(err);
}
console.log("callback(data)");
callback(data);
// console.log("Asynchronous read: " + data.toString());
});
}
var open_chest = function(response){
// console.log(response.toString());
var api_handle = JSON.parse(response.toString())["head"];
console.log("Loading API " + api_handle);
var commands = JSON.parse(response.toString())["tail"];
// console.log(commands);
for(var prop in commands)
{
var coin = commands[prop];
var tail = new Function(coin.tail);
cache.tails.push({description:coin.description,key:coin.head,payload:tail});//{key:input[1],payload:new Function(input.slice(2).join(" "))}
// cache.tails.push({new Function(coin.tail)});
cache.heads[coin.head] = cache.tails.length - 1;
// console.log("working");
}
// ===========================
rl.on('line', (line) => {
var input =line.trim().split(" ");
var arguments = input.slice(1).join(" ");
// console.log(arguments);
var command = input[0];
switch(command){
case 'heads':
console.log(cache.heads);
break;
case 'tails':
console.log(cache.tails);
break;
case 'exit':
console.log('Have a great day!');
process.exit(0);
break;
default:
// console.log(cache.tails);
if(typeof cache.heads[input[0]] !== "undefined"){
var key = cache.heads[input[0]];
console.log(cache.tails[key].key+"("+arguments+")");
if(input[1]=="description"){
console.log("*"+cache.tails[key].description);
}else{
// console.log("=>"+cache.tails[key].payload.apply(cache,arguments));
console.log('payload -> '+cache.tails[key].payload);
console.log('typeof input -> '+typeof input);
console.log('typeof arguments -> '+typeof arguments);
console.log("=>");
console.log(cache.tails[key].payload.apply(cache,arguments.split(" ")));
}
// cache.tails[key](arguments);
}else if(command==="new"){
if(typeof input[1]==="undefined" || typeof input[2]==="undefined" ){
console.log("Missing parameters for new.");
}else{
// var value = commands[prop];
console.log(typeof arguments.split(" ")[1]);
// console.log(input);
cache.tails.push({key:input[1],payload:new Function(arguments.split(" ")[1])});
// console.log(arguments.split(" "));
cache.heads[input[1]] = cache.tails.length-1;
}
}else if(true){
console.log(cache);
}else{
console.log("nossing!");
}
break;
}
rl.setPrompt(''+prompt+'> ');
rl.prompt();
}).on('close', () => {
console.log('Have a great day!');
process.exit(0);
});
// ===========================
};
var api = process.argv[2];
// if(typeof api === "undefined"){
loadJSON(api,open_chest);
// rl.setPrompt(''+prompt+'> ');
// }
// console.log("Exit");
//
////////////