From 6ff718b2b6243019d2b4b30983d021a8658ced19 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 9 Mar 2024 20:28:31 +1000 Subject: [PATCH] filed: auto install npm dependencies --- filed/build.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/filed/build.rs b/filed/build.rs index 65d912e..164f6f8 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -1,5 +1,5 @@ -use std::{fs, path::PathBuf, ffi::OsStr, process::Command, error::Error}; +use std::{error::Error, ffi::OsStr, fs, path::PathBuf, process::{self, Command}}; use css_minify::optimizations::{Minifier, Level}; @@ -36,8 +36,44 @@ fn system(cmd: &str, args: &[&str]) -> Result> { Ok(String::from_utf8(out.stdout)?) } +fn check_cmd>(cmd: T) -> bool { + Command::new(cmd) + .spawn() + .is_ok() +} + fn main() { + let html_exists = check_cmd("html-minifier"); + let ugjs_exists = check_cmd("uglify-js"); + + if ! (html_exists || ugjs_exists) { + eprintln!("html-minifier or uglify-js are not installed! Trying to install them via npm..."); + if ! check_cmd("npm") { + eprintln!("npm is not installed! Please install npm and/or html-minifier and uglify-js and build again"); + process::exit(1) + } + + let mut cmd = Command::new("npm"); + cmd.arg("install") + .arg("--global"); + + if ! html_exists { + cmd.arg("html-minifier"); + } + if ! ugjs_exists { + cmd.arg("uglify-js"); + } + + let res = cmd.spawn().unwrap().wait().unwrap(); + if res.success() { + eprintln!("Successfully installed html-minifier and/or uglify-js"); + } else { + eprintln!("Couldn't install html-minifier and/or uglify-js! Check the log above for details."); + process::exit(1) + } + } + println!("cargo:rerun-if-changed=static/assets"); println!("cargo:rerun-if-changed=templates/source");