sirrobert-shell-cmd-help
Version:
A Help command for the nodejs shell.
59 lines (50 loc) • 1.34 kB
JavaScript
var fs = require('fs');
var util = require("src/util.js");
var path = require('path');
var color = require("src/color.js");
// A Help file is loaded with a path. The path looks like this, say:
//
// shell.commands.quit
//
// which expands to the file
//
// ./help/shell/commands/quit.help
//
// This is a plain text file that is displayed without formatting.
function Help (path) {
this.text = null;
this.path = path;
this.cache = false;
};
Help.prototype.display = function (options) {
// if we've already loaded this help file, great just show it. If we
// haven't, we need to load it.
if (this.text) {
process.stdout.write(
color.set.blue("\n" + this.text + "\n")
);
if (options && options.after) {
options.after();
}
if (!this.cache) {
this.text = null;
}
return;
}
var disk_path = process.program_dir + "/help/" + this.path.replace(/\./g,'/') + ".help";
var help = this;
fs.readFile(disk_path, function (err, data) {
if (err) {
return util.log(
"error",
"Help.display()",
"File does not exist.",
[help.path, disk_path]);
}
// If we got a file, save it to the help.text variable and then
// re-call this method.
help.text = data;
help.display(options);
});
}
module.exports = Help;