Skip to content

Commit

Permalink
Reestructured crates
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed May 14, 2024
1 parent 6610553 commit 1c8107c
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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]]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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]]
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand All @@ -26,7 +26,9 @@ pub enum AddNpmDependencyError {
NpmPackageNotFoundError(String),
}

fn default_select_npm_package(npm_packages: Vec<String>) -> Result<usize, AddNpmDependencyError> {
fn default_select_npm_package(
npm_packages: Vec<String>,
) -> Result<usize, NpmScaffoldingUtilsError> {
Ok(Select::with_theme(&ColorfulTheme::default())
.with_prompt("Multiple NPM packages were found in this repository, choose one:")
.default(0)
Expand All @@ -40,13 +42,13 @@ pub fn add_npm_dependency(
dependency_source: String,
package_to_add_the_dependency_to: Option<String>,
custom_select_npm_package: Option<
Box<dyn Fn(Vec<String>) -> Result<usize, AddNpmDependencyError>>,
Box<dyn Fn(Vec<String>) -> Result<usize, NpmScaffoldingUtilsError>>,
>,
) -> Result<FileTree, AddNpmDependencyError> {
) -> Result<FileTree, NpmScaffoldingUtilsError> {
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(
Expand All @@ -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::<Result<Vec<String>, AddNpmDependencyError>>()?;
.collect::<Result<Vec<String>, 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 {
Expand All @@ -94,24 +96,26 @@ pub fn add_npm_dependency(
Ok(file_tree)
}

fn get_npm_package_name(package_json: &(PathBuf, String)) -> Result<String, AddNpmDependencyError> {
fn get_npm_package_name(
package_json: &(PathBuf, String),
) -> Result<String, NpmScaffoldingUtilsError> {
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"#),
));
Expand All @@ -124,11 +128,11 @@ pub fn add_npm_dependency_to_package(
package_json: &(PathBuf, String),
dependency: &String,
dependency_source: &String,
) -> Result<String, AddNpmDependencyError> {
) -> Result<String, NpmScaffoldingUtilsError> {
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"),
));
Expand All @@ -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"#),
));
Expand All @@ -157,11 +161,11 @@ pub fn add_npm_dev_dependency_to_package(
package_json: &(PathBuf, String),
dev_dependency: &String,
dev_dependency_source: &String,
) -> Result<String, AddNpmDependencyError> {
) -> Result<String, NpmScaffoldingUtilsError> {
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"),
));
Expand All @@ -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"#),
));
Expand All @@ -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<String, NpmScaffoldingUtilsError> {
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::*;
Expand Down
4 changes: 2 additions & 2 deletions crates/scaffold_remote_zome/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions crates/scaffold_remote_zome/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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),
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn scaffold_remote_zome(
Ok(file_tree)
}

fn select_npm_package(module_name: String, npm_packages: Vec<String>) -> Result<usize, AddNpmDependencyError> {
fn select_npm_package(module_name: String, npm_packages: Vec<String>) -> Result<usize, NpmScaffoldingUtilsError> {
let mut i = 0;
let mut found = false;

Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 1c8107c

Please sign in to comment.