-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpnpm.rs
67 lines (57 loc) · 2.09 KB
/
pnpm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::LockfileDependency;
use rustc_hash::FxHashMap;
use serde::Deserialize;
use serde_yaml::{Error, Value};
// https://pnpm.io/pnpm-workspace_yaml
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct PnpmWorkspaceYaml {
pub packages: Vec<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PnpmLockPackage {
pub dependencies: Option<FxHashMap<String, Value>>, // string or number
pub resolution: PnpmLockPackageResolution,
pub version: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PnpmLockPackageResolution {
pub commit: Option<String>, // git
pub integrity: Option<String>,
pub tarball: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PnpmLockYaml {
pub packages: Option<FxHashMap<String, PnpmLockPackage>>,
}
impl PnpmLockYaml {
pub fn parse<T: AsRef<str>>(content: T) -> Result<Vec<LockfileDependency>, Error> {
let data: PnpmLockYaml = serde_yaml::from_str(content.as_ref())?;
let mut deps = vec![];
if let Some(packages) = data.packages {
for (name, package) in packages {
let mut dependencies = FxHashMap::default();
if let Some(deps) = package.dependencies {
for (dep, value) in deps {
let value = match value {
Value::Number(num) => num.to_string(),
Value::String(val) => val,
_ => continue,
};
dependencies.insert(dep, value);
}
}
deps.push(LockfileDependency {
name,
version: package.version,
integrity: package.resolution.integrity.or(package.resolution.commit),
dependencies,
});
}
}
Ok(deps)
}
}