@fabrika/cli 0.3.3 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/cli",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,12 +30,29 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "zod": "^3.25.76",
33
- "@fabrika/app": "^0.3.3",
34
- "@fabrika/auth-core": "^0.3.3",
35
- "@fabrika/builder": "^0.3.3",
36
- "@fabrika/control-contract": "^0.3.3",
37
- "@fabrika/provider-cloudflare": "^0.3.3",
38
- "@fabrika/provider-contract": "^0.3.3",
39
- "@fabrika/provider-zerops": "^0.3.3"
33
+ "@fabrika/app": "^0.4.0",
34
+ "@fabrika/auth-core": "^0.4.0",
35
+ "@fabrika/control-contract": "^0.4.0",
36
+ "@fabrika/provider-contract": "^0.4.0",
37
+ "@fabrika/db": "^0.4.0",
38
+ "@fabrika/platform": "^0.4.0",
39
+ "@fabrika/platform-node": "^0.4.0",
40
+ "@fabrika/check": "^0.4.0"
41
+ },
42
+ "peerDependencies": {
43
+ "@fabrika/builder": "^0.4.0",
44
+ "@fabrika/provider-cloudflare": "^0.4.0",
45
+ "@fabrika/provider-zerops": "^0.4.0"
46
+ },
47
+ "peerDependenciesMeta": {
48
+ "@fabrika/builder": {
49
+ "optional": true
50
+ },
51
+ "@fabrika/provider-cloudflare": {
52
+ "optional": true
53
+ },
54
+ "@fabrika/provider-zerops": {
55
+ "optional": true
56
+ }
40
57
  }
41
58
  }
@@ -0,0 +1,119 @@
1
+ // The seed module of `fabrika app dev`: its default export runs once against the kit before the first request, and
2
+ // what it answers is parsed here, at the boundary, into the personas, link pages and scope values the picker offers.
3
+
4
+ import type { AnyContract } from '@fabrika/app/contract'
5
+ import type { DevKit, PersonTestCaller } from '@fabrika/app/testing'
6
+ import { existsSync } from 'node:fs'
7
+ import { isAbsolute, join } from 'node:path'
8
+ import { pathToFileURL } from 'node:url'
9
+ import { z } from 'zod/v4'
10
+
11
+ /** The seed `app dev` runs when `--seed` names none and the file exists. */
12
+ export const DEFAULT_SEED = 'dev/seed.ts'
13
+
14
+ const MAX_ENTRIES = 50
15
+ const MAX_NAME = 80
16
+ const MAX_DESCRIPTION = 240
17
+ const MAX_SCOPE_VALUE = 200
18
+
19
+ export interface SeedPersona {
20
+ readonly name: string
21
+ readonly description: string | null
22
+ readonly caller: PersonTestCaller
23
+ }
24
+
25
+ export interface SeedLink {
26
+ readonly title: string
27
+ readonly description: string | null
28
+ readonly url: string
29
+ }
30
+
31
+ /** What the seed answered, validated: unique persona names, the kit's own people, local link paths, known dimensions. */
32
+ export interface Seeded {
33
+ readonly personas: readonly SeedPersona[]
34
+ readonly links: readonly SeedLink[]
35
+ /** The labelled values of each dimension, by dimension. */
36
+ readonly scopes: ReadonlyMap<string, readonly { readonly value: string; readonly label: string }[]>
37
+ }
38
+
39
+ export const NOTHING_SEEDED: Seeded = { personas: [], links: [], scopes: new Map() }
40
+
41
+ /** A refusal of what the seed answered; it names an entry by its position, never by what the seed wrote. */
42
+ export class SeedProblem extends Error {
43
+ override readonly name = 'SeedProblem'
44
+ }
45
+
46
+ const Text = (max: number) => z.string().trim().min(1).max(max)
47
+
48
+ const SeedResult = z.strictObject({
49
+ personas: z.array(z.strictObject({ name: Text(MAX_NAME), description: Text(MAX_DESCRIPTION).optional(), caller: z.unknown() })).max(MAX_ENTRIES)
50
+ .optional(),
51
+ links: z.array(z.strictObject({ title: Text(MAX_NAME), description: Text(MAX_DESCRIPTION).optional(), url: z.string().max(4096) })).max(MAX_ENTRIES)
52
+ .optional(),
53
+ scopes: z.record(z.string(), z.array(z.strictObject({ value: Text(MAX_SCOPE_VALUE), label: Text(MAX_NAME) })).max(MAX_ENTRIES)).optional(),
54
+ }).optional()
55
+
56
+ /** A path of this origin; a link the application issued is one (`/s/#px_…`). */
57
+ export function isLocalPath(path: string): boolean {
58
+ return path.startsWith('/') && !path.startsWith('//') && !path.startsWith('/\\')
59
+ }
60
+
61
+ /** The seed module's path: `--seed`, relative to the root, or `dev/seed.ts` when it exists; `null` for none. */
62
+ export function seedPath(root: string, given: string | null): string | null {
63
+ if (given !== null) {
64
+ const path = isAbsolute(given) ? given : join(root, given)
65
+ if (!existsSync(path)) throw new SeedProblem(`app dev: --seed names no file: ${given}`)
66
+ return path
67
+ }
68
+ const conventional = join(root, DEFAULT_SEED)
69
+ return existsSync(conventional) ? conventional : null
70
+ }
71
+
72
+ /** The seed's default export, a function; its import runs application code, so the caller withholds what it throws. */
73
+ export async function importSeed(path: string): Promise<(dev: unknown) => unknown> {
74
+ const module: unknown = await import(pathToFileURL(path).href)
75
+ const seed: unknown = typeof module === 'object' && module !== null ? Reflect.get(module, 'default') : undefined
76
+ if (typeof seed !== 'function') {
77
+ throw new SeedProblem('app dev: the seed module default-exports no function: export default (async (dev) => { … }) satisfies DevSeed<…>')
78
+ }
79
+ return (dev) => Reflect.apply(seed, undefined, [dev])
80
+ }
81
+
82
+ /** What `answer`, the seed's result, offers the picker, or a `SeedProblem` naming the entry that is wrong. */
83
+ export function parseSeeded<C extends AnyContract>(dev: DevKit<C>, answer: unknown): Seeded {
84
+ const parsed = SeedResult.safeParse(answer)
85
+ if (!parsed.success) {
86
+ const issue = parsed.error.issues[0]
87
+ const where = issue === undefined ? '' : ` at ${issue.path.map(String).join('.')} (${issue.code})`
88
+ throw new SeedProblem(`app dev: the seed answered no { personas, links, scopes }${where}`)
89
+ }
90
+ const result = parsed.data ?? {}
91
+ const names = new Set<string>()
92
+ const personas = (result.personas ?? []).map((persona, index): SeedPersona => {
93
+ const caller = dev.personOf(persona.caller)
94
+ if (caller === undefined) {
95
+ throw new SeedProblem(`app dev: persona ${index + 1} of the seed holds no person of its kit: dev.testCaller(grants, { label, email })`)
96
+ }
97
+ if (caller.cookie === null) {
98
+ throw new SeedProblem(`app dev: persona ${index + 1} of the seed signs in with a key; a browser carries a session (via: 'session')`)
99
+ }
100
+ if (names.has(persona.name)) throw new SeedProblem(`app dev: persona ${index + 1} of the seed repeats the name of an earlier one`)
101
+ names.add(persona.name)
102
+ return { name: persona.name, description: persona.description ?? null, caller }
103
+ })
104
+ const links = (result.links ?? []).map((link, index): SeedLink => {
105
+ if (!isLocalPath(link.url)) {
106
+ throw new SeedProblem(`app dev: link ${index + 1} of the seed is not a path of the application, as ctx.links answers it`)
107
+ }
108
+ return { title: link.title, description: link.description ?? null, url: link.url }
109
+ })
110
+ const dimensions = dev.kit.artifact.dimensions
111
+ const scopes = new Map<string, readonly { readonly value: string; readonly label: string }[]>()
112
+ for (const [dimension, values] of Object.entries(result.scopes ?? {})) {
113
+ if (!Object.hasOwn(dimensions, dimension)) {
114
+ throw new SeedProblem('app dev: the seed names scope values of a dimension the vocabulary does not declare')
115
+ }
116
+ scopes.set(dimension, values)
117
+ }
118
+ return { personas, links, scopes }
119
+ }