pnpm_extra/
lib.rs

1//! # pnpm-extra
2//!
3//! The library for the `pnpm-extra` binary.
4//!
5//! At the moment this simply exports the `print_tree` function that implments the `pnpm-extra tree`
6//! command, and the `read_workspace` function that reads the `pnpm-workspace.yaml` file used by
7//! `pnpm-extra catalog add` command.
8//!
9//! There is no expectation of stability or backwards compatibility for this library at this time,
10//! use at your own risk!
11
12use anyhow::{bail, Context as _, Result};
13
14/// `pnpm-extra tree` implementation details.
15pub mod tree;
16
17/// Parse and return the content of pnpm-workspace.yaml as a serde_yaml::Mapping.
18///
19/// This is unlikely to be useful as this is a human edited file and serde_yaml does not preserve
20/// comments or formatting, but is provided for completeness.
21///
22/// # Errors
23/// - If the pnpm-workspace.yaml file cannot be read or parsed.
24pub fn read_workspace() -> Result<serde_yaml::Mapping> {
25    let data = std::fs::read("pnpm-workspace.yaml").context("reading pnpm-workspace.yaml");
26    let workspace = serde_yaml::from_slice::<serde_yaml::Value>(&data?)
27        .context("parsing pnpm-workspace.yaml")?;
28    let workspace = match workspace {
29        serde_yaml::Value::Mapping(map) => map,
30        _ => bail!("pnpm-workspace.yaml content is not a mapping?"),
31    };
32    Ok(workspace)
33}