docs.sheetjs.com/tests/installation/bun.ps1

66 lines
2.2 KiB
PowerShell
Raw Normal View History

2026-01-29 04:58:19 +00:00
#!/usr/bin/env pwsh
# https://docs.sheetjs.com/docs/getting-started/installation/bun
$oldDir = Get-Location
$tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-bun-dle"
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force }
New-Item -ItemType Directory -Path $tempDir | Out-Null
Set-Location -Path $tempDir
bun init -y
bun install https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz
if ($LASTEXITCODE -ne 0) { bun install xlsx@https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz }
@'
import * as XLSX from 'xlsx';
import * as fs from 'fs';
XLSX.set_fs(fs);
/* fetch JSON data and parse */
const url = "https://docs.sheetjs.com/executive.json";
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* sort by first presidential term */
prez.forEach(row => row.start = row.terms.find(term => term.type === "prez").start);
prez.sort((l,r) => l.start.localeCompare(r.start));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = XLSX.utils.json_to_sheet(rows);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
XLSX.utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
XLSX.writeFile(workbook, "Presidents.xlsx");
'@ | Out-File -FilePath "SheetJSBun.js" -Encoding utf8
bun build --target=bun SheetJSBun.js --outfile=app.js
Remove-Item -Path "package.json" -Force
Remove-Item -Path "bun.lockb" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "bun.lock" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "SheetJSBun.js" -Force
Remove-Item -Path "node_modules" -Recurse -Force -ErrorAction SilentlyContinue
bun app.js
bunx xlsx-cli Presidents.xlsx | Select-Object -First 10
Set-Location $oldDir
Remove-Item -Path $tempDir -Recurse -Force