Skip to content

Instantly share code, notes, and snippets.

@cmonyuk
Created February 4, 2014 03:32
Show Gist options
  • Save cmonyuk/8797751 to your computer and use it in GitHub Desktop.
Save cmonyuk/8797751 to your computer and use it in GitHub Desktop.
synchronous(???) console.log code
var Stream = require("stream").Stream;
var path = require("path");
var nopt = require("nopt");
var readJson = require("read-package-json");
var prompt = require("prompt");
//var ansi = require("ansi");
//var cursor = ansi(process.stdout);
var charm = require("charm")();
charm.pipe(process.stdout);
// interactive interface
function Iface() {
// list of known commands
this.commandList = {
"version" : {
"commandName" : version,
"commandOpts" : null,
"commandShort": null
},
"quit" : {
"commandName" : quit,
"commandOpts" : null,
"commandShort": null
}
};
// prompt for command
prompt.message = "";
prompt.delimiter = "";
prompt.start();
readCommand();
}
// Print version
function version(cb) {
readJson('package.json', console.error, false, function(er, data) {
if (er) {
console.log("package.json not found");
} else {
// process.stdout.write(data.name + " version " + data.version);
// charm.write(data.name + " version " + data.version);
// charm.write("v");
console.log(/*data.name + " */"version " + data.version);
}
});
cb();
}
function quit() {
process.exit();
}
function help(cb) {
console.log("helping you help yourself");
cb();
}
function readCommand() {
console.log("t");
prompt.get([{properties : {command : {description : ">".green}}}],
function(err, result) {
if (err) {
console.log("input error!");
return;
} else {
if (this.commandList[result.command]) {
this.commandList[result.command].commandName(readCommand);
} else {
if (result.command) {
console.log("unknown command " + result.command);
help(readCommand);
} else {
readCommand();
}
}
}
});
}
t
> version
t
> version 0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment