Skip to content

Commit ea4116f

Browse files
author
Anand Thakker
committed
Pull additional parsing & validation into args.js
1 parent 1ce6a37 commit ea4116f

File tree

2 files changed

+88
-62
lines changed

2 files changed

+88
-62
lines changed

bin/documentation.js

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,20 @@
55
var documentation = require('../'),
66

77
streamArray = require('stream-array'),
8-
path = require('path'),
98
fs = require('fs'),
109
vfs = require('vinyl-fs'),
1110
formatError = require('../lib/error'),
12-
loadConfig = require('../lib/load_config.js'),
13-
args = require('../lib/args.js'),
14-
argv = args.parse(process.argv.slice(2));
11+
args = require('../lib/args.js');
1512

16-
var inputs,
17-
name = argv.name,
18-
version = argv.version,
19-
transform;
13+
var parsedArgs = args(process.argv.slice(2));
14+
var inputs = parsedArgs.inputs,
15+
options = parsedArgs.options,
16+
formatterOptions = parsedArgs.formatterOptions,
17+
outputLocation = parsedArgs.output;
2018

21-
if (argv._.length > 0) {
22-
inputs = argv._;
23-
} else {
24-
try {
25-
var p = require(path.resolve('package.json'));
26-
inputs = [p.main];
27-
name = name || p.name;
28-
version = version || p.version;
29-
if (p.browserify && p.browserify.transform) {
30-
transform = p.browserify.transform;
31-
}
32-
} catch (e) {
33-
args.showHelp();
34-
throw new Error('documentation was given no files and was not run in a module directory');
35-
}
36-
}
37-
38-
if (!documentation.formats[argv.f]) {
39-
args.showHelp();
40-
throw new Error('Formatter not found');
41-
}
42-
43-
var formatterOptions = {
44-
name: name,
45-
version: version,
46-
theme: argv.theme
47-
};
48-
49-
var formatter = documentation.formats[argv.f];
50-
51-
if (argv.f === 'html' && argv.o === 'stdout') {
52-
args.showHelp();
53-
throw new Error('The HTML output mode requires a destination directory set with -o');
54-
}
55-
56-
var config = {};
57-
58-
if (argv.config) {
59-
config = loadConfig(argv.config);
60-
}
19+
var formatter = documentation.formats[parsedArgs.formatter];
6120

62-
documentation(inputs, {
63-
private: argv.private,
64-
transform: transform,
65-
github: argv.github,
66-
polyglot: argv.polyglot,
67-
order: config.order || [],
68-
shallow: argv.shallow
69-
}, function (err, result) {
21+
documentation(parsedArgs.inputs, parsedArgs.options, function (err, result) {
7022
if (err) {
7123
throw err;
7224
}
@@ -82,11 +34,11 @@ documentation(inputs, {
8234
throw err;
8335
}
8436

85-
if (argv.o !== 'stdout') {
86-
if (argv.f === 'html') {
87-
streamArray(output).pipe(vfs.dest(argv.o));
37+
if (outputLocation !== 'stdout') {
38+
if (parsedArgs.formatter === 'html') {
39+
streamArray(output).pipe(vfs.dest(outputLocation));
8840
} else {
89-
fs.writeFileSync(argv.o, output);
41+
fs.writeFileSync(outputLocation, output);
9042
}
9143
} else {
9244
process.stdout.write(output);

lib/args.js

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
var args = require('yargs')
1+
var documentation = require('../'),
2+
path = require('path'),
3+
loadConfig = require('../lib/load_config.js');
4+
5+
var yargs = require('yargs')
26
.usage('Usage: $0 <command> [options]')
37

48
.option('f', {
@@ -49,4 +53,74 @@ var args = require('yargs')
4953

5054
.example('$0 foo.js', 'parse documentation in a given file');
5155

52-
module.exports = args;
56+
/**
57+
* Parse and validate command-line options for documentation.
58+
* @param {Array} args The array of arguments to parse; e.g. process.argv.slice(2).
59+
* @return {object} {inputs, options, formatter, formatterOptions, output}
60+
* @private
61+
*/
62+
module.exports = function (args) {
63+
var argv = yargs.parse(args);
64+
65+
var inputs,
66+
name = argv.name,
67+
version = argv.version,
68+
transform;
69+
70+
if (argv._.length > 0) {
71+
inputs = argv._;
72+
} else {
73+
try {
74+
var p = require(path.resolve('package.json'));
75+
inputs = [p.main];
76+
name = name || p.name;
77+
version = version || p.version;
78+
if (p.browserify && p.browserify.transform) {
79+
transform = p.browserify.transform;
80+
}
81+
} catch (e) {
82+
yargs.showHelp();
83+
throw new Error('documentation was given no files and was not run in a module directory');
84+
}
85+
}
86+
87+
if (!documentation.formats[argv.f]) {
88+
yargs.showHelp();
89+
throw new Error('Formatter not found');
90+
}
91+
92+
if (argv.f === 'html' && argv.o === 'stdout') {
93+
yargs.showHelp();
94+
throw new Error('The HTML output mode requires a destination directory set with -o');
95+
}
96+
97+
var formatterOptions = {
98+
};
99+
100+
var config = {};
101+
102+
if (argv.config) {
103+
config = loadConfig(argv.config);
104+
}
105+
106+
return {
107+
inputs: inputs,
108+
options: {
109+
private: argv.private,
110+
transform: transform,
111+
github: argv.github,
112+
polyglot: argv.polyglot,
113+
order: config.order || [],
114+
shallow: argv.shallow
115+
},
116+
formatter: argv.f,
117+
formatterOptions: {
118+
name: name,
119+
version: version,
120+
theme: argv.theme
121+
},
122+
output: argv.o
123+
}
124+
}
125+
126+

0 commit comments

Comments
 (0)