Skip to content

Commit

Permalink
refactor(crate/package_json): extract duplicate (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 authored Aug 10, 2023
1 parent d832d2f commit 13d0186
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/package_json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ strum = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
insta = { workspace = true }
40 changes: 28 additions & 12 deletions crates/package_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,35 @@ impl PackageJson {
PackageJson { path, value }
}

fn write_to_file(path: &PathBuf) -> Result<Value, PackageJsonError> {
let mut file = fs::File::create(path)?;
let name = path
.parent()
.and_then(|folder| folder.file_name())
.and_then(|file_name| file_name.to_str())
.unwrap_or("");
fn get_init_package_json(name: &str) -> Result<Value, PackageJsonError> {
let package_json = json!({
"name": name,
"version": "1.0.0",
"description": "",
"main": "index.js",
"script": {
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
});
let contents = serde_json::to_string_pretty(&package_json)?;
Ok(package_json)
}

fn to_string_prettify(package_json: &Value) -> Result<String, PackageJsonError> {
Ok(serde_json::to_string_pretty(package_json)?)
}

fn write_to_file(path: &PathBuf) -> Result<Value, PackageJsonError> {
let mut file = fs::File::create(path)?;
let name = path
.parent()
.and_then(|folder| folder.file_name())
.and_then(|file_name| file_name.to_str())
.unwrap_or("");
let package_json = PackageJson::get_init_package_json(name)?;
let contents = PackageJson::to_string_prettify(&package_json)?;
file.write_all(contents.as_bytes())?;
Ok(package_json)
}
Expand All @@ -107,7 +116,7 @@ impl PackageJson {
println!(
"Wrote to {}\n\n{}",
path.display(),
serde_json::to_string_pretty(&package_json).unwrap_or(String::new())
PackageJson::to_string_prettify(&package_json).unwrap_or(String::new())
);
Ok(())
}
Expand All @@ -132,7 +141,7 @@ impl PackageJson {

pub fn save(&mut self) -> Result<(), PackageJsonError> {
let mut file = fs::File::create(&self.path)?;
let contents = serde_json::to_string_pretty(&self.value)?;
let contents = PackageJson::to_string_prettify(&self.value)?;
file.write_all(contents.as_bytes())?;
Ok(())
}
Expand Down Expand Up @@ -205,11 +214,18 @@ impl PackageJson {
mod tests {
use std::fs::read_to_string;

use insta::assert_snapshot;
use tempfile::{tempdir, NamedTempFile};

use super::*;
use crate::DependencyGroup;

#[test]
fn test_init_package_json_content() {
let package_json = PackageJson::get_init_package_json("test").unwrap();
assert_snapshot!(PackageJson::to_string_prettify(&package_json).unwrap_or(String::new()));
}

#[test]
fn test_dependency_group_into() {
assert_eq!(<DependencyGroup as Into<&str>>::into(DependencyGroup::Default), "dependencies");
Expand Down Expand Up @@ -264,7 +280,7 @@ mod tests {
let dir = tempdir().unwrap();
let tmp = dir.path().join("package.json");
let package_json = PackageJson::create_if_needed(&tmp).unwrap();
package_json.get_script("test", false).expect_err("test command should not exist");
package_json.get_script("dev", false).expect_err("dev command should not exist");
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/package_json/src/lib.rs
expression: "PackageJson::to_string_prettify(&package_json).unwrap_or(String::new())"
---
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

0 comments on commit 13d0186

Please sign in to comment.