Skip to content

Commit 4852b1a

Browse files
committedMay 10, 2018
Refactor code-style
1 parent b1ae985 commit 4852b1a

File tree

14 files changed

+774
-811
lines changed

14 files changed

+774
-811
lines changed
 

‎.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage/

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
'use strict';
2-
module.exports = require('./lib');
1+
'use strict'
2+
module.exports = require('./lib')

‎lib/index.js

+81-80
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,157 @@
1-
'use strict';
1+
'use strict'
22

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')
88

9-
module.exports = start;
9+
module.exports = start
1010

1111
/* No-op. */
12-
var noop = Function.prototype;
12+
var noop = Function.prototype
1313

1414
/* Fake TTY stream. */
15-
var ttyStream = new stream.Readable();
16-
ttyStream.isTTY = true;
15+
var ttyStream = new stream.Readable()
16+
ttyStream.isTTY = true
1717

1818
/* Exit, lazily, with the correct exit status code. */
19-
var exitStatus = 0;
19+
var exitStatus = 0
2020

21-
process.on('exit', onexit);
21+
process.on('exit', onexit)
2222

2323
/* Handle uncaught errors, such as from unexpected async
2424
* behaviour. */
25-
process.on('uncaughtException', fail);
25+
process.on('uncaughtException', fail)
2626

2727
/* 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
3232

3333
try {
34-
config = options(process.argv.slice(2), configuration);
34+
config = options(process.argv.slice(2), cliConfig)
3535
} catch (err) {
36-
return fail(err, true);
36+
return fail(err, true)
3737
}
3838

3939
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
5155
}
5256

5357
if (config.version) {
54-
process.stderr.write(configuration.version + '\n', noop);
58+
process.stderr.write(cliConfig.version + '\n', noop)
5559

56-
return;
60+
return
5761
}
5862

5963
/* Modify `config` for watching. */
6064
if (config.watch) {
61-
output = config.output;
65+
output = config.output
6266

6367
/* Do not read from stdin(4). */
64-
config.streamIn = ttyStream;
68+
config.streamIn = ttyStream
6569

6670
/* Do not write to stdout(4). */
67-
config.out = false;
71+
config.out = false
6872

6973
process.stderr.write(
7074
chalk.bold('Watching...') + ' (press CTRL+C to exit)\n',
7175
noop
72-
);
76+
)
7377

7478
/* Prevent infinite loop if set to regeneration. */
7579
if (output === true) {
76-
config.output = false;
80+
config.output = false
7781

7882
process.stderr.write(
7983
chalk.yellow('Note') + ': Ignoring `--output` until exit.\n',
8084
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
82111
}
83112
}
84113

85114
/* Subscribe a chokidar watcher to all processed files. */
86115
function subscribe(context) {
87116
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})
92118
.on('error', done)
93-
.on('change', onchange);
119+
.on('change', onchange)
94120

95-
process.on('SIGINT', onsigint);
121+
process.on('SIGINT', onsigint)
96122

97123
function onchange(filePath) {
98-
config.files = [filePath];
124+
config.files = [filePath]
99125

100-
engine(config, done);
126+
engine(config, done)
101127
}
102128

103129
function onsigint() {
104130
/* Hide the `^C` in terminal. */
105-
process.stderr.write('\n', noop);
131+
process.stderr.write('\n', noop)
106132

107-
clean();
133+
clean()
108134

109135
/* Do another process if `output` specified regeneration. */
110136
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)
114140
}
115141
}
116142
}
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-
}
142143
}
143144

144145
/* Print an error, optionally with stack. */
145146
function fail(err, pretty) {
146-
var message = (pretty ? String(err).trim() : err.stack) || err;
147+
var message = (pretty ? String(err).trim() : err.stack) || err
147148

148-
exitStatus = 1;
149+
exitStatus = 1
149150

150-
process.stderr.write(message.trim() + '\n', noop);
151+
process.stderr.write(message.trim() + '\n', noop)
151152
}
152153

153154
function onexit() {
154155
/* eslint-disable unicorn/no-process-exit */
155-
process.exit(exitStatus);
156+
process.exit(exitStatus)
156157
}

0 commit comments

Comments
 (0)
Please sign in to comment.