This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
selleck
executable file
·361 lines (294 loc) · 10.9 KB
/
selleck
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env node
/*
Selleck
Copyright (c) 2011 Yahoo! Inc.
Licensed under the BSD License.
*/
var fs = require('fs'),
fsPath = require('path'),
selleck = require('selleck'),
fileutils = require('selleck/lib/fileutils'),
higgins = require('selleck/lib/higgins'),
util = require('selleck/lib/util'), // Selleck's util, not Node's util.
arg,
args = [], // leftover args after params are parsed out of argv
argv = process.argv.slice(2), // get rid of the leading 'node' arg
docs = {components: []},
log = selleck.log,
nextArg,
// Default values for command line options.
options = {
'out' : './build_docs',
'out-ext' : '.html',
'overrideMeta': {},
'theme' : selleck.defaultTheme
},
components = {},
project = {
layouts : {},
meta : {},
pages : {},
partials : {},
project : true,
validator : projectValidator
},
stack = new util.Stack(),
theme = {
layouts : {},
meta : {},
partials: {}
},
help = [
"Selleck generates YUI user documentation, Mustache-style.",
"",
"Usage: selleck [options] [input path]",
"",
"Common Options:",
" -m, --meta <filename> Augment and override project/component metadata with",
" data in the specified file.",
" -o, --out <path> Write generated files to this output path.",
" -p, --project <path> Use the specified project path instead of searching for",
" the project.",
" -s, --server [port] Run Selleck as a web server for real-time previews of",
" changes while editing docs.",
" -t, --theme <path> Use the specified theme instead of the default.",
" -h, --help Display this help message.",
" -v, --version Display version information.",
"",
"Advanced Options:",
" --out-assets <path> Copy all assets to this output path instead of the",
" default. Component assets will be copied to",
" subdirectories named for the component.",
" --out-ext <extension> Use the specified file extension for generated pages",
" instead of the default (.html).",
" --out-meta <filename> Write combined project and component JSON metadata to",
" the specified filename.",
""
].join('\n');
// Process command line parameters.
while ((arg = argv.shift())) {
// If this isn't a parameter, push it onto the leftover args stack and move
// on.
if (arg.indexOf('-') !== 0) {
args.push(arg);
continue;
}
switch (arg) {
case '--help':
case '-h':
// WTF. This is seriously the only way I've been able to figure out to
// make Node reliably flush to stdout before exiting. It's ridiculous.
// This is stupid.
process.stdout.write(help);
while (!process.stdout.flush()) {};
process.exit();
break;
case '--metadata':
case '--meta':
case '-m':
options.overrideMeta = JSON.parse(fs.readFileSync(argv.shift(), 'utf8'));
break;
case '--output':
case '--out':
case '-o':
options.out = argv.shift();
break;
case '--output-assets':
case '--out-assets':
options['out-assets'] = argv.shift();
break;
case '--output-extension':
case '--output-ext':
case '--out-extension':
case '--out-ext':
options['out-ext'] = argv.shift();
break;
case '--output-metadata':
case '--output-meta':
case '--out-metadata':
case '--out-meta':
options['out-meta'] = argv.shift();
break;
case '--project':
case '-p':
options.project = docs.project = {path: argv.shift()};
if (!selleck.isProjectDirectory(docs.project.path)) {
log('Invalid project path: ' + docs.project.path, 'error');
process.exit(1);
}
break;
case '--server':
case '--serve':
case '-s':
options.server = true;
if ((nextArg = argv.shift())) {
if (nextArg.search(/^\d+$/) === -1) {
// Not a port number; put the argument back on the argv array.
argv.unshift(nextArg);
} else {
options.serverPort = +nextArg;
}
}
break;
case '--theme':
case '-t':
options.theme = argv.shift();
break;
case '--version':
case '-v':
pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf-8'));
version = [
"Selleck " + pkg.version,
pkg.copyright,
pkg.homepage,
""
].join('\n');
// See rant above re. forcing Node to flush stdout before exiting.
process.stdout.write(version);
while (!process.stdout.flush()) {};
process.exit();
break;
default:
log('Unrecognized parameter: ' + arg, 'error');
process.exit(1);
}
}
// First leftover arg (if any) is the root path; if no arg, then use the current
// directory.
options.rootPath = args.shift() || process.cwd();
options['out-assets'] || (options['out-assets'] = fsPath.join(options.out, 'assets'));
if (options.server) {
// If running as a server, start the server instead of continuing.
require('selleck/lib/server')(options);
} else {
// Load theme layouts and partials, and copy theme assets to the output dir,
// if there are any.
theme.layouts = selleck.getLayouts(options.theme);
theme.meta = selleck.getMetadata(options.theme, 'theme');
theme.partials = selleck.getPartials(options.theme);
// Traverse the root path and look for documentation directories.
docs = selleck.findDocs(options.rootPath, docs);
if (docs.project) {
log('Parsing project: ' + docs.project.path, 'info');
project.layouts = util.merge(theme.layouts, selleck.getLayouts(docs.project.path));
project.meta = util.merge(theme.meta, selleck.getMetadata(docs.project.path, 'project'));
project.partials = util.merge(theme.partials, selleck.getPartials(docs.project.path));
project.pages = selleck.getPages(docs.project.path);
}
log('Parsing components', 'info');
// Initial pass to load all component info.
docs.components.forEach(function(component, index) {
var path = component.path;
selleck.prepare(path, util.merge(options, project, {
component: true,
index : index,
path : path,
validator: componentValidator
}), stack.add(function (err, result) {
if (err) { throw err; }
components[result.meta.name] = result;
}));
});
// Second pass to cross-link examples.
stack.done(function () {
util.each(components, function (component) {
if (!component.meta.examples) { return; }
component.meta.examples.forEach(function (example) {
example.componentName || (example.componentName = component.meta.name);
if (!example.modules) { return; }
example.modules.forEach(function (moduleName) {
var module = components[moduleName];
if (module && moduleName !== example.componentName
&& !example.inherited) {
module.meta.examples || (module.meta.examples = []);
module.meta.examples.push(util.merge(example, {
inherited: true
}));
}
});
});
});
project.components = components;
generateProject();
});
function generateProject() {
selleck.createOutputDir(options.out);
selleck.createOutputDir(options['out-assets']);
selleck.copyAssets(fsPath.join(options.theme, 'assets'),
options['out-assets'], true, function (err) {
if (err) { throw err; }
});
if (docs.project) {
log('Generating project docs for ' + docs.project.path, 'info');
selleck.generate(docs.project.path, util.merge(options, project,
{skipLoad: true}), generateComponents);
} else {
generateComponents();
}
}
function generateComponents(err) {
if (err) { return finish(err); }
var toGenerate = util.size(project.components);
if (!toGenerate) { return finish(); }
// Generate component docs.
util.each(project.components, function(component) {
var path = component.path;
log('Generating component docs for ' + path, 'info');
selleck.generate(path, util.merge(options, component,
{skipLoad: true}), finishComponent);
});
function finishComponent(err) {
if (err) { return finish(err); }
toGenerate -= 1;
if (toGenerate === 0) {
finish();
}
}
}
function finish(err) {
if (err) { throw err; }
var meta;
if (options['out-meta']) {
log('Writing combined metadata to ' + options['out-meta'], 'info');
meta = {
components: {},
project : util.merge(project.meta, options.overrideMeta)
};
util.each(project.components, function (component) {
var data = util.merge(component.meta);
delete data.componentDefaults;
meta.components[component.meta.name] = data;
});
fs.writeFile(options['out-meta'], JSON.stringify(meta, null, 2), 'utf8', function (err) {
if (err) {
throw err;
} else {
reallyFinish();
}
});
} else {
reallyFinish();
}
function reallyFinish() {
log('Done! Docs were written to: ' + options.out, 'info');
}
}
}
// -- Functions ----------------------------------------------------------------
function componentValidator(data, docPath) {
// Validate the component name.
if (!data.meta.name) {
log('Component name not specified; skipping: ' + docPath, 'error');
return false;
}
if (data.meta.name.search(/^[\w\-]+$/) === -1) {
log('Invalid component name: ' + data.meta.name + '; skipping: ' + docPath, 'error');
return false;
}
}
function projectValidator(data, docPath) {
if (!data.meta.projectName) {
log('Project name not specified: ' + docPath, 'error');
process.exit(1);
}
}