Skip to content

Commit

Permalink
fix: use Cow in PackageJson struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Feb 4, 2024
1 parent 8d84652 commit 065f285
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ impl Cli {
let mut executed_script = false;

for package in packages.clone() {
let raw = fs::read(&package).await?;
if let Ok(package_data) = serde_json::from_slice::<PackageJson>(&raw) {
let raw = fs::read_to_string(&package).await?;
if let Ok(package_data) = serde_json::from_str::<PackageJson>(&raw) {
if let Some(script_cmd) = package_data.scripts.get(script_name) {
self.run_script_full(script_name, script_cmd, &package, &package_data)
.await?;
Expand Down Expand Up @@ -159,8 +159,8 @@ impl Cli {
let mut found_package = false;

for package in packages {
let raw = fs::read(package).await?;
if let Ok(package_data) = serde_json::from_slice::<PackageJson>(&raw) {
let raw = fs::read_to_string(package).await?;
if let Ok(package_data) = serde_json::from_str::<PackageJson>(&raw) {
eprint!("{}", package_data.make_prefix(None));

found_package = true;
Expand Down
8 changes: 4 additions & 4 deletions src/package_json.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use owo_colors::OwoColorize;
use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PackageJson<'a> {
#[serde(borrow)]
pub name: Option<&'a str>,
pub name: Option<Cow<'a, str>>,
#[serde(borrow)]
pub version: Option<&'a str>,
pub version: Option<Cow<'a, str>>,

#[serde(borrow, default)]
pub scripts: HashMap<String, &'a str>,
pub scripts: HashMap<String, Cow<'a, str>>,
}

impl PackageJson<'_> {
Expand Down
4 changes: 2 additions & 2 deletions src/run_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ pub async fn run_script(
subproc.env("npm_package_json", package_path);

if let Some(name) = &package_data.name {
subproc.env("npm_package_name", name);
subproc.env("npm_package_name", name.as_ref());
}
if let Some(version) = &package_data.version {
subproc.env("npm_package_version", version);
subproc.env("npm_package_version", version.as_ref());
}

let mut child = subproc.spawn()?;
Expand Down

0 comments on commit 065f285

Please sign in to comment.