From a34de542ed7e43c993fc0d120b334535c92cfac4 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Mon, 27 May 2019 16:07:54 -0500 Subject: [PATCH] feat(command): init --- src/commands/generate.rs | 2 +- src/commands/init.rs | 25 +++++++++++++++++++ src/commands/mod.rs | 2 ++ src/main.rs | 29 ++++++++++++++++++++++ src/settings/project.rs | 14 ++++++++--- tests/generate.rs | 2 +- tests/init.rs | 52 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 src/commands/init.rs create mode 100644 tests/init.rs diff --git a/src/commands/generate.rs b/src/commands/generate.rs index a38251150..0a36b6ea0 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -22,7 +22,7 @@ pub fn generate( let command_name = format!("{:?}", command); commands::run(command, &command_name)?; - Project::generate(name.to_string(), pt)?; + Project::generate(name.to_string(), pt, false)?; Ok(()) } diff --git a/src/commands/init.rs b/src/commands/init.rs new file mode 100644 index 000000000..aa29be65b --- /dev/null +++ b/src/commands/init.rs @@ -0,0 +1,25 @@ +use crate::emoji; +use crate::settings::project::{Project, ProjectType}; +use std::path::Path; + +pub fn init(name: Option<&str>, project_type: Option) -> Result<(), failure::Error> { + if Path::new("./wrangler.toml").exists() { + failure::bail!("A wrangler.toml file already exists! Please remove it before running this command again."); + } + let dirname = get_current_dirname()?; + let name = name.unwrap_or_else(|| &dirname); + let project_type = project_type.unwrap_or_default(); + Project::generate(name.to_string(), project_type, true)?; + println!("{} Succesfully created a `wrangler.toml`", emoji::SPARKLES); + Ok(()) +} + +fn get_current_dirname() -> Result { + let current_path = std::env::current_dir()?; + let parent = current_path.parent(); + let dirname = match parent { + Some(parent) => current_path.strip_prefix(parent)?.display().to_string(), + None => "worker".to_string(), + }; + Ok(dirname) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9f6daa244..fae41f154 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -5,6 +5,7 @@ use log::info; pub mod build; pub mod config; pub mod generate; +pub mod init; pub mod publish; pub mod subdomain; pub mod whoami; @@ -12,6 +13,7 @@ pub mod whoami; pub use self::config::global_config; pub use build::build; pub use generate::generate; +pub use init::init; pub use publish::preview::preview; pub use publish::preview::HTTPMethod; pub use publish::publish; diff --git a/src/main.rs b/src/main.rs index c22f85586..1c8e69525 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,25 @@ fn main() -> Result<(), failure::Error> { .help("the type of project you want generated"), ), ) + .subcommand( + SubCommand::with_name("init") + .about(&*format!( + "{} Generates a wrangler.toml for an existing project", + emoji::DANCERS + )) + .arg( + Arg::with_name("name") + .help("the name of your worker! defaults to 'worker'") + .index(1), + ) + .arg( + Arg::with_name("type") + .short("t") + .long("type") + .takes_value(true) + .help("the type of project you want generated"), + ), + ) .subcommand( SubCommand::with_name("preview") .about(&*format!( @@ -134,6 +153,7 @@ fn main() -> Result<(), failure::Error> { if matches.subcommand_matches("config").is_some() || matches.subcommand_matches("generate").is_some() + || matches.subcommand_matches("init").is_some() { if let Some(matches) = matches.subcommand_matches("config") { let email = matches @@ -160,6 +180,15 @@ fn main() -> Result<(), failure::Error> { ); commands::generate(name, template, project_type, &cache)?; } + + if let Some(matches) = matches.subcommand_matches("init") { + let name = matches.value_of("name"); + let project_type = match matches.value_of("type") { + Some(s) => Some(settings::project::ProjectType::from_str(&s.to_lowercase())?), + None => None, + }; + commands::init(name, project_type)?; + } } else if matches.subcommand_matches("build").is_some() || matches.subcommand_matches("preview").is_some() { diff --git a/src/settings/project.rs b/src/settings/project.rs index 1f542af9b..b9971462c 100644 --- a/src/settings/project.rs +++ b/src/settings/project.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::fmt; use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::str::FromStr; use log::info; @@ -60,7 +60,11 @@ impl FromStr for ProjectType { } impl Project { - pub fn generate(name: String, project_type: ProjectType) -> Result { + pub fn generate( + name: String, + project_type: ProjectType, + init: bool, + ) -> Result { let project = Project { name: name.clone(), project_type: project_type.clone(), @@ -72,7 +76,11 @@ impl Project { }; let toml = toml::to_string(&project)?; - let config_path = Path::new("./").join(&name); + let config_path = if init { + PathBuf::from("./") + } else { + Path::new("./").join(&name) + }; let config_file = config_path.join("wrangler.toml"); info!("Writing a wrangler.toml file at {}", config_file.display()); diff --git a/tests/generate.rs b/tests/generate.rs index e72549d2f..5cd969702 100644 --- a/tests/generate.rs +++ b/tests/generate.rs @@ -32,7 +32,7 @@ fn it_generates_with_arguments() { cleanup(name); } -fn generate(name: Option<&str>, template: Option<&str>, project_type: Option<&str>) { +pub fn generate(name: Option<&str>, template: Option<&str>, project_type: Option<&str>) { let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); if name.is_none() && template.is_none() && project_type.is_none() { wrangler.arg("generate").assert().success(); diff --git a/tests/init.rs b/tests/init.rs new file mode 100644 index 000000000..760b35e67 --- /dev/null +++ b/tests/init.rs @@ -0,0 +1,52 @@ +use assert_cmd::prelude::*; + +use std::fs; +use std::path::Path; +use std::process::Command; + +#[test] +fn it_works() { + let name = "init1"; + generate(Some(name)); + + let wranglertoml_path = format!("{}/wrangler.toml", name); + assert_eq!(Path::new(&wranglertoml_path).exists(), true); + fs::remove_file(&wranglertoml_path).unwrap(); + + init().current_dir(Path::new(name)).assert().success(); + + cleanup(name); +} + +#[test] +fn init_fails_if_wrangler_toml_exists() { + let name = "init2"; + generate(Some(name)); + + let wranglertoml_path = format!("{}/wrangler.toml", name); + assert_eq!(Path::new(&wranglertoml_path).exists(), true); + + init().current_dir(Path::new(name)).assert().failure(); + + cleanup(name); +} + +fn init() -> Command { + let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + wrangler.arg("init"); + wrangler +} + +fn generate(name: Option<&str>) { + let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + wrangler + .arg("generate") + .arg(name.unwrap()) + .assert() + .success(); +} + +fn cleanup(name: &str) { + fs::remove_dir_all(name).unwrap(); + assert_eq!(Path::new(name).exists(), false); +}