@cardanvil/frame-kit 0.3.0 → 0.4.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/README.md +19 -0
- package/dist/cli/atomic.d.ts +2 -1
- package/dist/cli/atomic.js +49 -4
- package/dist/cli/build.d.ts +2 -0
- package/dist/cli/build.js +20 -3
- package/dist/cli/main.js +11 -2
- package/dist/cli/watch.js +3 -0
- package/dist/schema/frameMeta.d.ts +1 -0
- package/dist/schema/frameMeta.js +12 -0
- package/package.json +1 -1
- package/schema/frame-meta.schema.json +4 -0
package/README.md
CHANGED
|
@@ -74,6 +74,25 @@ reports everything; under GitHub Actions they become inline annotations.
|
|
|
74
74
|
|
|
75
75
|
Exit codes: `0` clean, `1` one or more frames failed, `2` used wrongly.
|
|
76
76
|
|
|
77
|
+
### Frames you do not want to ship
|
|
78
|
+
|
|
79
|
+
A descriptor can opt its frame out of every build:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"id": "com.example.example",
|
|
84
|
+
"private": true,
|
|
85
|
+
"author": { "name": "Your Name" },
|
|
86
|
+
"license": "CC-BY-4.0"
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`validate` still loads and checks it; `build` reports it as skipped and packs
|
|
91
|
+
nothing, so it never reaches a bundle, the index, or a release. That is the
|
|
92
|
+
combination a worked example needs — a reference frame stays in the repository
|
|
93
|
+
and stays correct, without an `id` that is permanent identity ending up in
|
|
94
|
+
somebody's release by accident.
|
|
95
|
+
|
|
77
96
|
### `--watch`
|
|
78
97
|
|
|
79
98
|
`build --watch` builds once, then rebuilds only the frames whose files changed,
|
package/dist/cli/atomic.d.ts
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* written — Card Anvil re-reads a bundle the moment it changes. Writing in
|
|
6
6
|
* place would hand it a truncated zip whenever the timing was unlucky, so
|
|
7
7
|
* every output is written to a temp file and renamed into position, which is
|
|
8
|
-
* atomic on every platform this runs on
|
|
8
|
+
* atomic on every platform this runs on — see {@link renameWhenFree} for the
|
|
9
|
+
* one thing Windows asks for in return.
|
|
9
10
|
*/
|
|
10
11
|
export declare function writeFileAtomic(file: string, data: string | Uint8Array): Promise<void>;
|
|
11
12
|
/** Streams to `file` atomically. Returns the number of bytes written. */
|
package/dist/cli/atomic.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createWriteStream } from "node:fs";
|
|
|
2
2
|
import { copyFile, rename, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { pipeline } from "node:stream/promises";
|
|
5
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
6
|
/**
|
|
6
7
|
* A temp name beside the destination.
|
|
7
8
|
*
|
|
@@ -15,6 +16,49 @@ function tempFor(file) {
|
|
|
15
16
|
async function discard(temp) {
|
|
16
17
|
await rm(temp, { force: true });
|
|
17
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* What Windows reports when something else has the destination open.
|
|
21
|
+
*
|
|
22
|
+
* `EBUSY` and `EACCES` for the same situation on network shares and some
|
|
23
|
+
* antivirus filters.
|
|
24
|
+
*/
|
|
25
|
+
const HELD_OPEN = new Set(["EPERM", "EACCES", "EBUSY"]);
|
|
26
|
+
/** Backoff between rename attempts. */
|
|
27
|
+
const RETRY_DELAYS_MS = [10, 20, 40, 80, 160, 320, 640, 1000];
|
|
28
|
+
const TOTAL_WAIT_MS = RETRY_DELAYS_MS.reduce((a, b) => a + b, 0);
|
|
29
|
+
/**
|
|
30
|
+
* Renames, waiting out anything that has the destination open.
|
|
31
|
+
*
|
|
32
|
+
* POSIX lets a file be replaced while it is being read. Windows does not: a
|
|
33
|
+
* rename onto a path some process holds open fails outright. A linked folder
|
|
34
|
+
* exists precisely to be read — Card Anvil re-reads a bundle whenever it
|
|
35
|
+
* changes — so a rebuild that lands mid-read hits this, and so do antivirus
|
|
36
|
+
* and search indexers, which open files briefly on their own schedule.
|
|
37
|
+
*
|
|
38
|
+
* Waiting is the whole fix: whatever holds the file is finishing a read that
|
|
39
|
+
* takes milliseconds. A real permission problem still fails, two seconds later.
|
|
40
|
+
*/
|
|
41
|
+
async function renameWhenFree(temp, file) {
|
|
42
|
+
for (let attempt = 0;; attempt++) {
|
|
43
|
+
try {
|
|
44
|
+
await rename(temp, file);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
catch (cause) {
|
|
48
|
+
const code = cause.code ?? "";
|
|
49
|
+
if (!HELD_OPEN.has(code)) {
|
|
50
|
+
throw cause;
|
|
51
|
+
}
|
|
52
|
+
const wait = RETRY_DELAYS_MS[attempt];
|
|
53
|
+
if (wait === undefined) {
|
|
54
|
+
// `EPERM: operation not permitted, rename` says nothing about what to
|
|
55
|
+
// do. Whatever is holding the file is the thing to go and close.
|
|
56
|
+
throw new Error(`could not replace ${path.basename(file)}: something still has it open after ${String(TOTAL_WAIT_MS)}ms (${code}). On Windows a file cannot be replaced while another process holds it — close whatever is reading it, or point the build somewhere else.`, { cause });
|
|
57
|
+
}
|
|
58
|
+
await delay(wait);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
18
62
|
/**
|
|
19
63
|
* Writes `file` so that a reader never sees a partial one.
|
|
20
64
|
*
|
|
@@ -22,13 +66,14 @@ async function discard(temp) {
|
|
|
22
66
|
* written — Card Anvil re-reads a bundle the moment it changes. Writing in
|
|
23
67
|
* place would hand it a truncated zip whenever the timing was unlucky, so
|
|
24
68
|
* every output is written to a temp file and renamed into position, which is
|
|
25
|
-
* atomic on every platform this runs on
|
|
69
|
+
* atomic on every platform this runs on — see {@link renameWhenFree} for the
|
|
70
|
+
* one thing Windows asks for in return.
|
|
26
71
|
*/
|
|
27
72
|
export async function writeFileAtomic(file, data) {
|
|
28
73
|
const temp = tempFor(file);
|
|
29
74
|
try {
|
|
30
75
|
await writeFile(temp, data);
|
|
31
|
-
await
|
|
76
|
+
await renameWhenFree(temp, file);
|
|
32
77
|
}
|
|
33
78
|
catch (cause) {
|
|
34
79
|
await discard(temp);
|
|
@@ -47,7 +92,7 @@ stream) {
|
|
|
47
92
|
});
|
|
48
93
|
try {
|
|
49
94
|
await pipeline(stream, createWriteStream(temp));
|
|
50
|
-
await
|
|
95
|
+
await renameWhenFree(temp, file);
|
|
51
96
|
return bytes;
|
|
52
97
|
}
|
|
53
98
|
catch (cause) {
|
|
@@ -60,7 +105,7 @@ export async function copyFileAtomic(source, dest) {
|
|
|
60
105
|
const temp = tempFor(dest);
|
|
61
106
|
try {
|
|
62
107
|
await copyFile(source, temp);
|
|
63
|
-
await
|
|
108
|
+
await renameWhenFree(temp, dest);
|
|
64
109
|
}
|
|
65
110
|
catch (cause) {
|
|
66
111
|
await discard(temp);
|
package/dist/cli/build.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export interface BuiltFrame {
|
|
|
25
25
|
}
|
|
26
26
|
export interface BuildResult {
|
|
27
27
|
frames: BuiltFrame[];
|
|
28
|
+
/** Frames marked `private` in their descriptor. Checked, then left alone. */
|
|
29
|
+
skipped: LoadedFrame[];
|
|
28
30
|
/** Written to `<out>/frame-index.json` unless nothing built. */
|
|
29
31
|
index?: FrameIndex;
|
|
30
32
|
}
|
package/dist/cli/build.js
CHANGED
|
@@ -5,6 +5,7 @@ import { DEFAULT_CANVAS_HEIGHT, DEFAULT_CANVAS_WIDTH, } from "../schema/frame.js
|
|
|
5
5
|
import { loadFrames } from "./api.js";
|
|
6
6
|
import { copyFileAtomic, writeFileAtomic } from "./atomic.js";
|
|
7
7
|
import { describeBytes, writeBundle } from "./bundle.js";
|
|
8
|
+
import { relativePosix } from "./discover.js";
|
|
8
9
|
import { PackagingError } from "./errors.js";
|
|
9
10
|
import { FRAME_INDEX_FILENAME, buildIndex, downloadBase, } from "./frameIndex.js";
|
|
10
11
|
import { hashFile, mapWithConcurrency } from "./hash.js";
|
|
@@ -29,17 +30,33 @@ existingServer) {
|
|
|
29
30
|
`optionally with a pre-release suffix.`);
|
|
30
31
|
}
|
|
31
32
|
const loaded = await loadFrames(options, reporter, existingServer);
|
|
33
|
+
// Validated above like any other frame, then dropped: `private` says this
|
|
34
|
+
// one exists to be read, not shipped.
|
|
35
|
+
const skipped = loaded.filter((frame) => frame.discovered.meta.private);
|
|
36
|
+
const shippable = loaded.filter((frame) => !frame.discovered.meta.private);
|
|
37
|
+
for (const frame of skipped) {
|
|
38
|
+
// Silence would look like a broken build to whoever asked for this frame
|
|
39
|
+
// by name. Frames merely swept up by a whole-repository build say nothing.
|
|
40
|
+
if (options.only?.includes(frame.discovered.slug)) {
|
|
41
|
+
reporter.warn({
|
|
42
|
+
frame: frame.discovered.slug,
|
|
43
|
+
file: relativePosix(path.resolve(options.root), frame.discovered.metaFile),
|
|
44
|
+
message: `is private, so it was not built. Remove "private" from its ` +
|
|
45
|
+
`frame.meta.json to ship it.`,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
32
49
|
const outDir = path.resolve(options.outDir);
|
|
33
50
|
await mkdir(outDir, { recursive: true });
|
|
34
51
|
const built = [];
|
|
35
|
-
for (const frame of
|
|
52
|
+
for (const frame of shippable) {
|
|
36
53
|
const result = await buildOne(frame, outDir, options, reporter);
|
|
37
54
|
if (result) {
|
|
38
55
|
built.push(result);
|
|
39
56
|
}
|
|
40
57
|
}
|
|
41
58
|
if (built.length === 0) {
|
|
42
|
-
return { frames: built };
|
|
59
|
+
return { frames: built, skipped };
|
|
43
60
|
}
|
|
44
61
|
const index = buildIndex({
|
|
45
62
|
version: options.version,
|
|
@@ -50,7 +67,7 @@ existingServer) {
|
|
|
50
67
|
frames: built.map((frame) => frame.entry),
|
|
51
68
|
});
|
|
52
69
|
await writeFileAtomic(path.join(outDir, FRAME_INDEX_FILENAME), stableStringify(index));
|
|
53
|
-
return { frames: built, index };
|
|
70
|
+
return { frames: built, skipped, index };
|
|
54
71
|
}
|
|
55
72
|
async function buildOne(loaded, outDir, options, reporter) {
|
|
56
73
|
const slug = loaded.discovered.slug;
|
package/dist/cli/main.js
CHANGED
|
@@ -35,7 +35,8 @@ build only:
|
|
|
35
35
|
in the index
|
|
36
36
|
--watch rebuild a frame whenever its source changes
|
|
37
37
|
|
|
38
|
-
A frame is any directory containing a frame.meta.json.
|
|
38
|
+
A frame is any directory containing a frame.meta.json. One with "private": true
|
|
39
|
+
is checked by validate but never built.
|
|
39
40
|
`;
|
|
40
41
|
/** 0 clean · 1 one or more frames failed · 2 the command was used wrongly. */
|
|
41
42
|
const EXIT = { ok: 0, failed: 1, usage: 2 };
|
|
@@ -119,7 +120,7 @@ async function runBuild(root, only, values) {
|
|
|
119
120
|
return EXIT.ok;
|
|
120
121
|
}
|
|
121
122
|
const reporter = new Reporter();
|
|
122
|
-
const { frames: built } = await buildFrames({
|
|
123
|
+
const { frames: built, skipped } = await buildFrames({
|
|
123
124
|
root,
|
|
124
125
|
only,
|
|
125
126
|
outDir: values.out,
|
|
@@ -140,6 +141,11 @@ async function runBuild(root, only, values) {
|
|
|
140
141
|
bundle: frame.bundleName,
|
|
141
142
|
bytes: frame.bytes,
|
|
142
143
|
})),
|
|
144
|
+
skipped: skipped.map((frame) => ({
|
|
145
|
+
id: frame.discovered.meta.id,
|
|
146
|
+
slug: frame.discovered.slug,
|
|
147
|
+
reason: "private",
|
|
148
|
+
})),
|
|
143
149
|
problems: reporter.problems,
|
|
144
150
|
}, null, 2));
|
|
145
151
|
}
|
|
@@ -148,6 +154,9 @@ async function runBuild(root, only, values) {
|
|
|
148
154
|
for (const frame of built) {
|
|
149
155
|
console.log(`${frame.bundleName} ${describeBytes(frame.bytes)}`);
|
|
150
156
|
}
|
|
157
|
+
for (const frame of skipped) {
|
|
158
|
+
console.log(`${frame.discovered.slug} skipped (private)`);
|
|
159
|
+
}
|
|
151
160
|
summarise(reporter, built.length);
|
|
152
161
|
}
|
|
153
162
|
return reporter.ok ? EXIT.ok : EXIT.failed;
|
package/dist/cli/watch.js
CHANGED
|
@@ -156,6 +156,9 @@ export async function slugsFor(root, changed, only) {
|
|
|
156
156
|
if (!owner) {
|
|
157
157
|
return undefined; // outside every frame — something shared changed
|
|
158
158
|
}
|
|
159
|
+
if (owner.meta.private) {
|
|
160
|
+
continue; // a build would skip it anyway; do not announce a rebuild
|
|
161
|
+
}
|
|
159
162
|
if (wanted.size === 0 || wanted.has(owner.slug)) {
|
|
160
163
|
slugs.add(owner.slug);
|
|
161
164
|
}
|
|
@@ -14,6 +14,7 @@ export declare const FrameMetaSchema: z.ZodObject<{
|
|
|
14
14
|
slug: z.ZodOptional<z.ZodString>;
|
|
15
15
|
entry: z.ZodDefault<z.ZodString>;
|
|
16
16
|
export: z.ZodDefault<z.ZodString>;
|
|
17
|
+
private: z.ZodDefault<z.ZodBoolean>;
|
|
17
18
|
author: z.ZodObject<{
|
|
18
19
|
name: z.ZodString;
|
|
19
20
|
url: z.ZodOptional<z.ZodURL>;
|
package/dist/schema/frameMeta.js
CHANGED
|
@@ -33,6 +33,18 @@ export const FrameMetaSchema = z.object({
|
|
|
33
33
|
entry: z.string().default("./index.ts"),
|
|
34
34
|
/** Named export to read from `entry`. */
|
|
35
35
|
export: z.string().default("default"),
|
|
36
|
+
/**
|
|
37
|
+
* Keeps the frame out of every build: it is never packaged, never indexed
|
|
38
|
+
* and never released. `validate` still loads and checks it, so a frame
|
|
39
|
+
* nobody ships cannot quietly rot.
|
|
40
|
+
*
|
|
41
|
+
* The author template's `example` frame is the case this exists for. It has
|
|
42
|
+
* to stay in the repository because `new-frame` copies it, but an id is
|
|
43
|
+
* permanent identity — publishing it would put the same
|
|
44
|
+
* `com.example.example` in every release by every author who forgot to
|
|
45
|
+
* delete it.
|
|
46
|
+
*/
|
|
47
|
+
private: z.boolean().default(false),
|
|
36
48
|
author: z.object({
|
|
37
49
|
name: z.string().min(1),
|
|
38
50
|
url: z.url().optional(),
|
package/package.json
CHANGED