separate image gen util

This commit is contained in:
tree🌴 2024-02-13 14:22:58 +01:00
parent 68367e26b6
commit 28435f2463
4 changed files with 51 additions and 37 deletions

View file

@ -29,7 +29,7 @@ jobs:
deno-version: v1.x deno-version: v1.x
- name: Build bundle - name: Build bundle
run: make build run: make build images
- name: Setup node - name: Setup node
uses: actions/setup-node@v4 uses: actions/setup-node@v4

View file

@ -3,6 +3,9 @@ all: build frontend compile
build: build:
deno run --allow-all utils/build.js deno run --allow-all utils/build.js
images:
deno run --allow-all utils/images.js
frontend: frontend:
cd web && npm install && npm run build cd web && npm install && npm run build
@ -10,7 +13,7 @@ readme:
deno run --allow-all utils/readme.js deno run --allow-all utils/readme.js
sync: sync:
make readme make build readme
compile: compile:
cp -r web/dist/** dist cp -r web/dist/** dist

View file

@ -38,24 +38,6 @@ async function build() {
} }
await emptyDir(DEST_DIR); await emptyDir(DEST_DIR);
const imgDir = join(DEST_DIR, "img");
await emptyDir(imgDir);
// get images
for (const issue of issues) {
await genImage(
`https://news.web3privacy.info/image/${issue.week}?${
new Date().valueOf()
}`,
join(imgDir, `${issue.week}.png`),
);
}
// make cover
await genImage(
`https://news.web3privacy.info/cover`,
join(imgDir, "cover.png"),
);
const outputFn = join(DEST_DIR, "index.json"); const outputFn = join(DEST_DIR, "index.json");
await writeJSONFile(outputFn, issues); await writeJSONFile(outputFn, issues);
@ -95,21 +77,4 @@ async function writeJSONFile(fn, data) {
return Deno.writeTextFile(fn, JSON.stringify(data, null, 2)); return Deno.writeTextFile(fn, JSON.stringify(data, null, 2));
} }
async function genImage(url, fn) {
const imgResp = await fetch("https://html2svg.gwei.cz", {
method: "POST",
body: JSON.stringify({
url,
format: "png",
width: 1920,
height: 960,
}),
});
if (imgResp.body) {
const file = await Deno.open(fn, { write: true, create: true });
await imgResp.body.pipeTo(file.writable);
}
}
build(); build();

46
utils/images.js Normal file
View file

@ -0,0 +1,46 @@
import { join } from "https://deno.land/std@0.208.0/path/mod.ts";
import { emptyDir } from "https://deno.land/std@0.196.0/fs/empty_dir.ts";
const DEST_DIR = "./dist";
const imgDir = join(DEST_DIR, "img");
await emptyDir(imgDir);
const issues = JSON.parse(
await Deno.readTextFile(join(DEST_DIR, "index.json")),
);
// get images
for (const issue of issues) {
await genImage(
`https://news.web3privacy.info/image/${issue.week}?${new Date().valueOf()}`,
join(imgDir, `${issue.week}.png`),
);
}
// make cover
await genImage(
`https://news.web3privacy.info/cover`,
join(imgDir, "cover.png"),
);
console.log("Done");
async function genImage(url, fn) {
const imgResp = await fetch("https://html2svg.gwei.cz", {
method: "POST",
body: JSON.stringify({
url,
format: "png",
width: 1920,
height: 960,
}),
});
if (imgResp.body) {
const file = await Deno.open(fn, { write: true, create: true });
await imgResp.body.pipeTo(file.writable);
console.log(`Image written: ${fn}`);
}
}