@phnx-labs/artifacts-cli 0.4.6 → 0.5.0
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/CHANGELOG.md +130 -0
- package/README.md +127 -12
- package/dist/cli.d.ts +1 -6
- package/dist/cli.js +93 -85
- package/dist/cli.js.map +1 -1
- package/dist/commands/auth.d.ts +5 -0
- package/dist/commands/auth.js +112 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/favorite.d.ts +9 -0
- package/dist/commands/favorite.js +59 -0
- package/dist/commands/favorite.js.map +1 -0
- package/dist/commands/share.d.ts +297 -14
- package/dist/commands/share.js +1351 -71
- package/dist/commands/share.js.map +1 -1
- package/dist/lib/browser-detect.d.ts +28 -0
- package/dist/lib/browser-detect.js +126 -0
- package/dist/lib/browser-detect.js.map +1 -0
- package/dist/lib/config.d.ts +14 -0
- package/dist/lib/config.js +8 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/embedded-html.d.ts +1 -1
- package/dist/lib/embedded-html.js +12 -0
- package/dist/lib/embedded-html.js.map +1 -1
- package/dist/lib/excerpt.d.ts +48 -0
- package/dist/lib/excerpt.js +167 -0
- package/dist/lib/excerpt.js.map +1 -0
- package/dist/lib/favorites.d.ts +73 -0
- package/dist/lib/favorites.js +180 -0
- package/dist/lib/favorites.js.map +1 -0
- package/dist/lib/format.d.ts +2 -0
- package/dist/lib/format.js +14 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/freshness.d.ts +1 -1
- package/dist/lib/freshness.js +1 -1
- package/dist/lib/github.d.ts +14 -0
- package/dist/lib/github.js +36 -0
- package/dist/lib/github.js.map +1 -0
- package/dist/lib/header-actions.d.ts +44 -0
- package/dist/lib/header-actions.js +181 -0
- package/dist/lib/header-actions.js.map +1 -0
- package/dist/lib/identity/client.d.ts +49 -0
- package/dist/lib/identity/client.js +97 -0
- package/dist/lib/identity/client.js.map +1 -0
- package/dist/lib/identity/index.d.ts +60 -0
- package/dist/lib/identity/index.js +69 -0
- package/dist/lib/identity/index.js.map +1 -0
- package/dist/lib/link-previews.d.ts +67 -0
- package/dist/lib/link-previews.js +427 -0
- package/dist/lib/link-previews.js.map +1 -0
- package/dist/lib/links.d.ts +2 -0
- package/dist/lib/links.js +21 -2
- package/dist/lib/links.js.map +1 -1
- package/dist/lib/math.d.ts +31 -0
- package/dist/lib/math.js +122 -0
- package/dist/lib/math.js.map +1 -0
- package/dist/lib/metadata-menu.d.ts +16 -1
- package/dist/lib/metadata-menu.js +24 -0
- package/dist/lib/metadata-menu.js.map +1 -1
- package/dist/lib/open.d.ts +6 -0
- package/dist/lib/open.js +54 -0
- package/dist/lib/open.js.map +1 -0
- package/dist/lib/render.d.ts +3 -0
- package/dist/lib/render.js +164 -54
- package/dist/lib/render.js.map +1 -1
- package/dist/lib/revisions.d.ts +17 -0
- package/dist/lib/revisions.js +6 -1
- package/dist/lib/revisions.js.map +1 -1
- package/dist/lib/share/analytics.d.ts +13 -0
- package/dist/lib/share/analytics.js +46 -0
- package/dist/lib/share/analytics.js.map +1 -0
- package/dist/lib/share/backend.d.ts +105 -0
- package/dist/lib/share/backend.js +161 -0
- package/dist/lib/share/backend.js.map +1 -0
- package/dist/lib/share/capture.d.ts +31 -0
- package/dist/lib/share/capture.js +175 -0
- package/dist/lib/share/capture.js.map +1 -0
- package/dist/lib/share/config.d.ts +82 -0
- package/dist/lib/share/config.js +181 -0
- package/dist/lib/share/config.js.map +1 -0
- package/dist/lib/share/delete.d.ts +122 -0
- package/dist/lib/share/delete.js +174 -0
- package/dist/lib/share/delete.js.map +1 -0
- package/dist/lib/share/html.d.ts +22 -0
- package/dist/lib/share/html.js +89 -0
- package/dist/lib/share/html.js.map +1 -0
- package/dist/lib/share/http-error.d.ts +52 -0
- package/dist/lib/share/http-error.js +66 -0
- package/dist/lib/share/http-error.js.map +1 -0
- package/dist/lib/share/og.d.ts +25 -0
- package/dist/lib/share/og.js +85 -0
- package/dist/lib/share/og.js.map +1 -0
- package/dist/lib/share/provision.d.ts +99 -0
- package/dist/lib/share/provision.js +271 -0
- package/dist/lib/share/provision.js.map +1 -0
- package/dist/lib/share/publish.d.ts +393 -0
- package/dist/lib/share/publish.js +829 -0
- package/dist/lib/share/publish.js.map +1 -0
- package/dist/lib/share/qr.d.ts +36 -0
- package/dist/lib/share/qr.js +102 -0
- package/dist/lib/share/qr.js.map +1 -0
- package/dist/lib/share/storage-policy.d.ts +9 -0
- package/dist/lib/share/storage-policy.js +43 -0
- package/dist/lib/share/storage-policy.js.map +1 -0
- package/dist/lib/share/visibility.d.ts +92 -0
- package/dist/lib/share/visibility.js +108 -0
- package/dist/lib/share/visibility.js.map +1 -0
- package/dist/lib/share/worker-template.d.ts +29 -0
- package/dist/lib/share/worker-template.js +2154 -0
- package/dist/lib/share/worker-template.js.map +1 -0
- package/dist/lib/status-list.d.ts +49 -0
- package/dist/lib/status-list.js +153 -0
- package/dist/lib/status-list.js.map +1 -0
- package/dist/lib/validate.js +122 -8
- package/dist/lib/validate.js.map +1 -1
- package/package.json +12 -3
- package/dist/lib/share-provision.d.ts +0 -31
- package/dist/lib/share-provision.js +0 -83
- package/dist/lib/share-provision.js.map +0 -1
- package/dist/lib/share-worker.d.ts +0 -1
- package/dist/lib/share-worker.js +0 -66
- package/dist/lib/share-worker.js.map +0 -1
- package/dist/lib/share.d.ts +0 -39
- package/dist/lib/share.js +0 -159
- package/dist/lib/share.js.map +0 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
5
|
+
import { escapeHtml } from './html.js';
|
|
6
|
+
export function isExcerptFence(info) {
|
|
7
|
+
return /^excerpt(\s|$)/.test(info.trim());
|
|
8
|
+
}
|
|
9
|
+
function parseRange(value, label) {
|
|
10
|
+
const m = value.match(/^(\d+)(?:-(\d+))?$/);
|
|
11
|
+
if (!m)
|
|
12
|
+
return `${label} must be N or N-M, got "${value}"`;
|
|
13
|
+
const a = Number(m[1]);
|
|
14
|
+
const b = m[2] ? Number(m[2]) : a;
|
|
15
|
+
if (a < 1 || b < a)
|
|
16
|
+
return `${label} must be an ascending 1-based range, got "${value}"`;
|
|
17
|
+
return [a, b];
|
|
18
|
+
}
|
|
19
|
+
/** Parse the info string; returns the spec or a list of problems. */
|
|
20
|
+
export function parseExcerptInfo(info) {
|
|
21
|
+
const errors = [];
|
|
22
|
+
const attrs = {};
|
|
23
|
+
for (const token of info.trim().split(/\s+/).slice(1)) {
|
|
24
|
+
const eq = token.indexOf('=');
|
|
25
|
+
if (eq <= 0) {
|
|
26
|
+
errors.push(`excerpt attribute "${token}" is not key=value`);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
attrs[token.slice(0, eq)] = token.slice(eq + 1).replace(/^"|"$/g, '');
|
|
30
|
+
}
|
|
31
|
+
if (!attrs.path)
|
|
32
|
+
errors.push('excerpt needs path=<file>');
|
|
33
|
+
if (!attrs.lines)
|
|
34
|
+
errors.push('excerpt needs lines=A-B');
|
|
35
|
+
let lines;
|
|
36
|
+
let highlight;
|
|
37
|
+
if (attrs.lines) {
|
|
38
|
+
const r = parseRange(attrs.lines, 'lines');
|
|
39
|
+
if (typeof r === 'string')
|
|
40
|
+
errors.push(`excerpt ${r}`);
|
|
41
|
+
else
|
|
42
|
+
lines = r;
|
|
43
|
+
}
|
|
44
|
+
if (attrs.highlight) {
|
|
45
|
+
const r = parseRange(attrs.highlight, 'highlight');
|
|
46
|
+
if (typeof r === 'string')
|
|
47
|
+
errors.push(`excerpt ${r}`);
|
|
48
|
+
else
|
|
49
|
+
highlight = r;
|
|
50
|
+
}
|
|
51
|
+
if (lines && highlight && (highlight[0] < lines[0] || highlight[1] > lines[1])) {
|
|
52
|
+
errors.push(`excerpt highlight=${attrs.highlight} falls outside lines=${attrs.lines}`);
|
|
53
|
+
}
|
|
54
|
+
if (attrs.repo && !/^[\w.-]+\/[\w.-]+$/.test(attrs.repo))
|
|
55
|
+
errors.push(`excerpt repo must be org/name, got "${attrs.repo}"`);
|
|
56
|
+
if (errors.length || !lines || !attrs.path)
|
|
57
|
+
return { errors };
|
|
58
|
+
return { spec: { path: attrs.path, lines, highlight, repo: attrs.repo, ref: attrs.ref }, errors };
|
|
59
|
+
}
|
|
60
|
+
function findRepoRoot(start) {
|
|
61
|
+
let dir = resolve(start);
|
|
62
|
+
for (;;) {
|
|
63
|
+
if (existsSync(join(dir, '.git')))
|
|
64
|
+
return dir;
|
|
65
|
+
const parent = dirname(dir);
|
|
66
|
+
if (parent === dir)
|
|
67
|
+
return undefined;
|
|
68
|
+
dir = parent;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function git(root, args) {
|
|
72
|
+
try {
|
|
73
|
+
return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim() || undefined;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function githubRepo(root) {
|
|
80
|
+
const url = git(root, ['config', '--get', 'remote.origin.url']);
|
|
81
|
+
const m = url?.match(/github\.com[:/]([\w.-]+\/[\w.-]+?)(?:\.git)?$/);
|
|
82
|
+
return m?.[1];
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Resolve the excerpt's file from the artifact's directory: an absolute path or
|
|
86
|
+
* `~/` as given; otherwise relative to the enclosing git root, then the artifact
|
|
87
|
+
* directory, then the home directory. Returns the lines or a problem string.
|
|
88
|
+
*
|
|
89
|
+
* Confinement: the resolved file (symlinks followed) must be tracked by git in
|
|
90
|
+
* the checkout that contains it, and that checkout must be the artifact's own
|
|
91
|
+
* unless the fence names it with `repo=org/name` matching its origin. That is
|
|
92
|
+
* what makes the card citable at a commit, keeps `~/.ssh/id_rsa`, `.env`, or
|
|
93
|
+
* any untracked file out of a page that may later be published, and makes a
|
|
94
|
+
* citation into a sibling checkout an explicit, reviewable choice.
|
|
95
|
+
*/
|
|
96
|
+
export function resolveExcerpt(baseDir, spec) {
|
|
97
|
+
const expanded = spec.path.startsWith('~/') ? join(homedir(), spec.path.slice(2)) : spec.path;
|
|
98
|
+
const gitRoot = findRepoRoot(baseDir);
|
|
99
|
+
const candidates = isAbsolute(expanded)
|
|
100
|
+
? [expanded]
|
|
101
|
+
: [gitRoot ? join(gitRoot, expanded) : undefined, join(baseDir, expanded), join(homedir(), expanded)].filter((c) => Boolean(c));
|
|
102
|
+
const found = candidates.find((c) => existsSync(c));
|
|
103
|
+
if (!found)
|
|
104
|
+
return `excerpt path "${spec.path}" not found (looked under ${gitRoot ?? baseDir})`;
|
|
105
|
+
const absolutePath = realpathSync(found);
|
|
106
|
+
const fileRoot = findRepoRoot(dirname(absolutePath));
|
|
107
|
+
if (!fileRoot)
|
|
108
|
+
return `excerpt path "${spec.path}" is not inside a git checkout; an excerpt cites a tracked file at a commit`;
|
|
109
|
+
const tracked = git(fileRoot, ['ls-files', '--error-unmatch', '--', absolutePath.slice(fileRoot.length + 1)]);
|
|
110
|
+
if (!tracked)
|
|
111
|
+
return `excerpt path "${spec.path}" is not tracked by git in ${fileRoot}; untracked and ignored files cannot be cited`;
|
|
112
|
+
const ownRoot = gitRoot ? realpathSync(gitRoot) : undefined;
|
|
113
|
+
if (fileRoot !== ownRoot) {
|
|
114
|
+
const origin = githubRepo(fileRoot);
|
|
115
|
+
if (!spec.repo)
|
|
116
|
+
return `excerpt path "${spec.path}" lives in another checkout (${fileRoot}); cite it explicitly with repo=${origin ?? '<org/name>'} matching that checkout's origin`;
|
|
117
|
+
if (!origin || origin.toLowerCase() !== spec.repo.toLowerCase())
|
|
118
|
+
return `excerpt repo=${spec.repo} does not match the origin of the checkout holding "${spec.path}" (${origin ?? 'no GitHub origin'})`;
|
|
119
|
+
}
|
|
120
|
+
const all = readFileSync(absolutePath, 'utf8').split(/\r?\n/);
|
|
121
|
+
if (all.length && all[all.length - 1] === '')
|
|
122
|
+
all.pop();
|
|
123
|
+
const [a, b] = spec.lines;
|
|
124
|
+
if (b > all.length)
|
|
125
|
+
return `excerpt lines=${a}-${b} exceed "${spec.path}" (${all.length} lines)`;
|
|
126
|
+
const root = fileRoot;
|
|
127
|
+
const repo = spec.repo ?? githubRepo(root);
|
|
128
|
+
const ref = spec.ref ?? git(root, ['rev-parse', '--short=12', 'HEAD']);
|
|
129
|
+
const rel = absolutePath.startsWith(root + '/') ? absolutePath.slice(root.length + 1) : undefined;
|
|
130
|
+
const href = repo && ref && rel ? `https://github.com/${repo}/blob/${ref}/${rel}#L${a}${b > a ? `-L${b}` : ''}` : undefined;
|
|
131
|
+
return { spec, root, absolutePath, lines: all.slice(a - 1, b), totalLines: all.length, href };
|
|
132
|
+
}
|
|
133
|
+
/** Render the card. Invalid or unresolvable specs return a visible missing card;
|
|
134
|
+
* validate.ts errors before render ever reaches that branch. */
|
|
135
|
+
export function renderExcerpt(baseDir, info, body) {
|
|
136
|
+
const parsed = parseExcerptInfo(info);
|
|
137
|
+
if (!parsed.spec)
|
|
138
|
+
return missingCard(parsed.errors.join('; '));
|
|
139
|
+
const source = resolveExcerpt(baseDir, parsed.spec);
|
|
140
|
+
if (typeof source === 'string')
|
|
141
|
+
return missingCard(source);
|
|
142
|
+
const { spec, lines, href } = source;
|
|
143
|
+
const [a, b] = spec.lines;
|
|
144
|
+
const width = String(b).length;
|
|
145
|
+
const rows = lines.map((text, i) => {
|
|
146
|
+
const n = a + i;
|
|
147
|
+
const hl = spec.highlight && n >= spec.highlight[0] && n <= spec.highlight[1];
|
|
148
|
+
return `<span class="ex-line${hl ? ' ex-hl' : ''}"><span class="ex-no">${String(n).padStart(width, ' ')}</span><span class="ex-code">${escapeHtml(text) || ' '}</span></span>`;
|
|
149
|
+
});
|
|
150
|
+
const label = escapeHtml(spec.path);
|
|
151
|
+
const range = `lines ${a}${b > a ? `–${b}` : ''}`;
|
|
152
|
+
const file = href ? `<a href="${escapeHtml(href)}">${label}</a>` : `<code>${label}</code>`;
|
|
153
|
+
const note = body.trim() ? `<p class="artifact-excerpt-note">${escapeHtml(body.trim().replace(/^verdict:\s*/i, ''))}</p>` : '';
|
|
154
|
+
return `<figure class="artifact-excerpt"><figcaption class="artifact-excerpt-file">${file}<span class="artifact-excerpt-range">${range}</span></figcaption><pre class="artifact-excerpt-pre">${rows.join('')}</pre>${note}</figure>`;
|
|
155
|
+
}
|
|
156
|
+
function missingCard(problem) {
|
|
157
|
+
return `<figure class="artifact-excerpt artifact-excerpt-missing"><figcaption class="artifact-excerpt-file"><code>excerpt</code><span class="artifact-excerpt-range">${escapeHtml(problem)}</span></figcaption></figure>`;
|
|
158
|
+
}
|
|
159
|
+
/** Problems validate.ts reports for one excerpt fence. */
|
|
160
|
+
export function excerptProblems(baseDir, info) {
|
|
161
|
+
const parsed = parseExcerptInfo(info);
|
|
162
|
+
if (!parsed.spec)
|
|
163
|
+
return parsed.errors;
|
|
164
|
+
const source = resolveExcerpt(baseDir, parsed.spec);
|
|
165
|
+
return typeof source === 'string' ? [source] : [];
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=excerpt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"excerpt.js","sourceRoot":"","sources":["../../src/lib/excerpt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AA4BvC,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO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I,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/H,OAAO,8EAA8E,IAAI,wCAAwC,KAAK,yDAAyD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,IAAI,WAAW,CAAC;AACvO,CAAC;AAED,SAAS,WAAW,CAAC,OAAe;IAClC,OAAO,gKAAgK,UAAU,CAAC,OAAO,CAAC,+BAA+B,CAAC;AAC5N,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,eAAe,CAAC,OAAe,EAAE,IAAY;IAC3D,MAAM,MAAM,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACtC,IAAI,CAAC,MAAM,CAAC,IAAI;QAAE,OAAO,MAAM,CAAC,MAAM,CAAC;IACvC,MAAM,MAAM,GAAG,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACpD,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Favorites and the local artifact listing.
|
|
3
|
+
*
|
|
4
|
+
* The revision store (`revisions.ts`) already knows every rendered artifact:
|
|
5
|
+
* one directory per source under {@link historyRoot}, keyed by the SHA-256 of
|
|
6
|
+
* its canonical path, with a strict-integrity `manifest.json`. Favorites ride
|
|
7
|
+
* BESIDE that store in a single `favorites.json` at the same root rather than
|
|
8
|
+
* inside each manifest — a manifest is rewritten only inside the locked
|
|
9
|
+
* serialize→render→write transaction, and a favorite toggle must never touch
|
|
10
|
+
* (or be blocked by) that pipeline.
|
|
11
|
+
*
|
|
12
|
+
* Same env override as the revision store (`ARTIFACTS_HISTORY_DIR`), so a test
|
|
13
|
+
* or a scratch HOME relocates both together. Reads are lock-free (writes are
|
|
14
|
+
* atomic renames); writes take a lock on the store root so a served page and a
|
|
15
|
+
* CLI toggle never lose each other's update.
|
|
16
|
+
*/
|
|
17
|
+
export interface FavoriteEntry {
|
|
18
|
+
/** Canonical source path, when the favorite was recorded from a path. */
|
|
19
|
+
sourcePath?: string;
|
|
20
|
+
/** Display title, when known at toggle time (the served page sends document.title). */
|
|
21
|
+
title?: string;
|
|
22
|
+
favoritedAt: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function favoritesPath(): string;
|
|
25
|
+
/**
|
|
26
|
+
* Read the favorites file. Absent means "no favorites yet"; present but
|
|
27
|
+
* unparseable or wrong-shaped is fatal — never silently reset a user's marks
|
|
28
|
+
* behind a transient read glitch (same rule as the revision manifest).
|
|
29
|
+
*/
|
|
30
|
+
export declare function readFavorites(file?: string): Record<string, FavoriteEntry>;
|
|
31
|
+
/**
|
|
32
|
+
* Set, clear, or flip the favorite flag for an artifact (a source path or a
|
|
33
|
+
* bare 64-hex id). The current-state read for a flip happens INSIDE the lock
|
|
34
|
+
* on the store root — reading before acquiring it would let two concurrent
|
|
35
|
+
* toggles both compute the same next state and collapse into one.
|
|
36
|
+
* Returns the resulting state.
|
|
37
|
+
*/
|
|
38
|
+
export declare function setFavorite(sourceOrId: string, favorite: boolean | 'toggle', extra?: {
|
|
39
|
+
sourcePath?: string;
|
|
40
|
+
title?: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
id: string;
|
|
43
|
+
favorite: boolean;
|
|
44
|
+
}>;
|
|
45
|
+
/** Flip the favorite flag (atomically, under the store lock); returns the resulting state. */
|
|
46
|
+
export declare function toggleFavorite(sourceOrId: string, extra?: {
|
|
47
|
+
sourcePath?: string;
|
|
48
|
+
title?: string;
|
|
49
|
+
}): Promise<{
|
|
50
|
+
id: string;
|
|
51
|
+
favorite: boolean;
|
|
52
|
+
}>;
|
|
53
|
+
export declare function isFavorite(sourceOrId: string): boolean;
|
|
54
|
+
/** One artifact known to the local store, for `artifacts list`. */
|
|
55
|
+
export interface ArtifactSummary {
|
|
56
|
+
id: string;
|
|
57
|
+
sourcePath: string;
|
|
58
|
+
title: string;
|
|
59
|
+
/** First revision's timestamp; empty when the artifact has no saved revisions. */
|
|
60
|
+
createdAt: string;
|
|
61
|
+
/** Latest revision's timestamp; empty when the artifact has no saved revisions. */
|
|
62
|
+
updatedAt: string;
|
|
63
|
+
revisionCount: number;
|
|
64
|
+
favorite: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Enumerate every artifact the local store knows: the union of the revision
|
|
68
|
+
* manifests and the favorites file (a favorite can outlive — or predate — a
|
|
69
|
+
* manifest). A manifest that fails to parse is SKIPPED rather than fatal: a
|
|
70
|
+
* listing must degrade gracefully, unlike the integrity gate a targeted
|
|
71
|
+
* `history` read applies. Sorted most-recently-updated first.
|
|
72
|
+
*/
|
|
73
|
+
export declare function listArtifacts(): ArtifactSummary[];
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Favorites and the local artifact listing.
|
|
3
|
+
*
|
|
4
|
+
* The revision store (`revisions.ts`) already knows every rendered artifact:
|
|
5
|
+
* one directory per source under {@link historyRoot}, keyed by the SHA-256 of
|
|
6
|
+
* its canonical path, with a strict-integrity `manifest.json`. Favorites ride
|
|
7
|
+
* BESIDE that store in a single `favorites.json` at the same root rather than
|
|
8
|
+
* inside each manifest — a manifest is rewritten only inside the locked
|
|
9
|
+
* serialize→render→write transaction, and a favorite toggle must never touch
|
|
10
|
+
* (or be blocked by) that pipeline.
|
|
11
|
+
*
|
|
12
|
+
* Same env override as the revision store (`ARTIFACTS_HISTORY_DIR`), so a test
|
|
13
|
+
* or a scratch HOME relocates both together. Reads are lock-free (writes are
|
|
14
|
+
* atomic renames); writes take a lock on the store root so a served page and a
|
|
15
|
+
* CLI toggle never lose each other's update.
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync } from 'node:fs';
|
|
18
|
+
import { closeSync, fsyncSync, openSync, writeFileSync } from 'node:fs';
|
|
19
|
+
import { randomBytes } from 'node:crypto';
|
|
20
|
+
import { basename, dirname, join } from 'node:path';
|
|
21
|
+
import { lock } from 'proper-lockfile';
|
|
22
|
+
import { artifactTarget, historyRoot } from './revisions.js';
|
|
23
|
+
const FAVORITES_SCHEMA = 'artifacts.favorites.v1';
|
|
24
|
+
const FAVORITES_FILE = 'favorites.json';
|
|
25
|
+
const FILE_MODE = 0o600;
|
|
26
|
+
const ID_RE = /^[0-9a-f]{64}$/;
|
|
27
|
+
export function favoritesPath() {
|
|
28
|
+
return join(historyRoot(), FAVORITES_FILE);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Read the favorites file. Absent means "no favorites yet"; present but
|
|
32
|
+
* unparseable or wrong-shaped is fatal — never silently reset a user's marks
|
|
33
|
+
* behind a transient read glitch (same rule as the revision manifest).
|
|
34
|
+
*/
|
|
35
|
+
export function readFavorites(file = favoritesPath()) {
|
|
36
|
+
if (!existsSync(file))
|
|
37
|
+
return {};
|
|
38
|
+
let parsed;
|
|
39
|
+
try {
|
|
40
|
+
parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
throw new Error(`Corrupt favorites file at ${file}: ${error.message}`);
|
|
44
|
+
}
|
|
45
|
+
const v = parsed;
|
|
46
|
+
if (!v || v.schema !== FAVORITES_SCHEMA || typeof v.favorites !== 'object' || v.favorites === null) {
|
|
47
|
+
throw new Error(`Corrupt favorites file at ${file}: unexpected shape`);
|
|
48
|
+
}
|
|
49
|
+
return v.favorites;
|
|
50
|
+
}
|
|
51
|
+
function atomicWrite(target, data, mode) {
|
|
52
|
+
const tmp = join(dirname(target), `.${basename(target)}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`);
|
|
53
|
+
const fd = openSync(tmp, 'wx', mode);
|
|
54
|
+
try {
|
|
55
|
+
writeFileSync(fd, data);
|
|
56
|
+
fsyncSync(fd);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
closeSync(fd);
|
|
60
|
+
}
|
|
61
|
+
renameSync(tmp, target);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Set, clear, or flip the favorite flag for an artifact (a source path or a
|
|
65
|
+
* bare 64-hex id). The current-state read for a flip happens INSIDE the lock
|
|
66
|
+
* on the store root — reading before acquiring it would let two concurrent
|
|
67
|
+
* toggles both compute the same next state and collapse into one.
|
|
68
|
+
* Returns the resulting state.
|
|
69
|
+
*/
|
|
70
|
+
export async function setFavorite(sourceOrId, favorite, extra = {}) {
|
|
71
|
+
const { id, sourcePath } = artifactTarget(sourceOrId);
|
|
72
|
+
const root = historyRoot();
|
|
73
|
+
mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
74
|
+
const release = await lock(root, {
|
|
75
|
+
realpath: false,
|
|
76
|
+
// A concurrent toggle (a served page and a CLI invocation racing) waits
|
|
77
|
+
// briefly instead of failing outright.
|
|
78
|
+
retries: { retries: 20, factor: 1.4, minTimeout: 25, maxTimeout: 500 },
|
|
79
|
+
});
|
|
80
|
+
try {
|
|
81
|
+
const file = favoritesPath();
|
|
82
|
+
const favorites = readFavorites(file);
|
|
83
|
+
const next = favorite === 'toggle' ? !favorites[id] : favorite;
|
|
84
|
+
if (next) {
|
|
85
|
+
const prior = favorites[id];
|
|
86
|
+
favorites[id] = {
|
|
87
|
+
sourcePath: extra.sourcePath ?? sourcePath ?? prior?.sourcePath,
|
|
88
|
+
title: extra.title ?? prior?.title,
|
|
89
|
+
favoritedAt: prior?.favoritedAt ?? new Date().toISOString(),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
delete favorites[id];
|
|
94
|
+
}
|
|
95
|
+
atomicWrite(file, JSON.stringify({ schema: FAVORITES_SCHEMA, favorites }, null, 2) + '\n', FILE_MODE);
|
|
96
|
+
return { id, favorite: next };
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
await release();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Flip the favorite flag (atomically, under the store lock); returns the resulting state. */
|
|
103
|
+
export async function toggleFavorite(sourceOrId, extra = {}) {
|
|
104
|
+
return setFavorite(sourceOrId, 'toggle', extra);
|
|
105
|
+
}
|
|
106
|
+
export function isFavorite(sourceOrId) {
|
|
107
|
+
const { id } = artifactTarget(sourceOrId);
|
|
108
|
+
return Boolean(readFavorites()[id]);
|
|
109
|
+
}
|
|
110
|
+
/** Frontmatter `title:` of a Markdown source, when the file still exists. */
|
|
111
|
+
function sourceTitle(sourcePath) {
|
|
112
|
+
try {
|
|
113
|
+
const raw = readFileSync(sourcePath, 'utf8');
|
|
114
|
+
const frontmatter = /^---\r?\n([\s\S]*?)\r?\n---/.exec(raw);
|
|
115
|
+
const line = frontmatter && /^title:\s*(.+)$/m.exec(frontmatter[1]);
|
|
116
|
+
const title = line?.[1]?.trim().replace(/^["']|["']$/g, '').trim();
|
|
117
|
+
if (title)
|
|
118
|
+
return title;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
// Source moved or unreadable — the basename is the fallback below.
|
|
122
|
+
}
|
|
123
|
+
return '';
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Enumerate every artifact the local store knows: the union of the revision
|
|
127
|
+
* manifests and the favorites file (a favorite can outlive — or predate — a
|
|
128
|
+
* manifest). A manifest that fails to parse is SKIPPED rather than fatal: a
|
|
129
|
+
* listing must degrade gracefully, unlike the integrity gate a targeted
|
|
130
|
+
* `history` read applies. Sorted most-recently-updated first.
|
|
131
|
+
*/
|
|
132
|
+
export function listArtifacts() {
|
|
133
|
+
const root = historyRoot();
|
|
134
|
+
const favorites = readFavorites();
|
|
135
|
+
const byId = new Map();
|
|
136
|
+
if (existsSync(root)) {
|
|
137
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
138
|
+
if (!entry.isDirectory() || !ID_RE.test(entry.name))
|
|
139
|
+
continue;
|
|
140
|
+
const manifestFile = join(root, entry.name, 'manifest.json');
|
|
141
|
+
if (!existsSync(manifestFile))
|
|
142
|
+
continue;
|
|
143
|
+
let manifest;
|
|
144
|
+
try {
|
|
145
|
+
manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const revisions = Array.isArray(manifest.revisions) ? manifest.revisions : [];
|
|
151
|
+
const first = revisions[0];
|
|
152
|
+
const last = revisions.at(-1);
|
|
153
|
+
const sourcePath = typeof manifest.sourcePath === 'string' ? manifest.sourcePath : '';
|
|
154
|
+
byId.set(entry.name, {
|
|
155
|
+
id: entry.name,
|
|
156
|
+
sourcePath,
|
|
157
|
+
title: favorites[entry.name]?.title || sourceTitle(sourcePath) || basename(sourcePath),
|
|
158
|
+
createdAt: first?.createdAt ?? '',
|
|
159
|
+
updatedAt: last?.createdAt ?? '',
|
|
160
|
+
revisionCount: revisions.length,
|
|
161
|
+
favorite: Boolean(favorites[entry.name]),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
for (const [id, entry] of Object.entries(favorites)) {
|
|
166
|
+
if (byId.has(id) || !ID_RE.test(id))
|
|
167
|
+
continue;
|
|
168
|
+
byId.set(id, {
|
|
169
|
+
id,
|
|
170
|
+
sourcePath: entry.sourcePath ?? '',
|
|
171
|
+
title: entry.title || (entry.sourcePath ? basename(entry.sourcePath) : ''),
|
|
172
|
+
createdAt: '',
|
|
173
|
+
updatedAt: '',
|
|
174
|
+
revisionCount: 0,
|
|
175
|
+
favorite: true,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return [...byId.values()].sort((a, b) => (b.updatedAt || b.createdAt).localeCompare(a.updatedAt || a.createdAt));
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=favorites.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"favorites.js","sourceRoot":"","sources":["../../src/lib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AAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QACpD,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YAAE,SAAS;QAC9C,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE;YACX,EAAE;YACF,UAAU,EAAE,KAAK,CAAC,UAAU,IAAI,EAAE;YAClC,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC1E,SAAS,EAAE,EAAE;YACb,SAAS,EAAE,EAAE;YACb,aAAa,EAAE,CAAC;YAChB,QAAQ,EAAE,IAAI;SACf,CAAC,CAAC;IACL,CAAC;IACD,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;AACnH,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Human-readable bytes, e.g. `1.2 KB`, `640 B`. */
|
|
2
|
+
export function formatBytes(n) {
|
|
3
|
+
if (n < 1024)
|
|
4
|
+
return `${n} B`;
|
|
5
|
+
const units = ['KB', 'MB', 'GB', 'TB'];
|
|
6
|
+
let size = n / 1024;
|
|
7
|
+
let unit = 0;
|
|
8
|
+
while (size >= 1024 && unit < units.length - 1) {
|
|
9
|
+
size /= 1024;
|
|
10
|
+
unit++;
|
|
11
|
+
}
|
|
12
|
+
return `${size < 10 ? size.toFixed(1) : Math.round(size)} ${units[unit]}`;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../../src/lib/format.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,MAAM,UAAU,WAAW,CAAC,CAAS;IACnC,IAAI,CAAC,GAAG,IAAI;QAAE,OAAO,GAAG,CAAC,IAAI,CAAC;IAC9B,MAAM,KAAK,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACvC,IAAI,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC;IACpB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,OAAO,IAAI,IAAI,IAAI,IAAI,IAAI,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/C,IAAI,IAAI,IAAI,CAAC;QACb,IAAI,EAAE,CAAC;IACT,CAAC;IACD,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;AAC5E,CAAC"}
|
package/dist/lib/freshness.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ export declare function artifactTimestamp(value: unknown): {
|
|
|
3
3
|
iso: string;
|
|
4
4
|
relative: boolean;
|
|
5
5
|
} | undefined;
|
|
6
|
-
export declare const FRESHNESS_SCRIPT = "<script>\n(() => {\n const times = [...document.querySelectorAll('time[data-relative=\"true\"]')];\n const update = () => {\n const now = Date.now();\n for (const time of times) {\n const instant = new Date(time.dateTime);\n const seconds = Math.floor((now - instant.getTime()) / 1000);\n if (!Number.isFinite(seconds)) continue;\n const label = time.dataset.label;\n // Future dates are never called \"just now\".\n let value = seconds < 0 ? instant.toLocaleString() : instant.toISOString().slice(0, 10);\n if (seconds >= 0 && seconds < 60) value = 'just now';\n else if (seconds >= 60 && seconds < 3600) { const n = Math.floor(seconds / 60); value = n + (n === 1 ? '
|
|
6
|
+
export declare const FRESHNESS_SCRIPT = "<script>\n(() => {\n const times = [...document.querySelectorAll('time[data-relative=\"true\"]')];\n const update = () => {\n const now = Date.now();\n for (const time of times) {\n const instant = new Date(time.dateTime);\n const seconds = Math.floor((now - instant.getTime()) / 1000);\n if (!Number.isFinite(seconds)) continue;\n const label = time.dataset.label;\n // Future dates are never called \"just now\".\n let value = seconds < 0 ? instant.toLocaleString() : instant.toISOString().slice(0, 10);\n if (seconds >= 0 && seconds < 60) value = 'just now';\n else if (seconds >= 60 && seconds < 3600) { const n = Math.floor(seconds / 60); value = n + (n === 1 ? ' min ago' : ' mins ago'); }\n else if (seconds >= 3600 && seconds < 86400) { const n = Math.floor(seconds / 3600); value = n + (n === 1 ? ' hour ago' : ' hours ago'); }\n else if (seconds >= 86400 && seconds < 604800) { const n = Math.floor(seconds / 86400); value = n + (n === 1 ? ' day ago' : ' days ago'); }\n time.textContent = label + ' ' + value;\n time.title = label + ' ' + instant.toLocaleString() + ' (' + time.dateTime + ')';\n }\n };\n update();\n if (times.length) setInterval(() => { if (!document.hidden) update(); }, 30000);\n document.addEventListener('visibilitychange', () => { if (!document.hidden) update(); });\n})();\n</script>";
|
package/dist/lib/freshness.js
CHANGED
|
@@ -37,7 +37,7 @@ export const FRESHNESS_SCRIPT = `<script>
|
|
|
37
37
|
// Future dates are never called "just now".
|
|
38
38
|
let value = seconds < 0 ? instant.toLocaleString() : instant.toISOString().slice(0, 10);
|
|
39
39
|
if (seconds >= 0 && seconds < 60) value = 'just now';
|
|
40
|
-
else if (seconds >= 60 && seconds < 3600) { const n = Math.floor(seconds / 60); value = n + (n === 1 ? '
|
|
40
|
+
else if (seconds >= 60 && seconds < 3600) { const n = Math.floor(seconds / 60); value = n + (n === 1 ? ' min ago' : ' mins ago'); }
|
|
41
41
|
else if (seconds >= 3600 && seconds < 86400) { const n = Math.floor(seconds / 3600); value = n + (n === 1 ? ' hour ago' : ' hours ago'); }
|
|
42
42
|
else if (seconds >= 86400 && seconds < 604800) { const n = Math.floor(seconds / 86400); value = n + (n === 1 ? ' day ago' : ' days ago'); }
|
|
43
43
|
time.textContent = label + ' ' + value;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The publisher's GitHub username — the URL namespace a BYO (bring-your-own
|
|
3
|
+
* Cloudflare) share publishes under. The managed endpoint namespaces on the
|
|
4
|
+
* Phoenix handle instead and never calls this.
|
|
5
|
+
*/
|
|
6
|
+
/** Explicit override, so a machine with no `gh` and no git config can still publish. */
|
|
7
|
+
export declare const GITHUB_USER_ENV = "ARTIFACTS_GITHUB_USER";
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the GitHub username: the explicit env override, then the signed-in
|
|
10
|
+
* `gh` account, then `git config --global github.user`. `null` when none of the
|
|
11
|
+
* three answer — the caller turns that into an actionable error rather than
|
|
12
|
+
* inventing a namespace.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveGitHubUsername(): Promise<string | null>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The publisher's GitHub username — the URL namespace a BYO (bring-your-own
|
|
3
|
+
* Cloudflare) share publishes under. The managed endpoint namespaces on the
|
|
4
|
+
* Phoenix handle instead and never calls this.
|
|
5
|
+
*/
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
const execFileAsync = promisify(execFile);
|
|
9
|
+
/** Explicit override, so a machine with no `gh` and no git config can still publish. */
|
|
10
|
+
export const GITHUB_USER_ENV = 'ARTIFACTS_GITHUB_USER';
|
|
11
|
+
async function tryCommand(file, args) {
|
|
12
|
+
try {
|
|
13
|
+
const { stdout } = await execFileAsync(file, args, { encoding: 'utf8', timeout: 10_000 });
|
|
14
|
+
const value = stdout.trim();
|
|
15
|
+
return value || null;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the GitHub username: the explicit env override, then the signed-in
|
|
23
|
+
* `gh` account, then `git config --global github.user`. `null` when none of the
|
|
24
|
+
* three answer — the caller turns that into an actionable error rather than
|
|
25
|
+
* inventing a namespace.
|
|
26
|
+
*/
|
|
27
|
+
export async function resolveGitHubUsername() {
|
|
28
|
+
const fromEnv = process.env[GITHUB_USER_ENV]?.trim();
|
|
29
|
+
if (fromEnv)
|
|
30
|
+
return fromEnv;
|
|
31
|
+
const fromGh = await tryCommand('gh', ['api', 'user', '-q', '.login']);
|
|
32
|
+
if (fromGh)
|
|
33
|
+
return fromGh;
|
|
34
|
+
return tryCommand('git', ['config', '--global', 'github.user']);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=github.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"github.js","sourceRoot":"","sources":["../../src/lib/github.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAE1C,wFAAwF;AACxF,MAAM,CAAC,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAEvD,KAAK,UAAU,UAAU,CAAC,IAAY,EAAE,IAAc;IACpD,IAAI,CAAC;QACH,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QAC1F,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;QAC5B,OAAO,KAAK,IAAI,IAAI,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB;IACzC,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,IAAI,EAAE,CAAC;IACrD,IAAI,OAAO;QAAE,OAAO,OAAO,CAAC;IAC5B,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;IACvE,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAC1B,OAAO,UAAU,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document-header action buttons: Share and Favorite.
|
|
3
|
+
*
|
|
4
|
+
* One markup builder, one style block, one script. The renderer emits the
|
|
5
|
+
* slot between {@link HEADER_ACTIONS_START}/{@link HEADER_ACTIONS_END} markers
|
|
6
|
+
* with the buttons in their DEFAULT (unfavorited) state, so the render
|
|
7
|
+
* fingerprint never flips on a favorite toggle; the decorate step (which runs
|
|
8
|
+
* after fingerprinting) rewrites the slot with the favorite state actually
|
|
9
|
+
* recorded in the store.
|
|
10
|
+
*
|
|
11
|
+
* Click behavior is decided at runtime by where the page is being viewed:
|
|
12
|
+
* - served by `artifacts render --open` (loopback http): POST back to the
|
|
13
|
+
* local server, which performs the owner-only share / favorite toggle for
|
|
14
|
+
* real and reports the result;
|
|
15
|
+
* - a plain file:// page: the copy-command idiom — the button copies the
|
|
16
|
+
* exact CLI command (`artifacts share <id> --visibility me`,
|
|
17
|
+
* `artifacts favorite <id>`) to run in a terminal;
|
|
18
|
+
* - a hosted page (the managed share host, or any non-loopback origin): the
|
|
19
|
+
* artifact is already published, so Share degrades to copying the page's
|
|
20
|
+
* own URL rather than offering to re-share; Favorite still copies the
|
|
21
|
+
* command (only meaningful on the author's machine, harmless elsewhere).
|
|
22
|
+
*
|
|
23
|
+
* The copied commands address the artifact by its store ID, never by path —
|
|
24
|
+
* reader-facing HTML must not carry local absolute paths (the revision store
|
|
25
|
+
* has a standing test for that invariant), and `artifacts share` accepts an id
|
|
26
|
+
* exactly like `artifacts history` does.
|
|
27
|
+
*/
|
|
28
|
+
export declare const HEADER_ACTIONS_START = "<!--artifact-actions-start-->";
|
|
29
|
+
export declare const HEADER_ACTIONS_END = "<!--artifact-actions-end-->";
|
|
30
|
+
export interface HeaderActions {
|
|
31
|
+
/** The artifact's store identity (SHA-256 of its canonical source path). */
|
|
32
|
+
artifactId: string;
|
|
33
|
+
/** Render-time favorite state; the decorate step replaces it with the store's. */
|
|
34
|
+
favorited: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare function shareCommand(actions: HeaderActions): string;
|
|
37
|
+
export declare function favoriteCommand(actions: HeaderActions): string;
|
|
38
|
+
export declare function headerActions(actions: HeaderActions): string;
|
|
39
|
+
/** The slot the renderer emits; `decorateHeaderActions` rewrites its content. */
|
|
40
|
+
export declare function headerActionsSlot(actions: HeaderActions): string;
|
|
41
|
+
/** Replace a rendered document's action slot with fresh state. No markers → unchanged. */
|
|
42
|
+
export declare function decorateHeaderActions(html: string, actions: HeaderActions): string;
|
|
43
|
+
export declare const HEADER_ACTIONS_CSS = "\n.header-actions{display:inline-flex;align-items:center;gap:6px;flex:0 0 auto}\n.header-action{display:inline-flex;align-items:center;gap:6px;height:30px;padding:0 12px;border:1px solid var(--line);border-radius:999px;background:var(--surface);color:var(--muted);font:500 12px/1 var(--font-mono);cursor:pointer}\n.header-action:hover{color:var(--accent);border-color:var(--accent)}\n.header-action:focus-visible{outline:2px solid var(--accent);outline-offset:2px}\n.header-action:disabled{opacity:.6;cursor:default}\n.header-action svg{width:13px;height:13px;flex:0 0 auto}\n.favorite-action{padding:0 10px;border-color:color-mix(in srgb,var(--accent) 45%,var(--line));color:var(--accent)}\n.favorite-action.is-favorite{background:color-mix(in srgb,var(--accent) 16%,var(--surface));border-color:var(--accent)}\n.favorite-action.is-favorite svg{fill:currentColor}\n.header-action-feedback{color:var(--muted);font:11.5px/1.4 var(--font-mono);max-width:300px;overflow-wrap:anywhere}\n.header-action-feedback:empty{display:none}\n@media print{.header-actions{display:none}}\n";
|
|
44
|
+
export declare const HEADER_ACTIONS_SCRIPT = "<script>\n(() => {\n const root = document.querySelector('[data-header-actions]');\n if (!root) return;\n const shareButton = root.querySelector('.share-action');\n const favoriteButton = root.querySelector('.favorite-action');\n const status = root.querySelector('.header-action-feedback');\n let timer;\n const say = (message) => {\n status.textContent = message;\n clearTimeout(timer);\n timer = setTimeout(() => { if (status.textContent === message) status.textContent = ''; }, 8000);\n };\n const copy = async (text) => {\n try {\n if (!navigator.clipboard || !navigator.clipboard.writeText) throw new Error('unavailable');\n await navigator.clipboard.writeText(text);\n return true;\n } catch {\n const area = document.createElement('textarea');\n area.value = text;\n area.style.position = 'fixed'; area.style.opacity = '0';\n document.body.appendChild(area);\n area.focus(); area.select();\n let ok = false;\n try { ok = document.execCommand('copy'); } catch {}\n area.remove();\n return ok;\n }\n };\n // Where is this page being viewed? The loopback dev server (artifacts render\n // --open) answers POSTs for real actions; any other http(s) origin is an\n // already-published page; file:// has no server at all.\n const hostname = location.hostname;\n const web = location.protocol === 'http:' || location.protocol === 'https:';\n const loopback = web && (hostname === '127.0.0.1' || hostname === 'localhost' || hostname === '[::1]' || hostname === '::1');\n const hosted = web && !loopback;\n const copyCommand = async (command, what) => {\n const ok = await copy(command);\n say(ok ? what + ' command copied \u2014 run it in your terminal' : 'Could not copy. ' + what + ' command: ' + command);\n };\n const paintFavorite = (on) => {\n favoriteButton.classList.toggle('is-favorite', on);\n favoriteButton.setAttribute('aria-pressed', String(on));\n const label = on ? 'Remove from favorites' : 'Add to favorites';\n favoriteButton.setAttribute('aria-label', label);\n favoriteButton.setAttribute('title', on ? 'Remove from favorites' : 'Favorite this artifact');\n };\n if (hosted) {\n // Already published: re-sharing makes no sense here, so Share becomes a\n // plain copy-link of this page's URL.\n shareButton.querySelector('.share-action-label').textContent = 'Copy link';\n shareButton.setAttribute('aria-label', 'Copy link to this page');\n shareButton.setAttribute('title', 'Copy the link to this page');\n shareButton.addEventListener('click', async () => {\n const ok = await copy(location.href);\n say(ok ? 'Link copied' : 'Could not copy the link \u2014 copy it from the address bar');\n });\n } else if (loopback) {\n shareButton.addEventListener('click', async () => {\n shareButton.disabled = true;\n say('Sharing owner-only\u2026');\n try {\n const response = await fetch('/artifacts/share', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' });\n if (!response.ok) throw new Error(await response.text());\n const result = await response.json();\n const copied = await copy(result.url);\n say(copied ? 'Shared owner-only \u2014 link copied' : 'Shared owner-only: ' + result.url);\n } catch (error) {\n await copyCommand(shareButton.dataset.shareCommand, 'Share');\n } finally {\n shareButton.disabled = false;\n }\n });\n } else {\n shareButton.addEventListener('click', () => copyCommand(shareButton.dataset.shareCommand, 'Share'));\n }\n if (loopback) {\n favoriteButton.addEventListener('click', async () => {\n favoriteButton.disabled = true;\n try {\n const response = await fetch('/artifacts/favorite', {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ title: document.title }),\n });\n if (!response.ok) throw new Error(await response.text());\n const result = await response.json();\n paintFavorite(Boolean(result.favorite));\n say(result.favorite ? 'Favorited' : 'Removed from favorites');\n } catch {\n await copyCommand(favoriteButton.dataset.favoriteCommand, 'Favorite');\n } finally {\n favoriteButton.disabled = false;\n }\n });\n } else {\n favoriteButton.addEventListener('click', () => copyCommand(favoriteButton.dataset.favoriteCommand, 'Favorite'));\n }\n})();\n</script>";
|