Skip to content

Commit

Permalink
Refactor append fn
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Mar 8, 2024
1 parent a95bedd commit 016950a
Showing 1 changed file with 22 additions and 43 deletions.
65 changes: 22 additions & 43 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
ffi::OsStr,
fs::{copy, create_dir_all, metadata, read_dir, read_to_string, write},
io,
fs::{copy, create_dir_all, metadata, read_dir, read_to_string, write, File, OpenOptions},
io::{self, Read, Write},
num::NonZeroU32,
path::Path,
str,
Expand Down Expand Up @@ -174,9 +174,6 @@ fn init(
// clone the template repo into the temp dir
clone_repo(frontend_template, fe_template_dir.path())?;

// IF the frontend has a README.md, append it to the project's README.md and remove it
// `if stat fe_template_dir.path()/README.md > /dev/null 2>&1; then cat fe_template_dir.path()/README.md >> project_path/README.md && rm fe_template_dir.path()/README.md; fi`

// copy the frontend template files into the project
copy_frontend_files(fe_template_dir.path(), project_path)?;
}
Expand Down Expand Up @@ -267,21 +264,12 @@ fn copy_contents(from: &Path, to: &Path) -> Result<(), Error> {
})?;
copy_contents(&path, &new_path)?;
} else {
append_and_remove_if_exists(".gitignore", &path, &new_path)?;
append_and_remove_if_exists("README.md", &path, &new_path)?;
if file_exists(&new_path.to_string_lossy()) {
//if file is .gitignore, overwrite the file with a new .gitignore file
if path.to_string_lossy().contains(".gitignore") {
copy(&path, &new_path).map_err(|e| {
eprintln!(
"Error copying from {:?} to {:?}",
path.to_string_lossy(),
new_path
);

e
})?;
continue;
if new_path.to_string_lossy().contains(".gitignore") {
append_contents(&path, &new_path)?;
}
if new_path.to_string_lossy().contains("README.md") {
append_contents(&path, &new_path)?;
}

println!(
Expand Down Expand Up @@ -445,32 +433,16 @@ fn check_internet_connection() -> bool {
false
}

fn append_and_remove_if_exists(
file_name: &str,
from: &Path,
to: &Path,
) -> Result<(), Error> {
if file_exists(&to.to_string_lossy()) {
//if file is .gitignore, overwrite the file with a new .gitignore file
if from.to_string_lossy().contains(file_name) {
copy(&from, &to).map_err(|e| {
eprintln!(
"Error copying from {:?} to {:?}",
from.to_string_lossy(),
to
);
fn append_contents(from: &Path, to: &Path) -> Result<(), Error> {
let mut from_file = File::open(from)?;
let mut from_content = String::new();
from_file.read_to_string(&mut from_content)?;

e
})?;
continue;
}
let mut to_file = OpenOptions::new().append(true).open(to)?;
to_file.write_all("\n\n".as_bytes())?;
to_file.write_all(from_content.as_bytes())?;

println!(
"ℹ️ Skipped creating {} as it already exists",
&to.to_string_lossy()
);
continue;
}
println!("ℹ️ Merging {} contents", &to.to_string_lossy());
Ok(())
}

Expand Down Expand Up @@ -581,6 +553,7 @@ mod tests {
assert_astro_files_exist(&project_dir);
assert_gitignore_includes_astro_paths(&project_dir);
assert_package_json_files_have_correct_name(&project_dir);
assert_readme_includes_frontend_file_appended(&project_dir);

temp_dir.close().unwrap();
}
Expand Down Expand Up @@ -655,4 +628,10 @@ mod tests {
let package_lock_json_str = read_to_string(package_lock_json_path).unwrap();
assert!(package_lock_json_str.contains(&format!("\"name\":\"{TEST_PROJECT_NAME}\"")));
}

fn assert_readme_includes_frontend_file_appended(project_dir: &Path) {
let readme_path = project_dir.join("README.md");
let readme_str = read_to_string(readme_path).unwrap();
assert!(readme_str.contains("Soroban Frontend in Astro"));
}
}

0 comments on commit 016950a

Please sign in to comment.