#!/bin/bash
# https://docs.sheetjs.com/docs/demos/frontend/bundler/vitejs
# NOTE: this only checks whether data or SheetJS code is added to the bundle

cd /tmp
rm -rf sheetjs-vite-tests
mkdir sheetjs-vite-tests
cd sheetjs-vite-tests

for n in {3..5}; do
npm create vite@$n sheetjs-vite$n -- --template vue-ts
cd sheetjs-vite$n
npm i
npm i --save https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz
npm ls | grep vite

curl -O https://docs.sheetjs.com/vitejs/vite.config.ts

mkdir -p data
curl -L -o data/pres.xlsx https://sheetjs.com/pres.xlsx

cat >src/components/HelloWorld.vue <<EOF
<script setup lang="ts">
// @ts-ignore
import data from '../../data/pres.xlsx?sheetjs';
</script>

<template>
  <table>
    <tr><th>Name</th><th>Index</th></tr>
    <tr v-for="(row,R) in data" v-bind:key="R">
      <td>{{row.Name}}</td>
      <td>{{row.Index}}</td>
    </tr>
  </table>
</template>
EOF

npm run build
echo "### Results: " $(grep Clinton -R dist | wc -l) $(grep BESSELJ -R dist | wc -l)

cat >src/components/HelloWorld.vue <<EOF
<script setup lang="ts">
// @ts-ignore
import b64 from '../../data/pres.xlsx?b64';
import { read, utils } from "xlsx";
/* parse workbook and convert first sheet to row array */
const wb = read(b64);
const ws = wb.Sheets[wb.SheetNames[0]];
interface IPresident { Name: string; Index: number; };
const data = utils.sheet_to_json<IPresident>(ws);
</script>

<template>
  <table>
    <tr><th>Name</th><th>Index</th></tr>
    <tr v-for="(row,R) in data" v-bind:key="R">
      <td>{{row.Name}}</td>
      <td>{{row.Index}}</td>
    </tr>
  </table>
</template>
EOF

npm run build
echo "### Results: " $(grep Clinton -R dist | wc -l) $(grep BESSELJ -R dist | wc -l)
cd -

done
