Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

feat: allow --reporter to be relative to CWD #24

Merged
merged 5 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

const camelcase = require('camelcase');
const capitalize = require('titleize');
const merge = require('merge-options');
Expand Down Expand Up @@ -34,23 +36,31 @@ function makeCallback(options) {
/**
* Attempts to require a specified reporter. Tries the local built-ins first,
* and if that fails, attempts to load an npm module that exports a reporter
* handler function.
* handler function, and if that fails, attempts to load the reporter relative
* to the current working directory.
*/
function requireReporter(name, local = true) {
const prefix = capitalize(camelcase(name));
const target = local ? `./reporters/${prefix}Reporter` : name;

function requireReporter(name) {
try {
const prefix = capitalize(camelcase(name));
// eslint-disable-next-line import/no-dynamic-require, global-require
const result = require(target);
const result = require(`./reporters/${prefix}Reporter`);
return result;
} catch (e) {
if (local) {
return requireReporter(name, false);
} catch (e1) {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
const result = require(name);
return result;
} catch (e2) {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
const result = require(path.resolve(name));
return result;
} catch (e3) {
// empty
}
}

return null;
}
return null;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/tests/__snapshots__/cli.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Commands > should accept custom reporters relative to the current working directory #0 1`] = `
"ℹ 「webpack」: Starting Build
ℹ 「webpack」: Build Finished
Hash: 953ce188d35af8c002f0
<version>
<duration>
<datetime>
Asset Size Chunks Chunk Names
main.js 573 bytes 0 [emitted] main
Entrypoint main = main.js
[0] ../fixtures/flags/config/src/index.js 43 bytes {0} [built]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/"
`;

exports[`Commands > should add directories to entries #0 1`] = `
"ℹ 「webpack」: Starting Build
ℹ 「webpack」: Build Finished
Expand Down
19 changes: 19 additions & 0 deletions test/tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,23 @@ test('Commands', module, () => {
strip(result.stdout).replace(/Δt \d+ms/g, '<duration>')
).toMatchSnapshot();
}).timeout(4000);

it('should accept custom reporters relative to the current working directory', () => {
const cliPath = resolve(__dirname, '../../lib/cli.js');
const srcPath = resolve(__dirname, '../../test/fixtures/flags/config/src');
const result = execa.sync(
cliPath,
['--reporter', '../../lib/reporters/BasicReporter', srcPath],
{
cwd: __dirname,
}
);

expect(
strip(result.stdout)
.replace(/Version: webpack \d\.\d\.\d/, '<version>')
.replace(/Time: \d+ms/g, '<duration>')
.replace(/Built at: .+(?=\n)/g, '<datetime>')
).toMatchSnapshot();
}).timeout(4000);
});