#!/usr/bin/env pwsh # https://docs.sheetjs.com/docs/demos/frontend/bundler/browserify $oldDir = Get-Location $tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-browserify-tests" if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force } New-Item -ItemType Directory -Path $tempDir | Out-Null Set-Location -Path $tempDir for ($n = 3; $n -le 17; $n++) { $subDir = Join-Path -Path $tempDir -ChildPath "sheetjs-browserify-$n" New-Item -ItemType Directory -Path $subDir | Out-Null Set-Location -Path $subDir npm init -y npm i --save https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz puppeteer express@4 @' const { utils, version, writeFileXLSX } = require('xlsx'); document.getElementById("xport").addEventListener("click", function() { /* fetch JSON data and parse */ var url = "https://sheetjs.com/data/executive.json"; fetch(url).then(function(res) { return res.json(); }).then(function(raw_data) { /* filter for the Presidents */ var prez = raw_data.filter(function(row) { return row.terms.some(function(term) { return term.type === "prez"; }); }); /* sort by first presidential term */ prez.forEach(function(row) { row.start = row.terms.find(function(term) { return term.type === "prez"; }).start; }); prez.sort(function(l,r) { return l.start.localeCompare(r.start); }); /* flatten objects */ var rows = prez.map(function(row) { return { name: row.name.first + " " + row.name.last, birthday: row.bio.birthday }; }); /* generate worksheet and workbook */ var worksheet = utils.json_to_sheet(rows); var workbook = utils.book_new(); utils.book_append_sheet(workbook, worksheet, "Dates"); /* fix headers */ utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" }); /* calculate column width */ var max_width = rows.reduce(function(w, r) { return Math.max(w, r.name.length); }, 10); worksheet["!cols"] = [ { wch: max_width } ]; /* create an XLSX file and try to save to Presidents.xlsx */ writeFileXLSX(workbook, "Presidents.xlsx"); }); }); '@ | Out-File -FilePath "index.js" -Encoding utf8 npm install --save browserify@$n npm ls | Select-String "browserify" npx browserify@$n index.js > index.min.js @'