120 lines
3.5 KiB
Bash
Executable File
120 lines
3.5 KiB
Bash
Executable File
#!/bin/bash
|
|
# https://docs.sheetjs.com/docs/demos/frontend/bundler/rollup
|
|
|
|
cd /tmp
|
|
rm -rf sheetjs-rollup
|
|
mkdir sheetjs-rollup
|
|
cd sheetjs-rollup
|
|
npm init -y
|
|
|
|
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
|
|
|
|
cat >index.js <<EOF
|
|
import { utils, version, writeFileXLSX } from 'xlsx';
|
|
|
|
document.getElementById("xport").addEventListener("click", async() => {
|
|
/* 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 = 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 */
|
|
writeFileXLSX(workbook, "Presidents.xlsx");
|
|
});
|
|
EOF
|
|
|
|
cat >index.html <<EOF
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head></head>
|
|
<body>
|
|
<h1>SheetJS Presidents Demo</h1>
|
|
<button id="xport">Click here to export</button>
|
|
<script type="module" src="./bundle.js"></script>
|
|
</body>
|
|
</html>
|
|
EOF
|
|
|
|
npm i --save puppeteer express@4
|
|
|
|
cat >test.js <<EOF
|
|
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('./')
|
|
});
|
|
await page.goto('http://localhost:7262/');
|
|
await page.click("#xport");
|
|
await new Promise((res,rej) => setTimeout(res, 1000));
|
|
await browser.close();
|
|
process.exit();
|
|
});
|
|
EOF
|
|
|
|
## RollupJS 4.x
|
|
|
|
npm i --save rollup@4.x @rollup/plugin-node-resolve
|
|
npx -y rollup@4.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife
|
|
node test.js
|
|
npx -y xlsx-cli Presidents.xlsx | head -n 3
|
|
rm -f bundle.js Presidents.xlsx
|
|
|
|
## RollupJS 3.x
|
|
|
|
npm i --save rollup@3.x @rollup/plugin-node-resolve
|
|
npx -y rollup@3.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife
|
|
node test.js
|
|
npx -y xlsx-cli Presidents.xlsx | head -n 3
|
|
rm -f bundle.js Presidents.xlsx
|
|
|
|
## RollupJS 2.x
|
|
|
|
npm i --save rollup@2.x @rollup/plugin-node-resolve
|
|
npx -y rollup@2.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife
|
|
node test.js
|
|
npx -y xlsx-cli Presidents.xlsx | head -n 3
|
|
rm -f bundle.js Presidents.xlsx
|
|
|
|
## RollupJS 1.x
|
|
|
|
npm i --save rollup@1.x rollup-plugin-node-resolve
|
|
npx -y rollup@1.x index.js --plugin rollup-plugin-node-resolve --file bundle.js --format iife
|
|
node test.js
|
|
npx -y xlsx-cli Presidents.xlsx | head -n 3
|
|
rm -f bundle.js Presidents.xlsx
|
|
|