|
1 |
| -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 |
| -var stream = require('stream'); |
4 |
| -var engine = require('unified-engine'); |
5 |
| -var chalk = require('chalk'); |
6 |
| -var chokidar = require('chokidar'); |
7 |
| -var options = require('./options'); |
| 3 | +var stream = require('stream') |
| 4 | +var engine = require('unified-engine') |
| 5 | +var chalk = require('chalk') |
| 6 | +var chokidar = require('chokidar') |
| 7 | +var options = require('./options') |
8 | 8 |
|
9 |
| -module.exports = start; |
| 9 | +module.exports = start |
10 | 10 |
|
11 | 11 | /* No-op. */
|
12 |
| -var noop = Function.prototype; |
| 12 | +var noop = Function.prototype |
13 | 13 |
|
14 | 14 | /* Fake TTY stream. */
|
15 |
| -var ttyStream = new stream.Readable(); |
16 |
| -ttyStream.isTTY = true; |
| 15 | +var ttyStream = new stream.Readable() |
| 16 | +ttyStream.isTTY = true |
17 | 17 |
|
18 | 18 | /* Exit, lazily, with the correct exit status code. */
|
19 |
| -var exitStatus = 0; |
| 19 | +var exitStatus = 0 |
20 | 20 |
|
21 |
| -process.on('exit', onexit); |
| 21 | +process.on('exit', onexit) |
22 | 22 |
|
23 | 23 | /* Handle uncaught errors, such as from unexpected async
|
24 | 24 | * behaviour. */
|
25 |
| -process.on('uncaughtException', fail); |
| 25 | +process.on('uncaughtException', fail) |
26 | 26 |
|
27 | 27 | /* Start the CLI. */
|
28 |
| -function start(configuration) { |
29 |
| - var config; |
30 |
| - var output; |
31 |
| - var watcher; |
| 28 | +function start(cliConfig) { |
| 29 | + var config |
| 30 | + var output |
| 31 | + var watcher |
32 | 32 |
|
33 | 33 | try {
|
34 |
| - config = options(process.argv.slice(2), configuration); |
| 34 | + config = options(process.argv.slice(2), cliConfig) |
35 | 35 | } catch (err) {
|
36 |
| - return fail(err, true); |
| 36 | + return fail(err, true) |
37 | 37 | }
|
38 | 38 |
|
39 | 39 | if (config.help) {
|
40 |
| - process.stderr.write([ |
41 |
| - 'Usage: ' + configuration.name + ' [options] [path | glob ...]', |
42 |
| - '', |
43 |
| - ' ' + configuration.description, |
44 |
| - '', |
45 |
| - 'Options:', |
46 |
| - '', |
47 |
| - config.helpMessage |
48 |
| - ].join('\n') + '\n', noop); |
49 |
| - |
50 |
| - return; |
| 40 | + process.stderr.write( |
| 41 | + [ |
| 42 | + 'Usage: ' + cliConfig.name + ' [options] [path | glob ...]', |
| 43 | + '', |
| 44 | + ' ' + cliConfig.description, |
| 45 | + '', |
| 46 | + 'Options:', |
| 47 | + '', |
| 48 | + config.helpMessage, |
| 49 | + '' |
| 50 | + ].join('\n'), |
| 51 | + noop |
| 52 | + ) |
| 53 | + |
| 54 | + return |
51 | 55 | }
|
52 | 56 |
|
53 | 57 | if (config.version) {
|
54 |
| - process.stderr.write(configuration.version + '\n', noop); |
| 58 | + process.stderr.write(cliConfig.version + '\n', noop) |
55 | 59 |
|
56 |
| - return; |
| 60 | + return |
57 | 61 | }
|
58 | 62 |
|
59 | 63 | /* Modify `config` for watching. */
|
60 | 64 | if (config.watch) {
|
61 |
| - output = config.output; |
| 65 | + output = config.output |
62 | 66 |
|
63 | 67 | /* Do not read from stdin(4). */
|
64 |
| - config.streamIn = ttyStream; |
| 68 | + config.streamIn = ttyStream |
65 | 69 |
|
66 | 70 | /* Do not write to stdout(4). */
|
67 |
| - config.out = false; |
| 71 | + config.out = false |
68 | 72 |
|
69 | 73 | process.stderr.write(
|
70 | 74 | chalk.bold('Watching...') + ' (press CTRL+C to exit)\n',
|
71 | 75 | noop
|
72 |
| - ); |
| 76 | + ) |
73 | 77 |
|
74 | 78 | /* Prevent infinite loop if set to regeneration. */
|
75 | 79 | if (output === true) {
|
76 |
| - config.output = false; |
| 80 | + config.output = false |
77 | 81 |
|
78 | 82 | process.stderr.write(
|
79 | 83 | chalk.yellow('Note') + ': Ignoring `--output` until exit.\n',
|
80 | 84 | noop
|
81 |
| - ); |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /* Initial run */ |
| 90 | + engine(config, done) |
| 91 | + |
| 92 | + /* Handle complete run. */ |
| 93 | + function done(err, code, context) { |
| 94 | + if (err) { |
| 95 | + clean() |
| 96 | + fail(err) |
| 97 | + } else { |
| 98 | + exitStatus = code |
| 99 | + |
| 100 | + if (config.watch && !watcher) { |
| 101 | + subscribe(context) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /* Clean the watcher. */ |
| 107 | + function clean() { |
| 108 | + if (watcher) { |
| 109 | + watcher.close() |
| 110 | + watcher = null |
82 | 111 | }
|
83 | 112 | }
|
84 | 113 |
|
85 | 114 | /* Subscribe a chokidar watcher to all processed files. */
|
86 | 115 | function subscribe(context) {
|
87 | 116 | watcher = chokidar
|
88 |
| - .watch(context.fileSet.origins, { |
89 |
| - cwd: config.cwd, |
90 |
| - ignoreInitial: true |
91 |
| - }) |
| 117 | + .watch(context.fileSet.origins, {cwd: config.cwd, ignoreInitial: true}) |
92 | 118 | .on('error', done)
|
93 |
| - .on('change', onchange); |
| 119 | + .on('change', onchange) |
94 | 120 |
|
95 |
| - process.on('SIGINT', onsigint); |
| 121 | + process.on('SIGINT', onsigint) |
96 | 122 |
|
97 | 123 | function onchange(filePath) {
|
98 |
| - config.files = [filePath]; |
| 124 | + config.files = [filePath] |
99 | 125 |
|
100 |
| - engine(config, done); |
| 126 | + engine(config, done) |
101 | 127 | }
|
102 | 128 |
|
103 | 129 | function onsigint() {
|
104 | 130 | /* Hide the `^C` in terminal. */
|
105 |
| - process.stderr.write('\n', noop); |
| 131 | + process.stderr.write('\n', noop) |
106 | 132 |
|
107 |
| - clean(); |
| 133 | + clean() |
108 | 134 |
|
109 | 135 | /* Do another process if `output` specified regeneration. */
|
110 | 136 | if (output === true) {
|
111 |
| - config.output = output; |
112 |
| - config.watch = false; |
113 |
| - engine(config, done); |
| 137 | + config.output = output |
| 138 | + config.watch = false |
| 139 | + engine(config, done) |
114 | 140 | }
|
115 | 141 | }
|
116 | 142 | }
|
117 |
| - |
118 |
| - /* Initial run */ |
119 |
| - engine(config, done); |
120 |
| - |
121 |
| - /* Handle complete run. */ |
122 |
| - function done(err, code, context) { |
123 |
| - if (err) { |
124 |
| - clean(); |
125 |
| - fail(err); |
126 |
| - } else { |
127 |
| - exitStatus = code; |
128 |
| - |
129 |
| - if (config.watch && !watcher) { |
130 |
| - subscribe(context); |
131 |
| - } |
132 |
| - } |
133 |
| - } |
134 |
| - |
135 |
| - /* Clean the watcher. */ |
136 |
| - function clean() { |
137 |
| - if (watcher) { |
138 |
| - watcher.close(); |
139 |
| - watcher = null; |
140 |
| - } |
141 |
| - } |
142 | 143 | }
|
143 | 144 |
|
144 | 145 | /* Print an error, optionally with stack. */
|
145 | 146 | function fail(err, pretty) {
|
146 |
| - var message = (pretty ? String(err).trim() : err.stack) || err; |
| 147 | + var message = (pretty ? String(err).trim() : err.stack) || err |
147 | 148 |
|
148 |
| - exitStatus = 1; |
| 149 | + exitStatus = 1 |
149 | 150 |
|
150 |
| - process.stderr.write(message.trim() + '\n', noop); |
| 151 | + process.stderr.write(message.trim() + '\n', noop) |
151 | 152 | }
|
152 | 153 |
|
153 | 154 | function onexit() {
|
154 | 155 | /* eslint-disable unicorn/no-process-exit */
|
155 |
| - process.exit(exitStatus); |
| 156 | + process.exit(exitStatus) |
156 | 157 | }
|
0 commit comments