diff --git a/Cargo.lock b/Cargo.lock index 093ec5a..e52237e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1253,6 +1253,7 @@ dependencies = [ "serde_json", "tokio", "unicode-xid", + "walkdir", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9ca44bc..6a03d1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,5 @@ unicode-xid = "0.2.4" rust-i18n = "2" itertools = "0.12.0" reqwest = "0.11.22" -tokio = { version = "1", features = ["full"] } \ No newline at end of file +tokio = { version = "1", features = ["full"] } +walkdir = "2.4.0" diff --git a/src/template/README.md b/src/template/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/create_project.rs b/src/utils/create_project.rs index f5c5af0..2f0cdd4 100644 --- a/src/utils/create_project.rs +++ b/src/utils/create_project.rs @@ -12,6 +12,7 @@ use std::{ }; use super::{ + directory2md::write_directory_contents_to_markdown, get_selection::{get_user_selected, DbConnectionType, DbType, TemplateType, UserSelected}, print_util, restricted_names, warning, }; @@ -471,6 +472,7 @@ pub fn write_project_file( for (file_name, template) in &templates { render_and_write_to_file(&handlebars, template, &data, project_path.join(file_name))?; } + write_directory_contents_to_markdown(&project_path.join("README.md"))?; Ok(()) } diff --git a/src/utils/directory2md.rs b/src/utils/directory2md.rs new file mode 100644 index 0000000..2e648bc --- /dev/null +++ b/src/utils/directory2md.rs @@ -0,0 +1,32 @@ +use std::fs::{self, File}; +use std::io::{Result, Write}; +use std::path::Path; +use walkdir::WalkDir; + +pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result<()> { + let mut file = File::create(output_file)?; + writeln!(file, "# Directory Contents\n")?; + for entry in WalkDir::new(output_file.parent().unwrap()) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| e.path() != Path::new("./READEME.md")) + // Exclude the output file itself + { + let depth = entry.depth(); + let indent = " ".repeat(depth.saturating_sub(1)); // Markdown uses 4 spaces for each list level + let path = entry.path(); + let metadata = fs::metadata(path)?; + dbg!(&path); + if let Some(file_name) = path.file_name() { + let file_name = file_name.to_string_lossy(); + if metadata.is_dir() { + // Use `**Dir:**` to denote directories + writeln!(file, "{}- **Dir:** {}", indent, file_name)?; + } else { + // Use `*File:*` to denote files + writeln!(file, "{}- *File:* {}", indent, file_name)?; + } + } + } + Ok(()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 022fee9..3cb4d1b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,6 @@ mod check_for_updates; pub mod create_project; +mod directory2md; pub mod get_selection; mod print_util; mod restricted_names;