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

add scaffold_worker method to Site, use all the PathBufs #851

Merged
merged 7 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 25 additions & 43 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
mod bundle;
pub mod output;

use crate::commands::build::watch::wait_for_changes;
use crate::commands::build::watch::COOLDOWN_PERIOD;
use crate::commands::generate::run_generate;

use crate::commands::publish::package::Package;
use crate::install;
use crate::util;
pub use bundle::Bundle;
use fs2::FileExt;
use log::info;
use output::WranglerjsOutput;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use std::env;
use std::fs;
use std::fs::File;
use std::iter;
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::settings::target::Target;
use crate::terminal::message;

use notify::{self, RecursiveMode, Watcher};
use std::sync::mpsc::{channel, Sender};
use std::thread;
use std::time::Duration;

use fs2::FileExt;
use notify::{self, RecursiveMode, Watcher};
use output::WranglerjsOutput;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use crate::commands::build::watch::wait_for_changes;
use crate::commands::build::watch::COOLDOWN_PERIOD;
use crate::commands::publish::package::Package;
use crate::install;
use crate::settings::target::Target;
use crate::terminal::message;
use crate::util;

// Run the underlying {wranglerjs} executable.

// In Rust we create a virtual file, pass it to {wranglerjs}, run the
Expand All @@ -38,7 +36,7 @@ use std::time::Duration;
pub fn run_build(target: &Target) -> Result<(), failure::Error> {
let (mut command, temp_file, bundle) = setup_build(target)?;

info!("Running {:?}", command);
log::info!("Running {:?}", command);

let status = command.status()?;

Expand All @@ -60,7 +58,7 @@ pub fn run_build_and_watch(target: &Target, tx: Option<Sender<()>>) -> Result<()

let is_site = target.site.clone();

info!("Running {:?} in watch mode", command);
log::info!("Running {:?} in watch mode", command);

// Turbofish the result of the closure so we can use ?
thread::spawn::<_, Result<(), failure::Error>>(move || {
Expand All @@ -70,13 +68,13 @@ pub fn run_build_and_watch(target: &Target, tx: Option<Sender<()>>) -> Result<()
let mut watcher = notify::watcher(watcher_tx, Duration::from_secs(1))?;

watcher.watch(&temp_file, RecursiveMode::Recursive)?;
info!("watching temp file {:?}", &temp_file);
log::info!("watching temp file {:?}", &temp_file);

if let Some(site) = is_site {
let bucket = site.bucket;
if Path::new(&bucket).exists() {
watcher.watch(&bucket, RecursiveMode::Recursive)?;
info!("watching static sites asset file {:?}", &bucket);
log::info!("watching static sites asset file {:?}", &bucket);
} else {
failure::bail!(
"Attempting to watch static assets bucket \"{}\" which doesn't exist",
Expand Down Expand Up @@ -146,8 +144,8 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E

let build_dir = target.build_dir()?;

if target.site.is_some() {
scaffold_site_worker(&target)?;
if let Some(site) = &target.site {
site.scaffold_worker()?;
}

run_npm_install(&build_dir).expect("could not run `npm install`");
Expand Down Expand Up @@ -229,22 +227,6 @@ fn build_with_default_webpack(
Ok(())
}

pub fn scaffold_site_worker(target: &Target) -> Result<(), failure::Error> {
let build_dir = target.build_dir()?;
let template = "https://github.com/cloudflare/worker-sites-init";

if !Path::new(&build_dir).exists() {
// TODO: use site.entry_point instead of build_dir explicitly.
run_generate(build_dir.file_name().unwrap().to_str().unwrap(), template)?;

// This step is to prevent having a git repo within a git repo after
// generating the scaffold into an existing project.
fs::remove_dir_all(&build_dir.join(".git"))?;
}

Ok(())
}

// Run {npm install} in the specified directory. Skips the install if a
// {node_modules} is found in the directory.
fn run_npm_install(dir: &PathBuf) -> Result<(), failure::Error> {
Expand All @@ -257,15 +239,15 @@ fn run_npm_install(dir: &PathBuf) -> Result<(), failure::Error> {
let mut command = build_npm_command();
command.current_dir(dir.clone());
command.arg("install");
info!("Running {:?} in directory {:?}", command, dir);
log::info!("Running {:?} in directory {:?}", command, dir);

let status = command.status()?;

if !status.success() {
failure::bail!("failed to execute `{:?}`: exited with {}", command, status)
}
} else {
info!("skipping npm install because node_modules exists");
log::info!("skipping npm install because node_modules exists");
}

// TODO: (sven) figure out why the file doesn't exist in some cases
Expand Down Expand Up @@ -315,13 +297,13 @@ fn install() -> Result<PathBuf, failure::Error> {
let wranglerjs_path = if install::target::DEBUG {
let source_path = get_source_dir();
let wranglerjs_path = source_path.join("wranglerjs");
info!("wranglerjs at: {:?}", wranglerjs_path);
log::info!("wranglerjs at: {:?}", wranglerjs_path);
wranglerjs_path
} else {
let tool_name = "wranglerjs";
let version = env!("CARGO_PKG_VERSION");
let wranglerjs_path = install::install_artifact(tool_name, "cloudflare", version)?;
info!("wranglerjs downloaded at: {:?}", wranglerjs_path.path());
log::info!("wranglerjs downloaded at: {:?}", wranglerjs_path.path());
wranglerjs_path.path()
};

Expand Down
9 changes: 5 additions & 4 deletions src/commands/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ pub fn generate(
site: bool,
) -> Result<(), failure::Error> {
validate_worker_name(name)?;

log::info!("Generating a new worker project with name '{}'", name);
run_generate(name, template)?;

let config_path = PathBuf::from("./").join(&name);
// TODO: this is tightly coupled to our site template. Need to remove once
// we refine our generate logic.
Expand All @@ -32,15 +35,13 @@ pub fn run_generate(name: &str, template: &str) -> Result<(), failure::Error> {

let args = ["generate", "--git", template, "--name", name, "--force"];

let command = command(name, binary_path, &args);
let command = command(binary_path, &args);
let command_name = format!("{:?}", command);

commands::run(command, &command_name)
}

fn command(name: &str, binary_path: PathBuf, args: &[&str]) -> Command {
log::info!("Generating a new worker project with name '{}'", name);

fn command(binary_path: PathBuf, args: &[&str]) -> Command {
let mut c = if cfg!(target_os = "windows") {
let mut c = Command::new("cmd");
c.arg("/C");
Expand Down
31 changes: 17 additions & 14 deletions src/commands/init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::path::{Path, PathBuf};

use crate::commands;
use crate::commands::validate_worker_name;
use crate::settings::target::{Manifest, Site, TargetType};
use crate::terminal::message;

pub fn init(
name: Option<&str>,
target_type: Option<TargetType>,
site: bool,
site_flag: bool,
) -> 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.");
Expand All @@ -19,20 +18,24 @@ pub fn init(

let target_type = target_type.unwrap_or_default();
let config_path = PathBuf::from("./");
let initialized_site = if site { Some(Site::default()) } else { None };
let manifest = Manifest::generate(
name.to_string(),
Some(target_type),
&config_path,
initialized_site,
)?;
message::success("Succesfully created a `wrangler.toml`");

if site {
let env = None;
let target = manifest.get_target(env)?;
commands::build::wranglerjs::scaffold_site_worker(&target)?;
if site_flag {
let site = Site::default();
Manifest::generate(
name.to_string(),
Some(target_type),
&config_path,
Some(site.clone()),
)?;
message::success("Succesfully created a `wrangler.toml`");
ashleymichal marked this conversation as resolved.
Show resolved Hide resolved

site.scaffold_worker()?;
message::success("Succesfully scaffolded workers site");
} else {
Manifest::generate(name.to_string(), Some(target_type), &config_path, None)?;
message::success("Succesfully created a `wrangler.toml`");
}

Ok(())
}

Expand Down
58 changes: 22 additions & 36 deletions src/commands/kv/bucket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,9 @@ mod tests {

#[test]
fn it_can_ignore_node_modules() {
let target = make_target(Site {
bucket: "fake".to_string(),
entry_point: None,
include: None,
exclude: None,
});
let mut site = Site::default();
site.bucket = "fake".to_string();
let target = make_target(site);

let test_dir = "test1";
// If test dir already exists, delete it.
Expand All @@ -308,12 +305,9 @@ mod tests {

#[test]
fn it_can_ignore_hidden() {
let target = make_target(Site {
bucket: "fake".to_string(),
entry_point: None,
include: None,
exclude: None,
});
let mut site = Site::default();
site.bucket = "fake".to_string();
let target = make_target(site);

let test_dir = "test2";
// If test dir already exists, delete it.
Expand All @@ -338,12 +332,9 @@ mod tests {

#[test]
fn it_can_allow_unfiltered_files() {
let target = make_target(Site {
bucket: "fake".to_string(),
entry_point: None,
include: None,
exclude: None,
});
let mut site = Site::default();
site.bucket = "fake".to_string();
let target = make_target(site);

let test_dir = "test3";
// If test dir already exists, delete it.
Expand All @@ -368,12 +359,10 @@ mod tests {

#[test]
fn it_can_filter_by_include() {
let target = make_target(Site {
bucket: "fake".to_string(),
entry_point: None,
include: Some(vec!["this_isnt_here.txt".to_string()]),
exclude: None,
});
let mut site = Site::default();
site.bucket = "fake".to_string();
site.include = Some(vec!["this_isnt_here.txt".to_string()]);
let target = make_target(site);

let test_dir = "test4";
// If test dir already exists, delete it.
Expand All @@ -398,12 +387,10 @@ mod tests {

#[test]
fn it_can_filter_by_exclude() {
let target = make_target(Site {
bucket: "fake".to_string(),
entry_point: None,
include: None,
exclude: Some(vec!["ignore_me.txt".to_string()]),
});
let mut site = Site::default();
site.bucket = "fake".to_string();
site.exclude = Some(vec!["ignore_me.txt".to_string()]);
let target = make_target(site);

let test_dir = "test5";
// If test dir already exists, delete it.
Expand All @@ -428,12 +415,11 @@ mod tests {

#[test]
fn it_can_prioritize_include_over_exclude() {
let target = make_target(Site {
bucket: "fake".to_string(),
entry_point: None,
include: Some(vec!["notice_me.txt".to_string()]),
exclude: Some(vec!["notice_me.txt".to_string()]),
});
let mut site = Site::default();
site.bucket = "fake".to_string();
site.include = Some(vec!["notice_me.txt".to_string()]);
site.exclude = Some(vec!["notice_me.txt".to_string()]);
let target = make_target(site);

let test_dir = "test6";
// If test dir already exists, delete it.
Expand Down
29 changes: 25 additions & 4 deletions src/settings/target/site.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::env;
use std::fs;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::commands::generate::run_generate;

const SITE_ENTRY_POINT: &str = "workers-site";

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Site {
pub bucket: String,
#[serde(rename = "entry-point")]
pub entry_point: Option<String>,
entry_point: Option<PathBuf>,
ashleymichal marked this conversation as resolved.
Show resolved Hide resolved
pub include: Option<Vec<String>>,
pub exclude: Option<Vec<String>>,
}
Expand All @@ -24,20 +28,37 @@ impl Site {
// if the user has configured `site.entry-point`, use that
// as the build directory. Otherwise use the default const
// SITE_ENTRY_POINT
pub fn build_dir(&self, current_dir: PathBuf) -> Result<PathBuf, std::io::Error> {
pub fn entry_point(&self) -> Result<PathBuf, std::io::Error> {
let current_dir = env::current_dir()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventually methods like these that determine where a file should be found relative to the project root should probably take as an argument the actual project root path, rather than the current directory. for the moment, everything runs relative to root because that is where wrangler.toml is and we assume you are running wrangler adjacent to the toml.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which makes me want to write a refactor issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ this would also be a step towards better monorepo support!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and/or just being able to run wrangler from anywhere inside a project. i'll write the issue and merge this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok(current_dir.join(
self.entry_point
.to_owned()
.unwrap_or_else(|| format!("./{}", SITE_ENTRY_POINT)),
.unwrap_or_else(|| PathBuf::from(SITE_ENTRY_POINT)),
))
}

pub fn scaffold_worker(&self) -> Result<(), failure::Error> {
let entry_point = &self.entry_point()?;
let template = "https://github.com/cloudflare/worker-sites-init";

if !entry_point.exists() {
log::info!("Generating a new workers site project");
run_generate(entry_point.file_name().unwrap().to_str().unwrap(), template)?;

// This step is to prevent having a git repo within a git repo after
// generating the scaffold into an existing project.
fs::remove_dir_all(&entry_point.join(".git"))?;
}

Ok(())
}
}

impl Default for Site {
fn default() -> Site {
Site {
bucket: String::new(),
entry_point: Some(String::from(SITE_ENTRY_POINT)),
entry_point: Some(PathBuf::from(SITE_ENTRY_POINT)),
include: None,
exclude: None,
}
Expand Down
Loading