filed: auto install npm dependencies

This commit is contained in:
b1ek 2024-03-09 20:28:31 +10:00
parent 830677cc0e
commit 6ff718b2b6
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 37 additions and 1 deletions

View File

@ -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}; use css_minify::optimizations::{Minifier, Level};
@ -36,8 +36,44 @@ fn system(cmd: &str, args: &[&str]) -> Result<String, Box<dyn Error>> {
Ok(String::from_utf8(out.stdout)?) Ok(String::from_utf8(out.stdout)?)
} }
fn check_cmd<T: AsRef<OsStr>>(cmd: T) -> bool {
Command::new(cmd)
.spawn()
.is_ok()
}
fn main() { 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=static/assets");
println!("cargo:rerun-if-changed=templates/source"); println!("cargo:rerun-if-changed=templates/source");