Skip to content

Commit a55a866

Browse files
committed
rename bin + scripts, add optional peer deps
1 parent e515e87 commit a55a866

File tree

7 files changed

+117
-47
lines changed

7 files changed

+117
-47
lines changed

src/graphs.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ struct LockfileNpmGraphPackage {
7777
integrity: Option<String>,
7878
dependencies: BTreeMap<StackString, LockfileNpmPackageId>,
7979
optional_dependencies: BTreeMap<StackString, LockfileNpmPackageId>,
80+
optional_peer_dependencies: BTreeMap<StackString, LockfileNpmPackageId>,
8081
os: Vec<SmallStackString>,
8182
cpu: Vec<SmallStackString>,
8283
tarball: Option<StackString>,
8384
deprecated: bool,
84-
scripts: bool,
85-
bin: bool,
85+
has_scripts: bool,
86+
has_bin: bool,
8687
}
8788

8889
#[derive(Debug)]
@@ -181,8 +182,15 @@ impl LockfilePackageGraph {
181182
os: package.os.clone(),
182183
tarball: package.tarball.clone(),
183184
deprecated: package.deprecated,
184-
scripts: package.scripts,
185-
bin: package.bin,
185+
has_scripts: package.has_scripts,
186+
has_bin: package.has_bin,
187+
optional_peer_dependencies: package
188+
.optional_peer_dependencies
189+
.iter()
190+
.map(|(name, dep_id)| {
191+
(name.clone(), LockfileNpmPackageId(dep_id.clone()))
192+
})
193+
.collect(),
186194
}),
187195
);
188196
}
@@ -387,8 +395,13 @@ impl LockfilePackageGraph {
387395
.map(|(name, id)| (name, id.0))
388396
.collect(),
389397
deprecated: package.deprecated,
390-
scripts: package.scripts,
391-
bin: package.bin,
398+
has_scripts: package.has_scripts,
399+
has_bin: package.has_bin,
400+
optional_peer_dependencies: package
401+
.optional_peer_dependencies
402+
.into_iter()
403+
.map(|(name, id)| (name, id.0))
404+
.collect(),
392405
},
393406
);
394407
}

src/lib.rs

+44-18
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ pub struct NpmPackageLockfileInfo {
6969
pub integrity: Option<String>,
7070
pub dependencies: Vec<NpmPackageDependencyLockfileInfo>,
7171
pub optional_dependencies: Vec<NpmPackageDependencyLockfileInfo>,
72+
pub optional_peer_dependencies: Vec<NpmPackageDependencyLockfileInfo>,
7273
pub os: Vec<SmallStackString>,
7374
pub cpu: Vec<SmallStackString>,
7475
pub tarball: Option<StackString>,
7576
pub deprecated: bool,
76-
pub scripts: bool,
77-
pub bin: bool,
77+
pub has_scripts: bool,
78+
pub has_bin: bool,
7879
}
7980

8081
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -91,16 +92,18 @@ pub struct NpmPackageInfo {
9192
pub dependencies: BTreeMap<StackString, StackString>,
9293
#[serde(default)]
9394
pub optional_dependencies: BTreeMap<StackString, StackString>,
95+
#[serde(default)]
96+
pub optional_peer_dependencies: BTreeMap<StackString, StackString>,
9497
pub os: Vec<SmallStackString>,
9598
pub cpu: Vec<SmallStackString>,
9699
#[serde(skip_serializing_if = "Option::is_none")]
97100
pub tarball: Option<StackString>,
98101
#[serde(default, skip_serializing_if = "is_false")]
99102
pub deprecated: bool,
100103
#[serde(default, skip_serializing_if = "is_false")]
101-
pub scripts: bool,
104+
pub has_scripts: bool,
102105
#[serde(default, skip_serializing_if = "is_false")]
103-
pub bin: bool,
106+
pub has_bin: bool,
104107
}
105108

106109
fn is_false(value: &bool) -> bool {
@@ -302,6 +305,8 @@ impl LockfileContent {
302305
pub dependencies: Vec<StackString>,
303306
#[serde(default)]
304307
pub optional_dependencies: Vec<StackString>,
308+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
309+
pub optional_peer_dependencies: Vec<StackString>,
305310
#[serde(default)]
306311
pub os: Vec<SmallStackString>,
307312
#[serde(default)]
@@ -311,9 +316,9 @@ impl LockfileContent {
311316
#[serde(default, skip_serializing_if = "is_false")]
312317
pub deprecated: bool,
313318
#[serde(default, skip_serializing_if = "is_false")]
314-
pub scripts: bool,
319+
pub has_scripts: bool,
315320
#[serde(default, skip_serializing_if = "is_false")]
316-
pub bin: bool,
321+
pub has_bin: bool,
317322
}
318323

319324
#[derive(Debug, Deserialize)]
@@ -373,6 +378,8 @@ impl LockfileContent {
373378
BTreeMap::new();
374379
let mut optional_dependencies =
375380
BTreeMap::<StackString, StackString>::new();
381+
let mut optional_peer_dependencies =
382+
BTreeMap::<StackString, StackString>::new();
376383

377384
for dep in value.dependencies.into_iter() {
378385
handle_dep(dep, &version_by_dep_name, &mut dependencies)?;
@@ -384,6 +391,14 @@ impl LockfileContent {
384391
&mut optional_dependencies,
385392
)?;
386393
}
394+
for dep in value.optional_peer_dependencies.into_iter() {
395+
handle_dep(
396+
dep,
397+
&version_by_dep_name,
398+
&mut optional_peer_dependencies,
399+
)?;
400+
}
401+
387402
npm.insert(
388403
key,
389404
NpmPackageInfo {
@@ -393,9 +408,10 @@ impl LockfileContent {
393408
os: value.os,
394409
tarball: value.tarball,
395410
optional_dependencies,
411+
optional_peer_dependencies,
396412
deprecated: value.deprecated,
397-
scripts: value.scripts,
398-
bin: value.bin,
413+
has_scripts: value.has_scripts,
414+
has_bin: value.has_bin,
399415
},
400416
);
401417
}
@@ -909,18 +925,24 @@ impl Lockfile {
909925
.into_iter()
910926
.map(|dep| (dep.name, dep.id))
911927
.collect::<BTreeMap<StackString, StackString>>();
928+
let optional_peer_dependencies = package_info
929+
.optional_peer_dependencies
930+
.into_iter()
931+
.map(|dep| (dep.name, dep.id))
932+
.collect::<BTreeMap<StackString, StackString>>();
912933

913934
let entry = self.content.packages.npm.entry(package_info.serialized_id);
914935
let package_info = NpmPackageInfo {
915936
integrity: package_info.integrity,
916937
dependencies,
917938
optional_dependencies,
939+
optional_peer_dependencies,
918940
os: package_info.os,
919941
cpu: package_info.cpu,
920942
tarball: package_info.tarball,
921943
deprecated: package_info.deprecated,
922-
scripts: package_info.scripts,
923-
bin: package_info.bin,
944+
has_scripts: package_info.has_scripts,
945+
has_bin: package_info.has_bin,
924946
};
925947
match entry {
926948
BTreeMapEntry::Vacant(entry) => {
@@ -1286,12 +1308,13 @@ mod tests {
12861308
integrity: Some("sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==".to_string()),
12871309
dependencies: vec![],
12881310
optional_dependencies: vec![],
1311+
optional_peer_dependencies: vec![],
12891312
os: vec![],
12901313
cpu: vec![],
12911314
tarball: None,
12921315
deprecated: false,
1293-
scripts: false,
1294-
bin: false,
1316+
has_scripts: false,
1317+
has_bin: false,
12951318
};
12961319
lockfile.insert_npm_package(npm_package);
12971320
assert!(!lockfile.has_content_changed);
@@ -1302,12 +1325,13 @@ mod tests {
13021325
integrity: Some("sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==".to_string()),
13031326
dependencies: vec![],
13041327
optional_dependencies: vec![],
1328+
optional_peer_dependencies: vec![],
13051329
os: vec![],
13061330
cpu: vec![],
13071331
tarball: None,
13081332
deprecated: false,
1309-
scripts: false,
1310-
bin: false,
1333+
has_scripts: false,
1334+
has_bin: false,
13111335
};
13121336
lockfile.insert_npm_package(npm_package);
13131337
assert!(lockfile.has_content_changed);
@@ -1318,12 +1342,13 @@ mod tests {
13181342
integrity: Some("sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==".to_string()),
13191343
dependencies: vec![],
13201344
optional_dependencies: vec![],
1345+
optional_peer_dependencies: vec![],
13211346
os: vec![],
13221347
cpu: vec![],
13231348
tarball: None,
13241349
deprecated: false,
1325-
scripts: false,
1326-
bin: false,
1350+
has_scripts: false,
1351+
has_bin: false,
13271352
};
13281353
// Not present in lockfile yet, should be inserted
13291354
lockfile.insert_npm_package(npm_package.clone());
@@ -1339,12 +1364,13 @@ mod tests {
13391364
integrity: Some("sha512-foobar".to_string()),
13401365
dependencies: vec![],
13411366
optional_dependencies: vec![],
1367+
optional_peer_dependencies: vec![],
13421368
os: vec![],
13431369
cpu: vec![],
13441370
tarball: None,
13451371
deprecated: false,
1346-
scripts: false,
1347-
bin: false,
1372+
has_scripts: false,
1373+
has_bin: false,
13481374
};
13491375
// Now present in lockfile, should be changed due to different integrity
13501376
lockfile.insert_npm_package(npm_package);

src/printer.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ struct SerializedNpmPkg<'a> {
3737
#[serde(skip_serializing_if = "Vec::is_empty")]
3838
optional_dependencies: Vec<Cow<'a, str>>,
3939
#[serde(skip_serializing_if = "Vec::is_empty")]
40+
optional_peer_dependencies: Vec<Cow<'a, str>>,
41+
#[serde(skip_serializing_if = "Vec::is_empty")]
4042
os: Vec<SmallStackString>,
4143
#[serde(skip_serializing_if = "Vec::is_empty")]
4244
cpu: Vec<SmallStackString>,
4345
#[serde(skip_serializing_if = "is_false")]
4446
deprecated: bool,
4547
#[serde(skip_serializing_if = "is_false")]
46-
scripts: bool,
48+
has_scripts: bool,
4749
#[serde(skip_serializing_if = "is_false")]
48-
bin: bool,
50+
has_bin: bool,
4951
#[serde(skip_serializing_if = "Option::is_none")]
5052
tarball: Option<&'a str>,
5153
}
@@ -267,18 +269,23 @@ pub fn print_v5_content(content: &LockfileContent) -> String {
267269
handle_deps(&value.dependencies, &pkg_had_multiple_versions);
268270
let optional_dependencies =
269271
handle_deps(&value.optional_dependencies, &pkg_had_multiple_versions);
272+
let optional_peer_dependencies = handle_deps(
273+
&value.optional_peer_dependencies,
274+
&pkg_had_multiple_versions,
275+
);
270276
(
271277
key.as_str(),
272278
SerializedNpmPkg {
273279
integrity: value.integrity.as_deref(),
274280
dependencies,
275281
optional_dependencies,
282+
optional_peer_dependencies,
276283
os: value.os.clone(),
277284
cpu: value.cpu.clone(),
278285
tarball: value.tarball.as_deref(),
279286
deprecated: value.deprecated,
280-
scripts: value.scripts,
281-
bin: value.bin,
287+
has_scripts: value.has_scripts,
288+
has_bin: value.has_bin,
282289
},
283290
)
284291
})

src/transforms.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ pub async fn transform4_to_5(
295295
}
296296
value.insert("optionalDependencies".into(), new_optional_deps.into());
297297
}
298+
let mut new_optional_peer_deps = Vec::new();
299+
if !result.optional_peer_dependencies.is_empty() {
300+
for (key, value) in result.optional_peer_dependencies {
301+
new_optional_peer_deps.push(format!("{}@{}", key, value));
302+
}
303+
value.insert(
304+
"optionalPeerDependencies".into(),
305+
new_optional_peer_deps.into(),
306+
);
307+
}
298308

299309
if existing_deps.is_empty() {
300310
value.remove("dependencies");
@@ -320,11 +330,11 @@ pub async fn transform4_to_5(
320330
if result.deprecated {
321331
value.insert("deprecated".into(), true.into());
322332
}
323-
if result.scripts {
324-
value.insert("scripts".into(), true.into());
333+
if result.has_scripts {
334+
value.insert("hasScripts".into(), true.into());
325335
}
326-
if result.bin {
327-
value.insert("bin".into(), true.into());
336+
if result.has_bin {
337+
value.insert("hasBin".into(), true.into());
328338
}
329339
}
330340
json.insert("npm".into(), npm.into());
@@ -338,11 +348,12 @@ pub async fn transform4_to_5(
338348
pub struct Lockfile5NpmInfo {
339349
pub tarball_url: Option<String>,
340350
pub optional_dependencies: BTreeMap<String, String>,
351+
pub optional_peer_dependencies: BTreeMap<String, String>,
341352
pub cpu: Vec<String>,
342353
pub os: Vec<String>,
343354
pub deprecated: bool,
344-
pub scripts: bool,
345-
pub bin: bool,
355+
pub has_scripts: bool,
356+
pub has_bin: bool,
346357
}
347358

348359
#[async_trait::async_trait(?Send)]
@@ -590,18 +601,27 @@ mod test {
590601
(
591602
592603
Lockfile5NpmInfo {
593-
bin: true,
604+
has_bin: true,
594605
..Default::default()
595606
},
596607
),
597608
(
598609
599610
Lockfile5NpmInfo {
600-
scripts: true,
611+
has_scripts: true,
612+
..Default::default()
613+
},
614+
),
615+
(
616+
617+
Lockfile5NpmInfo {
618+
optional_peer_dependencies: [("package-z", "1.0.0")]
619+
.into_iter()
620+
.map(|(k, v)| (k.to_string(), v.to_string()))
621+
.collect(),
601622
..Default::default()
602623
},
603624
),
604-
default_info("[email protected]"),
605625
(
606626
607627
Lockfile5NpmInfo {
@@ -672,14 +692,15 @@ mod test {
672692
673693
"integrity": "sha512-foobar",
674694
"dependencies": ["[email protected]"],
675-
"bin": true,
695+
"hasBin": true,
676696
},
677697
678698
"integrity": "sha512-foobar",
679-
"scripts": true,
699+
"hasScripts": true,
680700
},
681701
682702
"integrity": "sha512-foobar",
703+
"optionalPeerDependencies": ["[email protected]"],
683704
},
684705
685706
"integrity": "sha512-foobar",

tests/registry_data/@[email protected]

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"cpu": ["x86_64"],
33
"os": ["darwin", "linux", "win32"],
44
"tarballUrl": "https://fake-registry.com/a/pkg-2.1.5.tgz",
5-
"optionalDependencies": { "other_name": "npm:@b/[email protected]" },
6-
"bin": true,
7-
"scripts": true
5+
"optionalDependencies": { "othername": "npm:@b/[email protected]" },
6+
"hasBin": true,
7+
"hasScripts": true,
8+
"optionalPeerDependencies": { "optionalpeerdep": "npm:@z/[email protected]" }
89
}

tests/spec_test.rs

-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ impl NpmPackageInfoProvider for TestNpmPackageInfoProvider {
297297
if path.exists() {
298298
let text = std::fs::read_to_string(path).unwrap();
299299
let info: Lockfile5NpmInfo = serde_json::from_str(&text).unwrap();
300-
println!("info: {:?}", info);
301300
self
302301
.cache
303302
.borrow_mut()

0 commit comments

Comments
 (0)