forked from get-alex/alex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·173 lines (161 loc) · 4.67 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
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
#!/usr/bin/env node
import fs from 'node:fs'
import process from 'node:process'
import {URL} from 'node:url'
import notifier from 'update-notifier'
import supportsColor from 'supports-color'
import meow from 'meow'
import {engine} from 'unified-engine'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import remarkParse from 'remark-parse'
import remarkFrontmatter from 'remark-frontmatter'
import remarkGfm from 'remark-gfm'
import remarkMdx from 'remark-mdx'
import retextEnglish from 'retext-english'
import remarkRetext from 'remark-retext'
import rehypeRetext from 'rehype-retext'
import vfileReporter from 'vfile-reporter'
import retextEquality from 'retext-equality'
import retextProfanities from 'retext-profanities'
import unifiedDiff from 'unified-diff'
import {filter} from './filter.js'
/** @type {import('type-fest').PackageJson} */
const pack = JSON.parse(
String(fs.readFileSync(new URL('package.json', import.meta.url)))
)
const textExtensions = [
'txt',
'text',
'md',
'markdown',
'mkd',
'mkdn',
'mkdown',
'ron'
]
const htmlExtensions = ['htm', 'html']
const mdxExtensions = ['mdx']
// Update messages.
/** @ts-expect-error: `package.json` is fine. */
notifier({pkg: pack}).notify()
// Set-up meow.
const cli = meow(
[
'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)',
' -l, --html treat input as html (not markdown)',
' --mdx treat input as mdx (not markdown)',
' -d, --diff ignore unchanged lines (affects Travis only)',
' --reporter=REPORTER use a custom vfile-reporter',
' --stdin read from stdin',
'',
'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 --stdin',
' $ alex *.md !example.md',
' $ alex'
].join('\n'),
{
importMeta: import.meta,
flags: {
version: {type: 'boolean', alias: 'v'},
help: {type: 'boolean', alias: 'h'},
stdin: {type: 'boolean'},
text: {type: 'boolean', alias: 't'},
mdx: {type: 'boolean'},
html: {type: 'boolean', alias: 'l'},
diff: {type: 'boolean', alias: 'd'},
reporter: {type: 'string'},
quiet: {type: 'boolean', alias: 'q'},
why: {type: 'boolean', alias: 'w'}
}
}
)
// Set-up.
const extensions = cli.flags.html
? htmlExtensions
: cli.flags.mdx
? mdxExtensions
: textExtensions
const defaultGlobs = ['{docs/**/,doc/**/,}*.{' + extensions.join(',') + '}']
/** @type {boolean|undefined} */
let silentlyIgnore
/** @type {string[]|undefined} */
let globs
if (cli.flags.stdin) {
if (cli.input.length > 0) {
throw new Error('Do not pass globs with `--stdin`')
}
} else if (cli.input.length === 0) {
globs = defaultGlobs
silentlyIgnore = true
} else {
globs = cli.input
}
engine(
{
processor: unified(),
files: globs,
extensions,
configTransform: transform,
out: false,
output: false,
rcName: '.alexrc',
packageField: 'alex',
color: Boolean(supportsColor.stderr),
reporter: cli.flags.reporter || vfileReporter,
reporterOptions: {
verbose: cli.flags.why
},
quiet: cli.flags.quiet,
ignoreName: '.alexignore',
silentlyIgnore,
frail: true,
defaultConfig: transform({})
},
function (error, code) {
if (error) console.error(error.message)
process.exit(code)
}
)
/**
* @param {import('./index.js').OptionsObject} [options]
*/
function transform(options = {}) {
/** @type {import('unified').PluggableList} */
let plugins = [
retextEnglish,
[retextProfanities, {sureness: options.profanitySureness}],
[retextEquality, {noBinary: options.noBinary}]
]
if (cli.flags.html) {
plugins = [rehypeParse, [rehypeRetext, unified().use({plugins})]]
} else if (cli.flags.mdx) {
// @ts-expect-error: types are having a hard time for bridges.
plugins = [remarkParse, remarkMdx, [remarkRetext, unified().use({plugins})]]
} else if (!cli.flags.text) {
plugins = [
// @ts-expect-error: hush.
remarkParse,
remarkGfm,
[remarkFrontmatter, ['yaml', 'toml']],
// @ts-expect-error: types are having a hard time for bridges.
[remarkRetext, unified().use({plugins})]
]
}
plugins.push([filter, {allow: options.allow, deny: options.deny}])
// Hard to check.
/* c8 ignore next 3 */
if (cli.flags.diff) {
plugins.push(unifiedDiff)
}
return {plugins}
}