arrakis
Version:
Helpfull development framework
98 lines (70 loc) • 2.11 kB
JavaScript
const EventEmitter = require('./EventEmitter');
const Scheduler = require('./Scheduler');
const readline = require('readline');
const stream = require('stream');
module.exports = class Console extends EventEmitter{
constructor(pool){
super();
this._scheduler = new Scheduler();
this._exec = [];
var a = require('../bin/system');
this.register(new a());
}
_encodeCMD(cmd){
return cmd.replace(/(["'])(?:(?=(\\?))\2.)*?\1/g, function(x){
return x.replace(/\s/g, '▓');
});
}
_decodeCMD(cmd){
return cmd.replace(/\▓/g, ' ').replace(/\"/g, "");
};
_getArgs(cmd){
cmd = this._encodeCMD(cmd);
cmd = cmd.split(' ');
var argv = [];
var bufferArgv;
for(var index = 0; index < cmd.length; index++){
bufferArgv = this._decodeCMD(cmd[index]);
if(Number(bufferArgv))
bufferArgv = Number(bufferArgv);
argv[index] = bufferArgv;
}
return argv;
}
register(instance, options={}){
var name;
if(instance == null)
throw new Error('Need a class instance!');
if(options.name != null)
name = options.name;
else
name = instance.constructor.name.toLowerCase();
if(options.systemLevel)
throw new Error('System Level not implement yet!');
else
this._exec[name] = instance;
}
exec(cmd=''){
var options = {}, callback, caller = this, cmdSP = ' | ', output = process.stdout;
if(cmd != ''){
for(var index in arguments)
if(typeof arguments[index] === 'object')
options = arguments[index];
else if(typeof arguments[index] === 'function')
callback = arguments[index];
if(options.caller != null)
caller = options.caller;
if(options.cmdSP != null)
cmdSP = options.cmdSP;
if(options.output != null)
output = options.output;
this._scheduler.pend(cmd.split(cmdSP));
this._scheduler.start(function(command){
var argv = this._getArgs(command);
}, this);
output.write('input: '+cmd+'\n');
}
if(callback != null)
callback.apply(caller, ['Exec cmd ['+cmd+']']);
}
}