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

Commit

Permalink
feat: allow --reporter to be relative to CWD (#24)
Browse files Browse the repository at this point in the history
* test: pass --reporter values relative to CWD

This test ensures that a --reporter value can be passed with a path
relative to the CWD.

The test should fail with this error:

      1) Commands
          should accept custom reporters relative to the current working directory:
        Command failed: /Users/spencerelliott/Dev/elliottsj/webpack-command/lib/cli.js --reporter ../../lib/reporters/BasicReporter /Users/spencerelliott/Dev/elliottsj/webpack-command/test/fixtures/flags/config/src
    TypeError: ReporterClass is not a constructor
        at module.exports (/Users/spencerelliott/Dev/elliottsj/webpack-command/lib/compiler.js:91:20)
        at load.then (/Users/spencerelliott/Dev/elliottsj/webpack-command/lib/index.js:45:47)
        at process._tickCallback (internal/process/next_tick.js:68:7)
        at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
        at startup (internal/bootstrap/node.js:238:19)
        at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

* feat: allow --reporter to be relative to CWD

Previously, the --reporter path must have been either absolute or in
node_modules or relative to lib/compiler.js.

This change ensures that a path passed to --reporter is resolved relative
to process.cwd().

e.g. you can now run this inside of a project directory to use a custom
reporter:

    webpack --reporter ./MyCustomReporter.js

* refactor: loop rather than nest. makes for a little better readability

* fix: exit the loop if reporter was found

* fix: remove whitespace leftover from github editor
  • Loading branch information
elliottsj authored and shellscape committed Jun 25, 2018
1 parent 5786adc commit a5cff71
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
31 changes: 19 additions & 12 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 chalk = require('chalk');
Expand Down Expand Up @@ -36,23 +38,28 @@ 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) {
function requireReporter(name) {
const prefix = capitalize(camelcase(name));
const target = local ? `./reporters/${prefix}Reporter` : name;

try {
// eslint-disable-next-line import/no-dynamic-require, global-require
const result = require(target);
return result;
} catch (e) {
if (local) {
return requireReporter(name, false);
const locations = [`./reporters/${prefix}Reporter`, name, path.resolve(name)];
let result = null;

for (const location of locations) {
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
result = require(location);
} catch (e) {
// noop
}

return null;
if (result) {
break;
}
}

return result;
}

/**
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);
});

0 comments on commit a5cff71

Please sign in to comment.