#!/bin/bash -e
WASMBINDGEN_VERSION=0.2.92
OUT=./nodejs/pkg

echo "Running cargo build"
cargo build --lib --release --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook

if [ -d $OUT ]; then
  echo "Clearing output directory '$OUT'"
  rm -rf $OUT
fi

if ! [ -x "$(command -v wasm-bindgen)" ]; then
  echo "Installing wasm-bindgen-cli via cargo"
  cargo install wasm-bindgen-cli --version $WASMBINDGEN_VERSION
fi

echo "Generating wasm-bindings"

# add supports for Weak References, see [1].
# TLDR: Structs passed from Rust to JS will be deallocated
# automatically, no need to call `.free` in JS.
#
# [1]: https://rustwasm.github.io/docs/wasm-bindgen/reference/weak-references.html
wasm-bindgen ./target/wasm32-unknown-unknown/release/panrelease.wasm \
  --out-dir $OUT \
  --target nodejs \
  --typescript \
  --weak-refs

echo "Find your wasm package in $OUT"
