docs.sheetjs.com/tests/bundler/vite.ps1
2026-01-28 23:58:19 -05:00

115 lines
3.7 KiB
PowerShell

#!/usr/bin/env pwsh
# https://docs.sheetjs.com/docs/demos/frontend/bundler/vitejs
$oldDir = Get-Location
$tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-vite-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 7; $n++) {
Write-Host "Processing Vite version $n"
$projectDir = Join-Path -Path $tempDir -ChildPath "sheetjs-vite$n"
npm create -y "vite@$n" sheetjs-vite$n -- --template vue-ts --no-rolldown --no-interactive
Set-Location -Path $projectDir
npm i
npm i --save puppeteer express@4
npm i --save https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz
npm ls | Select-String "vite"
## See https://github.com/vuejs/language-tools/issues/4484#issuecomment-2182469459
if ($n -eq 4) { npm i "vue-tsc@2" }
New-Item -ItemType Directory -Path "data" -Force | Out-Null
Invoke-WebRequest -Uri "https://docs.sheetjs.com/pres.xlsx" -OutFile "data\pres.xlsx"
@'
<script setup lang="ts">
import { version, utils, writeFileXLSX } from 'xlsx';
interface President {
terms: { "type": "prez" | "viceprez"; }[];
name: { first: string; last: string; }
bio: { birthday: string; }
}
async function xport() {
/* fetch JSON data and parse */
const url = "https://docs.sheetjs.com/executive.json";
const raw_data: President[] = 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 = utils.json_to_sheet(rows);
const 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 */
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 */
console.log(utils.sheet_to_csv(worksheet));
writeFileXLSX(workbook, "Presidents.xlsx");
}
</script>
<template>
<button type="button" @click="xport">Export with SheetJS version {{ version }}</button>
</template>
'@ | Out-File -FilePath "src\components\HelloWorld.vue" -Encoding utf8
npx vite build
@'
const puppeteer = require('puppeteer');
const express = require('express');
const app = express();
app.use(express.static('./dist/'));
app.listen(7262, async() => {
await new Promise((res,rej) => setTimeout(res, 1000));
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on("console", msg => console.log("PAGE LOG:", msg.text()));
await page.setViewport({width: 1920, height: 1080});
const client = await page.target().createCDPSession();
await client.send('Browser.setDownloadBehavior', {
behavior: 'allow',
downloadPath: require("path").resolve('./')
});
page.on('request', req => console.log(req.url()));
await page.goto('http://localhost:7262/');
await page.click("button");
await new Promise((res,rej) => setTimeout(res, 2000));
await browser.close();
process.exit();
});
'@ | Out-File -FilePath "test.cjs" -Encoding utf8
node test.cjs
npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3
Remove-Item -Path "Presidents.xlsx" -Force
Set-Location -Path $tempDir
}
Set-Location $oldDir
Remove-Item -Path $tempDir -Recurse -Force