Files
Docwell/Web-Clipper/scripts/package.mjs
T
tajniak81andClaude Opus 5.5 6984c7d162 Connect Google Calendar, import OneNote notebooks, sync Readwise, and package the clipper
Phase F follow-ups (ADR-16):

- Calendars: events are now stored in calendar_events by a
  calendars.sync job (every 15 minutes per account, 90 days back and
  400 ahead), so journals open at once and keep showing events while a
  source is down. Google accounts connect with OAuth (calendar.readonly)
  through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access.
- OneNote: whole notebooks import through Microsoft Graph (Notes.Read,
  DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with
  section groups, sections, subpages, pictures and attached files. The
  Microsoft sign-in lives in memory for an hour and is never stored.
- Readwise: an hourly sync with the person's sealed access token; new
  books get a page under "Readwise", new highlights are appended once.
  New readwise_syncs collection.
- internal/oauth: signed states and one callback route per provider,
  routed by purpose. directory gains ImportConverted and CallerOf.
- Phone app: "Today's journal" on the home screen and calendar events
  on journal pages.
- Web clipper: `npm run package` builds Chrome/Edge, Firefox and source
  zips; store/ holds listing texts, permission notes and a privacy
  policy.
- OpenAPI, generated clients, translations, compose and .env examples,
  and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:59:34 +01:00

122 lines
4.8 KiB
JavaScript

// SPDX-License-Identifier: AGPL-3.0-or-later
// Packs the built extension (dist/) for the stores, into release/:
//
// docwell-web-clipper-<version>-chrome.zip Chrome Web Store, Edge Add-ons
// docwell-web-clipper-<version>-firefox.zip addons.mozilla.org
// docwell-web-clipper-<version>-source.zip the source AMO asks for, since dist/ is built
//
// Run `npm run package -w @docwell/web-clipper` (it builds first). Uses only
// Node's own modules: the zips are written by hand with zlib's deflate.
import { deflateRawSync } from "node:zlib";
import { mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
import { dirname, join, relative, resolve, sep } from "node:path";
import { fileURLToPath } from "node:url";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const dist = join(root, "dist");
const out = join(root, "release");
const crcTable = new Uint32Array(256).map((_, n) => {
let c = n;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
return c >>> 0;
});
function crc32(buf) {
let c = 0xffffffff;
for (const b of buf) c = crcTable[(c ^ b) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
}
// A fixed time, so the same files make the same zip.
const DOS_TIME = 0;
const DOS_DATE = ((2024 - 1980) << 9) | (1 << 5) | 1;
/** Writes files ({name, data}) as a zip. */
function zip(path, files) {
const parts = [];
const central = [];
let offset = 0;
for (const { name, data } of files) {
const nameBuf = Buffer.from(name, "utf8");
const packed = deflateRawSync(data, { level: 9 });
const stored = packed.length >= data.length;
const body = stored ? data : packed;
const crc = crc32(data);
const local = Buffer.alloc(30);
local.writeUInt32LE(0x04034b50, 0);
local.writeUInt16LE(20, 4);
local.writeUInt16LE(0x0800, 6); // UTF-8 names
local.writeUInt16LE(stored ? 0 : 8, 8);
local.writeUInt16LE(DOS_TIME, 10);
local.writeUInt16LE(DOS_DATE, 12);
local.writeUInt32LE(crc, 14);
local.writeUInt32LE(body.length, 18);
local.writeUInt32LE(data.length, 22);
local.writeUInt16LE(nameBuf.length, 26);
local.writeUInt16LE(0, 28);
parts.push(local, nameBuf, body);
const entry = Buffer.alloc(46);
entry.writeUInt32LE(0x02014b50, 0);
entry.writeUInt16LE(20, 4);
entry.writeUInt16LE(20, 6);
entry.writeUInt16LE(0x0800, 8);
entry.writeUInt16LE(stored ? 0 : 8, 10);
entry.writeUInt16LE(DOS_TIME, 12);
entry.writeUInt16LE(DOS_DATE, 14);
entry.writeUInt32LE(crc, 16);
entry.writeUInt32LE(body.length, 20);
entry.writeUInt32LE(data.length, 24);
entry.writeUInt16LE(nameBuf.length, 28);
entry.writeUInt32LE(offset, 42);
central.push(entry, nameBuf);
offset += local.length + nameBuf.length + body.length;
}
const centralBuf = Buffer.concat(central);
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0);
end.writeUInt16LE(files.length, 8);
end.writeUInt16LE(files.length, 10);
end.writeUInt32LE(centralBuf.length, 12);
end.writeUInt32LE(offset, 16);
writeFileSync(path, Buffer.concat([...parts, centralBuf, end]));
console.log(`${relative(root, path)} (${files.length} files)`);
}
/** Every file under dir, as zip names relative to base. */
function walk(dir, base, skip = () => false) {
const files = [];
for (const entry of readdirSync(dir).sort()) {
const full = join(dir, entry);
const name = relative(base, full).split(sep).join("/");
if (skip(name)) continue;
if (statSync(full).isDirectory()) files.push(...walk(full, base, skip));
else files.push({ name, data: readFileSync(full) });
}
return files;
}
const manifest = JSON.parse(readFileSync(join(dist, "manifest.json"), "utf8"));
const version = manifest.version;
mkdirSync(out, { recursive: true });
const built = walk(dist, dist).filter((f) => f.name !== "manifest.json");
const withManifest = (m) => [{ name: "manifest.json", data: Buffer.from(JSON.stringify(m, null, 2) + "\n") }, ...built];
// Chrome and Edge do not know Firefox's settings and warn about them.
const chrome = { ...manifest };
delete chrome.browser_specific_settings;
zip(join(out, `docwell-web-clipper-${version}-chrome.zip`), withManifest(chrome));
zip(join(out, `docwell-web-clipper-${version}-firefox.zip`), withManifest(manifest));
// AMO reviews the code the extension is built from: this folder as
// Web-Clipper/, next to the repository files its build reads. It builds on
// its own: cd Web-Clipper && npm install && npm run build.
const repo = resolve(root, "..");
const source = walk(root, root, (n) => /^(node_modules|dist|release)(\/|$)/.test(n)).map((f) => ({
...f,
name: `Web-Clipper/${f.name}`,
}));
for (const f of ["tsconfig.base.json", "LICENSE"]) source.push({ name: f, data: readFileSync(join(repo, f)) });
zip(join(out, `docwell-web-clipper-${version}-source.zip`), source);