-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce7f86f
commit 0969229
Showing
11 changed files
with
297 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod add; | ||
|
||
pub use add::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::{error::Error, path::PathBuf}; | ||
|
||
use crate::utils::{ | ||
get_config::get_config, | ||
registry::{get_registry_index, resolve_tree}, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct AddOptions { | ||
pub components: Vec<String>, | ||
pub yes: bool, | ||
pub overwrite: bool, | ||
pub cwd: PathBuf, | ||
pub all: bool, | ||
pub path: Option<PathBuf>, | ||
} | ||
|
||
pub fn add(options: AddOptions) -> Result<(), Box<dyn Error>> { | ||
println!("{:?}", options); | ||
|
||
if !options.cwd.exists() { | ||
return Err(format!( | ||
"Path {} does not exist. Please try again.", | ||
options.cwd.display() | ||
) | ||
.into()); | ||
} | ||
|
||
if let Some(config) = get_config(&options.cwd) { | ||
let registry_index = get_registry_index(); | ||
|
||
let selected_components = match options.all { | ||
true => registry_index | ||
.iter() | ||
.map(|item| item.name.clone()) | ||
.collect(), | ||
false => options.components, | ||
}; | ||
|
||
// TODO: prompt | ||
|
||
if selected_components.is_empty() { | ||
return Err("No components selected.".into()); | ||
} | ||
|
||
let tree = resolve_tree(®istry_index, &selected_components); | ||
|
||
Ok(()) | ||
} else { | ||
Err("Configuration is missing. Please run `init` to create a components.toml file.".into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod commands; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod get_config; | ||
pub mod registry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::{ | ||
fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RawConfig { | ||
style: String, | ||
tailwind: TailwindConfig, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct TailwindConfig { | ||
config: String, | ||
css: String, | ||
base_color: String, | ||
css_variables: bool, | ||
prefix: String, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Config { | ||
pub style: String, | ||
pub tailwind: TailwindConfig, | ||
pub resolved_paths: ResolvedPaths, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ResolvedPaths { | ||
pub tailwind_config: PathBuf, | ||
pub tailwind_css: PathBuf, | ||
} | ||
|
||
pub fn get_config(cwd: &Path) -> Option<Config> { | ||
get_raw_config(cwd).map(|config| resolve_config_paths(cwd, config)) | ||
} | ||
|
||
pub fn resolve_config_paths(cwd: &Path, config: RawConfig) -> Config { | ||
Config { | ||
style: config.style, | ||
tailwind: config.tailwind.clone(), | ||
resolved_paths: ResolvedPaths { | ||
tailwind_config: cwd.join(config.tailwind.config), | ||
tailwind_css: cwd.join(config.tailwind.css), | ||
}, | ||
} | ||
} | ||
|
||
pub fn get_raw_config(cwd: &Path) -> Option<RawConfig> { | ||
// TODO: use search algorithm | ||
let config_path = cwd.join("components.toml"); | ||
|
||
if !config_path.exists() { | ||
return None; | ||
} | ||
|
||
fs::read_to_string(config_path) | ||
.ok() | ||
.and_then(|config_content| toml::from_str(&config_content).ok()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod index; | ||
mod schema; | ||
|
||
pub use index::*; | ||
pub use schema::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::error::Error; | ||
|
||
use futures::{stream, StreamExt}; | ||
use reqwest::Response; | ||
use serde::de::DeserializeOwned; | ||
use serde::Deserialize; | ||
|
||
use crate::utils::get_config::Config; | ||
use crate::utils::registry::schema::{RegistryItem, RegistryItemType}; | ||
|
||
const BASE_URL: &str = "https://ui.shadcn.com"; | ||
|
||
pub async fn get_registry_index() -> Vec<RegistryItem> { | ||
let results = fetch_registry::<Vec<RegistryItem>>(vec!["index.json".into()]).await; | ||
results.first().expect("TODO") | ||
} | ||
|
||
pub fn resolve_tree(index: &Vec<RegistryItem>, names: &Vec<String>) -> Vec<RegistryItem> { | ||
let mut tree: Vec<RegistryItem> = vec![]; | ||
|
||
for name in names { | ||
if let Some(item) = index.iter().find(|item| item.name == *name) { | ||
tree.push(item.clone()); | ||
|
||
if !item.registry_dependencies.is_empty() { | ||
tree.append(&mut resolve_tree(index, &item.registry_dependencies)); | ||
} | ||
} | ||
} | ||
|
||
tree.iter() | ||
.enumerate() | ||
.filter(|&(index, item)| { | ||
tree.iter() | ||
.position(|i| i.name == item.name) | ||
.is_some_and(|position| position == index) | ||
}) | ||
.map(|(_, component)| component) | ||
.cloned() | ||
.collect() | ||
} | ||
|
||
pub fn fetch_tree(style: String, tree: Vec<RegistryItem>) { | ||
let paths: Vec<String> = tree | ||
.iter() | ||
.map(|item| format!("styles/{}/{}.json", style, item.name)) | ||
.collect(); | ||
|
||
let result = fetch_registry(paths); | ||
} | ||
|
||
pub fn get_item_target_path( | ||
config: Config, | ||
item: RegistryItem, | ||
r#override: Option<String>, | ||
) -> String { | ||
if let Some(r#override) = r#override { | ||
return r#override; | ||
} | ||
|
||
// if (item.r#type == RegistryItemType::Ui) { | ||
// return config.resolved_paths.ui | ||
// } | ||
|
||
todo!("get_item_target_path") | ||
} | ||
|
||
async fn fetch_registry<T: DeserializeOwned + Send + 'static>(paths: Vec<String>) -> Vec<T> { | ||
let client = reqwest::Client::new(); | ||
|
||
let responses = stream::iter(paths) | ||
.map(|path| { | ||
let client = client.clone(); | ||
tokio::spawn(async move { | ||
let response = client | ||
.get(format!("{}/registry/{}", BASE_URL, path)) | ||
.send() | ||
.await?; | ||
response.bytes().await | ||
}) | ||
}) | ||
.buffer_unordered(10); | ||
|
||
vec![] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Copy, Debug, Deserialize, Serialize)] | ||
pub enum RegistryItemType { | ||
Example, | ||
Component, | ||
Ui, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistryItem { | ||
pub name: String, | ||
pub dependencies: Vec<String>, | ||
pub dev_dependencies: Vec<String>, | ||
pub registry_dependencies: Vec<String>, | ||
pub files: Vec<String>, | ||
pub r#type: RegistryItemType, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistryItemWithContent { | ||
pub name: String, | ||
pub dependencies: Vec<String>, | ||
pub dev_dependencies: Vec<String>, | ||
pub registry_dependencies: Vec<String>, | ||
pub files: Vec<RegistryItemFile>, | ||
pub r#type: RegistryItemType, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistryItemFile { | ||
pub name: String, | ||
pub content: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistryStyle { | ||
pub name: String, | ||
pub label: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistryBaseColor { | ||
inline_colors: RegistryBaseColorThemes, | ||
css_vars: RegistryBaseColorThemes, | ||
inline_colors_template: String, | ||
css_vars_template: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistryBaseColorThemes { | ||
pub light: String, | ||
pub dark: String, | ||
} |