forked from rf/nd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (139 loc) · 4.23 KB
/
app.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var flatiron = require('flatiron');
var path = require('path');
var fs = require('fs');
var _ = require('underscore');
var app = flatiron.app;
var find = require('./lib/find');
var async = require('async');
var npm = require('npm');
var request = require('request');
var moar;
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
app.use(flatiron.plugins.cli, {dir: __dirname, usage: [], argv: {
l: {boolean: true}
}});
function list (module, args) {
async.waterfall([
function (callback) { find.root(module, callback); },
function (root, callback) { find.docDir(root, callback); }
], function (err, data) {
if (err) {
app.log.error(err);
return;
}
var dir = path.join(data[0], path.join.apply(null, args));
fs.readdir(dir, function (err, files) {
if (err) return app.log.error(err.message);
if (_.size(files) === 0) {
return app.log.error('No packages found!');
}
_.each(files, function (file) {
console.log(file);
});
});
});
}
function markdisp (data) {
var marked = require('./deps/marked').setOptions({gfm: true, terminal: true});
if (!moar) {
moar = require('moar')({nowrap: true});
}
moar.write('\n' + marked.parse(data));
moar.end();
moar.on('done', function () { process.exit(0); });
}
function listModules () {
console.log("Available modules:".bold);
npm.dir = process.cwd();
npm.config.set('global', false);
npm.commands.ls([], false, function (err, data) {
if (err) return app.log.error(err);
});
}
function view (module, args) {
async.waterfall([
function (callback) { find.root(module, callback); },
function (root, callback) { find.docDir(root, callback); },
function (dirs, callback) { find.file(dirs, args, module, callback); }
], function (err, data) {
if (err) {
app.log.error(err);
return;
}
markdisp(data);
});
}
app.cmd('node *', function (page) {
var url = 'https://raw.github.com/joyent/node/master/doc/api/'+page+'.markdown';
request(url, function (error, res, body) {
if (error || res.statusCode == 404) return app.log.error('error retrieving page');
markdisp(body);
});
});
app.router.notfound = function () {
var args = app.argv._;
var module = args.shift();
if (app.argv.l) {
// list mode
if (args.length > 0 || module) {
list(module, args);
} else {
listModules();
}
} else {
// view
if (!process.stdin.isTTY && process.platform !== "win32") {
// we're getting piped some stuff, gather it up and send it over to
// markdisp to be displayed
// we have spawn less before resuming stdin, or it gets confused
moar = require('moar')({nowrap: true});
process.stdin.resume();
process.stdin.setEncoding('utf8');
var data = '';
process.stdin.on('data', function (chunk) {
data += chunk;
});
process.stdin.on('end', function () {
markdisp(data);
});
}
// the process.stdin.pauses below are because the call to
// process.stdin.isTTY above are enough to make the process hang without
// pausing stdin
else if (!module) {
process.stdin.pause();
// no module name provided, just give a list of modules
listModules();
}
else if (/^https?:\/\//.test(module)) {
process.stdin.pause();
request(module, function (error, res, body) {
if (error || res.statusCode == 404) return app.log.error('error retrieving page');
markdisp(body);
});
}
else {
process.stdin.pause();
// we have some arguments.. check to see if they're a filename
var file = path.join(process.cwd(), module);
path.exists(file, function (exists) {
if (exists) {
// if so, read and display the file
fs.readFile(file, 'utf8', function (err, data) {
if (err) return app.log.error(err);
markdisp(data);
});
}
else {
// otherwise send the arguments over to view() to do an actual
// module lookup
view(module, args);
}
});
}
}
};
npm.load({global: true, loglevel: 'silent'}, function(err) {
if (err) app.log.error(err);
else app.start();
});