webpack-dashboard/utils/format-versions.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

27 lines
500 B
JavaScript

"use strict";
const chalk = require("chalk");
const Handlebars = require("handlebars");
const template = Handlebars.compile(
`${chalk.yellow(chalk.underline("Version skews"))}
{{#each versions}}
{{name}}:
{{#each versions}}
{{@key}}:
{{#each this}}
- {{{this}}}
{{/each}}
{{/each}}
{{/each}}
`);
function formatVersions(versions) {
return ((versions || {}).versions || []).length && template({
versions: versions.versions
}) || "";
}
module.exports = formatVersions;