Skip to content

Commit

Permalink
feat: add force option to overwrite any existing component
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulgpt committed Aug 25, 2022
1 parent 9ece280 commit 5eb0785
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub struct Cli {
/// Generate typescript files
#[clap(long, short)]
pub typescript: bool,

/// Overwrite existing component
#[clap(long)]
pub force: bool,
}

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -62,6 +66,8 @@ pub struct Config {
pub verbose_output: Option<bool>,

pub typescript: Option<bool>,

pub force: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -134,10 +140,13 @@ fn main() {
let component_name = component_name[0];

// Check if the component already exists
if Path::new(&format!("{resolved_path}/{component_name}")).exists() {
warn_and_exit(&format!(
"A component with name \"{component_name}\" already exists"
));
if config.force.unwrap_or_default() || args.force {
} else {
if Path::new(&format!("{resolved_path}/{component_name}")).exists() {
warn_and_exit(&format!(
"A component with name \"{component_name}\" already exists"
));
}
}

// create missing directories
Expand Down
5 changes: 3 additions & 2 deletions src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn write_component_from_template(
let content = content.replace("_component", &format!("{}", name_and_extension.0));

if use_template_extension {
extension = get_extension(entry.file_name().to_str().unwrap());
extension = get_file_extension(entry.file_name().to_str().unwrap());
}

if entry.file_name().to_str().unwrap().contains("_component") {
Expand Down Expand Up @@ -158,6 +158,7 @@ pub fn generate_config() {
component_type: Some("functional".to_owned()),
typescript: Some(false),
verbose_output: Some(false),
force: Some(false),
};

fs::write(
Expand All @@ -167,7 +168,7 @@ pub fn generate_config() {
.unwrap();
}

fn get_extension(file_name_with_extension: &str) -> String {
fn get_file_extension(file_name_with_extension: &str) -> String {
file_name_with_extension
.split(".")
.next()
Expand Down

0 comments on commit 5eb0785

Please sign in to comment.