forked from get-alex/alex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·130 lines (115 loc) · 2.74 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
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
#!/usr/bin/env node
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
* @module alex
* @fileoverview CLI for alex.
*/
'use strict';
/* Dependencies. */
var PassThrough = require('stream').PassThrough;
var notifier = require('update-notifier');
var meow = require('meow');
var engine = require('unified-engine');
var unified = require('unified');
var markdown = require('remark-parse');
var english = require('retext-english');
var remark2retext = require('remark-retext');
var report = require('vfile-reporter');
var equality = require('retext-equality');
var profanities = require('retext-profanities');
var diff = require('unified-diff');
var pack = require('./package');
var filter = require('./filter');
var extensions = [
'txt',
'text',
'md',
'markdown',
'mkd',
'mkdn',
'mkdown',
'ron'
];
/* Update messages. */
notifier({pkg: pack}).notify();
/* Set-up meow. */
var cli = meow({
help: [
'Usage: alex [<glob> ...] [options ...]',
'',
'Options:',
'',
' -w, --why output sources (when available)',
' -q, --quiet output only warnings and errors',
' -t, --text treat input as plain-text (not markdown)',
' -d, --diff ignore unchanged lines (affects Travis only)',
'',
'When no input files are given, searches for markdown and text',
'files in the current directory, `doc`, and `docs`.',
'',
'Examples',
' $ echo "His network looks good" | alex',
' $ alex *.md !example.md',
' $ alex'
]
}, {
alias: {
v: 'version',
h: 'help',
t: 'text',
d: 'diff',
q: 'quiet',
w: 'why'
}
});
/* Set-up. */
var globs = ['{docs/**/,doc/**/,}*.{' + extensions.join(',') + '}'];
/* istanbul ignore else - Bug in tests. Something hangs, at least. */
if (cli.input.length !== 0) {
globs = cli.input;
}
engine({
processor: unified(),
files: globs,
extensions: extensions,
configTransform: transform,
output: false,
out: false,
streamError: new PassThrough(),
rcName: '.alexrc',
packageField: 'alex',
ignoreName: '.alexignore',
frail: true,
defaultConfig: transform()
}, function (err, code, result) {
var out = report(err || result.files, {
verbose: cli.flags.why,
quiet: cli.flags.quiet
});
if (out) {
console.error(out);
}
process.exit(code);
});
function transform(options) {
var settings = options || {};
var plugins = [
english,
profanities,
[equality, {noBinary: settings.noBinary}]
];
if (!cli.flags.text) {
plugins = [
markdown,
[remark2retext, unified().use({plugins: plugins})]
];
}
plugins.push([filter, {allow: settings.allow}]);
/* istanbul ignore if - hard to check. */
if (cli.flags.diff) {
plugins.push(diff);
}
return {plugins: plugins};
}