Skip to content

Commit

Permalink
#216 fix auto build JS assets
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Jul 27, 2023
1 parent 2b11f4d commit 15f12f7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "src/bin.rs"

[build-dependencies]
static-files = "0.2"
walkdir = "2"

[dependencies]
actix = ">= 0.12, < 0.14"
Expand Down
67 changes: 62 additions & 5 deletions server/build.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,82 @@
use std::time::SystemTime;

use static_files::resource_dir;

const JS_DIST: &str = "../browser/data-browser/dist";
const SRC_BROWSER: &str = "../browser/data-browser/src";

macro_rules! p {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}

/// Check if any JS files were modified since the last build
fn should_build() -> bool {
if let Ok(dist) = std::fs::metadata(format!("{}/index.html", JS_DIST)) {
let dist_time = dist
.modified()
.unwrap()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
for entry in walkdir::WalkDir::new(SRC_BROWSER)
.into_iter()
.filter_map(|e| {
// ignore ds store
if let Ok(e) = e {
if e.path().to_str().unwrap().contains(".DS_Store") {
return None;
}
Some(e)
} else {
None
}
})
{
if entry.path().is_file() {
let src_time = entry
.metadata()
.unwrap()
.modified()
.unwrap()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
if src_time >= dist_time {
p!("Source file modified: {:?}, rebuilding...", entry.path());
return true;
}
}
}

p!("No changes in JS, skipping build.");
false
} else {
p!("No dist folder found, building...");
true
}
}

fn main() -> std::io::Result<()> {
let js_build_path = "../browser/data-browser/dist";
println!("cargo:rerun-if-changed=../browser");

if std::fs::read_dir(js_build_path).is_err() {
println!("Running `pnpm run build` in the data-browser folder...");
if should_build() {
p!("pnpm install...");
std::process::Command::new("pnpm")
.current_dir("../browser")
.args(["install"])
.output()
.expect("failed to install deps");
p!("pnpm build...");
std::process::Command::new("pnpm")
.current_dir("../browser")
.args(["run", "build"])
.output()
.expect("failed to build js bundle");
}

resource_dir(js_build_path)
resource_dir(JS_DIST)
.build()
.unwrap_or_else(|_| panic!("failed to open data browser assets from {}", js_build_path));
.unwrap_or_else(|_| panic!("failed to open data browser assets from {}", JS_DIST));

Ok(())
}

0 comments on commit 15f12f7

Please sign in to comment.