@fabrika/cli 0.3.3 → 0.4.1
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 +25 -8
- package/src/app-dev-outside.ts +120 -0
- package/src/app-dev-seed.ts +119 -0
- package/src/app-dev-ui.ts +586 -0
- package/src/app-dev.ts +589 -0
- package/src/connect.ts +249 -0
- package/src/control.ts +106 -22
- package/src/db.ts +296 -0
- package/src/index.ts +140 -11
- package/src/job.ts +81 -0
- package/src/junit.ts +37 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
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.
|
|
34
|
-
"@fabrika/auth-core": "^0.
|
|
35
|
-
"@fabrika/
|
|
36
|
-
"@fabrika/
|
|
37
|
-
"@fabrika/
|
|
38
|
-
"@fabrika/
|
|
39
|
-
"@fabrika/
|
|
33
|
+
"@fabrika/app": "^0.4.1",
|
|
34
|
+
"@fabrika/auth-core": "^0.4.1",
|
|
35
|
+
"@fabrika/control-contract": "^0.4.1",
|
|
36
|
+
"@fabrika/provider-contract": "^0.4.1",
|
|
37
|
+
"@fabrika/db": "^0.4.1",
|
|
38
|
+
"@fabrika/platform": "^0.4.1",
|
|
39
|
+
"@fabrika/platform-node": "^0.4.1",
|
|
40
|
+
"@fabrika/check": "^0.4.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@fabrika/builder": "^0.4.1",
|
|
44
|
+
"@fabrika/provider-cloudflare": "^0.4.1",
|
|
45
|
+
"@fabrika/provider-zerops": "^0.4.1"
|
|
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,120 @@
|
|
|
1
|
+
// What `fabrika app dev` stands in for outside the application: the dev outbox, which captures every request the
|
|
2
|
+
// process sends to `DEV_OUTBOX_ORIGIN` (a mail provider's stand-in, say), and `dev/dev.env`, the application's variables.
|
|
3
|
+
|
|
4
|
+
import { DEV_OUTBOX_ORIGIN, type DevOutboxMessage, type DevOutside } from '@fabrika/app/testing'
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { z } from 'zod/v4'
|
|
8
|
+
|
|
9
|
+
/** The application's variables for `app dev`, beside the seed. */
|
|
10
|
+
export const DEV_ENV_FILE = 'dev/dev.env'
|
|
11
|
+
|
|
12
|
+
const MAX_MESSAGES = 200
|
|
13
|
+
const MAX_BODY_BYTES = 256 * 1024
|
|
14
|
+
const VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
15
|
+
|
|
16
|
+
/** A refusal of `dev/dev.env`; it names the line, never a value. */
|
|
17
|
+
export class DevEnvProblem extends Error {
|
|
18
|
+
override readonly name = 'DevEnvProblem'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The variables of `<root>/dev/dev.env`, or none without the file: `NAME=value` lines, `#` comments and blank lines; a
|
|
23
|
+
* value in matching single or double quotes loses them. A framework name (`FABRIKA_*`) is the platform's, not the
|
|
24
|
+
* application's, so it is refused.
|
|
25
|
+
*/
|
|
26
|
+
export function readDevEnv(root: string): Readonly<Record<string, string>> {
|
|
27
|
+
const path = join(root, DEV_ENV_FILE)
|
|
28
|
+
if (!existsSync(path)) return {}
|
|
29
|
+
const variables: Record<string, string> = {}
|
|
30
|
+
const lines = readFileSync(path, 'utf8').split(/\r?\n/)
|
|
31
|
+
for (let index = 0; index < lines.length; index++) {
|
|
32
|
+
const line = (lines[index] ?? '').trim()
|
|
33
|
+
if (line === '' || line.startsWith('#')) continue
|
|
34
|
+
const at = line.indexOf('=')
|
|
35
|
+
const name = at < 0 ? '' : line.slice(0, at).trim()
|
|
36
|
+
if (!VARIABLE_NAME.test(name)) {
|
|
37
|
+
throw new DevEnvProblem(`app dev: ${DEV_ENV_FILE} line ${index + 1} is no NAME=value`)
|
|
38
|
+
}
|
|
39
|
+
if (name.startsWith('FABRIKA_')) {
|
|
40
|
+
throw new DevEnvProblem(`app dev: ${DEV_ENV_FILE} line ${index + 1} sets ${name}, a framework variable the kit writes itself`)
|
|
41
|
+
}
|
|
42
|
+
const raw = line.slice(at + 1).trim()
|
|
43
|
+
const quoted = raw.length >= 2 && (raw[0] === '"' || raw[0] === "'") && raw.endsWith(raw[0])
|
|
44
|
+
variables[name] = quoted ? raw.slice(1, -1) : raw
|
|
45
|
+
}
|
|
46
|
+
return variables
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface DevOutbox extends DevOutside {
|
|
50
|
+
/** Captures the process's requests to `DEV_OUTBOX_ORIGIN` until the returned function restores `fetch`. */
|
|
51
|
+
install(): () => void
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Whether a `fetch` call goes to the dev outbox, read off its URL without touching its body. */
|
|
55
|
+
function toOutbox(input: string | URL | Request): boolean {
|
|
56
|
+
const url = input instanceof Request ? input.url : String(input)
|
|
57
|
+
return URL.canParse(url) && new URL(url).origin === DEV_OUTBOX_ORIGIN
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function devOutbox(): DevOutbox {
|
|
61
|
+
const messages: DevOutboxMessage[] = []
|
|
62
|
+
const capture = async (request: Request): Promise<Response> => {
|
|
63
|
+
const body = await request.arrayBuffer()
|
|
64
|
+
if (body.byteLength > MAX_BODY_BYTES) {
|
|
65
|
+
return Response.json({ error: `the dev outbox keeps a body of at most ${MAX_BODY_BYTES} bytes` }, { status: 413 })
|
|
66
|
+
}
|
|
67
|
+
if (messages.length >= MAX_MESSAGES) messages.shift()
|
|
68
|
+
const id = (messages[messages.length - 1]?.id ?? 0) + 1
|
|
69
|
+
messages.push({
|
|
70
|
+
id,
|
|
71
|
+
method: request.method,
|
|
72
|
+
path: new URL(request.url).pathname,
|
|
73
|
+
contentType: request.headers.get('Content-Type'),
|
|
74
|
+
body: new TextDecoder().decode(body),
|
|
75
|
+
})
|
|
76
|
+
return Response.json({ id: `dev-outbox-${id}` })
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
outbox: () => [...messages],
|
|
80
|
+
install() {
|
|
81
|
+
const original = globalThis.fetch
|
|
82
|
+
const captured: typeof fetch = Object.assign(
|
|
83
|
+
(input: string | URL | Request, init?: RequestInit): Promise<Response> =>
|
|
84
|
+
toOutbox(input) ? capture(new Request(input, init)) : original(input, init),
|
|
85
|
+
{ preconnect: original.preconnect },
|
|
86
|
+
)
|
|
87
|
+
globalThis.fetch = captured
|
|
88
|
+
return () => {
|
|
89
|
+
if (globalThis.fetch === captured) globalThis.fetch = original
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A message's body as the dev view shows it: JSON indented, anything else as it came. */
|
|
96
|
+
export function shownBody(message: DevOutboxMessage): string {
|
|
97
|
+
try {
|
|
98
|
+
return JSON.stringify(JSON.parse(message.body), null, 2)
|
|
99
|
+
} catch {
|
|
100
|
+
return message.body
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The fields of a mail provider's JSON body the dev view shows; any other field is kept out of it. */
|
|
105
|
+
const MailHead = z.looseObject({ to: z.union([z.string(), z.array(z.string())]).optional(), subject: z.string().optional() })
|
|
106
|
+
|
|
107
|
+
/** The recipient and subject a mail provider's JSON body names (`to`, `subject`), else the request's path. */
|
|
108
|
+
export function mailHeadOf(message: DevOutboxMessage): { readonly to: string; readonly subject: string } {
|
|
109
|
+
let parsed: z.infer<typeof MailHead> = {}
|
|
110
|
+
try {
|
|
111
|
+
parsed = MailHead.safeParse(JSON.parse(message.body)).data ?? {}
|
|
112
|
+
} catch {
|
|
113
|
+
parsed = {}
|
|
114
|
+
}
|
|
115
|
+
const recipients = Array.isArray(parsed.to) ? parsed.to.join(', ') : (parsed.to ?? '')
|
|
116
|
+
return {
|
|
117
|
+
to: recipients === '' ? message.path : recipients,
|
|
118
|
+
subject: parsed.subject === undefined || parsed.subject === '' ? `${message.method} ${message.path}` : parsed.subject,
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -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 of the kit and the dev outbox. */
|
|
73
|
+
export async function importSeed(path: string): Promise<(dev: unknown, outside: 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, outside) => Reflect.apply(seed, undefined, [dev, outside])
|
|
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
|
+
}
|