Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #162 from cloudflare/init-plz
Browse files Browse the repository at this point in the history
feat(command): init
  • Loading branch information
ashleygwilliams authored May 28, 2019
2 parents 7b4aa36 + a34de54 commit f066b22
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
25 changes: 25 additions & 0 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -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<ProjectType>) -> 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<String, failure::Error> {
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)
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use log::info;
pub mod build;
pub mod config;
pub mod generate;
pub mod init;
pub mod publish;
pub mod subdomain;
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;
Expand Down
29 changes: 29 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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
Expand All @@ -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()
{
Expand Down
14 changes: 11 additions & 3 deletions src/settings/project.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,7 +60,11 @@ impl FromStr for ProjectType {
}

impl Project {
pub fn generate(name: String, project_type: ProjectType) -> Result<Project, failure::Error> {
pub fn generate(
name: String,
project_type: ProjectType,
init: bool,
) -> Result<Project, failure::Error> {
let project = Project {
name: name.clone(),
project_type: project_type.clone(),
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
52 changes: 52 additions & 0 deletions tests/init.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit f066b22

Please sign in to comment.