This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from cloudflare/init-plz
feat(command): init
- Loading branch information
Showing
7 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |