Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: propagate build error and check if js files exist #297

Merged
merged 7 commits into from
Jan 19, 2025
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
9 changes: 3 additions & 6 deletions crates/tuono/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ edition = "2021"
authors = ["V. Ageno <valerioageno@yahoo.it>"]
description = "Superfast React fullstack framework"
homepage = "https://tuono.dev"
keywords = [ "react", "typescript", "fullstack", "web", "ssr"]
keywords = ["react", "typescript", "fullstack", "web", "ssr"]
repository = "/~https://github.com/tuono-labs/tuono"
readme = "../../README.md"
license-file = "../../LICENSE.md"
categories = ["web-programming"]
include = [
"src/*.rs",
"Cargo.toml"
]
include = ["src/*.rs", "Cargo.toml"]

[lib]
name = "tuono"
Expand All @@ -29,7 +26,7 @@ serde = { version = "1.0.202", features = ["derive"] }
watchexec-supervisor = "3.0.0"
glob = "0.3.1"
regex = "1.10.4"
reqwest = {version = "0.12.4", features =["blocking", "json"]}
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
serde_json = "1.0"
fs_extra = "1.3.0"
http = "1.1.0"
Expand Down
16 changes: 15 additions & 1 deletion crates/tuono/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::{hash_map::Entry, HashMap};
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;
use std::process::Child;
use std::process::Command;
Expand Down Expand Up @@ -138,9 +139,18 @@ impl App {
}

pub fn build_react_prod(&self) {
Command::new(BUILD_JS_SCRIPT)
if !Path::new(BUILD_JS_SCRIPT).exists() {
eprintln!("Failed to find the build script. Please run `npm install`");
std::process::exit(1);
}
let output = Command::new(BUILD_JS_SCRIPT)
.output()
.expect("Failed to build the react source");
if !output.status.success() {
eprintln!("Failed to build the react source");
eprintln!("Error: {}", String::from_utf8_lossy(&output.stderr));
std::process::exit(1);
}
}

pub fn run_rust_server(&self) -> Child {
Expand All @@ -154,6 +164,10 @@ impl App {
}

pub fn build_tuono_config(&self) -> Result<std::process::Output, std::io::Error> {
if !Path::new(BUILD_TUONO_CONFIG).exists() {
eprintln!("Failed to find the build script. Please run `npm install`");
std::process::exit(1);
}
Command::new(BUILD_TUONO_CONFIG)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down
9 changes: 9 additions & 0 deletions crates/tuono/src/watch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::Path;
use std::sync::Arc;
use watchexec_supervisor::command::{Command, Program};

Expand All @@ -20,6 +21,10 @@ const DEV_WATCH_BIN_SRC: &str = "node_modules/.bin/tuono-dev-watch";
const DEV_SSR_BIN_SRC: &str = "node_modules/.bin/tuono-dev-ssr";

fn watch_react_src() -> Job {
if !Path::new(DEV_SSR_BIN_SRC).exists() {
eprintln!("Failed to find script to run dev watch. Please run `npm install`");
std::process::exit(1);
}
start_job(Arc::new(Command {
program: Program::Exec {
prog: DEV_WATCH_BIN_SRC.into(),
Expand All @@ -42,6 +47,10 @@ fn build_rust_src() -> Job {
}

fn build_react_ssr_src() -> Job {
if !Path::new(DEV_SSR_BIN_SRC).exists() {
eprintln!("Failed to find script to run dev ssr. Please run `npm install`");
std::process::exit(1);
}
start_job(Arc::new(Command {
program: Program::Exec {
prog: DEV_SSR_BIN_SRC.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const GET_API_FILE: &str = r"#[tuono_lib::api(GET)]";
fn it_successfully_create_the_index_route() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project.add_route("./src/routes/index.rs");
temp_tuono_project.add_file("./src/routes/index.rs");

let mut test_tuono_build = Command::cargo_bin("tuono").unwrap();
test_tuono_build
Expand All @@ -38,7 +38,7 @@ fn it_successfully_create_the_index_route() {
fn it_successfully_create_an_api_route() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project.add_api("./src/routes/api/health_check.rs", POST_API_FILE);
temp_tuono_project.add_file_with_content("./src/routes/api/health_check.rs", POST_API_FILE);

let mut test_tuono_build = Command::cargo_bin("tuono").unwrap();
test_tuono_build
Expand Down Expand Up @@ -67,7 +67,7 @@ fn it_successfully_create_an_api_route() {
fn it_successfully_create_multiple_api_for_the_same_file() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project.add_api(
temp_tuono_project.add_file_with_content(
"./src/routes/api/health_check.rs",
&format!("{POST_API_FILE}{GET_API_FILE}"),
);
Expand Down Expand Up @@ -100,9 +100,9 @@ fn it_successfully_create_multiple_api_for_the_same_file() {
fn it_successfully_create_catch_all_routes() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project.add_route("./src/routes/[...all_routes].rs");
temp_tuono_project.add_file("./src/routes/[...all_routes].rs");

temp_tuono_project.add_api(
temp_tuono_project.add_file_with_content(
"./src/routes/api/[...all_apis].rs",
&format!("{POST_API_FILE}"),
);
Expand Down Expand Up @@ -140,3 +140,36 @@ fn it_successfully_create_catch_all_routes() {
assert!(temp_main_rs_content
.contains(r#".route("/__tuono/data/*all_routes", get(dyn_catch_all_all_routes::tuono__internal__api))"#));
}

#[test]
#[serial]
fn it_fails_without_installed_build_config_script() {
TempTuonoProject::new();

let mut test_tuono_build = Command::cargo_bin("tuono").unwrap();
test_tuono_build
.arg("build")
.assert()
.failure()
.stderr("Failed to find the build script. Please run `npm install`\n");
}

#[test]
#[serial]
fn it_fails_without_installed_build_script() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project
.add_file_with_content("./node_modules/.bin/tuono-build-config", "#!/bin/bash");
Command::new("chmod")
.arg("+x")
.arg("./node_modules/.bin/tuono-build-config")
.assert()
.success();
let mut test_tuono_build = Command::cargo_bin("tuono").unwrap();
test_tuono_build
.arg("build")
.assert()
.failure()
.stderr("Failed to find the build script. Please run `npm install`\n");
}
6 changes: 3 additions & 3 deletions crates/tuono/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ impl TempTuonoProject {
self.temp_dir.path()
}

pub fn add_route<'a>(&self, path: &'a str) {
pub fn add_file<'a>(&self, path: &'a str) -> File {
let path = PathBuf::from(path);
create_all(
path.parent().expect("Route path does not have any parent"),
false,
)
.expect("Failed to create parent route directory");
File::create(path).expect("Failed to create the route file");
File::create(path).expect("Failed to create the route file")
}

pub fn add_api<'a>(&self, path: &'a str, content: &'a str) {
pub fn add_file_with_content<'a>(&self, path: &'a str, content: &'a str) {
let path = PathBuf::from(path);
create_all(
path.parent().expect("Route path does not have any parent"),
Expand Down
Loading