@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,87 @@
|
|
|
1
|
+
import { host, presetFor } from '../hosts.mjs'
|
|
2
|
+
import { needsOf } from '../needs.mjs'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The steps `kerf deploy` takes for a target, as data: a dry run prints them, `--yes` runs them. A step reads env vars
|
|
6
|
+
* by name; their values are filled in only as it runs.
|
|
7
|
+
*/
|
|
8
|
+
export function deploySteps(root, config, { target, prod = false } = {}) {
|
|
9
|
+
const h = host(target)
|
|
10
|
+
const deploy = config.deploy ?? {}
|
|
11
|
+
const runtime = config.runtime ?? 'node'
|
|
12
|
+
const needs = needsOf(root, config)
|
|
13
|
+
const steps = []
|
|
14
|
+
const build = {
|
|
15
|
+
kind: 'build',
|
|
16
|
+
what: `kerf build --preset ${presetFor(h, runtime)}`,
|
|
17
|
+
preset: presetFor(h, runtime),
|
|
18
|
+
}
|
|
19
|
+
if (target === 'netlify') {
|
|
20
|
+
const site = process.env.NETLIFY_SITE_ID
|
|
21
|
+
? []
|
|
22
|
+
: deploy.netlify?.site
|
|
23
|
+
? ['--site', deploy.netlify.site]
|
|
24
|
+
: []
|
|
25
|
+
steps.push(build, {
|
|
26
|
+
kind: 'run',
|
|
27
|
+
cli: 'netlify',
|
|
28
|
+
args: [
|
|
29
|
+
'deploy',
|
|
30
|
+
'--dir',
|
|
31
|
+
'dist',
|
|
32
|
+
'--functions',
|
|
33
|
+
'.netlify/functions-internal',
|
|
34
|
+
'--json',
|
|
35
|
+
...site,
|
|
36
|
+
...(prod ? ['--prod'] : []),
|
|
37
|
+
],
|
|
38
|
+
reads: ['NETLIFY_AUTH_TOKEN', 'NETLIFY_SITE_ID'],
|
|
39
|
+
url: 'json',
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
if (target === 'vercel')
|
|
43
|
+
steps.push(build, {
|
|
44
|
+
kind: 'run',
|
|
45
|
+
cli: 'vercel',
|
|
46
|
+
args: ['deploy', '--prebuilt', '--yes', ...(prod ? ['--prod'] : [])],
|
|
47
|
+
token: 'VERCEL_TOKEN',
|
|
48
|
+
reads: ['VERCEL_TOKEN', 'VERCEL_ORG_ID', 'VERCEL_PROJECT_ID'],
|
|
49
|
+
url: 'last-line',
|
|
50
|
+
})
|
|
51
|
+
if (target === 'cloudflare')
|
|
52
|
+
steps.push(build, {
|
|
53
|
+
kind: 'run',
|
|
54
|
+
cli: 'wrangler',
|
|
55
|
+
args: ['deploy'],
|
|
56
|
+
cwd: '.output',
|
|
57
|
+
reads: ['CLOUDFLARE_API_TOKEN', 'CLOUDFLARE_ACCOUNT_ID'],
|
|
58
|
+
url: 'workers',
|
|
59
|
+
})
|
|
60
|
+
if (target === 'fly') {
|
|
61
|
+
// one machine unless a bus reaches the others: a second machine would split the rooms, and a volume is per machine
|
|
62
|
+
const one = needs.memoryBus || deploy.fly?.volume
|
|
63
|
+
steps.push({
|
|
64
|
+
kind: 'run',
|
|
65
|
+
cli: 'flyctl',
|
|
66
|
+
args: [
|
|
67
|
+
'deploy',
|
|
68
|
+
'--remote-only',
|
|
69
|
+
...(deploy.fly?.app ? ['--app', deploy.fly.app] : []),
|
|
70
|
+
...(one ? ['--ha=false'] : []),
|
|
71
|
+
],
|
|
72
|
+
reads: ['FLY_API_TOKEN'],
|
|
73
|
+
url: deploy.fly?.app ? `https://${deploy.fly.app}.fly.dev` : 'fly',
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
if (target === 'render')
|
|
77
|
+
steps.push({
|
|
78
|
+
kind: 'hook',
|
|
79
|
+
what: 'POST $RENDER_DEPLOY_HOOK_URL (Render builds the pushed commit)',
|
|
80
|
+
reads: ['RENDER_DEPLOY_HOOK_URL'],
|
|
81
|
+
url: deploy.render?.service ? `https://${deploy.render.service}.onrender.com` : undefined,
|
|
82
|
+
})
|
|
83
|
+
for (const s of steps)
|
|
84
|
+
if (s.cli)
|
|
85
|
+
s.what = `${s.cli} ${s.args.join(' ')}${s.token ? ` --token $${s.token}` : ''}${s.cwd ? ` (in ${s.cwd}/)` : ''}`
|
|
86
|
+
return steps
|
|
87
|
+
}
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
// `kerf deploy`, `kerf deploy hosts`, `kerf deploy init`, and what `kerf check` and `kerf doctor` say about the deploy
|
|
2
|
+
// (research 46). Nothing here talks to a provider except `kerf deploy --yes`, which runs the host's own CLI.
|
|
3
|
+
|
|
4
|
+
import { spawnSync } from 'node:child_process'
|
|
5
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { join, relative } from 'node:path'
|
|
7
|
+
import ts from 'typescript'
|
|
8
|
+
import { diagnostic, locate } from '../diag.mjs'
|
|
9
|
+
import { findConfig, isDataConfig } from '../load-config.mjs'
|
|
10
|
+
import { cdnFindings, writtenSecrets } from './cdn.mjs'
|
|
11
|
+
import { fileStates, filesFor, managerOf, writeFiles } from './files.mjs'
|
|
12
|
+
import { fit, hostsReport } from './fit.mjs'
|
|
13
|
+
import { HOSTS, TARGETS, host } from './hosts.mjs'
|
|
14
|
+
import { needsOf } from './needs.mjs'
|
|
15
|
+
import { cliFor } from './index/cli.mjs'
|
|
16
|
+
import { deploySteps } from './index/steps.mjs'
|
|
17
|
+
import { missingEnv, urlFrom } from './index/environment.mjs'
|
|
18
|
+
|
|
19
|
+
export { deploySteps, urlFrom }
|
|
20
|
+
|
|
21
|
+
export { HOSTS, TARGETS, fit, hostsReport, needsOf }
|
|
22
|
+
|
|
23
|
+
const usage = (message) => Object.assign(new Error(message), { code: 'E_USAGE' })
|
|
24
|
+
const refused = (message) => Object.assign(new Error(message), { code: 'E_REFUSED' })
|
|
25
|
+
|
|
26
|
+
/** A finding as a diagnostic, pointing at the file that shows the need. */
|
|
27
|
+
function toDiagnostic(root, f) {
|
|
28
|
+
let text = ''
|
|
29
|
+
try {
|
|
30
|
+
text = readFileSync(join(root, f.file), 'utf8')
|
|
31
|
+
} catch {}
|
|
32
|
+
const at = f.line
|
|
33
|
+
? { span: [f.line, 1, f.line, 1] }
|
|
34
|
+
: text && f.needle
|
|
35
|
+
? locate(text, f.needle)
|
|
36
|
+
: {}
|
|
37
|
+
return diagnostic({
|
|
38
|
+
code: f.code,
|
|
39
|
+
severity: f.severity,
|
|
40
|
+
file: f.file,
|
|
41
|
+
...at,
|
|
42
|
+
why: f.why,
|
|
43
|
+
help: f.fix,
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The target this run is about: `--target`, then `deploy.target`, then the host whose preset `--preset` names. */
|
|
48
|
+
export function targetOf(config, { target, preset } = {}) {
|
|
49
|
+
const id =
|
|
50
|
+
target ?? config.deploy?.target ?? (preset && HOSTS.find((h) => h.preset === preset)?.id)
|
|
51
|
+
if (id && !host(id)) throw usage(`unknown deploy target \`${id}\`: use ${TARGETS.join(', ')}`)
|
|
52
|
+
return id
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** E001–E004, E006, E008–E014 for `kerf check`: the configured target's fit, its files, written secrets, CDN setup. */
|
|
56
|
+
export function deployChecks(root, config, { preset } = {}) {
|
|
57
|
+
const out = writtenSecrets(root).map((s) => ({
|
|
58
|
+
code: 'E008',
|
|
59
|
+
severity: 'error',
|
|
60
|
+
file: s.file,
|
|
61
|
+
line: s.line,
|
|
62
|
+
why: `${s.file}:${s.line} has ${s.what} in it: it would be committed, and anyone with the repository could deploy or bill as you`,
|
|
63
|
+
fix: "Move it to the environment (the host's settings, or GitHub's secrets for the workflow), and rotate it: it has been seen",
|
|
64
|
+
}))
|
|
65
|
+
let id
|
|
66
|
+
try {
|
|
67
|
+
id = targetOf(config, { preset })
|
|
68
|
+
} catch {
|
|
69
|
+
id = undefined
|
|
70
|
+
}
|
|
71
|
+
const deploy = config.deploy ?? {}
|
|
72
|
+
if (id) {
|
|
73
|
+
out.push(...fit(needsOf(root, config), host(id), { deploy }).findings)
|
|
74
|
+
if (deploy.target === id && host(id).deploys) {
|
|
75
|
+
for (const f of fileStates(root, filesFor(initContext(root, config, id)))) {
|
|
76
|
+
if (f.state === 'missing' || f.state === 'stale')
|
|
77
|
+
out.push({
|
|
78
|
+
code: 'E006',
|
|
79
|
+
severity: 'warning',
|
|
80
|
+
file: f.path,
|
|
81
|
+
why: `\`${f.path}\` is ${f.state === 'missing' ? 'missing' : 'from an older config'}: ${host(id).name} would deploy ${f.state === 'missing' ? 'without it' : 'what it said then'}`,
|
|
82
|
+
fix: '`kerf deploy init` writes it (a file edited by hand is left alone)',
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
out.push(...cdnFindings(config))
|
|
88
|
+
return out.map((f) => toDiagnostic(root, f))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** What the generators read. */
|
|
92
|
+
export function initContext(root, config, target) {
|
|
93
|
+
return {
|
|
94
|
+
target,
|
|
95
|
+
deploy: config.deploy ?? {},
|
|
96
|
+
needs: needsOf(root, config),
|
|
97
|
+
runtime: config.runtime ?? 'node',
|
|
98
|
+
manager: managerOf(root),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** `kerf deploy hosts`, and the `deploy` part of `kerf doctor`: every host's fit and the suggestion (E005 when none is set). */
|
|
103
|
+
export function hostsCommand(root, config) {
|
|
104
|
+
const needs = needsOf(root, config)
|
|
105
|
+
const report = hostsReport(needs, { deploy: config.deploy })
|
|
106
|
+
const used = Object.entries(needs)
|
|
107
|
+
.filter(([k, v]) => k !== 'runtime' && v)
|
|
108
|
+
.map(([k, v]) => ({ need: k, ...v }))
|
|
109
|
+
const diagnostics = config.deploy?.target
|
|
110
|
+
? []
|
|
111
|
+
: [
|
|
112
|
+
diagnostic({
|
|
113
|
+
code: 'E005',
|
|
114
|
+
severity: 'info',
|
|
115
|
+
file: findConfig(root) ? relative(root, findConfig(root)) : 'kerf.config.ts',
|
|
116
|
+
why: `No \`deploy.target\`. ${report.fits
|
|
117
|
+
.filter((f) => f.fits && f.deploys)
|
|
118
|
+
.map((f) => f.name)
|
|
119
|
+
.join(
|
|
120
|
+
', ',
|
|
121
|
+
)} can run this app; kerf suggests ${host(report.suggestion.host).name}: ${report.suggestion.why}.`,
|
|
122
|
+
help: `\`kerf deploy init ${report.suggestion.host}\``,
|
|
123
|
+
}),
|
|
124
|
+
]
|
|
125
|
+
return { needs: used, ...report, target: config.deploy?.target ?? null, diagnostics }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function formatHosts(r) {
|
|
129
|
+
const lines = [
|
|
130
|
+
`needs: ${r.needs.length ? r.needs.map((n) => `${n.what} (${n.file})`).join(', ') : "nothing a CDN with functions can't give"}`,
|
|
131
|
+
'',
|
|
132
|
+
]
|
|
133
|
+
for (const f of r.fits) {
|
|
134
|
+
const mark = f.fits ? (f.findings.length ? '~' : '✓') : '✗'
|
|
135
|
+
lines.push(
|
|
136
|
+
`${mark} ${f.host.padEnd(11)} ${f.name}${f.deploys ? '' : " (fit only: `kerf deploy` doesn't drive it yet)"}`,
|
|
137
|
+
)
|
|
138
|
+
for (const x of f.findings) lines.push(` ${x.code} ${x.why}. Fix: ${x.fix}`)
|
|
139
|
+
lines.push(` cost: ${f.cost} (${f.pricing})`)
|
|
140
|
+
}
|
|
141
|
+
lines.push(
|
|
142
|
+
'',
|
|
143
|
+
`suggested: ${r.suggestion.host}, because ${r.suggestion.why}${r.suggestion.alternative ? `. Next best: ${r.suggestion.alternative}` : ''}`,
|
|
144
|
+
)
|
|
145
|
+
return lines.join('\n')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The object literal `export default` gives, directly or through `defineConfig(…)`. */
|
|
149
|
+
function configObject(sf) {
|
|
150
|
+
for (const st of sf.statements) {
|
|
151
|
+
if (!ts.isExportAssignment(st)) continue
|
|
152
|
+
const value = ts.isCallExpression(st.expression) ? st.expression.arguments[0] : st.expression
|
|
153
|
+
if (value && ts.isObjectLiteralExpression(value)) return value
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The `deploy` block init writes when the config has none: the names a host needs, from package.json, and a disk when data needs one. */
|
|
158
|
+
export function defaultDeploy(root, target, needs) {
|
|
159
|
+
let name = 'my-app'
|
|
160
|
+
try {
|
|
161
|
+
name =
|
|
162
|
+
JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
|
|
163
|
+
.name?.replace(/^@[^/]+\//, '')
|
|
164
|
+
.replace(/[^a-z0-9-]/gi, '-')
|
|
165
|
+
.toLowerCase() || name
|
|
166
|
+
} catch {}
|
|
167
|
+
const kept = needs.sqlite || needs.uploads
|
|
168
|
+
const per = {
|
|
169
|
+
netlify: { site: name },
|
|
170
|
+
vercel: { project: name },
|
|
171
|
+
cloudflare: { name },
|
|
172
|
+
fly: { app: name, region: 'iad', ...(kept && { volume: { name: 'data', sizeGb: 1 } }) },
|
|
173
|
+
render: { service: name, ...(kept && { plan: 'starter', disk: { sizeGb: 1 } }) },
|
|
174
|
+
}
|
|
175
|
+
return { target, [target]: per[target], ci: 'github', branch: 'main' }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const literal = (v) => {
|
|
179
|
+
if (v && typeof v === 'object')
|
|
180
|
+
return `{ ${Object.entries(v)
|
|
181
|
+
.map(([k, x]) => `${k}: ${literal(x)}`)
|
|
182
|
+
.join(', ')} }`
|
|
183
|
+
return typeof v === 'string' ? `'${v}'` : String(v)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The config with `deploy` added. Refuses to touch one that already has it, or that isn't a plain object. */
|
|
187
|
+
export function withDeploy(file, text, deploy) {
|
|
188
|
+
if (isDataConfig(file)) {
|
|
189
|
+
if (file.endsWith('.json'))
|
|
190
|
+
return `${JSON.stringify({ ...JSON.parse(text), deploy }, null, 2)}\n`
|
|
191
|
+
return `${text.replace(/\n*$/, '\n')}deploy:\n${Object.entries(deploy)
|
|
192
|
+
.map(([k, v]) => ` ${k}: ${JSON.stringify(v)}`)
|
|
193
|
+
.join('\n')}\n`
|
|
194
|
+
}
|
|
195
|
+
const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true)
|
|
196
|
+
const object = configObject(sf)
|
|
197
|
+
if (!object)
|
|
198
|
+
throw refused(
|
|
199
|
+
`${file} doesn't export a plain object, so \`kerf deploy init\` can't add \`deploy\` to it. Add \`deploy: ${literal(deploy)}\` by hand. Nothing was changed.`,
|
|
200
|
+
)
|
|
201
|
+
const block = `deploy: ${literal(deploy)},`
|
|
202
|
+
const last = object.properties.at(-1)
|
|
203
|
+
return last
|
|
204
|
+
? text.slice(0, object.properties.end) +
|
|
205
|
+
`${object.properties.hasTrailingComma ? '' : ','}\n ${block}` +
|
|
206
|
+
text.slice(object.properties.end)
|
|
207
|
+
: text.slice(0, object.getStart(sf) + 1) +
|
|
208
|
+
`\n ${block}\n` +
|
|
209
|
+
text.slice(object.getStart(sf) + 1)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* `kerf deploy init [target]`: the host's files and workflow, and `deploy` in the config when it has none. Refuses a
|
|
214
|
+
* target the app doesn't fit unless `unsafe` lets the codes through.
|
|
215
|
+
*/
|
|
216
|
+
export function initCommand(root, config, { target, dryRun = false, force = false, unsafe } = {}) {
|
|
217
|
+
const needs = needsOf(root, config)
|
|
218
|
+
const id = targetOf(config, { target }) ?? hostsReport(needs).suggestion.host
|
|
219
|
+
const h = host(id)
|
|
220
|
+
if (!h.deploys)
|
|
221
|
+
throw usage(
|
|
222
|
+
`\`kerf deploy\` doesn't drive ${h.name} yet (research 46 §11): ${TARGETS.join(', ')} it does`,
|
|
223
|
+
)
|
|
224
|
+
if (config.deploy?.target && target && config.deploy.target !== target)
|
|
225
|
+
throw refused(
|
|
226
|
+
`\`deploy.target\` is \`${config.deploy.target}\` in the config. Change it there to move to \`${target}\`, then run this again. Nothing was changed.`,
|
|
227
|
+
)
|
|
228
|
+
const deploy = config.deploy ?? defaultDeploy(root, id, needs)
|
|
229
|
+
const result = fit(needs, h, { deploy })
|
|
230
|
+
const blocking = result.findings.filter((f) => f.severity === 'error' && !unsafe?.allows(f))
|
|
231
|
+
if (blocking.length)
|
|
232
|
+
throw refused(
|
|
233
|
+
`${h.name} can't run this app:\n${blocking.map((f) => ` ${f.code} ${f.file}: ${f.why}.\n Fix: ${f.fix}`).join('\n')}\n\`kerf deploy hosts\` shows which hosts can. Nothing was changed.`,
|
|
234
|
+
)
|
|
235
|
+
const ctx = { ...initContext(root, { ...config, deploy }, id) }
|
|
236
|
+
const states = fileStates(root, filesFor(ctx))
|
|
237
|
+
const configFile = findConfig(root)
|
|
238
|
+
const actions = []
|
|
239
|
+
let written = []
|
|
240
|
+
if (!config.deploy) {
|
|
241
|
+
const file = configFile ?? join(root, 'kerf.config.ts')
|
|
242
|
+
const text = configFile
|
|
243
|
+
? readFileSync(configFile, 'utf8')
|
|
244
|
+
: "import { defineConfig } from '@kerf-js/core/config'\n\nexport default defineConfig({})\n"
|
|
245
|
+
actions.push(`${relative(root, file)}: ${configFile ? '' : '(new) '}deploy: ${literal(deploy)}`)
|
|
246
|
+
if (!dryRun) writeFileSync(file, withDeploy(file, text, deploy))
|
|
247
|
+
}
|
|
248
|
+
for (const f of states)
|
|
249
|
+
actions.push(
|
|
250
|
+
`${f.path}: ${{ missing: 'write', stale: 'rewrite (from an older config)', current: 'current', edited: force ? 'overwrite (edited by hand; --force)' : 'edited by hand: left alone', foreign: force ? 'overwrite (--force)' : 'not written by kerf: left alone' }[f.state]}`,
|
|
251
|
+
)
|
|
252
|
+
if (!dryRun) written = writeFiles(root, states, { force })
|
|
253
|
+
const next = [
|
|
254
|
+
...{
|
|
255
|
+
netlify: [
|
|
256
|
+
`netlify sites:create --name ${deploy.netlify?.site ?? '<name>'} (once; or link an existing site with \`netlify link\`)`,
|
|
257
|
+
],
|
|
258
|
+
vercel: [
|
|
259
|
+
`vercel link --project ${deploy.vercel?.project ?? '<name>'} (once: writes .vercel/project.json with the org and project ids)`,
|
|
260
|
+
],
|
|
261
|
+
cloudflare: ['wrangler login (once, locally; CI reads CLOUDFLARE_API_TOKEN)'],
|
|
262
|
+
fly: [
|
|
263
|
+
`flyctl apps create ${deploy.fly?.app ?? '<name>'} (once; billed per running machine)`,
|
|
264
|
+
...(deploy.fly?.volume
|
|
265
|
+
? [
|
|
266
|
+
`flyctl volumes create ${deploy.fly.volume.name ?? 'data'} --size ${deploy.fly.volume.sizeGb ?? 1} --region ${deploy.fly.region ?? 'iad'} -a ${deploy.fly.app} (once; billed)`,
|
|
267
|
+
]
|
|
268
|
+
: []),
|
|
269
|
+
],
|
|
270
|
+
render: [
|
|
271
|
+
'Create the service from render.yaml: Render dashboard → New → Blueprint, on this repository (once)',
|
|
272
|
+
"Copy the service's deploy hook (Settings → Deploy Hook) into RENDER_DEPLOY_HOOK_URL",
|
|
273
|
+
],
|
|
274
|
+
}[id],
|
|
275
|
+
...((deploy.ci ?? 'github') === 'github'
|
|
276
|
+
? [
|
|
277
|
+
`gh secret set ${h.env.join(' / ')} (each one, from the host's dashboard: kerf never writes them)`,
|
|
278
|
+
]
|
|
279
|
+
: []),
|
|
280
|
+
`kerf deploy (a dry run: shows what --yes would run)`,
|
|
281
|
+
]
|
|
282
|
+
return {
|
|
283
|
+
ok: true,
|
|
284
|
+
target: id,
|
|
285
|
+
dryRun,
|
|
286
|
+
actions,
|
|
287
|
+
written,
|
|
288
|
+
next,
|
|
289
|
+
findings: result.findings.map((f) => toDiagnostic(root, f)),
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* `kerf deploy`: fit, then the steps. Without `yes` nothing runs: the result says what would. `build` is `kerf build`
|
|
295
|
+
* in-process (start.mjs's buildCommand), `exec` runs a host CLI; both are injectable for tests.
|
|
296
|
+
*/
|
|
297
|
+
export async function deployCommand(
|
|
298
|
+
root,
|
|
299
|
+
config,
|
|
300
|
+
{
|
|
301
|
+
target,
|
|
302
|
+
prod = false,
|
|
303
|
+
yes = false,
|
|
304
|
+
unsafe,
|
|
305
|
+
env = process.env,
|
|
306
|
+
build,
|
|
307
|
+
exec = defaultExec,
|
|
308
|
+
which,
|
|
309
|
+
post = (url) => fetch(url, { method: 'POST' }),
|
|
310
|
+
} = {},
|
|
311
|
+
) {
|
|
312
|
+
const id = targetOf(config, { target })
|
|
313
|
+
if (!id) {
|
|
314
|
+
const s = hostsReport(needsOf(root, config)).suggestion
|
|
315
|
+
throw refused(
|
|
316
|
+
`no deploy target: set \`deploy.target\` (\`kerf deploy init\` does, and writes the host's files), or pass --target. kerf suggests \`${s.host}\`: ${s.why}`,
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
const h = host(id)
|
|
320
|
+
if (!h.deploys) throw usage(`\`kerf deploy\` doesn't drive ${h.name} yet (research 46 §11)`)
|
|
321
|
+
if (h.kind === 'server' && !prod)
|
|
322
|
+
throw usage(`${h.name} has no preview deploys: pass --prod to deploy production`)
|
|
323
|
+
const needs = needsOf(root, config)
|
|
324
|
+
const findings = fit(needs, h, { deploy: config.deploy ?? {}, local: !env.CI }).findings
|
|
325
|
+
const blocking = findings.filter((f) => f.severity === 'error' && !unsafe?.allows(f))
|
|
326
|
+
if (blocking.length)
|
|
327
|
+
throw refused(
|
|
328
|
+
`${h.name} can't run this app:\n${blocking.map((f) => ` ${f.code} ${f.file}: ${f.why}.\n Fix: ${f.fix}`).join('\n')}\nTo deploy anyway: --unsafe-allow ${[...new Set(blocking.map((f) => f.code))].join(',')}`,
|
|
329
|
+
)
|
|
330
|
+
const steps = deploySteps(root, config, { target: id, prod })
|
|
331
|
+
const envFindings = missingEnv(root, id, config, env)
|
|
332
|
+
const diagnostics = [...findings, ...envFindings, ...cdnFindings(config, { env })].map((f) =>
|
|
333
|
+
toDiagnostic(root, f),
|
|
334
|
+
)
|
|
335
|
+
if (!yes)
|
|
336
|
+
return {
|
|
337
|
+
ok: true,
|
|
338
|
+
dryRun: true,
|
|
339
|
+
target: id,
|
|
340
|
+
prod,
|
|
341
|
+
steps: steps.map((s) => ({ what: s.what, reads: s.reads ?? [] })),
|
|
342
|
+
diagnostics,
|
|
343
|
+
}
|
|
344
|
+
const stop = envFindings.find((f) => f.severity === 'error')
|
|
345
|
+
if (stop) throw refused(`E007: ${stop.why}. ${stop.fix}.`)
|
|
346
|
+
let url
|
|
347
|
+
for (const s of steps) {
|
|
348
|
+
if (s.kind === 'build') {
|
|
349
|
+
const built = await build(root, { preset: s.preset })
|
|
350
|
+
diagnostics.push(...built.diagnostics)
|
|
351
|
+
if (!built.ok) throw refused('kerf build failed: nothing was deployed. Fix the errors above.')
|
|
352
|
+
} else if (s.kind === 'hook') {
|
|
353
|
+
const res = await post(env.RENDER_DEPLOY_HOOK_URL)
|
|
354
|
+
if (!res.ok)
|
|
355
|
+
throw refused(
|
|
356
|
+
`Render's deploy hook answered ${res.status}: nothing was deployed. Check RENDER_DEPLOY_HOOK_URL.`,
|
|
357
|
+
)
|
|
358
|
+
url = s.url
|
|
359
|
+
} else {
|
|
360
|
+
const [cmd, ...pre] = cliFor(s.cli, { which })
|
|
361
|
+
const args = [
|
|
362
|
+
...pre,
|
|
363
|
+
...s.args,
|
|
364
|
+
...(s.token && env[s.token] ? ['--token', env[s.token]] : []),
|
|
365
|
+
]
|
|
366
|
+
const r = await Promise.resolve(exec(cmd, args, { cwd: join(root, s.cwd ?? ''), env }))
|
|
367
|
+
if (r.code !== 0)
|
|
368
|
+
throw refused(
|
|
369
|
+
`${s.cli} failed (exit ${r.code}): nothing more was run.\n${(r.stderr || r.stdout).trim().split('\n').slice(-15).join('\n')}`,
|
|
370
|
+
)
|
|
371
|
+
url = urlFrom(s, r.stdout) ?? url
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return { ok: true, dryRun: false, target: id, prod, url: url ?? null, diagnostics }
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** A host CLI with its progress on stderr, as it goes, and stdout kept for the URL. */
|
|
378
|
+
function defaultExec(cmd, args, { cwd, env }) {
|
|
379
|
+
const r = spawnSync(cmd, args, {
|
|
380
|
+
cwd,
|
|
381
|
+
env,
|
|
382
|
+
encoding: 'utf8',
|
|
383
|
+
stdio: ['inherit', 'pipe', 'inherit'],
|
|
384
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
385
|
+
})
|
|
386
|
+
return { code: r.status ?? 1, stdout: r.stdout ?? '', stderr: r.error?.message ?? '' }
|
|
387
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// What an app needs from a host (research 46 §2), read from its files in one pass: no Vite server, no build. Each need
|
|
2
|
+
// carries the file that shows it, so a diagnostic can point there.
|
|
3
|
+
|
|
4
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
5
|
+
import { join, relative } from 'node:path'
|
|
6
|
+
|
|
7
|
+
const SKIP = new Set(['node_modules', '.kerf', '.output', 'dist', '.netlify', '.vercel'])
|
|
8
|
+
/** npm packages with a compiled addon: one built on a Mac doesn't load on a Linux function. */
|
|
9
|
+
const ADDONS =
|
|
10
|
+
/^(better-sqlite3|sqlite3|sharp|bcrypt|argon2|canvas|@node-rs\/.+|@napi-rs\/canvas|bufferutil|utf-8-validate)$/
|
|
11
|
+
/** A bus that reaches every instance. The memory bus is what's left when none is named. */
|
|
12
|
+
const BUSES =
|
|
13
|
+
/\b(postgresBus|redisBus|natsBus|durableObjectBus)\s*\(|@kerf-js\/realtime-(redis|nats|durable-object)/
|
|
14
|
+
|
|
15
|
+
const posix = (p) => p.replaceAll('\\', '/')
|
|
16
|
+
|
|
17
|
+
function sources(root) {
|
|
18
|
+
const out = []
|
|
19
|
+
const walk = (dir) => {
|
|
20
|
+
for (const d of readdirSync(dir, { withFileTypes: true })) {
|
|
21
|
+
const f = join(dir, d.name)
|
|
22
|
+
if (d.isDirectory()) {
|
|
23
|
+
if (!SKIP.has(d.name)) walk(f)
|
|
24
|
+
} else if (/\.[cm]?[jt]sx?$/.test(d.name) && !/\.(test|spec)\.|\.d\.ts$/.test(d.name))
|
|
25
|
+
out.push(f)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (existsSync(join(root, 'src'))) walk(join(root, 'src'))
|
|
29
|
+
return out
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** `.kerf/contract.json`, when it's newer than every source file: what the last build saw is then what's there. */
|
|
33
|
+
function freshContract(root, files) {
|
|
34
|
+
const file = join(root, '.kerf', 'contract.json')
|
|
35
|
+
if (!existsSync(file)) return
|
|
36
|
+
const at = statSync(file).mtimeMs
|
|
37
|
+
if (files.some((f) => statSync(f).mtimeMs > at)) return
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(readFileSync(file, 'utf8'))
|
|
40
|
+
} catch {}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const read = (file) => {
|
|
44
|
+
try {
|
|
45
|
+
return readFileSync(file, 'utf8')
|
|
46
|
+
} catch {
|
|
47
|
+
return ''
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {string} root
|
|
53
|
+
* @param {any} [config] the loaded kerf.config
|
|
54
|
+
* @returns {{ sockets?: Need, required?: Need, feed?: Need, memoryBus?: Need, sidecar?: Need, native?: Need, sqlite?: Need, uploads?: Need, addons?: Need, runtime: string }}
|
|
55
|
+
* @typedef {{ file: string, what: string }} Need
|
|
56
|
+
*/
|
|
57
|
+
export function needsOf(root, config = {}) {
|
|
58
|
+
const files = sources(root)
|
|
59
|
+
const rel = (f) => posix(relative(root, f))
|
|
60
|
+
const needs = { runtime: config.runtime ?? 'node' }
|
|
61
|
+
const contract = freshContract(root, files)
|
|
62
|
+
const realtimeFile = join(root, 'src/realtime.server.ts')
|
|
63
|
+
const realtime = read(realtimeFile)
|
|
64
|
+
let streams = false
|
|
65
|
+
if (contract) {
|
|
66
|
+
const live = contract.functions?.find((f) => f.kind === 'channel' || f.kind === 'shared')
|
|
67
|
+
if (live)
|
|
68
|
+
needs.sockets = {
|
|
69
|
+
file: '.kerf/contract.json',
|
|
70
|
+
what: `\`${live.name}\` is a ${live.kind === 'shared' ? '`sharedState`' : '`channel`'}`,
|
|
71
|
+
}
|
|
72
|
+
streams = contract.functions?.some((f) => ['stream', 'channel', 'shared'].includes(f.kind))
|
|
73
|
+
const native = Object.entries(contract.services ?? {}).find(([, s]) => s.kind === 'native')
|
|
74
|
+
if (native) needs.native = { file: '.kerf/contract.json', what: `\`native('${native[0]}')\`` }
|
|
75
|
+
}
|
|
76
|
+
streams = scanNeeds({ files, rel, contract, needs, config, realtime, streams })
|
|
77
|
+
if (!needs.sockets && /\bshare\s*[:,}]/.test(realtime))
|
|
78
|
+
needs.sockets = { file: 'src/realtime.server.ts', what: '`realtime({ share })`' }
|
|
79
|
+
if (/\bchanges\s*[:,}]/.test(realtime) && !/listener:\s*['"]external['"]/.test(realtime))
|
|
80
|
+
needs.feed = { file: 'src/realtime.server.ts', what: '`changes`' }
|
|
81
|
+
const plugins = (config.plugins ?? [])
|
|
82
|
+
.map((p) => p?.name ?? p)
|
|
83
|
+
.filter((n) => typeof n === 'string')
|
|
84
|
+
const configText = ['kerf.config.ts', 'kerf.config.mts', 'kerf.config.js', 'kerf.config.mjs']
|
|
85
|
+
.map((n) => read(join(root, n)))
|
|
86
|
+
.join('\n')
|
|
87
|
+
if (
|
|
88
|
+
(streams || needs.sockets) &&
|
|
89
|
+
!BUSES.test(realtime) &&
|
|
90
|
+
!plugins.some((n) => /realtime-(redis|nats|durable-object)/.test(n)) &&
|
|
91
|
+
!BUSES.test(configText)
|
|
92
|
+
)
|
|
93
|
+
needs.memoryBus = {
|
|
94
|
+
file: existsSync(realtimeFile) ? 'src/realtime.server.ts' : 'src',
|
|
95
|
+
what: 'the memory bus',
|
|
96
|
+
}
|
|
97
|
+
if (!needs.uploads && /encoder:\s*['"]server['"]/.test(configText))
|
|
98
|
+
needs.uploads = { file: 'kerf.config.ts', what: "`media({ encoder: 'server' })`" }
|
|
99
|
+
if (
|
|
100
|
+
needs.uploads &&
|
|
101
|
+
!plugins.some((n) => n.includes('@kerf-js/media')) &&
|
|
102
|
+
!configText.includes('@kerf-js/media')
|
|
103
|
+
)
|
|
104
|
+
delete needs.uploads // M017 says that one
|
|
105
|
+
const db = read(join(root, 'src/db/index.server.ts'))
|
|
106
|
+
const drizzle = read(join(root, 'drizzle.config.ts'))
|
|
107
|
+
if (
|
|
108
|
+
/\/db\/sqlite['"]/.test(db) ||
|
|
109
|
+
(/dialect:\s*['"](sqlite|turso)['"]/.test(drizzle) && !/libsql:|turso/.test(drizzle + db))
|
|
110
|
+
)
|
|
111
|
+
needs.sqlite = {
|
|
112
|
+
file: db ? 'src/db/index.server.ts' : 'drizzle.config.ts',
|
|
113
|
+
what: 'a SQLite file',
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const pkg = JSON.parse(read(join(root, 'package.json')) || '{}')
|
|
117
|
+
const addon = Object.keys(pkg.dependencies ?? {}).find((n) => ADDONS.test(n))
|
|
118
|
+
if (addon) needs.addons = { file: 'package.json', what: `\`${addon}\`` }
|
|
119
|
+
} catch {}
|
|
120
|
+
return needs
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function scanNeeds({ files, rel, contract, needs, config, realtime, streams }) {
|
|
124
|
+
for (const f of files) {
|
|
125
|
+
const text = read(f)
|
|
126
|
+
const server = /^src\/server\/|\.server\.[cm]?[jt]sx?$/.test(rel(f))
|
|
127
|
+
if (!contract && server) {
|
|
128
|
+
const m = /\b(channel|sharedState)\s*[(<]/.exec(text)
|
|
129
|
+
if (m && !needs.sockets) needs.sockets = { file: rel(f), what: `a \`${m[1]}\`` }
|
|
130
|
+
if (/\b(stream|channel|sharedState)\s*[(<]/.test(text)) streams = true
|
|
131
|
+
}
|
|
132
|
+
if (/\.live\(/.test(text)) streams = true
|
|
133
|
+
if (/['"]@kerf-js\/start\/rust['"]/.test(text)) {
|
|
134
|
+
for (const m of text.matchAll(/\bremote\(\s*['"`]rust:\/\/([\w-]+)/g))
|
|
135
|
+
if (!config.rust?.services?.[m[1]]?.external)
|
|
136
|
+
needs.sidecar ??= { file: rel(f), what: `\`remote('rust://${m[1]}')\`` }
|
|
137
|
+
const n = /\bnative\(\s*['"`]([\w-]+)/.exec(text)
|
|
138
|
+
if (n) needs.native ??= { file: rel(f), what: `\`native('${n[1]}')\`` }
|
|
139
|
+
}
|
|
140
|
+
if (
|
|
141
|
+
!/listener:\s*['"]external['"]/.test(realtime) &&
|
|
142
|
+
/\.live\(([^;\n]*?)\brequired:\s*true|\blive:\s*\{[^}]*required:\s*true/.test(text)
|
|
143
|
+
)
|
|
144
|
+
needs.required ??= { file: rel(f), what: 'a live query marked `required`' }
|
|
145
|
+
if (/\bctx\.media\b|\bz\.media\b/.test(text))
|
|
146
|
+
needs.uploads ??= { file: rel(f), what: 'media uploads' }
|
|
147
|
+
}
|
|
148
|
+
return streams
|
|
149
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { named, hasName, GENERIC, UNNAMED } from './rules.mjs'
|
|
2
|
+
|
|
3
|
+
export function checkAccessibleNames({ el, report, labelled }) {
|
|
4
|
+
// -- accessible names --
|
|
5
|
+
if (['input', 'select', 'textarea'].includes(el.name)) {
|
|
6
|
+
const type = el.attr('type')?.value
|
|
7
|
+
const skip = ['hidden', 'submit', 'button', 'reset', 'image'].includes(type)
|
|
8
|
+
const id = el.attr('id')
|
|
9
|
+
// A computed id can pair with a label anywhere, a component around the field can render its label, and an
|
|
10
|
+
// aria-hidden field (a honeypot) has no name to give. `for` counts on a component too: a `<Field for>` passes it on.
|
|
11
|
+
const unseen =
|
|
12
|
+
id?.kind === 'expr' ||
|
|
13
|
+
el.attr('aria-hidden')?.value === 'true' ||
|
|
14
|
+
el.ancestors.some((a) => /^[A-Z]/.test(a))
|
|
15
|
+
if (
|
|
16
|
+
!skip &&
|
|
17
|
+
!unseen &&
|
|
18
|
+
!named(el) &&
|
|
19
|
+
!(id?.value && labelled.has(id.value)) &&
|
|
20
|
+
!el.ancestors.includes('label')
|
|
21
|
+
)
|
|
22
|
+
report(
|
|
23
|
+
'D030',
|
|
24
|
+
el.start,
|
|
25
|
+
el.tagEnd,
|
|
26
|
+
`<${el.name}> has no label: a screen reader announces only "edit" (WCAG 1.3.1, 4.1.2).${el.attr('placeholder') ? ' A placeholder is not a label.' : ''}`,
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
if (
|
|
30
|
+
['button', 'a', 'Button', 'Link'].includes(el.name) &&
|
|
31
|
+
!el.attr('label') &&
|
|
32
|
+
!named(el) &&
|
|
33
|
+
!(el.name === 'a' && !el.attr('href')) &&
|
|
34
|
+
!hasName(el.children)
|
|
35
|
+
)
|
|
36
|
+
report(
|
|
37
|
+
'D031',
|
|
38
|
+
el.start,
|
|
39
|
+
el.tagEnd,
|
|
40
|
+
`<${el.name}> has no name a screen reader can read: only an icon, or nothing (WCAG 4.1.2).`,
|
|
41
|
+
)
|
|
42
|
+
const label = el.attr('aria-label') ?? el.attr('aria-labelledby')
|
|
43
|
+
const role = el.attr('role')
|
|
44
|
+
if (
|
|
45
|
+
label &&
|
|
46
|
+
GENERIC.has(el.name) &&
|
|
47
|
+
(!role || (role.kind === 'string' && UNNAMED.has(role.value)))
|
|
48
|
+
)
|
|
49
|
+
report(
|
|
50
|
+
'D041',
|
|
51
|
+
label.start,
|
|
52
|
+
label.end,
|
|
53
|
+
`\`${label.name}\` on a <${el.name}>${role ? ` with role "${role.value}"` : ''}: ARIA forbids naming it, so screen readers skip the name (axe aria-prohibited-attr, Lighthouse).`,
|
|
54
|
+
)
|
|
55
|
+
if (el.name === 'img' && !el.attr('alt') && !el.attrs.some((a) => a.kind === 'spread'))
|
|
56
|
+
report(
|
|
57
|
+
'D032',
|
|
58
|
+
el.start,
|
|
59
|
+
el.tagEnd,
|
|
60
|
+
'<img> has no `alt`: a screen reader reads out the file name (WCAG 1.1.1).',
|
|
61
|
+
)
|
|
62
|
+
}
|