This repository was 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
Expand file tree
/
Copy pathindex.js
More file actions
213 lines (183 loc) · 4.65 KB
/
Copy pathindex.js
File metadata and controls
213 lines (183 loc) · 4.65 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var types = {
json: 'json',
help: 'help',
man: 'man',
zsh: 'zsh'
};
/**
* Creates documentation for command line interfaces.
*
* @private {function} cli
* @param {Object} [opts] processing options.
* @param {Function} [cb] callback function.
*
* @option {Readable} [input] input stream.
* @option {Writable} [output] output stream.
*
* @returns an output stream.
*/
function cli(opts, cb) {
opts = opts || {};
opts.type = opts.type || types.json;
// do we need a compiler phase?
var compiles = opts.type === cli.JSON || opts.type === cli.ZSH;
opts.recursive = opts.recursive !== undefined
? opts.recursive : (compiles ? true : false);
var stream = src(opts)
, parser = stream
, ast = require('mkast')
, renderer
, builder;
try {
renderer = dest(opts)
}catch(e) {
if(typeof cb === 'function') {
return cb(e);
}
throw e;
}
if(!opts.input || !opts.output) {
return stream;
}
// set up input stream
stream = ast.parser(opts.input)
.pipe(stream);
if(compiles) {
builder = compiler(opts);
stream = stream.pipe(builder);
}
stream = stream.pipe(renderer)
// convert output to newline delimited json
if(opts.type !== cli.JSON) {
stream = stream.pipe(ast.stringify());
}
stream.pipe(opts.output);
if(cb) {
parser.once('error', cb);
if(builder) {
builder.once('error', cb);
}
opts.output
.once('error', cb)
.once('finish', cb);
}
return opts.output;
}
/**
* Gets a source parser stream that transforms the incoming tree nodes into
* parser state information.
*
* @function src
* @param {Object} [opts] parser options.
*
* @returns a parser stream.
*/
function src(opts) {
opts = opts || {};
var Parser = require('./lib/parser');
return new Parser(opts);
}
/**
* Gets a compiler stream that transforms the parser state information to
* a program definition.
*
* @function compiler
* @param {Object} [opts] compiler options.
*
* @returns a compiler stream.
*/
function compiler(opts) {
opts = opts || {};
var Compiler = require('./lib/compiler');
return new Compiler(opts);
}
/**
* Gets a destination renderer stream.
*
* When no type is specified the JSON renderer is assumed.
*
* @function dest
* @param {Object} [opts] renderer options.
*
* @option {String=json} type the renderer type.
*
* @returns a renderer stream of the specified type.
*/
function dest(opts) {
opts = opts || {};
var type = opts.type || types.json
if(!types[type]) {
throw new Error('unknown output type: ' + type);
}
var Type = require('./lib/render/' + type)
return new Type(opts);
}
/**
* Load a program definition into a new program assigning the definition
* properties to the program.
*
* Properties are passed by reference so if you modify the definition the
* program is also modified.
*
* @function load
* @param {Object} def the program definition.
* @param {Object} [opts] program options.
*
* @returns a new program.
*/
//function load(def) {
//var Program = require('./lib/program')
//, prg = new Program();
//for(var k in def) {
//prg[k] = def[k];
//}
//return prg;
//}
/**
* Load a program definition into a new program assigning the definition
* properties to the program.
*
* Properties are passed by reference so if you modify the definition the
* program is also modified.
*
* The callback function signature is `function(err, req)` where `req` is a
* request object that contains state information for program execution.
*
* Plugins may decorate the request object with pertinent information that
* does not affect the `target` object that receives the parsed arguments.
*
* @function run
* @param {Object} src the source program or definition.
* @param {Array} argv the program arguments.
* @param {Object} [runtime] runtime configuration.
* @param {Function} cb callback function.
*
* @returns a new program.
*/
//function run(src, argv, runtime, cb) {
//var Program = require('./lib/program')
//, runner = require('./lib/run');
//if(!(src instanceof Program)) {
//src = load(src);
//}
//runner.call(src, argv, runtime, cb);
//}
var runtime = require('mkcli-runtime');
cli.load = runtime.load;
cli.run = runtime.run;
cli.camelcase = runtime.camelcase;
//cli.load = load;
cli.types = types;
cli.src = src;
cli.compiler = compiler;
cli.dest = dest;
//cli.run = run;
//cli.camelcase = function() {
//// lazy require
//var camel = require('cli-argparse').camelcase;
//return camel.apply(this, arguments);
//}
Object.keys(types).forEach(function(nm) {
cli[nm.toUpperCase()] = nm;
});
module.exports = cli;