Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] npm outdated --json only lists a single entry for each package, producing less than without --json #6630

Closed
2 tasks done
asos-tomp opened this issue Jul 6, 2023 · 1 comment · Fixed by #7538
Closed
2 tasks done
Assignees
Labels
Bug thing that needs fixing config:display Issues dealing with display of data to terminal

Comments

@asos-tomp
Copy link

asos-tomp commented Jul 6, 2023

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

npm outdated in a project with workspaces can produce:

Package    Current    Wanted    Latest  Location           Depended by
xxx        1.0.00     1.0.00    1.1.00  node_modules/xxx   workspace1@npm:[email protected]
xxx        1.0.00     1.0.00    1.1.00  node_modules/xxx   workspace2@npm:[email protected]
xxx        1.0.00     1.0.00    1.1.00  node_modules/xxx   rootProject

...but when doing npm outdated --json, only details of the last dependent is shown:

{
  "xxx": {
    "current": "1.0.00",
    "wanted": "1.0.00",
    "latest": "1.1.00",
    "dependent": "rootProject",
    "location": "../rootProject/node_modules/xxx"
  }

Hence, seems like whatever reducer is producing the json is flattening to the last dependent, rather than providing a full list that would be available without --json.

To get around this, one must call npm outdated --json --workspace=xxx for each workspace, then consolidate the results. Presumably the --json option shouldn't produce less information than the tabular format?

Expected Behavior

Either the output needs to include an array per dependency:

{
  "xxx": [
    {
      "current": "1.0.00",
      "wanted": "1.0.00",
      "latest": "1.1.00",
      "dependent": "workspace1",
      "location": "../rootProject/node_modules/xxx"
    },
    {
      "current": "1.0.00",
      "wanted": "1.0.00",
      "latest": "1.1.00",
      "dependent": "workspace2",
      "location": "../rootProject/node_modules/xxx"
    },
    {
      "current": "1.0.00",
      "wanted": "1.0.00",
      "latest": "1.1.00",
      "dependent": "rootProject",
      "location": "../rootProject/node_modules/xxx"
    }
  ]
}

..or if "dependent" isn't needed in the output as a key, just key a map by dependent:

{
  "xxx": {
    "rootProject": {
       "current": "1.0.00",
       "wanted": "1.0.00",
       "latest": "1.1.00",
       "location": "../rootProject/node_modules/xxx"
    },
    "workspace1": {
       "current": "1.0.00",
       "wanted": "1.0.00",
       "latest": "1.1.00",
       "location": "../rootProject/node_modules/xxx"
    },
    "workspace2": {
       "current": "1.0.00",
       "wanted": "1.0.00",
       "latest": "1.1.00",
       "location": "../rootProject/node_modules/xxx"
    }
  }
}

Steps To Reproduce

npm outdated --json with a project that includes workspaces (with or without the --workspaces flag)

Environment

  • npm: 9.5.0
  • Node.js: v18.16.1
  • OS Name: Ventura 13.4.1
  • System Model Name: Macbook Pro
@asos-tomp asos-tomp added Bug thing that needs fixing Needs Triage needs review for next steps Release 9.x work is associated with a specific npm 9 release labels Jul 6, 2023
@asos-tomp
Copy link
Author

asos-tomp commented Jul 6, 2023

For the final suggestion, could modify makeJSON in /lib/comands/outdated to be:

  makeJSON (list) {
    const out = {}
    list.forEach(dep => {
      const {
        name,
        current,
        wanted,
        latest,
        path,
        type,
        dependent,
        homepage,
      } = dep
      out[dependent] = {
        ...out[dependent],
        [name]: {
          current,
          wanted,
          latest,
          location: path,
        }
      }
      if (this.npm.config.get('long')) {
        out[dependent][name] = {
          ...out[dependent][name],
          type,
          homepage,
        }
      }
    })
    return JSON.stringify(out, null, 2)
  }

...or perhaps using reduce:

  makeJSON (list) {
    const out = list.reduce(
      (
        list,
        {
          name,
          current,
          wanted,
          latest,
          path: location,
          type,
          dependent,
          homepage
        }
      ) => ({
        ...list,
        [dependent]: {
          ...list[dependent],
          [name]: {
            current,
            wanted,
            latest,
            location,
            ...(this.npm.config.get('long')
              ? {
                  type,
                  homepage
                }
              : {})
          }
        }
      }),
      {}
    );
    return JSON.stringify(out, null, 2)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing config:display Issues dealing with display of data to terminal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants