Skip to content

Commit 257b34d

Browse files
novemberbornsindresorhus
authored andcommitted
Upgrade XO (#1080)
* Avoid running XO on older Node.js versions * xo@^0.17.0 * Split up cli.js Move the actual implementation into lib/cli.js to avoid top-level returns. xo@^0.17.0 cannot parse the file with those returns present. Throw errors from lib/cli.js and write them to stderr in cli.js before exiting. XO doesn't appreciate process.exit() calls in modules. Note that the error for invalid Babel config is now written to stderr, which seems like an improvement to me. * Actually register loud-rejection
1 parent 3ea2ba1 commit 257b34d

File tree

12 files changed

+200
-199
lines changed

12 files changed

+200
-199
lines changed

cli.js

Lines changed: 7 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -13,187 +13,16 @@ var localCLI = resolveCwd('ava/cli');
1313
// see https://github.com/nodejs/node/issues/6624
1414
if (localCLI && path.relative(localCLI, __filename) !== '') {
1515
debug('Using local install of AVA');
16-
require(localCLI);
17-
return;
18-
}
19-
20-
if (debug.enabled) {
21-
require('time-require');
22-
}
23-
24-
var updateNotifier = require('update-notifier');
25-
var figures = require('figures');
26-
var arrify = require('arrify');
27-
var meow = require('meow');
28-
var Promise = require('bluebird');
29-
var pkgConf = require('pkg-conf');
30-
var isCi = require('is-ci');
31-
var hasFlag = require('has-flag');
32-
var colors = require('./lib/colors');
33-
var verboseReporter = require('./lib/reporters/verbose');
34-
var miniReporter = require('./lib/reporters/mini');
35-
var tapReporter = require('./lib/reporters/tap');
36-
var Logger = require('./lib/logger');
37-
var Watcher = require('./lib/watcher');
38-
var babelConfig = require('./lib/babel-config');
39-
var Api = require('./api');
40-
41-
// Bluebird specific
42-
Promise.longStackTraces();
43-
44-
var conf = pkgConf.sync('ava');
45-
46-
var pkgDir = path.dirname(pkgConf.filepath(conf));
47-
48-
try {
49-
conf.babel = babelConfig.validate(conf.babel);
50-
} catch (err) {
51-
console.log('\n ' + err.message);
52-
process.exit(1);
53-
}
54-
55-
var cli = meow([
56-
'Usage',
57-
' ava [<file|directory|glob> ...]',
58-
'',
59-
'Options',
60-
' --init Add AVA to your project',
61-
' --fail-fast Stop after first test failure',
62-
' --serial, -s Run tests serially',
63-
' --tap, -t Generate TAP output',
64-
' --verbose, -v Enable verbose output',
65-
' --no-cache Disable the transpiler cache',
66-
' --no-power-assert Disable Power Assert',
67-
' --match, -m Only run tests with matching title (Can be repeated)',
68-
' --watch, -w Re-run tests when tests and source files change',
69-
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
70-
' --timeout, -T Set global timeout',
71-
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
72-
'',
73-
'Examples',
74-
' ava',
75-
' ava test.js test2.js',
76-
' ava test-*.js',
77-
' ava test',
78-
' ava --init',
79-
' ava --init foo.js',
80-
'',
81-
'Default patterns when no arguments:',
82-
'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js'
83-
], {
84-
string: [
85-
'_',
86-
'timeout',
87-
'source',
88-
'match',
89-
'concurrency'
90-
],
91-
boolean: [
92-
'fail-fast',
93-
'verbose',
94-
'serial',
95-
'tap',
96-
'watch'
97-
],
98-
default: conf,
99-
alias: {
100-
t: 'tap',
101-
v: 'verbose',
102-
s: 'serial',
103-
m: 'match',
104-
w: 'watch',
105-
S: 'source',
106-
T: 'timeout',
107-
c: 'concurrency'
108-
}
109-
});
110-
111-
updateNotifier({pkg: cli.pkg}).notify();
112-
113-
if (cli.flags.init) {
114-
require('ava-init')();
115-
return;
116-
}
117-
118-
if (
119-
((hasFlag('--watch') || hasFlag('-w')) && (hasFlag('--tap') || hasFlag('-t'))) ||
120-
(conf.watch && conf.tap)
121-
) {
122-
console.error(' ' + colors.error(figures.cross) + ' The TAP reporter is not available when using watch mode.');
123-
process.exit(1);
124-
}
125-
126-
if (hasFlag('--require') || hasFlag('-r')) {
127-
console.error(' ' + colors.error(figures.cross) + ' The --require and -r flags are deprecated. Requirements should be configured in package.json - see documentation.');
128-
process.exit(1);
129-
}
130-
131-
var api = new Api({
132-
failFast: cli.flags.failFast,
133-
serial: cli.flags.serial,
134-
require: arrify(conf.require),
135-
cacheEnabled: cli.flags.cache !== false,
136-
powerAssert: cli.flags.powerAssert !== false,
137-
explicitTitles: cli.flags.watch,
138-
match: arrify(cli.flags.match),
139-
babelConfig: conf.babel,
140-
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
141-
pkgDir: pkgDir,
142-
timeout: cli.flags.timeout,
143-
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0
144-
});
145-
146-
var reporter;
147-
148-
if (cli.flags.tap && !cli.flags.watch) {
149-
reporter = tapReporter();
150-
} else if (cli.flags.verbose || isCi) {
151-
reporter = verboseReporter();
16+
require(localCLI); // eslint-disable-line import/no-dynamic-require
15217
} else {
153-
reporter = miniReporter({watching: cli.flags.watch});
154-
}
155-
156-
reporter.api = api;
157-
var logger = new Logger(reporter);
158-
159-
logger.start();
160-
161-
api.on('test-run', function (runStatus) {
162-
reporter.api = runStatus;
163-
runStatus.on('test', logger.test);
164-
runStatus.on('error', logger.unhandledError);
165-
166-
runStatus.on('stdout', logger.stdout);
167-
runStatus.on('stderr', logger.stderr);
168-
});
169-
170-
var files = cli.input.length ? cli.input : arrify(conf.files);
18+
if (debug.enabled) {
19+
require('time-require'); // eslint-disable-line import/no-unassigned-import
20+
}
17121

172-
if (cli.flags.watch) {
17322
try {
174-
var watcher = new Watcher(logger, api, files, arrify(cli.flags.source));
175-
watcher.observeStdin(process.stdin);
23+
require('./lib/cli').run();
17624
} catch (err) {
177-
if (err.name === 'AvaError') {
178-
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
179-
console.error(' ' + colors.error(figures.cross) + ' ' + err.message);
180-
logger.exit(1);
181-
} else {
182-
// Rethrow so it becomes an uncaught exception.
183-
throw err;
184-
}
25+
console.error('\n ' + err.message);
26+
process.exit(1);
18527
}
186-
} else {
187-
api.run(files)
188-
.then(function (runStatus) {
189-
logger.finish(runStatus);
190-
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
191-
})
192-
.catch(function (err) {
193-
// Don't swallow exceptions. Note that any expected error should already
194-
// have been logged.
195-
setImmediate(function () {
196-
throw err;
197-
});
198-
});
19928
}

lib/cli.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
'use strict';
2+
var path = require('path');
3+
var updateNotifier = require('update-notifier');
4+
var figures = require('figures');
5+
var arrify = require('arrify');
6+
var meow = require('meow');
7+
var Promise = require('bluebird');
8+
var pkgConf = require('pkg-conf');
9+
var isCi = require('is-ci');
10+
var hasFlag = require('has-flag');
11+
var Api = require('../api');
12+
var colors = require('./colors');
13+
var verboseReporter = require('./reporters/verbose');
14+
var miniReporter = require('./reporters/mini');
15+
var tapReporter = require('./reporters/tap');
16+
var Logger = require('./logger');
17+
var Watcher = require('./watcher');
18+
var babelConfig = require('./babel-config');
19+
20+
// Bluebird specific
21+
Promise.longStackTraces();
22+
23+
exports.run = function () {
24+
var conf = pkgConf.sync('ava');
25+
var pkgDir = path.dirname(pkgConf.filepath(conf));
26+
27+
var cli = meow([
28+
'Usage',
29+
' ava [<file|directory|glob> ...]',
30+
'',
31+
'Options',
32+
' --init Add AVA to your project',
33+
' --fail-fast Stop after first test failure',
34+
' --serial, -s Run tests serially',
35+
' --tap, -t Generate TAP output',
36+
' --verbose, -v Enable verbose output',
37+
' --no-cache Disable the transpiler cache',
38+
' --no-power-assert Disable Power Assert',
39+
' --match, -m Only run tests with matching title (Can be repeated)',
40+
' --watch, -w Re-run tests when tests and source files change',
41+
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
42+
' --timeout, -T Set global timeout',
43+
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
44+
'',
45+
'Examples',
46+
' ava',
47+
' ava test.js test2.js',
48+
' ava test-*.js',
49+
' ava test',
50+
' ava --init',
51+
' ava --init foo.js',
52+
'',
53+
'Default patterns when no arguments:',
54+
'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js'
55+
], {
56+
string: [
57+
'_',
58+
'timeout',
59+
'source',
60+
'match',
61+
'concurrency'
62+
],
63+
boolean: [
64+
'fail-fast',
65+
'verbose',
66+
'serial',
67+
'tap',
68+
'watch'
69+
],
70+
default: conf,
71+
alias: {
72+
t: 'tap',
73+
v: 'verbose',
74+
s: 'serial',
75+
m: 'match',
76+
w: 'watch',
77+
S: 'source',
78+
T: 'timeout',
79+
c: 'concurrency'
80+
}
81+
});
82+
83+
updateNotifier({pkg: cli.pkg}).notify();
84+
85+
if (cli.flags.init) {
86+
require('ava-init')();
87+
return;
88+
}
89+
90+
if (
91+
((hasFlag('--watch') || hasFlag('-w')) && (hasFlag('--tap') || hasFlag('-t'))) ||
92+
(conf.watch && conf.tap)
93+
) {
94+
throw new Error(colors.error(figures.cross) + ' The TAP reporter is not available when using watch mode.');
95+
}
96+
97+
if (hasFlag('--require') || hasFlag('-r')) {
98+
throw new Error(colors.error(figures.cross) + ' The --require and -r flags are deprecated. Requirements should be configured in package.json - see documentation.');
99+
}
100+
101+
var api = new Api({
102+
failFast: cli.flags.failFast,
103+
serial: cli.flags.serial,
104+
require: arrify(conf.require),
105+
cacheEnabled: cli.flags.cache !== false,
106+
powerAssert: cli.flags.powerAssert !== false,
107+
explicitTitles: cli.flags.watch,
108+
match: arrify(cli.flags.match),
109+
babelConfig: babelConfig.validate(conf.babel),
110+
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
111+
pkgDir: pkgDir,
112+
timeout: cli.flags.timeout,
113+
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0
114+
});
115+
116+
var reporter;
117+
118+
if (cli.flags.tap && !cli.flags.watch) {
119+
reporter = tapReporter();
120+
} else if (cli.flags.verbose || isCi) {
121+
reporter = verboseReporter();
122+
} else {
123+
reporter = miniReporter({watching: cli.flags.watch});
124+
}
125+
126+
reporter.api = api;
127+
var logger = new Logger(reporter);
128+
129+
logger.start();
130+
131+
api.on('test-run', function (runStatus) {
132+
reporter.api = runStatus;
133+
runStatus.on('test', logger.test);
134+
runStatus.on('error', logger.unhandledError);
135+
136+
runStatus.on('stdout', logger.stdout);
137+
runStatus.on('stderr', logger.stderr);
138+
});
139+
140+
var files = cli.input.length ? cli.input : arrify(conf.files);
141+
142+
if (cli.flags.watch) {
143+
try {
144+
var watcher = new Watcher(logger, api, files, arrify(cli.flags.source));
145+
watcher.observeStdin(process.stdin);
146+
} catch (err) {
147+
if (err.name === 'AvaError') {
148+
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
149+
console.error(' ' + colors.error(figures.cross) + ' ' + err.message);
150+
logger.exit(1);
151+
} else {
152+
// Rethrow so it becomes an uncaught exception.
153+
throw err;
154+
}
155+
}
156+
} else {
157+
api.run(files)
158+
.then(function (runStatus) {
159+
logger.finish(runStatus);
160+
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
161+
})
162+
.catch(function (err) {
163+
// Don't swallow exceptions. Note that any expected error should already
164+
// have been logged.
165+
setImmediate(function () {
166+
throw err;
167+
});
168+
});
169+
}
170+
};

lib/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ Logger.prototype.exit = function (code) {
9898

9999
// timeout required to correctly flush IO on Node.js 0.10 on Windows
100100
setTimeout(function () {
101-
process.exit(code); // eslint-disable-line xo/no-process-exit
101+
process.exit(code); // eslint-disable-line unicorn/no-process-exit
102102
}, process.env.AVA_APPVEYOR ? 500 : 0);
103103
};

lib/process-adapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (!isForked) {
1616
console.log();
1717
console.error('Test files must be run with the AVA CLI:\n\n ' + chalk.grey.dim('$') + ' ' + chalk.cyan('ava ' + fp) + '\n');
1818

19-
process.exit(1); // eslint-disable-line xo/no-process-exit
19+
process.exit(1); // eslint-disable-line unicorn/no-process-exit
2020
}
2121

2222
exports.send = function (name, data) {

0 commit comments

Comments
 (0)