@kerf-js/cli 0.0.0-stage → 0.1.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/LICENSE +16 -0
- package/README.md +554 -2
- package/bin/kerf.mjs +15 -0
- package/docs/async.md +50 -0
- package/docs/browser.md +53 -0
- package/docs/config.md +127 -0
- package/docs/core.md +30 -0
- package/docs/deploy.md +108 -0
- package/docs/design.md +106 -0
- package/docs/entrypoints.md +83 -0
- package/docs/fonts.md +118 -0
- package/docs/icons.md +108 -0
- package/docs/lists.md +25 -0
- package/docs/meta.md +92 -0
- package/docs/plugins.md +166 -0
- package/docs/props.md +88 -0
- package/docs/reactivity.md +26 -0
- package/docs/rules.md +39 -0
- package/docs/rust.md +253 -0
- package/docs/start-short.md +10 -0
- package/docs/start.md +642 -0
- package/docs/state.md +122 -0
- package/docs/style.md +82 -0
- package/docs/surface.md +158 -0
- package/docs/testing.md +81 -0
- package/docs/theme.md +263 -0
- package/docs/ui-state.md +105 -0
- package/docs/verify.md +61 -0
- package/package.json +47 -3
- package/src/add/config.mjs +93 -0
- package/src/add/errors.mjs +2 -0
- package/src/add/setup.mjs +88 -0
- package/src/add-rust.mjs +178 -0
- package/src/add.mjs +392 -0
- package/src/agents-md.mjs +240 -0
- package/src/alias.mjs +70 -0
- package/src/audit/page.mjs +313 -0
- package/src/audit/report.mjs +69 -0
- package/src/audit/utils.mjs +5 -0
- package/src/audit.mjs +319 -0
- package/src/browser/cli.mjs +53 -0
- package/src/browser/client.mjs +299 -0
- package/src/browser/commands.mjs +258 -0
- package/src/capture.mjs +21 -0
- package/src/catalog/audit.mjs +129 -0
- package/src/catalog/auth.mjs +40 -0
- package/src/catalog/compiler.mjs +337 -0
- package/src/catalog/contracts.mjs +18 -0
- package/src/catalog/database.mjs +52 -0
- package/src/catalog/deploy.mjs +117 -0
- package/src/catalog/design.mjs +159 -0
- package/src/catalog/forms.mjs +25 -0
- package/src/catalog/hydration.mjs +29 -0
- package/src/catalog/icons.mjs +24 -0
- package/src/catalog/media.mjs +163 -0
- package/src/catalog/metadata.mjs +116 -0
- package/src/catalog/plugins.mjs +228 -0
- package/src/catalog/realtime.mjs +195 -0
- package/src/catalog/rust.mjs +75 -0
- package/src/catalog/server-functions.mjs +100 -0
- package/src/catalog/state.mjs +39 -0
- package/src/catalog/style-groups.mjs +54 -0
- package/src/catalog/tests.mjs +127 -0
- package/src/catalog/text.mjs +200 -0
- package/src/catalog/ui-state.mjs +42 -0
- package/src/catalog.mjs +103 -0
- package/src/check/runtime.mjs +280 -0
- package/src/check.mjs +321 -0
- package/src/cli/authoring.mjs +213 -0
- package/src/cli/project.mjs +302 -0
- package/src/cli.mjs +340 -0
- package/src/config.d.ts +1 -0
- package/src/config.mjs +5 -0
- package/src/contributions.mjs +119 -0
- package/src/db-layer.mjs +41 -0
- package/src/deploy/cdn.mjs +142 -0
- package/src/deploy/files.mjs +266 -0
- package/src/deploy/fit.mjs +166 -0
- package/src/deploy/hosts.mjs +126 -0
- package/src/deploy/index/cli.mjs +20 -0
- package/src/deploy/index/environment.mjs +69 -0
- package/src/deploy/index/steps.mjs +87 -0
- package/src/deploy/index.mjs +387 -0
- package/src/deploy/needs.mjs +149 -0
- package/src/design-checks/accessible-names.mjs +62 -0
- package/src/design-checks/contrast.mjs +43 -0
- package/src/design-checks/csp.mjs +60 -0
- package/src/design-checks/files.mjs +59 -0
- package/src/design-checks/heading-links.mjs +61 -0
- package/src/design-checks/interaction.mjs +65 -0
- package/src/design-checks/jsx.mjs +67 -0
- package/src/design-checks/layout-values.mjs +68 -0
- package/src/design-checks/native-controls.mjs +48 -0
- package/src/design-checks/outline.mjs +39 -0
- package/src/design-checks/reporter.mjs +37 -0
- package/src/design-checks/rules.mjs +274 -0
- package/src/design-checks/values.mjs +262 -0
- package/src/design-checks.mjs +49 -0
- package/src/diag.mjs +98 -0
- package/src/docs.mjs +228 -0
- package/src/doctor.mjs +42 -0
- package/src/files.mjs +60 -0
- package/src/fix.mjs +48 -0
- package/src/gate/after.mjs +40 -0
- package/src/gate/before.mjs +54 -0
- package/src/gate/classify.mjs +72 -0
- package/src/gate/mutation.mjs +209 -0
- package/src/gate/resolve-cases.mjs +22 -0
- package/src/gate/result.mjs +63 -0
- package/src/gate/run.mjs +75 -0
- package/src/gate.mjs +178 -0
- package/src/index.mjs +9 -0
- package/src/infer/box-props.cjs +140 -0
- package/src/infer/core-boxes.cjs +46 -0
- package/src/infer/derived-writes.cjs +95 -0
- package/src/infer/engine/checks.cjs +193 -0
- package/src/infer/engine/flow.cjs +293 -0
- package/src/infer/engine/report.cjs +158 -0
- package/src/infer/engine/utils.cjs +15 -0
- package/src/infer/engine.cjs +265 -0
- package/src/infer/facts/sites.cjs +114 -0
- package/src/infer/facts.cjs +345 -0
- package/src/infer/hosts.cjs +161 -0
- package/src/infer/lsp.mjs +240 -0
- package/src/infer/session.cjs +161 -0
- package/src/infer/shadow.cjs +54 -0
- package/src/infer/worker.cjs +145 -0
- package/src/jsx-scan.mjs +128 -0
- package/src/kerf.mjs +98 -0
- package/src/load-config/schema.mjs +300 -0
- package/src/load-config.mjs +302 -0
- package/src/map.mjs +177 -0
- package/src/mcp/browser-tools.mjs +155 -0
- package/src/mcp.mjs +273 -0
- package/src/meta-table.mjs +63 -0
- package/src/mutate.mjs +224 -0
- package/src/plugin-checks.mjs +118 -0
- package/src/plugins.mjs +258 -0
- package/src/project.mjs +310 -0
- package/src/props-checks.mjs +64 -0
- package/src/props-listener.mjs +56 -0
- package/src/react-checks.mjs +67 -0
- package/src/server-state-checks.mjs +34 -0
- package/src/share-checks.mjs +158 -0
- package/src/site.mjs +46 -0
- package/src/slots.mjs +181 -0
- package/src/start/build.mjs +116 -0
- package/src/start/routes.mjs +38 -0
- package/src/start.mjs +394 -0
- package/src/state-checks/core-state.mjs +223 -0
- package/src/state-checks/modules.mjs +17 -0
- package/src/state-checks/scan.mjs +232 -0
- package/src/state-checks/server-graph.mjs +65 -0
- package/src/state-checks.mjs +189 -0
- package/src/style/find.mjs +121 -0
- package/src/style.mjs +327 -0
- package/src/surface-checks.mjs +384 -0
- package/src/tailwind.mjs +23 -0
- package/src/tasks.mjs +261 -0
- package/src/test-rules/context.mjs +109 -0
- package/src/test-rules/file-rules.mjs +279 -0
- package/src/test-rules/rule-helpers.mjs +244 -0
- package/src/test-rules/syntax.mjs +223 -0
- package/src/test-rules/test-rules.mjs +127 -0
- package/src/testdiff.mjs +219 -0
- package/src/tests.mjs +120 -0
- package/src/text-checks.mjs +332 -0
- package/src/tokens.mjs +33 -0
- package/src/trust/scan.mjs +95 -0
- package/src/trust.mjs +394 -0
- package/src/workspace.mjs +182 -0
- package/ts-plugin/context.js +57 -0
- package/ts-plugin/index.js +17 -0
- package/ts-plugin/mapping.js +65 -0
- package/ts-plugin/package.json +1 -0
- package/ts-plugin/proxy.js +144 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// Find, load and validate kerf.config (TypeScript, JSON or YAML, one schema: research 34 §2). Plugins run only their pure
|
|
2
|
+
// `contribute()` (plugins.mjs); `slots` is data.
|
|
3
|
+
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import nodeModule, { createRequire } from 'node:module'
|
|
6
|
+
import { join, relative } from 'node:path'
|
|
7
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
8
|
+
import ts from 'typescript'
|
|
9
|
+
import { z } from 'zod'
|
|
10
|
+
import { createSchemas } from './load-config/schema.mjs'
|
|
11
|
+
import { diagnostic, locate } from './diag.mjs'
|
|
12
|
+
import { checkSite } from './site.mjs'
|
|
13
|
+
import { contributions } from './contributions.mjs'
|
|
14
|
+
import { bindSlots } from './slots.mjs'
|
|
15
|
+
|
|
16
|
+
export const configNames = [
|
|
17
|
+
'kerf.config.ts',
|
|
18
|
+
'kerf.config.mts',
|
|
19
|
+
'kerf.config.js',
|
|
20
|
+
'kerf.config.mjs',
|
|
21
|
+
'kerf.config.json',
|
|
22
|
+
'kerf.config.yaml',
|
|
23
|
+
'kerf.config.yml',
|
|
24
|
+
]
|
|
25
|
+
export const builtinNames = [
|
|
26
|
+
'add',
|
|
27
|
+
'map',
|
|
28
|
+
'dev',
|
|
29
|
+
'build',
|
|
30
|
+
'start',
|
|
31
|
+
'db',
|
|
32
|
+
'test',
|
|
33
|
+
'check',
|
|
34
|
+
'doctor',
|
|
35
|
+
'fix',
|
|
36
|
+
'tokens',
|
|
37
|
+
'browser',
|
|
38
|
+
'mcp',
|
|
39
|
+
'tasks',
|
|
40
|
+
'run',
|
|
41
|
+
'create',
|
|
42
|
+
'docs',
|
|
43
|
+
'agents-md',
|
|
44
|
+
'plugins',
|
|
45
|
+
'deploy',
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
const configModule = pathToFileURL(
|
|
49
|
+
join(fileURLToPath(new URL('.', import.meta.url)), 'config.mjs'),
|
|
50
|
+
).href
|
|
51
|
+
const nativeRequire = createRequire(import.meta.url)
|
|
52
|
+
/** Node's sync module hooks. Bun has none, and Deno's break `.node` requires, like Rolldown's; both read TypeScript without help (`importConfig`). */
|
|
53
|
+
const registerHooks =
|
|
54
|
+
process.versions.bun || process.versions.deno ? null : nodeModule.registerHooks
|
|
55
|
+
/** `kerf.config.ts.kerf-load-<n>`: a fresh module id per load, so a long-lived `kerf mcp` sees edits. */
|
|
56
|
+
const tag = /\.kerf-load-\d+$/
|
|
57
|
+
let hooked = false
|
|
58
|
+
|
|
59
|
+
const transpile = (path) =>
|
|
60
|
+
ts.transpileModule(readFileSync(path, 'utf8'), {
|
|
61
|
+
fileName: path,
|
|
62
|
+
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 },
|
|
63
|
+
}).outputText
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sync module hooks, so loading is Node's own `require(esm)` and works inside test runners' vm contexts too.
|
|
67
|
+
* `@kerf-js/core/config` resolves to the project's kerf when it ships one, else to ours; `.ts` is transpiled, not stripped.
|
|
68
|
+
*/
|
|
69
|
+
function hook() {
|
|
70
|
+
if (hooked) return
|
|
71
|
+
hooked = true
|
|
72
|
+
registerHooks({
|
|
73
|
+
resolve(specifier, context, next) {
|
|
74
|
+
if (tag.test(specifier))
|
|
75
|
+
return { url: pathToFileURL(specifier).href, format: 'module', shortCircuit: true }
|
|
76
|
+
if (specifier === '@kerf-js/core/config' || specifier === '@kerf-js/cli/config') {
|
|
77
|
+
try {
|
|
78
|
+
return next(specifier, context)
|
|
79
|
+
} catch {
|
|
80
|
+
return { url: configModule, shortCircuit: true }
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// the app's own imports are bundler-style (`./src/expression`): try what Vite would before failing
|
|
84
|
+
if (/^\.{1,2}\//.test(specifier) && !/\.[cm]?[jt]sx?$|\.json$/.test(specifier)) {
|
|
85
|
+
for (const end of [
|
|
86
|
+
'',
|
|
87
|
+
'.ts',
|
|
88
|
+
'.tsx',
|
|
89
|
+
'.mts',
|
|
90
|
+
'.js',
|
|
91
|
+
'/index.ts',
|
|
92
|
+
'/index.tsx',
|
|
93
|
+
'/index.js',
|
|
94
|
+
]) {
|
|
95
|
+
try {
|
|
96
|
+
return next(specifier + end, context)
|
|
97
|
+
} catch {}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return next(specifier, context)
|
|
101
|
+
},
|
|
102
|
+
load(url, context, next) {
|
|
103
|
+
if (!url.startsWith('file:')) return next(url, context)
|
|
104
|
+
const path = fileURLToPath(url)
|
|
105
|
+
if (tag.test(path)) {
|
|
106
|
+
const real = path.replace(tag, '')
|
|
107
|
+
return {
|
|
108
|
+
format: 'module',
|
|
109
|
+
source: /\.[mc]?ts$/.test(real) ? transpile(real) : readFileSync(real, 'utf8'),
|
|
110
|
+
shortCircuit: true,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (/\.[mc]?ts$/.test(path) && !path.includes('/node_modules/'))
|
|
114
|
+
return { format: 'module', source: transpile(path), shortCircuit: true }
|
|
115
|
+
return next(url, context)
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The config on Bun and Deno: their own TypeScript, with a query for a fresh module per load. When the project has no
|
|
122
|
+
* `@kerf-js/core/config` of its own, a copy next to the config, so its relative imports still resolve, imports ours instead.
|
|
123
|
+
*/
|
|
124
|
+
async function importConfig(file) {
|
|
125
|
+
try {
|
|
126
|
+
return (await import(`${pathToFileURL(file).href}?kerf-load=${generation++}`)).default ?? {}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (!/kerf\/config|@kerf-js\/cli\/config/.test(error.message)) throw error
|
|
129
|
+
}
|
|
130
|
+
const copy = `${file}.kerf-load-${generation++}.mjs`
|
|
131
|
+
writeFileSync(
|
|
132
|
+
copy,
|
|
133
|
+
transpile(file).replace(
|
|
134
|
+
/(['"])(?:kerf\/config|@kerf-js\/cli\/config)\1/g,
|
|
135
|
+
JSON.stringify(configModule),
|
|
136
|
+
),
|
|
137
|
+
)
|
|
138
|
+
try {
|
|
139
|
+
return (await import(pathToFileURL(copy).href)).default ?? {}
|
|
140
|
+
} finally {
|
|
141
|
+
rmSync(copy, { force: true })
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { configSchema, dataSchema } = createSchemas(z)
|
|
146
|
+
export { configSchema }
|
|
147
|
+
|
|
148
|
+
export const isDataConfig = (file) => /\.(json|ya?ml)$/.test(file)
|
|
149
|
+
|
|
150
|
+
const configFiles = (root) => configNames.map((name) => join(root, name)).filter(existsSync)
|
|
151
|
+
|
|
152
|
+
export function findConfig(root) {
|
|
153
|
+
return configFiles(root)[0] ?? null
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The schema `$schema` points at, for completion in `kerf.config.json` and `.yaml`. */
|
|
157
|
+
export function writeConfigSchema(root) {
|
|
158
|
+
mkdirSync(join(root, '.kerf'), { recursive: true })
|
|
159
|
+
writeFileSync(
|
|
160
|
+
join(root, '.kerf', 'config.schema.json'),
|
|
161
|
+
`${JSON.stringify(z.toJSONSchema(dataSchema, { unrepresentable: 'any' }), null, 2)}\n`,
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** `@kerf-js/text` → `text`, `@kerf-js/db/plugin-native` → `native`: the name a plugin's named export goes by. */
|
|
166
|
+
export const pluginName = (pkg) =>
|
|
167
|
+
pkg
|
|
168
|
+
.replace(/^@[^/]+\/(?:[^/]+\/)?/, '')
|
|
169
|
+
.replace(/^plugin-/, '')
|
|
170
|
+
.replace(/[^a-zA-Z0-9]+(.)?/g, (_, c = '') => c.toUpperCase())
|
|
171
|
+
|
|
172
|
+
/** A plugin module's factory, and its import name: the default, or a named export (`surface-keel` may export `keel`). */
|
|
173
|
+
export function pluginFactory(mod, pkg) {
|
|
174
|
+
if (typeof mod.default === 'function')
|
|
175
|
+
return { value: mod.default, name: pluginName(pkg), named: false }
|
|
176
|
+
const name = [pluginName(pkg), pkg.split('-').at(-1)].find((n) => typeof mod[n] === 'function')
|
|
177
|
+
return name ? { value: mod[name], name, named: true } : null
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A data plugin from the project's own install: `<name>/plugin` when the package has one, else its main entry. */
|
|
181
|
+
function loadPlugin(root, entry) {
|
|
182
|
+
if (typeof entry !== 'string' && !Array.isArray(entry)) return entry
|
|
183
|
+
const [name, options] = [entry].flat()
|
|
184
|
+
const require = createRequire(join(root, 'package.json'))
|
|
185
|
+
for (const spec of [`${name}/plugin`, name]) {
|
|
186
|
+
let path
|
|
187
|
+
try {
|
|
188
|
+
path = require.resolve(spec)
|
|
189
|
+
} catch {
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
const factory = pluginFactory(nativeRequire(path), name)
|
|
193
|
+
if (!factory) throw new Error(`plugin \`${name}\` exports no factory to call`)
|
|
194
|
+
return factory.value(options)
|
|
195
|
+
}
|
|
196
|
+
throw new Error(`plugin \`${name}\` isn't installed`)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** A JSON or YAML config as what a `.ts` one evaluates to. */
|
|
200
|
+
function readDataConfig(root, file, text) {
|
|
201
|
+
const data = file.endsWith('.json') ? JSON.parse(text) : nativeRequire('yaml').parse(text)
|
|
202
|
+
const { $schema: _schema, ...config } = data ?? {}
|
|
203
|
+
if (Array.isArray(config.plugins)) config.plugins = config.plugins.map((p) => loadPlugin(root, p))
|
|
204
|
+
return config
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let generation = 0
|
|
208
|
+
|
|
209
|
+
function validateTasks(config, report) {
|
|
210
|
+
const tasks = config.tasks ?? {}
|
|
211
|
+
const commands = new Map()
|
|
212
|
+
for (const [name, t] of Object.entries(tasks)) {
|
|
213
|
+
if (builtinNames.includes(name))
|
|
214
|
+
report('P003', `Task \`${name}\` has the same name as the built-in \`kerf ${name}\`.`, name)
|
|
215
|
+
const twin = commands.get(t.run.trim())
|
|
216
|
+
if (twin)
|
|
217
|
+
report('P003', `Task \`${name}\` runs the same command as \`${twin}\` (\`${t.run}\`).`, name)
|
|
218
|
+
else commands.set(t.run.trim(), name)
|
|
219
|
+
const missing = [
|
|
220
|
+
!t.description?.trim() && '`description`',
|
|
221
|
+
!t.sideEffects && '`sideEffects`',
|
|
222
|
+
].filter(Boolean)
|
|
223
|
+
if (missing.length) report('P004', `Task \`${name}\` has no ${missing.join(' or ')}.`, name)
|
|
224
|
+
for (const env of t.env ?? []) {
|
|
225
|
+
if (config.environments && !(env in config.environments))
|
|
226
|
+
report(
|
|
227
|
+
'P007',
|
|
228
|
+
`Task \`${name}\` allows environment \`${env}\`, which \`environments\` does not define.`,
|
|
229
|
+
name,
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @param {string} root project root
|
|
237
|
+
* @returns {Promise<{ file: string | null, config: import('./config').KerfConfig, diagnostics: object[], slots: object, contributions: ReturnType<typeof contributions> }>}
|
|
238
|
+
*/
|
|
239
|
+
export async function loadConfig(root) {
|
|
240
|
+
const file = findConfig(root)
|
|
241
|
+
const diagnostics = []
|
|
242
|
+
const rel = file ? relative(root, file).split('\\').join('/') : 'kerf.config.ts'
|
|
243
|
+
const text = file ? readFileSync(file, 'utf8') : ''
|
|
244
|
+
const report = (code, why, near, extra) =>
|
|
245
|
+
diagnostics.push(diagnostic({ code, file: rel, why, ...locate(text, near), ...extra }))
|
|
246
|
+
const others = configFiles(root)
|
|
247
|
+
.slice(1)
|
|
248
|
+
.map((f) => relative(root, f))
|
|
249
|
+
if (others.length)
|
|
250
|
+
report(
|
|
251
|
+
'P010',
|
|
252
|
+
`\`${rel}\` is the config in use, and \`${others.join('`, `')}\` would be ignored.`,
|
|
253
|
+
null,
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
let config = {}
|
|
257
|
+
if (file) {
|
|
258
|
+
if (registerHooks) hook()
|
|
259
|
+
try {
|
|
260
|
+
config = isDataConfig(file)
|
|
261
|
+
? readDataConfig(root, file, text)
|
|
262
|
+
: registerHooks
|
|
263
|
+
? (nativeRequire(`${file}.kerf-load-${generation++}`).default ?? {})
|
|
264
|
+
: await importConfig(file)
|
|
265
|
+
} catch (error) {
|
|
266
|
+
// A plugin factory rejecting its options throws while the config module evaluates (@kerf-js/core/plugin).
|
|
267
|
+
if (error?.code === 'P115')
|
|
268
|
+
report('P115', error.message.split('\n')[0], error.message.match(/^`([^`]+)`/)?.[1] ?? null)
|
|
269
|
+
else report('P007', `kerf.config could not be loaded: ${error.message.split('\n')[0]}`, null)
|
|
270
|
+
return { file, config: {}, diagnostics, slots: bindSlots({}, () => {}), contributions: [] }
|
|
271
|
+
}
|
|
272
|
+
const parsed = configSchema.safeParse(config)
|
|
273
|
+
if (!parsed.success) {
|
|
274
|
+
for (const issue of parsed.error.issues) {
|
|
275
|
+
const path = issue.path.join('.')
|
|
276
|
+
report(
|
|
277
|
+
'P007',
|
|
278
|
+
`\`${path || '(root)'}\`: ${issue.message}`,
|
|
279
|
+
String(issue.path.at(-1) ?? ''),
|
|
280
|
+
{ help: 'Fix the key at the path shown (see `kerf docs config`).' },
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
return { file, config, diagnostics, slots: bindSlots({}, () => {}), contributions: [] }
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
validateTasks(config, report)
|
|
288
|
+
|
|
289
|
+
const seen = new Set()
|
|
290
|
+
for (const p of config.plugins ?? []) {
|
|
291
|
+
if (seen.has(p.name))
|
|
292
|
+
report('P116', `Plugin \`${p.name}\` is listed twice in \`plugins\`.`, p.name)
|
|
293
|
+
seen.add(p.name)
|
|
294
|
+
}
|
|
295
|
+
const unique = (config.plugins ?? []).filter(
|
|
296
|
+
(p, i, all) => all.findIndex((q) => q.name === p.name) === i,
|
|
297
|
+
)
|
|
298
|
+
const contributed = contributions(unique, report, { root })
|
|
299
|
+
const slots = bindSlots({ ...config, plugins: unique }, report)
|
|
300
|
+
if (config.site) checkSite(config.site, report)
|
|
301
|
+
return { file, config, diagnostics, slots, contributions: contributed }
|
|
302
|
+
}
|
package/src/map.mjs
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// `kerf map`: a matrix of what to call in this app, generated from its config and code (research 34 §8). It runs on every
|
|
2
|
+
// `kerf check`, so it reads files and never starts Vite. Rows point at where an answer lives instead of copying it, and
|
|
3
|
+
// a system the app doesn't have gets no row.
|
|
4
|
+
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { join, resolve } from 'node:path'
|
|
7
|
+
import { discover } from './files.mjs'
|
|
8
|
+
import { loadConfig } from './load-config.mjs'
|
|
9
|
+
import { dbLayer } from './db-layer.mjs'
|
|
10
|
+
import { listSurfaces } from './surface-checks.mjs'
|
|
11
|
+
|
|
12
|
+
const read = (root, rel) =>
|
|
13
|
+
existsSync(join(root, rel)) ? readFileSync(join(root, rel), 'utf8') : ''
|
|
14
|
+
const names = (text, re) => [...text.matchAll(re)].map((m) => m[1])
|
|
15
|
+
/** Up to `max` names: enough to recognise the app, while the row's call gives the whole list. */
|
|
16
|
+
const sample = (list, max = 6) =>
|
|
17
|
+
list.length > max ? `${list.slice(0, max).join(', ')}, +${list.length - max}` : list.join(', ')
|
|
18
|
+
|
|
19
|
+
function serverFunctions(root) {
|
|
20
|
+
const files = discover(root, {
|
|
21
|
+
include: ['src/server/**/*.{ts,mts}', 'src/**/*.server.{ts,mts}'],
|
|
22
|
+
exclude: [],
|
|
23
|
+
})
|
|
24
|
+
const out = { functions: [], channels: [] }
|
|
25
|
+
for (const rel of files) {
|
|
26
|
+
const found = [
|
|
27
|
+
...read(root, rel).matchAll(
|
|
28
|
+
/export\s+const\s+([A-Za-z_$][\w$]*)\s*=\s*(query|mutation|stream|channel)\s*\(/g,
|
|
29
|
+
),
|
|
30
|
+
]
|
|
31
|
+
const fns = found.filter((m) => m[2] !== 'channel').map((m) => m[1])
|
|
32
|
+
if (fns.length) out.functions.push(`\`${rel}\`: ${fns.join(', ')}`)
|
|
33
|
+
out.channels.push(...found.filter((m) => m[2] === 'channel').map((m) => m[1]))
|
|
34
|
+
}
|
|
35
|
+
return out
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const sqlText = (root, dir) =>
|
|
39
|
+
existsSync(join(root, dir))
|
|
40
|
+
? readdirSync(join(root, dir))
|
|
41
|
+
.filter((f) => f.endsWith('.sql'))
|
|
42
|
+
.map((f) => read(root, join(dir, f)))
|
|
43
|
+
.join('\n')
|
|
44
|
+
: ''
|
|
45
|
+
const ENGINE = { PrismaLibSql: 'sqlite', PrismaPg: 'postgres', PrismaMariaDb: 'mysql' }
|
|
46
|
+
|
|
47
|
+
export function database(root, layer) {
|
|
48
|
+
if (!layer) return null
|
|
49
|
+
const entry = read(root, 'src/db/index.server.ts')
|
|
50
|
+
const engine =
|
|
51
|
+
entry.match(/@kerf-js\/(?:start\/db|db\/plugin-native)\/(\w+)/)?.[1] ??
|
|
52
|
+
ENGINE[entry.match(/Prisma(?:LibSql|Pg|MariaDb)/)?.[0]] ??
|
|
53
|
+
'unknown'
|
|
54
|
+
const tables =
|
|
55
|
+
layer.layer === 'prisma'
|
|
56
|
+
? names(read(root, layer.schema), /^model\s+(\w+)/gm)
|
|
57
|
+
: layer.layer === 'native'
|
|
58
|
+
? names(
|
|
59
|
+
sqlText(root, layer.migrations),
|
|
60
|
+
/create\s+table\s+(?:if\s+not\s+exists\s+)?["`]?(\w+)/gi,
|
|
61
|
+
)
|
|
62
|
+
: names(
|
|
63
|
+
read(root, 'src/db/schema.ts'),
|
|
64
|
+
/export\s+const\s+([A-Za-z_$][\w$]*)\s*=\s*\w+Table\s*\(/g,
|
|
65
|
+
)
|
|
66
|
+
return { engine, layer: layer.layer, tables }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function routes(root) {
|
|
70
|
+
const files = discover(root, { include: ['src/routes/**/*.{ts,tsx}'], exclude: [] })
|
|
71
|
+
return files
|
|
72
|
+
.map((f) => f.replace(/^src\/routes\//, '').replace(/\.[jt]sx?$/, ''))
|
|
73
|
+
.filter((f) => !/(^|\/)_/.test(f))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The map as data. */
|
|
77
|
+
export async function agentMap(root) {
|
|
78
|
+
root = resolve(root)
|
|
79
|
+
const loaded = await loadConfig(root)
|
|
80
|
+
const { config } = loaded
|
|
81
|
+
const pages = routes(root)
|
|
82
|
+
const fns = serverFunctions(root)
|
|
83
|
+
const env = ['src/env.ts', 'src/env.server.ts'].find((f) => existsSync(join(root, f)))
|
|
84
|
+
const layer = await dbLayer(root, loaded)
|
|
85
|
+
const db = database(root, layer)
|
|
86
|
+
const authFile = read(root, 'src/auth.server.ts')
|
|
87
|
+
const auth = authFile && (authFile.includes('better-auth') ? 'better-auth' : 'custom')
|
|
88
|
+
const surfaces = listSurfaces(root).map((s) => s.name)
|
|
89
|
+
const styled = existsSync(join(root, '.kerf/styles.json'))
|
|
90
|
+
? Object.keys(JSON.parse(read(root, '.kerf/styles.json')).groups ?? {})
|
|
91
|
+
: []
|
|
92
|
+
const bound = Object.entries(loaded.slots ?? {})
|
|
93
|
+
.filter(([, v]) => typeof v === 'string' && !v.startsWith('kerf:default'))
|
|
94
|
+
.map(([slot]) => slot)
|
|
95
|
+
const tasks = Object.keys(config.tasks ?? {})
|
|
96
|
+
const row = (want, call, have) => ({ want, call, ...(have && { have }) })
|
|
97
|
+
|
|
98
|
+
const rows = [
|
|
99
|
+
pages.length && row('Pages, their data and metadata', '`kerf routes --meta`', sample(pages)),
|
|
100
|
+
fns.functions.length &&
|
|
101
|
+
row(
|
|
102
|
+
'Server functions',
|
|
103
|
+
'The file named here; signatures in `.kerf/contract.json` after a build',
|
|
104
|
+
fns.functions.join('; '),
|
|
105
|
+
),
|
|
106
|
+
fns.channels.length && row('Channels', '`kerf docs start`', sample(fns.channels)),
|
|
107
|
+
db &&
|
|
108
|
+
row(
|
|
109
|
+
'Tables and migrations',
|
|
110
|
+
layer.schema
|
|
111
|
+
? `\`${layer.schema}\`, then \`kerf db generate\``
|
|
112
|
+
: `\`kerf db generate <name>\`, then SQL in \`${layer.migrations}/\``,
|
|
113
|
+
sample(db.tables),
|
|
114
|
+
),
|
|
115
|
+
auth && row('Who the user is', '`ctx.user` on the server; `src/auth.server.ts`', auth),
|
|
116
|
+
row(
|
|
117
|
+
'Styles',
|
|
118
|
+
'Before writing one: `kerf style find <what it is>`',
|
|
119
|
+
styled.length ? sample(styled) : undefined,
|
|
120
|
+
),
|
|
121
|
+
env && row('Environment variables', `\`${env}\`; values in \`.env\``),
|
|
122
|
+
surfaces.length && row('Canvas and WebGL', '`kerf surfaces`', sample(surfaces)),
|
|
123
|
+
...loaded.contributions
|
|
124
|
+
.flatMap((c) => c.manifest.map ?? [])
|
|
125
|
+
.map((r) => row(r.want, r.call, r.have)),
|
|
126
|
+
(config.plugins?.length || bound.length) &&
|
|
127
|
+
row('Plugins and slots', '`kerf plugins`, `kerf plugins explain <slot>`', sample(bound)),
|
|
128
|
+
tasks.length && row('Project tasks', '`kerf tasks --json`', sample(tasks)),
|
|
129
|
+
row('A rule or a diagnostic', '`kerf docs <topic or code>`'),
|
|
130
|
+
row('The running app', '`kerf browser snapshot`'),
|
|
131
|
+
].filter(Boolean)
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
version: 1,
|
|
135
|
+
app: config.site?.name ?? null,
|
|
136
|
+
stack: {
|
|
137
|
+
runtime: config.runtime ?? 'node',
|
|
138
|
+
ui: config.plugins?.map((p) => p.name) ?? [],
|
|
139
|
+
db,
|
|
140
|
+
auth: auth || null,
|
|
141
|
+
},
|
|
142
|
+
rows,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function formatMap(map) {
|
|
147
|
+
const { stack } = map
|
|
148
|
+
const parts = [
|
|
149
|
+
`runtime ${stack.runtime}`,
|
|
150
|
+
stack.db && `${stack.db.layer} on ${stack.db.engine}`,
|
|
151
|
+
stack.auth,
|
|
152
|
+
...stack.ui,
|
|
153
|
+
].filter(Boolean)
|
|
154
|
+
const table = map.rows.map((r) => `| ${r.want} | ${r.call} | ${r.have ?? ''} |`)
|
|
155
|
+
return [
|
|
156
|
+
`# ${map.app ?? 'This app'}: agent map`,
|
|
157
|
+
'',
|
|
158
|
+
"<!-- Generated by `kerf map` from this app's config and code. Do not edit. -->",
|
|
159
|
+
'',
|
|
160
|
+
`Stack: ${parts.join(', ')}.`,
|
|
161
|
+
'',
|
|
162
|
+
'| To find or do | Call | In this app |',
|
|
163
|
+
'|---|---|---|',
|
|
164
|
+
...table,
|
|
165
|
+
'',
|
|
166
|
+
].join('\n')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Write `.kerf/map.md` and `.kerf/map.json`. */
|
|
170
|
+
export async function writeMap(root) {
|
|
171
|
+
root = resolve(root)
|
|
172
|
+
const map = await agentMap(root)
|
|
173
|
+
mkdirSync(join(root, '.kerf'), { recursive: true })
|
|
174
|
+
writeFileSync(join(root, '.kerf/map.md'), formatMap(map))
|
|
175
|
+
writeFileSync(join(root, '.kerf/map.json'), `${JSON.stringify(map, null, 2)}\n`)
|
|
176
|
+
return map
|
|
177
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { expectations, actions as browserActions } from '../browser/commands.mjs'
|
|
2
|
+
|
|
3
|
+
const target = {
|
|
4
|
+
description:
|
|
5
|
+
'What the user sees: { role, name } (name exact), { label }, { text }, or a ref from the latest snapshot ("@e12")',
|
|
6
|
+
anyOf: [
|
|
7
|
+
{ type: 'string', pattern: '^@?e\\d+$' },
|
|
8
|
+
{
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
role: { type: 'string' },
|
|
12
|
+
name: { type: 'string' },
|
|
13
|
+
exact: { type: 'boolean' },
|
|
14
|
+
},
|
|
15
|
+
required: ['role'],
|
|
16
|
+
},
|
|
17
|
+
{ type: 'object', properties: { label: { type: 'string' } }, required: ['label'] },
|
|
18
|
+
{ type: 'object', properties: { text: { type: 'string' } }, required: ['text'] },
|
|
19
|
+
],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The `kerf browser` commands as tools. The page is the app open in any browser; the dev server relays to it. */
|
|
23
|
+
export function browserTools() {
|
|
24
|
+
const tab = {
|
|
25
|
+
url: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: 'Dev server URL (default: .kerf/dev.json, then environments.dev.url)',
|
|
28
|
+
},
|
|
29
|
+
tab: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: 'Tab id from browser_tabs (default: the focused or latest tab)',
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
const t = (name, description, properties = {}, required) => ({
|
|
35
|
+
name,
|
|
36
|
+
description,
|
|
37
|
+
inputSchema: {
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: { ...properties, ...tab },
|
|
40
|
+
...(required ? { required } : {}),
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
return [
|
|
44
|
+
t(
|
|
45
|
+
'browser_tabs',
|
|
46
|
+
"Tabs of the app connected to the dev server. Any browser works (yours, the person's); with none, call browser_launch. If your own browser can run JS, `await __kerf__.agent.run(command, args)` in the page does the same as these tools.",
|
|
47
|
+
),
|
|
48
|
+
t(
|
|
49
|
+
'browser_launch',
|
|
50
|
+
'Start a headless Chrome on the app (only when you have no browser of your own)',
|
|
51
|
+
{ route: { type: 'string' }, headed: { type: 'boolean' } },
|
|
52
|
+
),
|
|
53
|
+
t('browser_open', 'Navigate the tab to a route', { route: { type: 'string' } }, ['route']),
|
|
54
|
+
t(
|
|
55
|
+
'browser_snapshot',
|
|
56
|
+
'The page as roles, names and refs (`- button "Add Banana" [ref=e15]`), plus kerf\'s live reactive graph with real names (`Cart.total = 42.5 ← Cart.subtotal → 1 binding in <Cart>`), and errors since the last call. Big payloads come back as a file path.',
|
|
57
|
+
{
|
|
58
|
+
interactive: { type: 'boolean', description: 'Only interactive elements' },
|
|
59
|
+
delta: { type: 'boolean', description: 'Only what changed since the last snapshot' },
|
|
60
|
+
scope: target,
|
|
61
|
+
graph: {
|
|
62
|
+
description: "false: no graph; 'only': only the graph; a string filters it by name",
|
|
63
|
+
anyOf: [{ type: 'boolean' }, { type: 'string' }],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
),
|
|
67
|
+
t(
|
|
68
|
+
'browser_act',
|
|
69
|
+
`Act like a user (user-event, as in @kerf-js/core/test) and get back what changed on the page. Actions: ${browserActions.join(', ')}`,
|
|
70
|
+
{
|
|
71
|
+
target,
|
|
72
|
+
action: { type: 'string', enum: browserActions },
|
|
73
|
+
value: {
|
|
74
|
+
type: 'string',
|
|
75
|
+
description: 'fill/type text, press key (`{Enter}`), select option',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
['target', 'action'],
|
|
79
|
+
),
|
|
80
|
+
t(
|
|
81
|
+
'browser_assert',
|
|
82
|
+
`Check what the user sees (retries up to timeout ms). Expectations: ${expectations.join(', ')}. Recorded for browser_record.`,
|
|
83
|
+
{
|
|
84
|
+
target,
|
|
85
|
+
expect: {
|
|
86
|
+
type: 'object',
|
|
87
|
+
description:
|
|
88
|
+
'{ text: "Total $4" } | { value: "SAVE10" } | { disabled: true } | { count: 3 } | { absent: true } | …',
|
|
89
|
+
},
|
|
90
|
+
timeout: { type: 'number' },
|
|
91
|
+
},
|
|
92
|
+
['target', 'expect'],
|
|
93
|
+
),
|
|
94
|
+
...browserStateTools(t),
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function browserStateTools(t) {
|
|
99
|
+
return [
|
|
100
|
+
t(
|
|
101
|
+
'browser_state',
|
|
102
|
+
'Read the named reactive graph, or write a box by its graph name to reach a state fast (dev only)',
|
|
103
|
+
{
|
|
104
|
+
filter: { type: 'string', description: 'Name prefix (`Cart`, `cart.`)' },
|
|
105
|
+
set: { type: 'string', description: 'Graph name to write, e.g. Cart.coupon' },
|
|
106
|
+
value: { description: 'JSON value to write' },
|
|
107
|
+
},
|
|
108
|
+
),
|
|
109
|
+
t(
|
|
110
|
+
'browser_time',
|
|
111
|
+
"The page's clock: pause (reload: from page load, so startup timers are faked too), advance ms (runs due timers in order), resume, status",
|
|
112
|
+
{
|
|
113
|
+
op: { type: 'string', enum: ['pause', 'advance', 'resume', 'status'] },
|
|
114
|
+
ms: { type: 'number' },
|
|
115
|
+
reload: { type: 'boolean' },
|
|
116
|
+
},
|
|
117
|
+
['op'],
|
|
118
|
+
),
|
|
119
|
+
t(
|
|
120
|
+
'browser_net',
|
|
121
|
+
'The network as the app sees it: offline (navigator.onLine false, offline event, fetch fails, sockets dropped), online, latency ms (fetch), drop (close open WebSockets/EventSources: a server restart), reset, status',
|
|
122
|
+
{
|
|
123
|
+
op: { type: 'string', enum: ['offline', 'online', 'latency', 'drop', 'reset', 'status'] },
|
|
124
|
+
ms: { type: 'number' },
|
|
125
|
+
},
|
|
126
|
+
['op'],
|
|
127
|
+
),
|
|
128
|
+
t(
|
|
129
|
+
'browser_record',
|
|
130
|
+
'Record what happens in the tab (your acts and asserts, and clicks/typing from any browser tool) and turn it into a @kerf-js/core/test test, which then has to pass the gate',
|
|
131
|
+
{
|
|
132
|
+
op: { type: 'string', enum: ['start', 'stop', 'steps', 'note', 'code'] },
|
|
133
|
+
text: { type: 'string' },
|
|
134
|
+
component: { type: 'string' },
|
|
135
|
+
from: { type: 'string' },
|
|
136
|
+
props: { type: 'string' },
|
|
137
|
+
name: { type: 'string' },
|
|
138
|
+
out: {
|
|
139
|
+
type: 'string',
|
|
140
|
+
description: 'Write the test to this file (relative to the project)',
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
['op'],
|
|
144
|
+
),
|
|
145
|
+
t(
|
|
146
|
+
'browser_logs',
|
|
147
|
+
'Console errors and warnings, and uncaught errors with the kerf binding they came from, since the last call',
|
|
148
|
+
),
|
|
149
|
+
t(
|
|
150
|
+
'browser_screenshot',
|
|
151
|
+
'PNG of the launched Chrome (browser_launch); in your own browser, use its screenshot tool',
|
|
152
|
+
{ out: { type: 'string' } },
|
|
153
|
+
),
|
|
154
|
+
]
|
|
155
|
+
}
|