webpack-dashboard/utils/format-problems.js
Ryan Roemer 5135858040 Add more project root inference for versions (aka Unable to find project root package.json issue) (#194)
We have a bug opened in `inspectpack` repo that is really a `webpack-dashboard` issue. Fixes https://github.com/FormidableLabs/inspectpack/issues/45

The `inspectpack` `versions` action detects versions skews in installed packages within a webpack bundle by actually traversing the `node_modules` installed from the "project root" which is basically the directory that contains the operating `package.json`.

Some bespoke webpack configurations change `bundle.context` to something _other_ than a directory that is our "project root". This causes `inspectpack` to rightfully bomb out with a `Error: Unable to find project root package.json` error -- because if you're running `versions` you need to have this accessible.

In `webpack-dashboard`-land, it pays to be a little more permissive, because folks might not mind missing `versions` for getting all the _other_ cool dashboard stuff. So, accordingly, here's how the dashboard now deals with the problem:

1. If the `DashboardPlugin` specifies a `root` option to the root project directory, we use that. **Note**: We don't _check_ it's valid -- if it's invalid and manually specified you **will** get this same error.
2. If no `root` is specified, try to see if `bundle.context` has a `package.json`, use that.
3. If `bundle.context` isn't workable, try `process.cwd()`.
4. If `process.cwd()` isn't usable, then just _disable_ the `versions` action for the dashboard.
2017-09-21 10:43:01 -07:00

28 lines
854 B
JavaScript

"use strict";
const chalk = require("chalk");
const formatDuplicates = require("./format-duplicates");
const formatVersions = require("./format-versions");
function formatProblems(bundle) {
const duplicates = formatDuplicates(bundle.duplicates);
// Versions may be undefined if we couldn't get a project root.
const versions = typeof bundle.versions === "undefined" ?
`${chalk.yellow("Unable to diagnose possible version skews\n")}` :
formatVersions(bundle.versions);
if (!duplicates && !versions) {
return chalk.green("No problems detected!");
}
if (duplicates && !versions) {
return `${chalk.green("No version skews!\n")}\n${duplicates}`;
}
if (!duplicates && versions) {
return `${chalk.green("No duplicate files!")}\n${versions}`;
}
return `${duplicates}\n${versions}`;
}
module.exports = formatProblems;