npm_parser/
outdated.rs

1//! This parses the output of npm-outdated
2use std::collections::BTreeMap;
3use std::process::Command;
4use std::str::from_utf8;
5use tracing::{debug, warn};
6
7/// Outer structure for parsing npm-outdated output
8#[derive(Debug, serde::Serialize, serde::Deserialize)]
9pub struct NpmOutdatedData(pub BTreeMap<String, PackageStatus>);
10
11/// Inner, per-package structure when parsing npm-outdated output
12///
13/// Meaning of the fields is from [npm-outdated](https://docs.npmjs.com/cli/v7/commands/npm-outdated)
14#[derive(Debug, serde::Serialize, serde::Deserialize)]
15pub struct PackageStatus {
16    /// wanted is the maximum version of the package that satisfies the
17    /// semver range specified in package.json. If there's no available
18    /// semver range (i.e. you're running npm outdated --global, or
19    /// the package isn't included in package.json), then wanted shows
20    /// the currently-installed version.
21    pub wanted: String,
22    /// latest is the version of the package tagged as latest in the registry.
23    /// Running npm publish with no special configuration will publish the
24    /// package with a dist-tag of latest. This may or may not be the maximum
25    /// version of the package, or the most-recently published version of the
26    /// package, depending on how the package's developer manages the latest
27    /// dist-tag.
28    pub latest: String,
29    /// where in the physical tree the package is located.
30    pub location: Option<String>,
31    /// shows which package depends on the displayed dependency
32    ///
33    /// optional since it is new between npm version 6 and 8
34    pub dependent: Option<String>,
35    /// tells you whether this package is a dependency or a dev/peer/optional
36    /// dependency. Packages not included in package.json are always marked
37    /// dependencies.
38    #[serde(rename = "type")]
39    pub package_type: String,
40    /// the homepage value contained in the package's packument
41    ///
42    /// optional since it is not included in all npm versions
43    pub homepage: Option<String>,
44}
45
46/// What the exit code indicated about required updates
47#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
48pub enum IndicatedUpdateRequirement {
49    /// No update is required
50    UpToDate,
51    /// An update is required
52    UpdateRequired,
53}
54
55impl std::fmt::Display for IndicatedUpdateRequirement {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self {
58            IndicatedUpdateRequirement::UpToDate => {
59                write!(f, "up-to-date")
60            }
61            IndicatedUpdateRequirement::UpdateRequired => {
62                write!(f, "update-required")
63            }
64        }
65    }
66}
67
68/// main entry point for the npm-oudated call
69pub fn outdated() -> Result<(IndicatedUpdateRequirement, NpmOutdatedData), crate::Error> {
70    let mut cmd = Command::new("npm");
71
72    cmd.args(["outdated", "--json", "--long"]);
73
74    let output = cmd.output()?;
75
76    if !output.status.success() {
77        warn!(
78            "npm outdated did not return with a successful exit code: {}",
79            output.status
80        );
81        debug!("stdout:\n{}", from_utf8(&output.stdout)?);
82        if !output.stderr.is_empty() {
83            warn!("stderr:\n{}", from_utf8(&output.stderr)?);
84        }
85    }
86
87    let update_requirement = if output.status.success() {
88        IndicatedUpdateRequirement::UpToDate
89    } else {
90        IndicatedUpdateRequirement::UpdateRequired
91    };
92
93    let json_str = from_utf8(&output.stdout)?;
94    let jd = &mut serde_json::Deserializer::from_str(json_str);
95    let data: NpmOutdatedData = serde_path_to_error::deserialize(jd)?;
96    Ok((update_requirement, data))
97}
98
99#[cfg(test)]
100mod test {
101    use super::*;
102    use crate::Error;
103
104    /// this test requires a package.json and package-lock.json in the main crate
105    /// directory (working dir of the tests)
106    #[test]
107    fn test_run_npm_outdated() -> Result<(), Error> {
108        outdated()?;
109        Ok(())
110    }
111}