docs.sheetjs.com/tests/data/pouchdb.ps1

110 lines
3.8 KiB
PowerShell
Raw Normal View History

2026-01-29 04:58:19 +00:00
#!/usr/bin/env pwsh
# https://docs.sheetjs.com/docs/demos/data/pouchdb
$oldDir = Get-Location
$tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-pouch"
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force }
New-Item -ItemType Directory -Path $tempDir | Out-Null
Set-Location -Path $tempDir
Invoke-WebRequest -Uri "https://docs.sheetjs.com/pouchdb/master.zip" -OutFile "master.zip"
Expand-Archive -Path "master.zip" -DestinationPath "."
Set-Location -Path "getting-started-todo-master"
# awk 'NR>1{print p} {p = $0}' js/app.js > export_code.js
$prev = $null
Get-Content js/app.js | ForEach-Object {
if ($prev -ne $null) { $_out += "`n" + $prev }
$prev = $_
}
$_out | Set-Content export_code.js
@'
document.getElementById("xport").addEventListener("click", function() {
console.log("clicked");
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
const aoo = doc.rows.map(r => {
const { _id, _rev, ...rest } = r.doc;
return rest;
});
const ws = XLSX.utils.json_to_sheet(aoo);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSPouch.xlsx");
});
});
})();
'@ | Out-File -FilePath "export_code.js" -Append -Encoding utf8
Copy-Item -Path "export_code.js" -Destination "js\app.js" -Force
$indexContent = Get-Content -Path "index.html" -Raw
$modifiedIndex = $indexContent -replace '<body>', '<body><script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script><button id="xport">Export!</button>'
$modifiedIndex | Out-File -FilePath "index.html" -Encoding utf8
npm init -y
npm i --save puppeteer express@4
@'
const puppeteer = require('puppeteer');
const express = require('express');
const app = express();
app.use(express.static('./'));
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 new Promise((res,rej) => setTimeout(res, 3000));
await page.focus('#new-todo');
await page.keyboard.type('JS');
await page.keyboard.press('Enter');
await new Promise((res,rej) => setTimeout(res, 1000));
await page.focus('#new-todo');
await page.keyboard.type('Sheet');
await page.keyboard.press('Enter');
await new Promise((res,rej) => setTimeout(res, 1000));
const toggles = await page.$$('.toggle');
await toggles[0].click();
await new Promise((res,rej) => setTimeout(res, 1000));
// Click export button
await page.click('#xport');
await new Promise((res,rej) => setTimeout(res, 3000));
await browser.close();
process.exit();
});
'@ | Out-File -FilePath "test.js" -Encoding utf8
$versions = @("9.0.0", "8.0.1", "7.3.1", "6.4.3", "5.4.5", "4.0.3", "3.6.0")
foreach ($version in $versions) {
Write-Host "PouchDB $version"
Copy-Item -Path "index.html" -Destination "index.html.bak" -Force
$currentIndex = Get-Content -Path "index.html" -Raw
$modifiedIndex = $currentIndex -replace 'pouchdb/3.2.0/pouchdb.min.js', "npm/pouchdb@${version}/dist/pouchdb.min.js"
$modifiedIndex | Out-File -FilePath "index.html" -Encoding utf8
node test.js
npx xlsx-cli SheetJSPouch.xlsx | Select-Object -First 3
Remove-Item -Path "SheetJSPouch.xlsx" -Force
Copy-Item -Path "index.html.bak" -Destination "index.html" -Force
}
Set-Location $oldDir
Remove-Item -Path $tempDir -Recurse -Force