From 1c8107c5d20ce27e082c02a3bea6e44a7ff524ac Mon Sep 17 00:00:00 2001 From: "guillem.cordoba" Date: Tue, 14 May 2024 14:20:16 +0200 Subject: [PATCH] Reestructured crates --- .../Cargo.toml | 4 +- .../fixture/flake.nix | 0 .../src/bin/main.rs | 2 +- .../src/lib.rs | 0 .../Cargo.toml | 4 +- .../fixture/package.json | 0 .../fixture/packages/package-1/package.json | 0 .../fixture/packages/package-2/package.json | 0 .../src/bin/main.rs | 2 +- .../src/lib.rs | 71 ++++++++++++++----- crates/scaffold_remote_zome/Cargo.toml | 4 +- crates/scaffold_remote_zome/src/lib.rs | 10 +-- 12 files changed, 67 insertions(+), 30 deletions(-) rename crates/{add_flake_input => nix_scaffolding_utils}/Cargo.toml (89%) rename crates/{add_flake_input => nix_scaffolding_utils}/fixture/flake.nix (100%) rename crates/{add_flake_input => nix_scaffolding_utils}/src/bin/main.rs (96%) rename crates/{add_flake_input => nix_scaffolding_utils}/src/lib.rs (100%) rename crates/{add_npm_dependency => npm_scaffolding_utils}/Cargo.toml (89%) rename crates/{add_npm_dependency => npm_scaffolding_utils}/fixture/package.json (100%) rename crates/{add_npm_dependency => npm_scaffolding_utils}/fixture/packages/package-1/package.json (100%) rename crates/{add_npm_dependency => npm_scaffolding_utils}/fixture/packages/package-2/package.json (100%) rename crates/{add_npm_dependency => npm_scaffolding_utils}/src/bin/main.rs (97%) rename crates/{add_npm_dependency => npm_scaffolding_utils}/src/lib.rs (76%) diff --git a/crates/add_flake_input/Cargo.toml b/crates/nix_scaffolding_utils/Cargo.toml similarity index 89% rename from crates/add_flake_input/Cargo.toml rename to crates/nix_scaffolding_utils/Cargo.toml index f988a68..fc27350 100644 --- a/crates/add_flake_input/Cargo.toml +++ b/crates/nix_scaffolding_utils/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "add_flake_input" +name = "nix_scaffolding_utils" version = "0.1.0" edition = "2021" [lib] -name = "add_flake_input" +name = "nix_scaffolding_utils" path = "src/lib.rs" [[bin]] diff --git a/crates/add_flake_input/fixture/flake.nix b/crates/nix_scaffolding_utils/fixture/flake.nix similarity index 100% rename from crates/add_flake_input/fixture/flake.nix rename to crates/nix_scaffolding_utils/fixture/flake.nix diff --git a/crates/add_flake_input/src/bin/main.rs b/crates/nix_scaffolding_utils/src/bin/main.rs similarity index 96% rename from crates/add_flake_input/src/bin/main.rs rename to crates/nix_scaffolding_utils/src/bin/main.rs index c420f78..7fbaa01 100644 --- a/crates/add_flake_input/src/bin/main.rs +++ b/crates/nix_scaffolding_utils/src/bin/main.rs @@ -1,8 +1,8 @@ -use add_flake_input::add_flake_input; use anyhow::Result; use build_fs_tree::{Build, MergeableFileSystemTree}; use clap::Parser; use colored::Colorize; +use nix_scaffolding_utils::add_flake_input; use std::{ffi::OsString, path::PathBuf, process::ExitCode}; /// Adds a flake input to your flake.nix. diff --git a/crates/add_flake_input/src/lib.rs b/crates/nix_scaffolding_utils/src/lib.rs similarity index 100% rename from crates/add_flake_input/src/lib.rs rename to crates/nix_scaffolding_utils/src/lib.rs diff --git a/crates/add_npm_dependency/Cargo.toml b/crates/npm_scaffolding_utils/Cargo.toml similarity index 89% rename from crates/add_npm_dependency/Cargo.toml rename to crates/npm_scaffolding_utils/Cargo.toml index 7c45035..b614c2d 100644 --- a/crates/add_npm_dependency/Cargo.toml +++ b/crates/npm_scaffolding_utils/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "add_npm_dependency" +name = "npm_scaffolding_utils" version = "0.1.0" edition = "2021" [lib] -name = "add_npm_dependency" +name = "npm_scaffolding_utils" path = "src/lib.rs" [[bin]] diff --git a/crates/add_npm_dependency/fixture/package.json b/crates/npm_scaffolding_utils/fixture/package.json similarity index 100% rename from crates/add_npm_dependency/fixture/package.json rename to crates/npm_scaffolding_utils/fixture/package.json diff --git a/crates/add_npm_dependency/fixture/packages/package-1/package.json b/crates/npm_scaffolding_utils/fixture/packages/package-1/package.json similarity index 100% rename from crates/add_npm_dependency/fixture/packages/package-1/package.json rename to crates/npm_scaffolding_utils/fixture/packages/package-1/package.json diff --git a/crates/add_npm_dependency/fixture/packages/package-2/package.json b/crates/npm_scaffolding_utils/fixture/packages/package-2/package.json similarity index 100% rename from crates/add_npm_dependency/fixture/packages/package-2/package.json rename to crates/npm_scaffolding_utils/fixture/packages/package-2/package.json diff --git a/crates/add_npm_dependency/src/bin/main.rs b/crates/npm_scaffolding_utils/src/bin/main.rs similarity index 97% rename from crates/add_npm_dependency/src/bin/main.rs rename to crates/npm_scaffolding_utils/src/bin/main.rs index e5c9ce6..676043c 100644 --- a/crates/add_npm_dependency/src/bin/main.rs +++ b/crates/npm_scaffolding_utils/src/bin/main.rs @@ -1,8 +1,8 @@ -use add_npm_dependency::add_npm_dependency; use anyhow::Result; use build_fs_tree::{Build, MergeableFileSystemTree}; use clap::Parser; use colored::Colorize; +use npm_scaffolding_utils::add_npm_dependency; use std::{ffi::OsString, path::PathBuf, process::ExitCode}; /// Adds a flake input to your flake.nix. diff --git a/crates/add_npm_dependency/src/lib.rs b/crates/npm_scaffolding_utils/src/lib.rs similarity index 76% rename from crates/add_npm_dependency/src/lib.rs rename to crates/npm_scaffolding_utils/src/lib.rs index 9f24783..3ae0339 100644 --- a/crates/add_npm_dependency/src/lib.rs +++ b/crates/npm_scaffolding_utils/src/lib.rs @@ -6,7 +6,7 @@ use serde_json::{Map, Value}; use thiserror::Error; #[derive(Error, Debug)] -pub enum AddNpmDependencyError { +pub enum NpmScaffoldingUtilsError { #[error(transparent)] FileTreeError(#[from] FileTreeError), @@ -26,7 +26,9 @@ pub enum AddNpmDependencyError { NpmPackageNotFoundError(String), } -fn default_select_npm_package(npm_packages: Vec) -> Result { +fn default_select_npm_package( + npm_packages: Vec, +) -> Result { Ok(Select::with_theme(&ColorfulTheme::default()) .with_prompt("Multiple NPM packages were found in this repository, choose one:") .default(0) @@ -40,13 +42,13 @@ pub fn add_npm_dependency( dependency_source: String, package_to_add_the_dependency_to: Option, custom_select_npm_package: Option< - Box) -> Result>, + Box) -> Result>, >, -) -> Result { +) -> Result { let mut package_jsons = find_files_by_name(&file_tree, PathBuf::from("package.json").as_path()); match package_jsons.len() { - 0 => Err(AddNpmDependencyError::NoNpmPackagesFoundError)?, + 0 => Err(NpmScaffoldingUtilsError::NoNpmPackagesFoundError)?, 1 => { let package_json = package_jsons.pop_first().unwrap(); map_file( @@ -63,13 +65,13 @@ pub fn add_npm_dependency( let packages_names = package_jsons .iter() .map(|package_json| get_npm_package_name(package_json)) - .collect::, AddNpmDependencyError>>()?; + .collect::, NpmScaffoldingUtilsError>>()?; let package_index = match package_to_add_the_dependency_to { Some(package_to_add_to) => packages_names .iter() .position(|package_name| package_name.eq(&package_to_add_to)) - .ok_or(AddNpmDependencyError::NpmPackageNotFoundError( + .ok_or(NpmScaffoldingUtilsError::NpmPackageNotFoundError( package_to_add_to.clone(), ))?, None => match custom_select_npm_package { @@ -94,24 +96,26 @@ pub fn add_npm_dependency( Ok(file_tree) } -fn get_npm_package_name(package_json: &(PathBuf, String)) -> Result { +fn get_npm_package_name( + package_json: &(PathBuf, String), +) -> Result { let json: serde_json::Value = serde_json::from_str(package_json.1.as_str())?; let Some(map) = json.as_object() else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.to_path_buf(), String::from("package.json did not contain a json object at the root level"), )); }; let Some(name) = map.get("name") else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.to_path_buf(), String::from(r#"package.json did not contain a "name" property at the root level"#), )); }; let Value::String(name) = name else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.to_path_buf(), String::from(r#"the "name" property is not a string"#), )); @@ -124,11 +128,11 @@ pub fn add_npm_dependency_to_package( package_json: &(PathBuf, String), dependency: &String, dependency_source: &String, -) -> Result { +) -> Result { let mut json: serde_json::Value = serde_json::from_str(package_json.1.as_str())?; let Some(map) = json.as_object_mut() else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.clone(), String::from("package.json did not contain a json object at the root level"), )); @@ -139,7 +143,7 @@ pub fn add_npm_dependency_to_package( let dependencies_value = map.get_mut("dependencies").unwrap_or(&mut stub); let Some(dependencies) = dependencies_value.as_object_mut() else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.to_path_buf(), String::from(r#"the "dependencies" property is not a json object"#), )); @@ -157,11 +161,11 @@ pub fn add_npm_dev_dependency_to_package( package_json: &(PathBuf, String), dev_dependency: &String, dev_dependency_source: &String, -) -> Result { +) -> Result { let mut json: serde_json::Value = serde_json::from_str(package_json.1.as_str())?; let Some(map) = json.as_object_mut() else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.clone(), String::from("package.json did not contain a json object at the root level"), )); @@ -172,7 +176,7 @@ pub fn add_npm_dev_dependency_to_package( let dependencies_value = map.get_mut("devDependencies").unwrap_or(&mut stub); let Some(dependencies) = dependencies_value.as_object_mut() else { - return Err(AddNpmDependencyError::MalformedJsonError( + return Err(NpmScaffoldingUtilsError::MalformedJsonError( package_json.0.to_path_buf(), String::from(r#"the "devDependencies" property is not a json object"#), )); @@ -186,6 +190,39 @@ pub fn add_npm_dev_dependency_to_package( Ok(serde_json::to_string_pretty(&json)?) } +pub fn add_npm_script_to_package( + package_json: &(PathBuf, String), + script_name: &String, + script: &String, +) -> Result { + let mut json: serde_json::Value = serde_json::from_str(package_json.1.as_str())?; + + let Some(map) = json.as_object_mut() else { + return Err(NpmScaffoldingUtilsError::MalformedJsonError( + package_json.0.clone(), + String::from("package.json did not contain a json object at the root level"), + )); + }; + + let mut stub = serde_json::Value::Object(Map::new()); + + let scripts_value = map.get_mut("scripts").unwrap_or(&mut stub); + + let Some(scripts) = scripts_value.as_object_mut() else { + return Err(NpmScaffoldingUtilsError::MalformedJsonError( + package_json.0.to_path_buf(), + String::from(r#"The "scripts" property is not a json object"#), + )); + }; + + scripts.insert( + script_name.clone(), + serde_json::Value::String(script.clone()), + ); + + Ok(serde_json::to_string_pretty(&json)?) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/scaffold_remote_zome/Cargo.toml b/crates/scaffold_remote_zome/Cargo.toml index 04504fc..87b08f3 100644 --- a/crates/scaffold_remote_zome/Cargo.toml +++ b/crates/scaffold_remote_zome/Cargo.toml @@ -20,10 +20,10 @@ serde_json = {version ="1", features = ["std", "preserve_order"]} serde_yaml = "=0.9.33" serde = "1" clap = {version = "4.5.4", features = ["derive"]} -add_flake_input = { path = "../add_flake_input" } +nix_scaffolding_utils = { path = "../nix_scaffolding_utils" } file_tree_utils = { path = "../file_tree_utils" } sync_npm_git_dependencies_with_nix = { path = "../sync_npm_git_dependencies_with_nix" } -add_npm_dependency = { path = "../add_npm_dependency" } +npm_scaffolding_utils= { path = "../npm_scaffolding_utils" } thiserror = "1" build-fs-tree = "0.4" regex = "1.10.4" diff --git a/crates/scaffold_remote_zome/src/lib.rs b/crates/scaffold_remote_zome/src/lib.rs index c2fbb82..d62e4e0 100644 --- a/crates/scaffold_remote_zome/src/lib.rs +++ b/crates/scaffold_remote_zome/src/lib.rs @@ -1,5 +1,5 @@ -use add_flake_input::{add_flake_input, AddFlakeInputError}; -use add_npm_dependency::{add_npm_dependency, AddNpmDependencyError}; +use nix_scaffolding_utils::{add_flake_input, AddFlakeInputError}; +use npm_scaffolding_utils::{add_npm_dependency, NpmScaffoldingUtilsError}; use anyhow::Result; use dialoguer::{theme::ColorfulTheme, Select}; use file_tree_utils::{ @@ -16,7 +16,7 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum ScaffoldRemoteZomeError { #[error(transparent)] - AddNpmDependencyError(#[from] AddNpmDependencyError), + AddNpmDependencyError(#[from] NpmScaffoldingUtilsError), #[error(transparent)] IoError(#[from] std::io::Error), @@ -100,7 +100,7 @@ pub fn scaffold_remote_zome( Ok(file_tree) } -fn select_npm_package(module_name: String, npm_packages: Vec) -> Result { +fn select_npm_package(module_name: String, npm_packages: Vec) -> Result { let mut i = 0; let mut found = false; @@ -265,7 +265,7 @@ fn get_or_choose_dna( Some(local_dna) => dna_names .iter() .position(|dna_name| dna_name.eq(&local_dna)) - .ok_or(AddNpmDependencyError::NpmPackageNotFoundError( + .ok_or(NpmScaffoldingUtilsError::NpmPackageNotFoundError( local_dna.clone(), ))?, None => {