This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (80 loc) · 2.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var help = require('cli-mid-help').action;
var man = help.documents.man;
function open(file, exit, next) {
process.env.CLI_TOOLKIT_HELP_CMD = '';
process.stdin.pause();
var ps = spawn('man', [file], {stdio: 'inherit'});
ps.once('close', function(code) {
if(exit) process.exit(code);
process.stdin.resume();
next();
})
}
function handler(info, req, next) {
var scope = this, cmd, command;
var conf = this.configure().manual
, exit = this.configure().exit
, alias = this.finder.getCommandByName
, sub = null;
if(info.args.length) {
cmd = info.args[0];
command = alias(cmd, this.commands(), {recurse: true});
if(!command) {
return next(this.errors.EUNKNOWN_HELP_CMD, [cmd]);
}
// handle subcommand references
sub = command.getParents().length > 1;
if(sub) {
command = command.parent();
cmd = command.key();
}
process.env.CLI_TOOLKIT_HELP_CMD = cmd;
}else{
command = this;
}
if(!conf.dir || process.env.CLI_TOOLKIT_MAN_PUBLISH) {
help.call(this, false, next);
}else{
var name = command.getFullName() + '.1';
var file = path.join(conf.dir, name);
fs.exists(file, function(exists) {
if(!exists && !conf.dynamic) {
return next(scope.errors.EMAN_NOTFOUND, [cmd]);
}else if(conf.dynamic) {
var stream = fs.createWriteStream(file, {flags: 'w'});
process.env.CLI_TOOLKIT_HELP_STYLE='man';
process.env.CLI_TOOLKIT_HELP_CMD=cmd;
help.call(scope, true, req, function() {
delete process.env.CLI_TOOLKIT_HELP_CMD;
delete process.env.CLI_TOOLKIT_HELP_STYLE;
stream.end(function() {
open.call(scope, file, exit, next);
});
}, stream, man(scope));
}else{
open.call(this, file, exit, next);
}
});
}
}
/**
* Adds a help command to the program.
*
* @param name The command name.
* @param description The command description.
*/
module.exports = function(name, description) {
var conf = this.configure().manual;
if(!conf) return this;
name = name || 'help';
description = description || 'Show help for commands';
this.command(name)
.description(description)
.usage(name + ' <command>')
.action(handler);
return this;
}
module.exports.action = handler;