-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcli.js
101 lines (83 loc) · 2.5 KB
/
cli.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
let doc = `
Usage:
sassdoc - [options]
sassdoc <src>... [options]
sassdoc [options]
Arguments:
<src> Path to your Sass folder.
Options:
-h, --help Bring help.
--version Show version.
-v, --verbose Enable verbose mode.
-d, --dest=<dir> Documentation folder [default: sassdoc].
-c, --config=<path> Path to JSON/YAML configuration file.
-t, --theme=<name> Theme to use.
-p, --parse Parse the input and output JSON data to stdout.
--no-update-notifier Disable update notifier check.
--strict Turn warnings into errors.
--debug Output debugging information.
`;
const docopt = require('docopt').docopt;
const source = require('vinyl-source-stream');
const pkg = require('../package.json');
const Environment = require('./environment');
const Logger = require('./logger');
const sassdoc = require('./sassdoc');
const errors = require('./errors');
export default function cli(argv = process.argv.slice(2)) {
let options = docopt(doc, { version: pkg.version, argv: argv });
let logger = new Logger(options['--verbose'], options['--debug'] || process.env.SASSDOC_DEBUG);
let env = new Environment(logger, options['--strict']);
logger.debug('argv:', () => JSON.stringify(argv));
env.on('error', error => {
if (error instanceof errors.Warning) {
process.exit(2);
}
process.exit(1);
});
env.load(options['--config']);
// Ensure CLI options.
ensure(env, options, {
dest: '--dest',
theme: '--theme',
noUpdateNotifier: '--no-update-notifier',
});
env.postProcess();
// Run update notifier if not explicitely disabled.
if (!env.noUpdateNotifier) {
require('./notifier')(pkg, logger);
}
let handler, cb;
// Whether to parse only or to documentize.
if (!options['--parse']) {
handler = sassdoc;
cb = () => {};
} else {
handler = sassdoc.parse;
cb = data => console.log(JSON.stringify(data, null, 2));
}
if (options['-']) {
return process.stdin
.pipe(source())
.pipe(handler(env))
.on('data', cb);
}
if (!options['<src>'].length) {
options['<src>'].push('.');
}
handler(options['<src>'], env).then(cb);
}
/**
* Ensure that CLI options take precedence over configuration values.
*
* For each name/option tuple, if the option is set, override configuration
* value.
*/
function ensure(env, options, names) {
for (let k of Object.keys(names)) {
let v = names[k];
if (options[v]) {
env[k] = options[v];
}
}
}