Commit js

This commit is contained in:
Jonathan Kelley committed 2024-02-27 16:33:34 -08:00
1 parent 4b64894fc5
commit f447f01403
12 files changed
+903 -45

No files matched your search

+36 -34
View File
@@ -6,11 +6,41 @@ use std::{
fn main() {
// If any TS changes, re-run the build script
println!("cargo:rerun-if-changed=src/*.ts");
println!("cargo:rerun-if-changed=src/ts/*.ts");
// for entry in ["common", "form", "interpreter"].iter() {
// gen_bindings(entry);
// }
// Compute the hash of the ts files
let hash = hash_dir("src/ts");
// If the hash matches the one on disk, we're good and don't need to update bindings
if let Ok(contents) = read_to_string("src/js/hash.txt") {
if contents.trim() == hash.to_string() {
return;
}
}
// Otherwise, generate the bindings and write the new hash to disk
// Generate the bindings for both native and web
gen_bindings("native");
gen_bindings("web");
std::fs::write("src/js/hash.txt", hash.to_string()).unwrap();
}
/// Hashes the contents of a directory
fn hash_dir(dir: &str) -> u64 {
let mut hasher = DefaultHasher::new();
for entry in std::fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let metadata = std::fs::metadata(&path).unwrap();
if metadata.is_file() {
let contents = std::fs::read(&path).unwrap();
contents.hash(&mut hasher);
}
}
hasher.finish()
}
// okay...... so tsc might fail if the user doesn't have it installed
@@ -21,27 +51,10 @@ fn main() {
// if the hashes don't match, we need to fail the build
// that way we also don't need
fn gen_bindings(name: &str) {
let contents = read_to_string(&format!("src/{name}.ts")).unwrap();
let generated = read_to_string(&format!("src/gen/{name}.js")).unwrap_or_default();
let hashed = hash_it(&contents);
// If the file is generated, and the hash is the same, we're good, don't do anything
if generated
.lines()
.next()
.unwrap_or_default()
.starts_with(&format!("// DO NOT EDIT THIS FILE. HASH: {}", hashed))
{
return;
}
// If the file is generated, and the hash is different, we need to generate it
let status = Command::new("tsc")
.arg(format!("src/{name}.ts"))
.arg("--outDir")
.arg("gen")
.arg("--target")
.arg("es6")
.arg("--p")
.arg(format!("tsconfig.{name}.json"))
.status()
.unwrap();
@@ -51,15 +64,4 @@ fn gen_bindings(name: &str) {
name
);
}
// The file should exist, and now we need write the TS hash to the file
let generated = read_to_string(&format!("gen/{name}.js")).unwrap();
let generated = format!("// DO NOT EDIT THIS FILE. HASH: {}\n{}", hashed, generated);
std::fs::write(&format!("src/gen/{name}.js"), generated).unwrap();
}
fn hash_it(obj: impl Hash) -> u64 {
let mut hasher = DefaultHasher::new();
obj.hash(&mut hasher);
hasher.finish()
}