Skip to content

Commit 4b3d315

Browse files
committed
Better parsing of local package json
1 parent c9d534f commit 4b3d315

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src-tauri/src/generate.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ pub fn generate_project(app: &tauri::AppHandle, app_state: &tauri::State<'_, App
5757
.to_str()
5858
.ok_or(errors::str_error("Failed to convert path to string"))?
5959
.to_string();
60-
// let tmp_path = package_cache_dir
61-
// .join("project")
62-
// .join("ProjectData~")
63-
// .to_str()
64-
// .ok_or(errors::str_error("Failed to convert path to string"))?
65-
// .to_string();
6660
let args = vec!["-createProject".to_string(), package_cache_dir_out_str, "-quit".to_string()];
6761
editor::open(editor_version.clone(), args, &app_state, true)?;
6862

@@ -465,17 +459,17 @@ fn modify_package_json(json_root: &PathBuf, packages: &Vec<MinimalPackage>, outp
465459
None => package_json_path.clone()
466460
};
467461

462+
#[derive(serde::Deserialize)]
463+
#[serde(rename_all = "camelCase")]
464+
struct JsonData {
465+
name: String,
466+
}
467+
468468
let json = std::fs::read_to_string(&package_json_path)?;
469-
let json: serde_json::Value = serde_json::from_str(&json)?;
470-
let name = json.as_object()
471-
.ok_or(errors::str_error(&format!("Failed to get json object from {}", package_json_path.display())))?
472-
.get("name")
473-
.ok_or(errors::str_error(&format!("Failed to get name from {}", package_json_path.display())))?
474-
.as_str()
475-
.ok_or(errors::str_error(&format!("Failed to get name from {}", package_json_path.display())))?
476-
.to_string();
469+
let json: JsonData = serde_json::from_str(&json)
470+
.map_err(|err| errors::str_error(&format!("Failed to parse json from {}, error: {:?}", package_json_path.display(), err)))?;
477471

478-
let name = name.clone();
472+
let name = json.name.clone();
479473
let version = format!("file:{}", relative_path
480474
.to_str()
481475
.ok_or(errors::str_error("Failed to get str"))?

0 commit comments

Comments
 (0)