-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
158 lines (124 loc) · 3.13 KB
/
index.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
'use strict'
var stream = require('stream')
var engine = require('unified-engine')
var chalk = require('chalk')
var chokidar = require('chokidar')
var options = require('./options')
module.exports = start
var noop = Function.prototype
// Fake TTY stream.
var ttyStream = new stream.Readable()
ttyStream.isTTY = true
// Exit, lazily, with the correct exit status code.
var exitStatus = 0
process.on('exit', onexit)
// Handle uncaught errors, such as from unexpected async behaviour.
process.on('uncaughtException', fail)
// Start the CLI.
function start(cliConfig) {
var config
var output
var watcher
try {
config = options(process.argv.slice(2), cliConfig)
} catch (error) {
return fail(error, true)
}
if (config.help) {
process.stdout.write(
[
'Usage: ' + cliConfig.name + ' [options] [path | glob ...]',
'',
' ' + cliConfig.description,
'',
'Options:',
'',
config.helpMessage,
''
].join('\n'),
noop
)
return
}
if (config.version) {
process.stdout.write(cliConfig.version + '\n', noop)
return
}
// Modify `config` for watching.
if (config.watch) {
output = config.output
// Do not read from stdin(4).
config.streamIn = ttyStream
// Do not write to stdout(4).
config.out = false
process.stderr.write(
chalk.bold('Watching...') + ' (press CTRL+C to exit)\n',
noop
)
// Prevent infinite loop if set to regeneration.
if (output === true) {
config.output = false
process.stderr.write(
chalk.yellow('Note') + ': Ignoring `--output` until exit.\n',
noop
)
}
}
// Initial run.
engine(config, done)
// Handle complete run.
function done(err, code, context) {
if (err) {
clean()
fail(err)
} else {
exitStatus = code
if (config.watch && !watcher) {
subscribe(context)
}
}
}
// Clean the watcher.
function clean() {
if (watcher) {
watcher.close()
watcher = null
}
}
// Subscribe a chokidar watcher to all processed files.
function subscribe(context) {
watcher = chokidar
.watch(context.fileSet.origins, {cwd: config.cwd, ignoreInitial: true})
.on('error', done)
.on('change', onchange)
process.on('SIGINT', onsigint)
function onchange(filePath) {
config.files = [filePath]
engine(config, done)
}
function onsigint() {
// Hide the `^C` in terminal.
process.stderr.write('\n', noop)
clean()
// Do another process if `output` specified regeneration.
if (output === true) {
config.output = output
config.watch = false
engine(config, done)
}
}
}
}
// Print an error, optionally with stack.
function fail(err, pretty) {
var message =
(pretty ? String(err).trim() : err.stack) ||
/* istanbul ignore next - Old versions of Node */ err
exitStatus = 1
process.stderr.write(message.trim() + '\n', noop)
}
function onexit() {
/* eslint-disable unicorn/no-process-exit */
process.exit(exitStatus)
/* eslint-enable unicorn/no-process-exit */
}