wrapt-cli 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/cli.js +80 -8
- package/dist/open.js +31 -0
- package/dist/publish.js +9 -1
- package/dist/share/airdrop.js +59 -0
- package/dist/share/page.js +19 -12
- package/dist/share/server.js +32 -1
- package/dist/share/zip.js +68 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,21 @@ Put your own first name in place of Ada. When it's done, a QR code appears in yo
|
|
|
24
24
|
Scan it with your phone on the same Wi-Fi to open your cards, save them and copy a ready-made
|
|
25
25
|
caption for each platform.
|
|
26
26
|
|
|
27
|
+
Your cards are saved in `~/.wrapt/<year>/`, with a ZIP of each set. To open that folder:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx wrapt-cli open
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**On a Mac with an iPhone?** Skip the QR code and AirDrop the cards straight into Photos:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npx wrapt-cli airdrop
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Pick your iPhone in the AirDrop window and tap **Accept** on the phone. Add `--set carousel` for
|
|
40
|
+
the carousel cards, or `--set all` for every card.
|
|
41
|
+
|
|
27
42
|
Step-by-step guide: **https://wrapt.cc**
|
|
28
43
|
|
|
29
44
|
## What you get
|
package/dist/cli.js
CHANGED
|
@@ -6,7 +6,9 @@ import { analyze, wraptHome } from "./analyze.js";
|
|
|
6
6
|
import { NarrativeSchema, narrativeJsonSchema } from "./narrative.js";
|
|
7
7
|
import { renderAll } from "./render/index.js";
|
|
8
8
|
import { SITE_URL } from "./site.js";
|
|
9
|
-
import { startShare } from "./share/server.js";
|
|
9
|
+
import { cardZips, collectAssets, startShare } from "./share/server.js";
|
|
10
|
+
import { openFolder, tildify } from "./open.js";
|
|
11
|
+
import { airdrop } from "./share/airdrop.js";
|
|
10
12
|
import qrcode from "qrcode-terminal";
|
|
11
13
|
import readlinePromises from "node:readline/promises";
|
|
12
14
|
import { buildSite, pushSite, resolveTarget } from "./publish.js";
|
|
@@ -22,6 +24,8 @@ Usage
|
|
|
22
24
|
wrapt render [--year 2026] [--name "Ada"] [--show-projects] [--theme bold|mono|pastel] [--out <dir>]
|
|
23
25
|
wrapt schema [--validate <narrative.json>]
|
|
24
26
|
wrapt share [--year 2026] [--minutes 15] [--port <n>]
|
|
27
|
+
wrapt airdrop [--year 2026] [--set story|carousel|all]
|
|
28
|
+
wrapt open [--year 2026] [--set story|carousel]
|
|
25
29
|
wrapt publish --repo <owner/name> [--branch gh-pages] [--url <base>] [--dry-run] [--yes] [--remove]
|
|
26
30
|
|
|
27
31
|
Commands
|
|
@@ -29,6 +33,8 @@ Commands
|
|
|
29
33
|
render Turn stats.json (+ narrative.json) into story/carousel PNGs, linkedin.pdf and x-card.png
|
|
30
34
|
schema Print the JSON Schema for narrative.json, or --validate a file against it
|
|
31
35
|
share Serve the cards on your Wi-Fi and show a QR code to open them on your phone. No upload.
|
|
36
|
+
airdrop Mac only: AirDrop the cards to your iPhone, where they go straight into Photos.
|
|
37
|
+
open Open the folder with your cards and ZIPs (Finder, File Explorer or your file manager).
|
|
32
38
|
publish Opt-in: put the cards on GitHub Pages (a gh-pages branch in YOUR repo) with link previews.
|
|
33
39
|
Uploads only the rendered images, PDF and page. --remove takes the year down again.
|
|
34
40
|
|
|
@@ -61,6 +67,7 @@ async function main(argv) {
|
|
|
61
67
|
theme: { type: "string" },
|
|
62
68
|
validate: { type: "string" },
|
|
63
69
|
minutes: { type: "string" },
|
|
70
|
+
set: { type: "string" },
|
|
64
71
|
repo: { type: "string" },
|
|
65
72
|
branch: { type: "string" },
|
|
66
73
|
url: { type: "string" },
|
|
@@ -79,6 +86,8 @@ async function main(argv) {
|
|
|
79
86
|
const year = values.year ? Number(values.year) : new Date().getFullYear();
|
|
80
87
|
if (!Number.isInteger(year) || year < 2000 || year > 3000)
|
|
81
88
|
throw new Error(`Invalid --year: ${values.year}`);
|
|
89
|
+
/** The full command to run next, keeping --year/--out so it finds the same cards. */
|
|
90
|
+
const next = (sub) => `npx wrapt-cli ${sub}${values.year ? ` --year ${year}` : ""}${values.out ? ` --out "${values.out}"` : ""}`;
|
|
82
91
|
switch (cmd) {
|
|
83
92
|
case "analyze": {
|
|
84
93
|
const res = await analyze({ year, projectsDir: values["projects-dir"], outDir: values.out, source: values.source });
|
|
@@ -108,16 +117,32 @@ async function main(argv) {
|
|
|
108
117
|
}
|
|
109
118
|
const t0 = Date.now();
|
|
110
119
|
const res = await renderAll({ stats, narrative, name: values.name, hideProjects: values["hide-projects"] || !values["show-projects"], theme: values.theme, outDir: dir });
|
|
120
|
+
// Every card of a set in one file, for downloading or sending in one go.
|
|
121
|
+
const zipNames = [];
|
|
122
|
+
for (const [set, buf] of Object.entries(cardZips(dir, year, collectAssets(dir)))) {
|
|
123
|
+
const name = `wrapt-${year}-${set}.zip`;
|
|
124
|
+
fs.writeFileSync(path.join(dir, name), buf);
|
|
125
|
+
zipNames.push(name);
|
|
126
|
+
}
|
|
127
|
+
const n = res.story.length;
|
|
128
|
+
const row = (left, right) => ` ${left.padEnd(26)}${right}`;
|
|
111
129
|
process.stdout.write([
|
|
112
130
|
"",
|
|
113
|
-
`
|
|
114
|
-
|
|
115
|
-
`
|
|
116
|
-
|
|
117
|
-
|
|
131
|
+
` ✓ Your cards are ready (${((Date.now() - t0) / 1000).toFixed(1)}s)${narrative ? "" : ". Card 4 shows your week, and the archetype comes from your rhythm."}`,
|
|
132
|
+
"",
|
|
133
|
+
` Saved in ${tildify(dir)}`,
|
|
134
|
+
row("story/", `${n} Instagram Story cards (1080 × 1920)`),
|
|
135
|
+
row("carousel/", `${res.carousel.length} Instagram carousel cards (1080 × 1350)`),
|
|
136
|
+
...zipNames.map((z) => row(z, `all the ${z.includes("story") ? "story" : "carousel"} cards in one file`)),
|
|
137
|
+
row(path.basename(res.pdf), "LinkedIn document post"),
|
|
138
|
+
row(path.basename(res.xCard), "X post"),
|
|
139
|
+
"",
|
|
140
|
+
" Next",
|
|
141
|
+
row("Open the folder:", next("open")),
|
|
142
|
+
row("Send them to your phone:", next("share")),
|
|
143
|
+
...(process.platform === "darwin" ? [row("Mac + iPhone:", next("airdrop"))] : []),
|
|
118
144
|
"",
|
|
119
145
|
` When you post, link ${SITE_URL} so people can make their own.`,
|
|
120
|
-
" Run `npx wrapt-cli share` for ready-to-copy captions on your phone.",
|
|
121
146
|
"",
|
|
122
147
|
].join("\n") + "\n");
|
|
123
148
|
return;
|
|
@@ -148,6 +173,7 @@ async function main(argv) {
|
|
|
148
173
|
` ${share.url}`,
|
|
149
174
|
share.lan ? "" : " ! No Wi-Fi/LAN address found, so only this computer can open the link.",
|
|
150
175
|
` Serving only your rendered cards and PDF. Nothing is uploaded. Stops in ${minutes} min or on Ctrl+C.`,
|
|
176
|
+
...(process.platform === "darwin" ? [" Sending to an iPhone? In another terminal, `npx wrapt-cli airdrop` saves them straight to Photos."] : []),
|
|
151
177
|
"",
|
|
152
178
|
].join("\n") + "\n");
|
|
153
179
|
const stop = () => {
|
|
@@ -204,7 +230,7 @@ async function main(argv) {
|
|
|
204
230
|
const L = [
|
|
205
231
|
"",
|
|
206
232
|
` Site built: ${siteDir} (${files.length} files)`,
|
|
207
|
-
" Contains only: story + carousel PNGs, linkedin.pdf, x-card.png, index.html, font.",
|
|
233
|
+
" Contains only: story + carousel PNGs (plus a ZIP of each), linkedin.pdf, x-card.png, index.html, font.",
|
|
208
234
|
" Never uploaded: stats.json, samples.json, narrative.json, report.md.",
|
|
209
235
|
];
|
|
210
236
|
const projects = stats.stack.projects.slice(0, 3).map((p) => p.name);
|
|
@@ -229,6 +255,52 @@ async function main(argv) {
|
|
|
229
255
|
].join("\n") + "\n");
|
|
230
256
|
return;
|
|
231
257
|
}
|
|
258
|
+
case "airdrop": {
|
|
259
|
+
const dir = values.out ?? path.join(wraptHome(), String(year));
|
|
260
|
+
const set = values.set ?? "story";
|
|
261
|
+
if (!["story", "carousel", "all"].includes(set))
|
|
262
|
+
throw new Error("--set must be story, carousel or all");
|
|
263
|
+
if (process.platform !== "darwin") {
|
|
264
|
+
throw new Error("AirDrop needs a Mac. Use `wrapt share` instead: it shows a QR code to open the cards on your phone.");
|
|
265
|
+
}
|
|
266
|
+
const assets = collectAssets(dir);
|
|
267
|
+
const rels = set === "story" ? assets.story : set === "carousel" ? assets.carousel : [...assets.story, ...assets.carousel, ...(assets.xCard ? [assets.xCard] : [])];
|
|
268
|
+
if (!rels.length)
|
|
269
|
+
throw new Error(`No cards in ${dir}. Run \`wrapt render\` first.`);
|
|
270
|
+
process.stdout.write([
|
|
271
|
+
"",
|
|
272
|
+
` Opening AirDrop with ${rels.length} ${set === "all" ? "cards" : `${set} cards`}…`,
|
|
273
|
+
" 1. In the AirDrop window on your Mac, pick your iPhone.",
|
|
274
|
+
" 2. On your iPhone, tap Accept. The cards go straight into Photos.",
|
|
275
|
+
" (Your iPhone needs AirDrop on: Settings → General → AirDrop → Contacts Only or Everyone.)",
|
|
276
|
+
"",
|
|
277
|
+
].join("\n") + "\n");
|
|
278
|
+
const result = await airdrop(rels.map((r) => path.join(dir, r)));
|
|
279
|
+
const messages = {
|
|
280
|
+
sent: ` ✓ Sent ${rels.length} cards. Check the Photos app on your iPhone.`,
|
|
281
|
+
cancelled: " AirDrop was closed before sending. Run it again when you're ready.",
|
|
282
|
+
timeout: " Stopped waiting after 10 minutes. Run it again when you're ready.",
|
|
283
|
+
unavailable: " AirDrop isn't available. Check that Wi-Fi and Bluetooth are on, or use `wrapt share` instead.",
|
|
284
|
+
};
|
|
285
|
+
process.stdout.write((messages[result] ?? ` AirDrop ${result}. Try again, or use \`wrapt share\` instead.`) + "\n");
|
|
286
|
+
if (result !== "sent")
|
|
287
|
+
process.exitCode = 1;
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
case "open": {
|
|
291
|
+
const dir = values.out ?? path.join(wraptHome(), String(year));
|
|
292
|
+
const set = values.set;
|
|
293
|
+
if (set && !["story", "carousel"].includes(set))
|
|
294
|
+
throw new Error("--set must be story or carousel");
|
|
295
|
+
const target = set ? path.join(dir, set) : dir;
|
|
296
|
+
if (!fs.existsSync(target))
|
|
297
|
+
throw new Error(`No cards in ${tildify(dir)} yet. Run \`${next("render")}\` first.`);
|
|
298
|
+
const opened = await openFolder(target);
|
|
299
|
+
process.stdout.write(opened
|
|
300
|
+
? ` Opened ${tildify(target)}\n`
|
|
301
|
+
: ` Couldn't open a file browser here. Your cards are in:\n ${target}\n`);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
232
304
|
case "schema": {
|
|
233
305
|
if (!values.validate) {
|
|
234
306
|
process.stdout.write(JSON.stringify(narrativeJsonSchema(), null, 2) + "\n");
|
package/dist/open.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
/** The command that opens a folder in the system's file browser (Finder, File Explorer, or the Linux default). */
|
|
4
|
+
export function openerFor(platform, dir) {
|
|
5
|
+
if (platform === "darwin")
|
|
6
|
+
return ["open", [dir]];
|
|
7
|
+
if (platform === "win32")
|
|
8
|
+
return ["explorer.exe", [dir]];
|
|
9
|
+
return ["xdg-open", [dir]];
|
|
10
|
+
}
|
|
11
|
+
/** Opens `dir` in the file browser. Resolves false if no file browser could be started. */
|
|
12
|
+
export function openFolder(dir, platform = process.platform) {
|
|
13
|
+
const [cmd, args] = openerFor(platform, dir);
|
|
14
|
+
return new Promise((resolve) => {
|
|
15
|
+
try {
|
|
16
|
+
const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
|
|
17
|
+
child.once("error", () => resolve(false));
|
|
18
|
+
child.once("spawn", () => {
|
|
19
|
+
child.unref();
|
|
20
|
+
resolve(true);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
resolve(false);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** "/Users/ada/.wrapt/2026" -> "~/.wrapt/2026", for shorter, copyable output. */
|
|
29
|
+
export function tildify(p, home = os.homedir()) {
|
|
30
|
+
return p === home || p.startsWith(home + "/") ? "~" + p.slice(home.length) : p;
|
|
31
|
+
}
|
package/dist/publish.js
CHANGED
|
@@ -7,7 +7,7 @@ import { brandFor } from "./brand.js";
|
|
|
7
7
|
import { human } from "./format.js";
|
|
8
8
|
import { possessive } from "./render/cards.js";
|
|
9
9
|
import { sharePage } from "./share/page.js";
|
|
10
|
-
import { collectAssets } from "./share/server.js";
|
|
10
|
+
import { cardZips, collectAssets } from "./share/server.js";
|
|
11
11
|
const FONT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../assets/fonts/SpaceGrotesk-Bold.ttf");
|
|
12
12
|
/**
|
|
13
13
|
* "owner/name", "git@github.com:owner/name.git", "https://github.com/owner/name" or a local
|
|
@@ -54,6 +54,13 @@ export function buildSite(o) {
|
|
|
54
54
|
if (rel)
|
|
55
55
|
copy(rel);
|
|
56
56
|
copy("font.ttf", FONT);
|
|
57
|
+
const zips = {};
|
|
58
|
+
for (const [set, buf] of Object.entries(cardZips(o.dir, o.stats.year, assets))) {
|
|
59
|
+
const rel = `wrapt-${o.stats.year}-${set}.zip`;
|
|
60
|
+
fs.writeFileSync(path.join(siteDir, rel), buf);
|
|
61
|
+
files.push(rel);
|
|
62
|
+
zips[set] = rel;
|
|
63
|
+
}
|
|
57
64
|
const brand = brandFor(o.stats);
|
|
58
65
|
const t = o.stats.totals;
|
|
59
66
|
const who = possessive(o.name);
|
|
@@ -68,6 +75,7 @@ export function buildSite(o) {
|
|
|
68
75
|
fontUrl: "font.ttf",
|
|
69
76
|
heading: `${esc(who)} <em>${o.stats.year}</em>, wrapped.`,
|
|
70
77
|
og: { title, description, image: `${o.pageUrl}${assets.xCard}`, url: o.pageUrl },
|
|
78
|
+
zips,
|
|
71
79
|
});
|
|
72
80
|
fs.writeFileSync(path.join(siteDir, "index.html"), html);
|
|
73
81
|
files.push("index.html");
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
/**
|
|
3
|
+
* Opens macOS's AirDrop panel with the given files, so a Mac user can send every card to their
|
|
4
|
+
* iPhone in one go, where they land straight in Photos. Uses the system's own sharing service
|
|
5
|
+
* through JavaScript for Automation (osascript), so nothing extra is installed and nothing goes
|
|
6
|
+
* over the internet: AirDrop is a direct device-to-device transfer.
|
|
7
|
+
*/
|
|
8
|
+
const SCRIPT = `
|
|
9
|
+
ObjC.import('AppKit');
|
|
10
|
+
function run(argv) {
|
|
11
|
+
var timeoutSec = parseInt(argv.shift(), 10);
|
|
12
|
+
var app = $.NSApplication.sharedApplication;
|
|
13
|
+
app.setActivationPolicy($.NSApplicationActivationPolicyAccessory);
|
|
14
|
+
var svc = $.NSSharingService.sharingServiceNamed('com.apple.share.AirDrop.send');
|
|
15
|
+
if (!svc || svc.isNil()) return 'unavailable';
|
|
16
|
+
var items = $.NSMutableArray.array;
|
|
17
|
+
argv.forEach(function (p) { items.addObject($.NSURL.fileURLWithPath(p)); });
|
|
18
|
+
if (!svc.canPerformWithItems(items)) return 'unavailable';
|
|
19
|
+
var result = null;
|
|
20
|
+
ObjC.registerSubclass({
|
|
21
|
+
name: 'WraptAirDropDelegate',
|
|
22
|
+
protocols: ['NSSharingServiceDelegate'],
|
|
23
|
+
methods: {
|
|
24
|
+
'sharingService:didShareItems:': { types: ['void', ['id', 'id']], implementation: function () { result = 'sent'; } },
|
|
25
|
+
'sharingService:didFailToShareItems:error:': { types: ['void', ['id', 'id', 'id']], implementation: function (s, i, e) {
|
|
26
|
+
result = e.code == 3072 ? 'cancelled' : 'failed: ' + ObjC.unwrap(e.localizedDescription);
|
|
27
|
+
} }
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
svc.delegate = $.WraptAirDropDelegate.alloc.init;
|
|
31
|
+
app.activateIgnoringOtherApps(true);
|
|
32
|
+
svc.performWithItems(items);
|
|
33
|
+
var deadline = Date.now() + timeoutSec * 1000;
|
|
34
|
+
while (result === null && Date.now() < deadline) {
|
|
35
|
+
$.NSRunLoop.currentRunLoop.runUntilDate($.NSDate.dateWithTimeIntervalSinceNow(0.2));
|
|
36
|
+
}
|
|
37
|
+
return result || 'timeout';
|
|
38
|
+
}
|
|
39
|
+
`;
|
|
40
|
+
const run = (cmd, args) => new Promise((resolve, reject) => execFile(cmd, args, { maxBuffer: 1 << 20 }, (err, stdout, stderr) => {
|
|
41
|
+
if (!err)
|
|
42
|
+
return resolve(stdout);
|
|
43
|
+
// err.message repeats the whole command line (the script and every path), so keep it short.
|
|
44
|
+
const why = stderr.trim().split("\n").pop() || (err.signal ? `stopped (${err.signal})` : `exited with code ${err.code}`);
|
|
45
|
+
reject(new Error(`AirDrop ${why}`));
|
|
46
|
+
}));
|
|
47
|
+
/** The osascript command line that opens AirDrop with `files`. */
|
|
48
|
+
export function airdropArgs(files, timeoutSec) {
|
|
49
|
+
return ["-l", "JavaScript", "-e", SCRIPT, String(timeoutSec), ...files];
|
|
50
|
+
}
|
|
51
|
+
/** Opens the AirDrop panel with `files` and waits until they're sent, cancelled or `timeoutSec` passes. */
|
|
52
|
+
export async function airdrop(files, opts = {}) {
|
|
53
|
+
if ((opts.platform ?? process.platform) !== "darwin")
|
|
54
|
+
return "unavailable";
|
|
55
|
+
if (!files.length)
|
|
56
|
+
throw new Error("No files to send.");
|
|
57
|
+
const out = await (opts.exec ?? run)("osascript", airdropArgs(files, opts.timeoutSec ?? 600));
|
|
58
|
+
return out.trim();
|
|
59
|
+
}
|
package/dist/share/page.js
CHANGED
|
@@ -148,6 +148,8 @@ ${a.carousel.length
|
|
|
148
148
|
<script>
|
|
149
149
|
(function () {
|
|
150
150
|
var sets = ${JSON.stringify({ story: a.story.map(f), carousel: a.carousel.map(f) })};
|
|
151
|
+
var zips = ${JSON.stringify(o.zips ?? {})};
|
|
152
|
+
var YEAR = ${JSON.stringify(o.year)};
|
|
151
153
|
var LOCAL = ${local ? "true" : "false"};
|
|
152
154
|
var ua = navigator.userAgent;
|
|
153
155
|
var iOS = /iPhone|iPad|iPod/.test(ua) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
|
|
@@ -167,14 +169,12 @@ ${a.carousel.length
|
|
|
167
169
|
return true;
|
|
168
170
|
}
|
|
169
171
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
a.href = LOCAL ? u + "?dl=1" : u; a.download = ""; document.body.appendChild(a); a.click(); a.remove();
|
|
175
|
-
}, i * 400);
|
|
176
|
-
});
|
|
172
|
+
// One file, one download: browsers block or ask about a burst of separate downloads.
|
|
173
|
+
function downloadZip(url, filename) {
|
|
174
|
+
var a = document.createElement("a");
|
|
175
|
+
a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove();
|
|
177
176
|
}
|
|
177
|
+
var touch = navigator.maxTouchPoints > 0 || "ontouchstart" in window;
|
|
178
178
|
|
|
179
179
|
// This page is plain HTTP on the local network, where navigator.clipboard is unavailable; fall back to execCommand.
|
|
180
180
|
document.querySelectorAll("button[data-cap]").forEach(function (btn) {
|
|
@@ -196,18 +196,25 @@ ${a.carousel.length
|
|
|
196
196
|
var urls = sets[name];
|
|
197
197
|
btn.disabled = true;
|
|
198
198
|
try {
|
|
199
|
-
//
|
|
200
|
-
if (window.isSecureContext && navigator.share && (await shareFiles(urls))) return;
|
|
199
|
+
// On a phone over HTTPS, the share sheet saves every card straight to Photos in one go.
|
|
200
|
+
if (touch && window.isSecureContext && navigator.share && (await shareFiles(urls))) return;
|
|
201
201
|
} catch (e) {
|
|
202
202
|
if (e && e.name === "AbortError") return;
|
|
203
203
|
} finally {
|
|
204
204
|
btn.disabled = false;
|
|
205
205
|
}
|
|
206
|
+
var file = "wrapt-" + YEAR + "-" + name + ".zip";
|
|
207
|
+
if (!zips[name]) {
|
|
208
|
+
hint(name, "Use the <b>Save</b> button under each card.");
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
downloadZip(zips[name], file);
|
|
206
212
|
if (iOS) {
|
|
207
|
-
hint(name, "<b>
|
|
213
|
+
hint(name, "Saved <b>" + file + "</b> to <b>Files → Downloads</b>. Open it there to unzip, tap <b>Select</b> → select all → <b>Share</b> → <b>Save " + urls.length + " Images</b> to put them in Photos.");
|
|
214
|
+
} else if (touch) {
|
|
215
|
+
hint(name, "Saved <b>" + file + "</b> to your Downloads. Open it in your Files app and choose <b>Extract</b> to add the " + urls.length + " cards to your gallery.");
|
|
208
216
|
} else {
|
|
209
|
-
hint(name, "
|
|
210
|
-
downloadAll(urls);
|
|
217
|
+
hint(name, "Downloaded <b>" + file + "</b> with all " + urls.length + " cards.");
|
|
211
218
|
}
|
|
212
219
|
});
|
|
213
220
|
});
|
package/dist/share/server.js
CHANGED
|
@@ -7,6 +7,7 @@ import { fileURLToPath } from "node:url";
|
|
|
7
7
|
import { brandFor } from "../brand.js";
|
|
8
8
|
import { StatsSchema } from "../stats/schema.js";
|
|
9
9
|
import { sharePage } from "./page.js";
|
|
10
|
+
import { zip } from "./zip.js";
|
|
10
11
|
const FONT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../assets/fonts/SpaceGrotesk-Bold.ttf");
|
|
11
12
|
const TYPES = {
|
|
12
13
|
".png": "image/png",
|
|
@@ -50,6 +51,17 @@ export function collectAssets(dir) {
|
|
|
50
51
|
const maybe = (f) => (fs.existsSync(path.join(dir, f)) ? f : undefined);
|
|
51
52
|
return { story: pngs("story"), carousel: pngs("carousel"), pdf: maybe("linkedin.pdf"), xCard: maybe("x-card.png") };
|
|
52
53
|
}
|
|
54
|
+
/** One ZIP per card set ("story", "carousel"), named wrapt-<year>-<set>.zip with wrapt-<year>-<set>-01.png… inside. */
|
|
55
|
+
export function cardZips(dir, year, assets) {
|
|
56
|
+
const out = {};
|
|
57
|
+
for (const set of ["story", "carousel"]) {
|
|
58
|
+
const files = assets[set];
|
|
59
|
+
if (!files.length)
|
|
60
|
+
continue;
|
|
61
|
+
out[set] = zip(files.map((rel) => ({ name: `wrapt-${year}-${set}-${path.basename(rel)}`, data: fs.readFileSync(path.join(dir, rel)) })));
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
53
65
|
/** Names the tools behind this year's cards for the captions. Falls back to Claude Code when stats.json is missing or unreadable. */
|
|
54
66
|
function toolName(dir) {
|
|
55
67
|
try {
|
|
@@ -70,7 +82,15 @@ export async function startShare(opts) {
|
|
|
70
82
|
allowed.set(`/${token}/f/${rel}`, path.join(opts.dir, rel));
|
|
71
83
|
}
|
|
72
84
|
allowed.set(`/${token}/font.ttf`, FONT);
|
|
73
|
-
const
|
|
85
|
+
const zips = cardZips(opts.dir, opts.year, assets);
|
|
86
|
+
const zipPath = (set) => `/${token}/zip/wrapt-${opts.year}-${set}.zip`;
|
|
87
|
+
const zipByPath = new Map(Object.entries(zips).map(([set, buf]) => [zipPath(set), buf]));
|
|
88
|
+
const html = sharePage(assets, {
|
|
89
|
+
year: opts.year,
|
|
90
|
+
base: `/${token}`,
|
|
91
|
+
tool: toolName(opts.dir),
|
|
92
|
+
zips: Object.fromEntries(Object.keys(zips).map((set) => [set, zipPath(set)])),
|
|
93
|
+
});
|
|
74
94
|
const server = http.createServer((req, res) => {
|
|
75
95
|
const url = new URL(req.url ?? "/", "http://x");
|
|
76
96
|
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
@@ -89,6 +109,17 @@ export async function startShare(opts) {
|
|
|
89
109
|
res.end(req.method === "HEAD" ? undefined : html);
|
|
90
110
|
return;
|
|
91
111
|
}
|
|
112
|
+
const zipBuf = zipByPath.get(url.pathname);
|
|
113
|
+
if (zipBuf) {
|
|
114
|
+
opts.onRequest?.(url.pathname.slice(token.length + 1));
|
|
115
|
+
res.writeHead(200, {
|
|
116
|
+
"Content-Type": "application/zip",
|
|
117
|
+
"Content-Length": String(zipBuf.length),
|
|
118
|
+
"Content-Disposition": `attachment; filename="${path.basename(url.pathname)}"`,
|
|
119
|
+
});
|
|
120
|
+
res.end(req.method === "HEAD" ? undefined : zipBuf);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
92
123
|
const file = allowed.get(url.pathname);
|
|
93
124
|
if (!file) {
|
|
94
125
|
res.writeHead(404, { "Content-Type": "text/plain" }).end("Not found");
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal ZIP writer, so "Save all" can hand over every card as one download.
|
|
3
|
+
* Files are stored without compression: PNGs and PDFs are already compressed.
|
|
4
|
+
*/
|
|
5
|
+
const CRC_TABLE = (() => {
|
|
6
|
+
const t = new Uint32Array(256);
|
|
7
|
+
for (let n = 0; n < 256; n++) {
|
|
8
|
+
let c = n;
|
|
9
|
+
for (let k = 0; k < 8; k++)
|
|
10
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
11
|
+
t[n] = c >>> 0;
|
|
12
|
+
}
|
|
13
|
+
return t;
|
|
14
|
+
})();
|
|
15
|
+
export function crc32(buf) {
|
|
16
|
+
let c = 0xffffffff;
|
|
17
|
+
for (let i = 0; i < buf.length; i++)
|
|
18
|
+
c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
|
|
19
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
20
|
+
}
|
|
21
|
+
/** DOS date/time for 2026-01-01 00:00, so archives are reproducible and carry no local timestamp. */
|
|
22
|
+
const DOS_TIME = 0;
|
|
23
|
+
const DOS_DATE = ((2026 - 1980) << 9) | (1 << 5) | 1;
|
|
24
|
+
export function zip(entries) {
|
|
25
|
+
const parts = [];
|
|
26
|
+
const central = [];
|
|
27
|
+
let offset = 0;
|
|
28
|
+
for (const e of entries) {
|
|
29
|
+
const name = Buffer.from(e.name, "utf8");
|
|
30
|
+
const crc = crc32(e.data);
|
|
31
|
+
const local = Buffer.alloc(30);
|
|
32
|
+
local.writeUInt32LE(0x04034b50, 0); // local file header
|
|
33
|
+
local.writeUInt16LE(20, 4); // version needed
|
|
34
|
+
local.writeUInt16LE(0x0800, 6); // flags: UTF-8 names
|
|
35
|
+
local.writeUInt16LE(0, 8); // method: stored
|
|
36
|
+
local.writeUInt16LE(DOS_TIME, 10);
|
|
37
|
+
local.writeUInt16LE(DOS_DATE, 12);
|
|
38
|
+
local.writeUInt32LE(crc, 14);
|
|
39
|
+
local.writeUInt32LE(e.data.length, 18);
|
|
40
|
+
local.writeUInt32LE(e.data.length, 22);
|
|
41
|
+
local.writeUInt16LE(name.length, 26);
|
|
42
|
+
local.writeUInt16LE(0, 28);
|
|
43
|
+
parts.push(local, name, e.data);
|
|
44
|
+
const cd = Buffer.alloc(46);
|
|
45
|
+
cd.writeUInt32LE(0x02014b50, 0); // central directory header
|
|
46
|
+
cd.writeUInt16LE(20, 4); // version made by
|
|
47
|
+
cd.writeUInt16LE(20, 6); // version needed
|
|
48
|
+
cd.writeUInt16LE(0x0800, 8);
|
|
49
|
+
cd.writeUInt16LE(0, 10);
|
|
50
|
+
cd.writeUInt16LE(DOS_TIME, 12);
|
|
51
|
+
cd.writeUInt16LE(DOS_DATE, 14);
|
|
52
|
+
cd.writeUInt32LE(crc, 16);
|
|
53
|
+
cd.writeUInt32LE(e.data.length, 20);
|
|
54
|
+
cd.writeUInt32LE(e.data.length, 24);
|
|
55
|
+
cd.writeUInt16LE(name.length, 28);
|
|
56
|
+
cd.writeUInt32LE(offset, 42);
|
|
57
|
+
central.push(cd, name);
|
|
58
|
+
offset += local.length + name.length + e.data.length;
|
|
59
|
+
}
|
|
60
|
+
const cdSize = central.reduce((n, b) => n + b.length, 0);
|
|
61
|
+
const end = Buffer.alloc(22);
|
|
62
|
+
end.writeUInt32LE(0x06054b50, 0); // end of central directory
|
|
63
|
+
end.writeUInt16LE(entries.length, 8);
|
|
64
|
+
end.writeUInt16LE(entries.length, 10);
|
|
65
|
+
end.writeUInt32LE(cdSize, 12);
|
|
66
|
+
end.writeUInt32LE(offset, 16);
|
|
67
|
+
return Buffer.concat([...parts, ...central, end]);
|
|
68
|
+
}
|