Skip to content

Commit 38cb150

Browse files
authored
feat: autofix packages-without-package-json (#31)
1 parent 624fb24 commit 38cb150

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

src/collect.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn collect_packages(args: &Args) -> Result<PackagesList> {
5050
|packages_issues: &mut Vec<BoxIssue>, path: PathBuf| match Package::new(path.clone()) {
5151
Ok(package) => packages.push(package),
5252
Err(error) => {
53-
if error.to_string().contains("package.json") {
53+
if error.to_string().contains("not found") {
5454
packages_issues.push(PackagesWithoutPackageJsonIssue::new(
5555
path.to_string_lossy().to_string(),
5656
));
@@ -170,8 +170,10 @@ pub fn collect_issues(args: &Args, packages_list: PackagesList) -> IssuesList<'_
170170
let mut all_dependencies = IndexMap::new();
171171

172172
for package in packages {
173-
if args.ignore_package.contains(package.get_name()) {
174-
continue;
173+
if let Some(package_name) = package.get_name() {
174+
if args.ignore_package.contains(package_name) {
175+
continue;
176+
}
175177
}
176178

177179
let package_type = PackageType::Package(package.get_path());
@@ -402,7 +404,7 @@ mod test {
402404

403405
let mut packages = packages
404406
.into_iter()
405-
.map(|package| package.get_name().to_string())
407+
.map(|package| package.get_name().clone().unwrap().to_string())
406408
.collect::<Vec<_>>();
407409
packages.sort();
408410

src/packages/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct PackagesList {
1919

2020
#[derive(Deserialize, Debug)]
2121
struct PackageInner {
22-
name: String,
22+
name: Option<String>,
2323
private: Option<bool>,
2424
workspaces: Option<Vec<String>>,
2525
dependencies: Option<IndexMap<String, String>>,
@@ -61,7 +61,7 @@ impl Package {
6161
})
6262
}
6363

64-
pub fn get_name(&self) -> &String {
64+
pub fn get_name(&self) -> &Option<String> {
6565
&self.inner.name
6666
}
6767

src/packages/root.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ impl RootPackage {
1818
}
1919

2020
#[cfg(test)]
21-
pub fn get_name(&self) -> &String {
22-
self.0.get_name()
21+
pub fn get_name(&self) -> String {
22+
self.0.get_name().clone().unwrap_or_default()
2323
}
2424

2525
pub fn get_workspaces(&self) -> Option<Vec<String>> {

src/rules/packages_without_package_json.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
use super::{Issue, IssueLevel};
2-
use std::borrow::Cow;
1+
use super::{Issue, IssueLevel, PackageType};
2+
use anyhow::Result;
3+
use std::{borrow::Cow, fs, path::PathBuf};
34

45
#[derive(Debug)]
56
pub struct PackagesWithoutPackageJsonIssue {
67
package: String,
8+
fixed: bool,
79
}
810

911
impl PackagesWithoutPackageJsonIssue {
1012
pub fn new(package: String) -> Box<Self> {
11-
Box::new(Self { package })
13+
Box::new(Self {
14+
package,
15+
fixed: false,
16+
})
1217
}
1318
}
1419

@@ -18,7 +23,10 @@ impl Issue for PackagesWithoutPackageJsonIssue {
1823
}
1924

2025
fn level(&self) -> IssueLevel {
21-
IssueLevel::Warning
26+
match self.fixed {
27+
true => IssueLevel::Fixed,
28+
false => IssueLevel::Warning,
29+
}
2230
}
2331

2432
fn message(&self) -> String {
@@ -28,6 +36,30 @@ impl Issue for PackagesWithoutPackageJsonIssue {
2836
fn why(&self) -> Cow<'static, str> {
2937
Cow::Borrowed("All packages matching the workspace should have a package.json file.")
3038
}
39+
40+
fn fix(&mut self, _package_type: &PackageType) -> Result<()> {
41+
let path = PathBuf::from(&self.package).join("package.json");
42+
let package_name = path
43+
.parent()
44+
.unwrap()
45+
.file_name()
46+
.unwrap()
47+
.to_str()
48+
.unwrap();
49+
50+
let value = serde_json::json!({
51+
"name": package_name,
52+
"version": "0.0.0",
53+
"private": true,
54+
});
55+
56+
let value = serde_json::to_string_pretty(&value)?;
57+
fs::write(path, value)?;
58+
59+
self.fixed = true;
60+
61+
Ok(())
62+
}
3163
}
3264

3365
#[cfg(test)]

src/rules/root_package_private_field.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ impl Issue for RootPackagePrivateFieldIssue {
5050
let value = fs::read_to_string(&path)?;
5151
let mut value = serde_json::from_str::<serde_json::Value>(&value)?;
5252

53-
value.as_object_mut().unwrap().insert(
54-
"private".to_string(),
55-
serde_json::Value::String("true".to_string()),
56-
);
53+
value
54+
.as_object_mut()
55+
.unwrap()
56+
.insert("private".to_string(), serde_json::Value::Bool(true));
5757

5858
let value = serde_json::to_string_pretty(&value)?;
5959
fs::write(path, value)?;

0 commit comments

Comments
 (0)