-
Notifications
You must be signed in to change notification settings - Fork 3
/
mann.js
executable file
·136 lines (109 loc) · 3.21 KB
/
mann.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
var fs = require('fs');
var spawn = require('child_process').spawn;
var colors = require('colors');
var eol = require('os').EOL;
var optimist = require('optimist')
.alias('e', 'edit')
.alias('a', 'add')
.alias('d', 'delete')
.alias('l', 'list')
.describe('e', 'Edit command\'s mann page.')
.describe('a', 'Add line to command\'s mann page.')
.describe('d', 'Delete command\'s mann page.')
.describe('l', 'List all available mann pages.')
.usage('Usage:' + eol +
' mann <command>' + eol +
' mann -e <command>' + eol +
' mann -a <command> <line>' + eol +
' mann -d <command>' + eol +
' mann -l');
fs.mkdir(getMannDir(), function() {});
if (optimist.argv.e) {
var command = optimist.argv.e;
if (typeof command != 'string') {
console.log(optimist.help());
return;
}
var mannFile = getMannFile(command);
launchEditor(mannFile);
}
else if (optimist.argv.a) {
var command = optimist.argv.a;
if (typeof command != 'string') {
console.log(optimist.help());
return;
}
var line = optimist.argv._.join(' ');
if (!line) {
console.log(optimist.help());
return;
}
var mann = '';
var mannFile = getMannFile(command);
if (fs.existsSync(mannFile))
mann = fs.readFileSync(mannFile).toString();
mann = mann + eol + line;
fs.writeFileSync(mannFile, mann);
}
else if (optimist.argv.l) {
var mdExtensionRegex = /\.md$/;
fs.readdirSync(getMannDir()).forEach(function(file) {
if (file.match(mdExtensionRegex)) {
console.log(file.replace(mdExtensionRegex, ''));
}
})
}
else if (optimist.argv.d) {
var command = optimist.argv.d;
if (typeof command != 'string') {
console.log(optimist.help());
return;
}
var mannFile = getMannFile(command);
if (!fs.existsSync(mannFile)) {
console.error('No mann page exists for ' + command);
return;
}
fs.unlinkSync(mannFile);
}
else {
if (optimist.argv._.length == 0) {
console.log(optimist.help());
return;
}
optimist.argv._.forEach(function(command) {
var mannFile = getMannFile(command);
if (!fs.existsSync(mannFile)) {
console.error('No mann page exists for ' + command);
console.error('Type \'mann -e ' + command + '\' to add a mann page for it.');
return;
}
var mann = fs.readFileSync(mannFile).toString();
mann = mann.replace(/^(# *.*)$/gm, '$1'.green);
mann = mann.replace(/^(\n|\r|\r\n)+/, '').replace(/(\n|\r|\r\n)+$/, '');
console.log(mann);
});
}
function launchEditor(file) {
var editor = process.env.EDITOR;
if (!editor) {
console.error('No editor found. Please set the EDITOR environment variable.');
return;
}
var editorArgs = editor.split(' ');
var editorExecutable = editorArgs.shift();
spawn(editorExecutable, editorArgs.concat([file]), {stdio: 'inherit'})
.on('error', function() {
console.error('Failed to launch the editor. Make sure the EDITOR environment variable is correctly set.');
});
}
function getMannFile(command) {
return getMannDir() + '/' + command + '.md';
}
function getMannDir() {
return getUserHome() + '/.mann'
}
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}