#!/usr/bin/env moshell

val base = std::env("BASE").unwrap()
val outputs = std::env("OUTPUT").unwrap()

mkdir -p $outputs/public

npm run build -- --base=$base --mode PROD

// Read generated mappings from build
val result = $(jq -r 'to_entries|map(.key + " " +.value.file)|.[]' dist/manifest.json)
val mappings = $result.split('\n')

echo '<?php const ASSETS = [' > views-mappings.php

while $mappings.len() > 0 {
    val mapping = $mappings.pop().unwrap();
    val mapping = $mapping.split(' ');
    val source_file = $mapping[0]
    val build_file = $mapping[1]
    echo "\t'$source_file' => '$build_file'," >> views-mappings.php
}

echo "];" >> views-mappings.php

chmod +r views-mappings.php

cp -r dist/* front/assets/ front/style/ public/* $outputs/public/
cp -r views-mappings.php $outputs/
