Better logging for tailwind subprocess

This commit is contained in:
Daniel Flanagan 2024-05-12 20:27:13 -05:00
parent 888359fead
commit 7b79205148
2 changed files with 30 additions and 4 deletions

View file

@ -4,13 +4,15 @@ use axum::routing::get;
use axum::{http::StatusCode, response::Html, serve, Router}; use axum::{http::StatusCode, response::Html, serve, Router};
use minijinja::{context, Environment}; use minijinja::{context, Environment};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::process::Stdio;
use std::sync::Arc; use std::sync::Arc;
use std::{path::PathBuf, str::FromStr, sync::OnceLock}; use std::{path::PathBuf, str::FromStr, sync::OnceLock};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tower_livereload::{LiveReloadLayer, Reloader}; use tower_livereload::{LiveReloadLayer, Reloader};
pub use tracing::{debug, error, info, warn}; pub use tracing::{debug, error, event, info, span, warn, Level};
#[derive(Debug)] #[derive(Debug)]
struct Berr(Box<dyn std::error::Error>); struct Berr(Box<dyn std::error::Error>);
@ -48,10 +50,33 @@ async fn main() -> Besult<()> {
// TODO: only start tailwind if in dev mode? // TODO: only start tailwind if in dev mode?
tokio::spawn(async move { tokio::spawn(async move {
info!("Starting tailwind..."); info!("Starting tailwind...");
let tw = tokio::process::Command::new("tailwindcss") match tokio::process::Command::new("tailwindcss")
.args(["-i", "src/style.css", "-o", "static/style.css", "--watch"]) .args(["-i", "src/style.css", "-o", "static/style.css", "--watch"])
.spawn(); .stdout(Stdio::piped())
info!("Tailwind spawned. {tw:#?}"); .stderr(Stdio::piped())
.spawn()
{
Ok(mut tw) => {
info!("Tailwind spawned!");
let mut stdout_reader = BufReader::new(tw.stdout.take().unwrap()).lines();
tokio::spawn(async move {
while let Ok(Some(l)) = stdout_reader.next_line().await {
if l.trim().len() > 0 {
event!(target: "tailwind::stdout", Level::INFO, "{l}");
}
}
});
let mut stderr_reader = BufReader::new(tw.stderr.take().unwrap()).lines();
tokio::spawn(async move {
while let Ok(Some(l)) = stderr_reader.next_line().await {
if l.trim().len() > 0 {
event!(target: "tailwind::stderr", Level::INFO, "{l}");
}
}
});
}
Err(e) => error!("Failed to spawn Tailwind: {e}"),
}
}); });
let templates = Arc::new(Mutex::new(Environment::new())); let templates = Arc::new(Mutex::new(Environment::new()));

View file

@ -7,6 +7,7 @@
</head> </head>
<body class="bg-base text-text"> <body class="bg-base text-text">
Page Template
{% block body %}{% endblock %} {% block body %}{% endblock %}
</body> </body>