#!/bin/bash
# https://docs.sheetjs.com/docs/demos/cli/nodesea
cd /tmp
rm -rf sheetjs-sea
mkdir sheetjs-sea
cd sheetjs-sea

node --version

npm init -y

cat >sheet2csv.js <<EOF
// For NodeJS SEA, the CommonJS \`require\` must be used
const { createRequire } = require('node:module');
require = createRequire(__filename);
const { readFile, utils } = require("xlsx");

// argv[2] is the first argument to the script
const filename = process.argv[2];

// read file
const wb = readFile(filename);

// generate CSV of first sheet
const ws = wb.Sheets[wb.SheetNames[0]];
const csv = utils.sheet_to_csv(ws);

// print to terminal
console.log(csv);
EOF

npm i --save https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz

### Script Test

curl -o pres.numbers https://docs.sheetjs.com/pres.numbers

node sheet2csv.js pres.numbers

### SEA Bundle

cat >sheet2csv.json <<EOF
{
  "main": "sheet2csv.js",
  "output": "sheet2csv.blob"
}
EOF

node --experimental-sea-config sheet2csv.json

## NOTE: these steps are for darwin-x64

cp `which node` sheet2csv

codesign --remove-signature ./sheet2csv

npx -y postject --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 --macho-segment-name NODE_SEA sheet2csv NODE_SEA_BLOB sheet2csv.blob

codesign -s - ./sheet2csv

./sheet2csv pres.numbers

codesign -dv ./sheet2csv