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

Move metadata generation at publish-time #237

Merged
merged 1 commit into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 9 additions & 17 deletions src/commands/build/wranglerjs/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ impl Bundle {

script_file.write_all(script.as_bytes())?;

let mut metadata_file = File::create(self.metadata_path())?;
metadata_file.write_all(create_metadata(self).as_bytes())?;

// cleanup {Webpack} dist, if specified.
if let Some(dist_to_clean) = &wranglerjs_output.dist_to_clean {
info!("Remove {}", dist_to_clean);
Expand Down Expand Up @@ -106,8 +103,7 @@ pub fn create_prologue() -> String {
}

// This metadata describe the bindings on the Worker.
fn create_metadata(bundle: &Bundle) -> String {
info!("create metadata; wasm={}", bundle.has_wasm());
pub fn create_metadata(bundle: &Bundle) -> String {
if bundle.has_wasm() {
format!(
r#"
Expand Down Expand Up @@ -149,8 +145,8 @@ mod tests {
}

#[test]
fn it_writes_the_bundle_metadata() {
let out = create_temp_dir("it_writes_the_bundle_metadata");
fn it_creates_the_bundle_metadata() {
let out = create_temp_dir("it_creates_the_bundle_metadata");
let wranglerjs_output = WranglerjsOutput {
errors: vec![],
script: "".to_string(),
Expand All @@ -160,12 +156,10 @@ mod tests {
let bundle = Bundle::new_at(out.clone());

bundle.write(&wranglerjs_output).unwrap();
assert!(Path::new(&bundle.metadata_path()).exists());
let contents =
fs::read_to_string(&bundle.metadata_path()).expect("could not read metadata");
assert_eq!(Path::new(&bundle.metadata_path()).exists(), false);

assert_eq!(
contents,
create_metadata(&bundle),
r#"
{
"body_part": "script"
Expand Down Expand Up @@ -213,8 +207,8 @@ mod tests {
}

#[test]
fn it_writes_the_bundle_wasm_metadata() {
let out = create_temp_dir("it_writes_the_bundle_wasm_metadata");
fn it_creates_the_bundle_wasm_metadata() {
let out = create_temp_dir("it_creates_the_bundle_wasm_metadata");
let wranglerjs_output = WranglerjsOutput {
errors: vec![],
script: "".to_string(),
Expand All @@ -224,12 +218,10 @@ mod tests {
let bundle = Bundle::new_at(out.clone());

bundle.write(&wranglerjs_output).unwrap();
assert!(Path::new(&bundle.metadata_path()).exists());
let contents =
fs::read_to_string(&bundle.metadata_path()).expect("could not read metadata");
assert_eq!(Path::new(&bundle.metadata_path()).exists(), false);

assert_eq!(
contents,
create_metadata(&bundle),
r#"
{
"body_part": "script",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod bundle;
pub mod bundle;
pub mod output;

use crate::commands::publish::package::Package;
Expand Down
17 changes: 14 additions & 3 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ pub mod package;
use package::Package;

use log::info;

use reqwest::multipart::Form;
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

use crate::commands::build::wranglerjs::Bundle;
use reqwest::multipart::Form;

use crate::commands::build::wranglerjs::{bundle, Bundle};
use crate::commands::subdomain::Subdomain;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::{Project, ProjectType};
Expand Down Expand Up @@ -116,6 +118,15 @@ fn publish_script(
}
ProjectType::Webpack => {
info!("Webpack project detected. Publishing...");

// FIXME(sven): shouldn't new
let bundle = Bundle::new();

let metadata = bundle::create_metadata(&bundle);
info!("generate metadata {:?}", metadata);
let mut metadata_file = File::create(bundle.metadata_path())?;
metadata_file.write_all(metadata.as_bytes())?;

client
.put(&worker_addr)
.header("X-Auth-Key", &*user.api_key)
Expand Down
7 changes: 0 additions & 7 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ fn it_builds_with_webpack_single_js() {

build(fixture);
assert!(fixture_out_path(fixture).join("script.js").exists());
assert!(fixture_out_path(fixture).join("metadata.json").exists());
cleanup(fixture);
}

Expand All @@ -53,7 +52,6 @@ fn it_builds_with_webpack_single_js_use_package_main() {

build(fixture);
assert!(fixture_out_path(fixture).join("script.js").exists());
assert!(fixture_out_path(fixture).join("metadata.json").exists());
cleanup(fixture);
}

Expand Down Expand Up @@ -84,13 +82,8 @@ fn it_builds_with_webpack_wast() {

build(fixture);
assert!(fixture_out_path(fixture).join("script.js").exists());
assert!(fixture_out_path(fixture).join("metadata.json").exists());
assert!(fixture_out_path(fixture).join("module.wasm").exists());

let metadata = fs::read_to_string(fixture_out_path(fixture).join("metadata.json"))
.expect("could not read metadata");
assert!(metadata.contains("wasm_module"));

cleanup(fixture);
}

Expand Down