Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Nov 26, 2023
1 parent c299592 commit 999a966
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,19 @@ pub fn write_project_file(

favicon_file.write_all(favicon_bytes)?;
if need_db_conn {
copy_binary_file(
include_bytes!("../template/assets/js/alpinejs.js"),
"assets/js/alpinejs.js",
)?;
copy_binary_file(
include_bytes!("../template/assets/js/sweetalert2.js"),
"assets/js/sweetalert2.js",
)?;
copy_binary_file(
include_bytes!("../template/assets/js/tailwindcss.js"),
"assets/js/tailwindcss.js",
)?;
let mut web_db_templates = vec![
(
"assets/js/alpinejs.js",
include_str!("../template/assets/js/alpinejs.js"),
),
(
"assets/js/sweetalert2.js",
include_str!("../template/assets/js/sweetalert2.js"),
),
(
"assets/js/tailwindcss.js",
include_str!("../template/assets/js/tailwindcss.js"),
),
(
"templates/login.html",
include_str!("../template/templates/login.hbs"),
Expand Down Expand Up @@ -545,6 +545,7 @@ pub fn write_project_file(
}
}
for (file_name, template) in &templates {
dbg!(&project_path.join(file_name));
render_and_write_to_file(&handlebars, template, &data, project_path.join(file_name))?;
}
Ok(())
Expand Down Expand Up @@ -807,6 +808,17 @@ fn render_and_write_to_file<T: AsRef<Path>>(
Ok(())
}

fn copy_binary_file<T: AsRef<Path>>(file_bytes: &[u8], target_path: T) -> std::io::Result<()> {
// Ensure the target directory exists
if let Some(parent) = target_path.as_ref().parent() {
fs::create_dir_all(parent)?;
}

// Create the target file and write the source file bytes into it
let mut target_file = File::create(target_path)?;
target_file.write_all(file_bytes)
}

fn check_name(name: &str) -> Result<()> {
restricted_names::validate_package_name(name, "package name")?;

Expand Down

0 comments on commit 999a966

Please sign in to comment.