Skip to content

Commit

Permalink
Allow init to be called from within an existing directory
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Feb 26, 2024
1 parent ca587c2 commit a600f8e
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
env,
ffi::OsStr,
fs::{copy, create_dir_all, metadata, read_dir, read_to_string, write},
io,
Expand Down Expand Up @@ -404,12 +405,22 @@ fn copy_frontend_files(from: &Path, to: &Path) -> Result<(), Error> {
}

fn edit_package_json_files(project_path: &Path) -> Result<(), Error> {
let package_name = project_path.file_name().unwrap();
edit_package_name(project_path, package_name, "package.json").map_err(|e| {
let package_name = if let Some(name) = project_path.file_name() {
name.to_owned()
} else {
let current_dir = env::current_dir()?;
let file_name = current_dir
.file_name()
.unwrap_or(OsStr::new("soroban-astro-template"))
.to_os_string();
file_name
};

edit_package_name(project_path, &package_name, "package.json").map_err(|e| {
eprintln!("Error editing package.json file in: {project_path:?}");
e
})?;
edit_package_name(project_path, package_name, "package-lock.json")
edit_package_name(project_path, &package_name, "package-lock.json")
}

fn edit_package_name(
Expand Down Expand Up @@ -546,7 +557,37 @@ 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_package_json_files_have_correct_name(&project_dir, TEST_PROJECT_NAME);

temp_dir.close().unwrap();
}

#[test]
fn test_init_from_within_an_existing_project() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join("./");
let with_examples = vec![];
init(
project_dir.as_path(),
&"https://github.com/AhaLabs/soroban-astro-template".to_string(),
&with_examples,
)
.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
assert_base_excluded_paths_do_not_exist(&project_dir);

// check that the contract's Cargo.toml file uses the workspace for dependencies
assert_contract_cargo_file_uses_workspace(&project_dir, "hello_world");
assert_base_excluded_paths_do_not_exist(&project_dir);

assert_astro_files_exist(&project_dir);
assert_gitignore_includes_astro_paths(&project_dir);
assert_package_json_files_have_correct_name(
&project_dir,
&project_dir.file_name().unwrap().to_string_lossy(),
);

temp_dir.close().unwrap();
}
Expand Down Expand Up @@ -612,13 +653,17 @@ mod tests {
assert!(project_dir.join("tsconfig.json").exists());
}

fn assert_package_json_files_have_correct_name(project_dir: &Path) {
fn assert_package_json_files_have_correct_name(
project_dir: &Path,
expected_package_name: &str,
) {
let package_json_path = project_dir.join("package.json");
let package_json_str = read_to_string(package_json_path).unwrap();
assert!(package_json_str.contains(&format!("\"name\":\"{TEST_PROJECT_NAME}\"")));
println!("package json: {package_json_str} ");
assert!(package_json_str.contains(&format!("\"name\":\"{expected_package_name}\"")));

let package_lock_json_path = project_dir.join("package-lock.json");
let package_lock_json_str = read_to_string(package_lock_json_path).unwrap();
assert!(package_lock_json_str.contains(&format!("\"name\":\"{TEST_PROJECT_NAME}\"")));
assert!(package_lock_json_str.contains(&format!("\"name\":\"{expected_package_name}\"")));
}
}

0 comments on commit a600f8e

Please sign in to comment.