@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
package/src/trust.mjs
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
// Plugin trust (PLAN §7c, research 13 §2.9): `.kerf/plugins.lock.json` records what each plugin may do and hashes its
|
|
2
|
+
// code, so a change is visible and blocked until `kerf plugins approve`. It is a diff, not a sandbox: `contribute()` has
|
|
3
|
+
// already run by the time any of this looks at a plugin, and the audit reads source, which code can hide from.
|
|
4
|
+
|
|
5
|
+
import { createHash } from 'node:crypto'
|
|
6
|
+
import {
|
|
7
|
+
existsSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
readdirSync,
|
|
10
|
+
readFileSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from 'node:fs'
|
|
14
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
15
|
+
import { diagnostic, locate } from './diag.mjs'
|
|
16
|
+
import { scanFile } from './trust/scan.mjs'
|
|
17
|
+
|
|
18
|
+
export const lockFile = '.kerf/plugins.lock.json'
|
|
19
|
+
|
|
20
|
+
const refused = (message) => Object.assign(new Error(message), { code: 'E_TRUST' })
|
|
21
|
+
const posix = (p) => p.split(sep).join('/')
|
|
22
|
+
/** @param {string[]} list */
|
|
23
|
+
const sorted = (list) => [...new Set(list)].sort()
|
|
24
|
+
|
|
25
|
+
/** Permissions as flat strings, one per grant, so a lock diff reads line by line: `fs.read:fonts/**`, `native`. */
|
|
26
|
+
export function grants(p = {}) {
|
|
27
|
+
const each = (kind, list) => (list ?? []).map((v) => `${kind}:${v}`)
|
|
28
|
+
return sorted([
|
|
29
|
+
...each('fs.read', p.fs?.read),
|
|
30
|
+
...each('fs.write', p.fs?.write),
|
|
31
|
+
...each('net', p.net),
|
|
32
|
+
...each('env', p.env),
|
|
33
|
+
...each('spawn', p.spawn),
|
|
34
|
+
...(p.native ? ['native'] : []),
|
|
35
|
+
])
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The lockfile, or `broken` when it can't be read: an unresolved merge shouldn't read as "nothing approved". */
|
|
39
|
+
function readLock(root) {
|
|
40
|
+
const file = join(root, lockFile)
|
|
41
|
+
if (!existsSync(file)) return { found: false, plugins: {} }
|
|
42
|
+
try {
|
|
43
|
+
const data = JSON.parse(readFileSync(file, 'utf8'))
|
|
44
|
+
if (data?.version !== 1 || typeof data.plugins !== 'object' || data.plugins === null)
|
|
45
|
+
return { found: true, plugins: {}, broken: 'it is not a version 1 lockfile' }
|
|
46
|
+
return { found: true, plugins: data.plugins }
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return { found: true, plugins: {}, broken: String(error.message).split('\n')[0] }
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The package a plugin name is in: `@kerf-js/db/mongo` is a subpath of `@kerf-js/db`. */
|
|
53
|
+
export const packageOf = (name) =>
|
|
54
|
+
name
|
|
55
|
+
.split('/')
|
|
56
|
+
.slice(0, name.startsWith('@') ? 2 : 1)
|
|
57
|
+
.join('/')
|
|
58
|
+
|
|
59
|
+
/** Where a plugin is installed: the nearest `node_modules/<package>`, through its symlink. Null for one defined in the config. */
|
|
60
|
+
function packageDir(root, name) {
|
|
61
|
+
for (let dir = root; ; dir = dirname(dir)) {
|
|
62
|
+
const at = join(dir, 'node_modules', packageOf(name))
|
|
63
|
+
if (existsSync(join(at, 'package.json'))) return realpathSync(at)
|
|
64
|
+
if (dirname(dir) === dir) return null
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A hash of the package's files, the same from any installer. Its own dependencies are the package manager's lock's job. */
|
|
69
|
+
function hashPackage(dir) {
|
|
70
|
+
const hash = createHash('sha512')
|
|
71
|
+
const files = readdirSync(dir, { recursive: true, withFileTypes: true })
|
|
72
|
+
.filter((f) => f.isFile() && !relative(dir, f.parentPath).split(sep).includes('node_modules'))
|
|
73
|
+
.map((f) => join(f.parentPath, f.name))
|
|
74
|
+
.sort()
|
|
75
|
+
for (const file of files) {
|
|
76
|
+
const bytes = readFileSync(file)
|
|
77
|
+
hash.update(`${posix(relative(dir, file))}\0${bytes.length}\0`).update(bytes)
|
|
78
|
+
}
|
|
79
|
+
return `sha512-${hash.digest('base64')}`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const EXTENSIONS = [
|
|
83
|
+
'',
|
|
84
|
+
'.mjs',
|
|
85
|
+
'.js',
|
|
86
|
+
'.cjs',
|
|
87
|
+
'.ts',
|
|
88
|
+
'.tsx',
|
|
89
|
+
'.mts',
|
|
90
|
+
'/index.mjs',
|
|
91
|
+
'/index.js',
|
|
92
|
+
'/index.ts',
|
|
93
|
+
]
|
|
94
|
+
const sourceFile = /\.(?:[cm]?[jt]s|[jt]sx)$/
|
|
95
|
+
const onDisk = (path) =>
|
|
96
|
+
EXTENSIONS.map((e) => path + e).find((p) => sourceFile.test(p) && existsSync(p)) ?? null
|
|
97
|
+
|
|
98
|
+
/** A package export's file for this runtime. */
|
|
99
|
+
function exportTarget(value) {
|
|
100
|
+
if (typeof value === 'string') return value
|
|
101
|
+
if (!value || typeof value !== 'object') return null
|
|
102
|
+
return exportTarget(value.node ?? value.import ?? value.default ?? value.require)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function entryFile(dir, pkg, subpath) {
|
|
106
|
+
const map = pkg.exports
|
|
107
|
+
const keyed = map && typeof map === 'object' && Object.keys(map).some((k) => k.startsWith('.'))
|
|
108
|
+
const target = exportTarget(
|
|
109
|
+
keyed ? map[subpath] : subpath === '.' ? (map ?? pkg.main ?? 'index.js') : null,
|
|
110
|
+
)
|
|
111
|
+
return (
|
|
112
|
+
(target && onDisk(join(dir, target))) ?? (subpath === '.' ? null : onDisk(join(dir, subpath)))
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The plugin's entry, and each module it names for the build or the server (a browser has no `fs` to reach). Only the
|
|
118
|
+
* package's own files are read; `dependencies` says what it stopped at.
|
|
119
|
+
*/
|
|
120
|
+
export function audit(root, dir, contribution) {
|
|
121
|
+
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'))
|
|
122
|
+
// A package can hold several plugins, `<package>/<sub>/plugin`, and each answers for its own files.
|
|
123
|
+
const sub = contribution.name?.startsWith(`${pkg.name}/`)
|
|
124
|
+
? contribution.name.slice(pkg.name.length)
|
|
125
|
+
: ''
|
|
126
|
+
const roots = [entryFile(dir, pkg, `.${sub}/plugin`) ?? entryFile(dir, pkg, `.${sub}`)]
|
|
127
|
+
for (const ref of contribution.manifest.provides ?? []) {
|
|
128
|
+
for (const [side, { module }] of Object.entries(ref.sides ?? {})) {
|
|
129
|
+
if (side === 'client') continue
|
|
130
|
+
if (module === pkg.name || module.startsWith(`${pkg.name}/`))
|
|
131
|
+
roots.push(entryFile(dir, pkg, `.${module.slice(pkg.name.length)}`))
|
|
132
|
+
else if (isAbsolute(module) || module.startsWith('.'))
|
|
133
|
+
roots.push(onDisk(isAbsolute(module) ? module : resolve(root, module)))
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const seen = new Set()
|
|
137
|
+
const queue = roots.filter((f) => f && !relative(dir, f).startsWith('..'))
|
|
138
|
+
const uses = []
|
|
139
|
+
/** @type {Set<string>} */
|
|
140
|
+
const dependencies = new Set()
|
|
141
|
+
while (queue.length) {
|
|
142
|
+
const file = queue.pop()
|
|
143
|
+
if (seen.has(file)) continue
|
|
144
|
+
seen.add(file)
|
|
145
|
+
const found = scanFile(readFileSync(file, 'utf8'), file)
|
|
146
|
+
for (const u of found.uses) uses.push({ ...u, file: posix(relative(dir, file)) })
|
|
147
|
+
for (const name of found.imports) {
|
|
148
|
+
if (!name.startsWith('.')) {
|
|
149
|
+
if (name !== pkg.name && !name.startsWith(`${pkg.name}/`))
|
|
150
|
+
dependencies.add(
|
|
151
|
+
name
|
|
152
|
+
.split('/')
|
|
153
|
+
.slice(0, name.startsWith('@') ? 2 : 1)
|
|
154
|
+
.join('/'),
|
|
155
|
+
)
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
const next = onDisk(resolve(dirname(file), name))
|
|
159
|
+
if (next && !relative(dir, next).startsWith('..')) queue.push(next)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const granted = grants(contribution.permissions)
|
|
163
|
+
return {
|
|
164
|
+
uses: uses.sort(
|
|
165
|
+
(a, b) => a.need.localeCompare(b.need) || a.file.localeCompare(b.file) || a.line - b.line,
|
|
166
|
+
),
|
|
167
|
+
undeclared: sorted(uses.map((u) => u.need).filter((need) => !declared(need, granted))),
|
|
168
|
+
dependencies: [...dependencies].sort(),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Is `need` from the audit covered by what the plugin declared? */
|
|
173
|
+
function declared(need, granted) {
|
|
174
|
+
if (need === 'env:*') return granted.some((g) => g.startsWith('env:'))
|
|
175
|
+
if (need.startsWith('env:')) return granted.includes(need) || granted.includes('env:*')
|
|
176
|
+
if (need === 'native') return granted.includes('native')
|
|
177
|
+
const kind = { fs: 'fs.', net: 'net:', spawn: 'spawn:' }[need]
|
|
178
|
+
return granted.some((g) => g.startsWith(kind))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Every plugin against the lock. A plugin whose code is pinned and unchanged has nothing new to find, so only `full`
|
|
183
|
+
* reads it again; one with nothing to pin (a workspace package) is read every time.
|
|
184
|
+
* @param {{ full?: boolean }} [options]
|
|
185
|
+
*/
|
|
186
|
+
export function trustStatus(root, loaded, { full = false } = {}) {
|
|
187
|
+
const lock = readLock(root)
|
|
188
|
+
const plugins = loaded.contributions.map((c) => {
|
|
189
|
+
const entry = lock.plugins[c.name] ?? null
|
|
190
|
+
const dir = packageDir(root, c.name)
|
|
191
|
+
const source = !dir
|
|
192
|
+
? 'config'
|
|
193
|
+
: dir.split(sep).includes('node_modules')
|
|
194
|
+
? 'package'
|
|
195
|
+
: 'workspace'
|
|
196
|
+
const integrity = source === 'package' ? hashPackage(dir) : null
|
|
197
|
+
const permissions = grants(c.permissions)
|
|
198
|
+
const provides = sorted(
|
|
199
|
+
(c.manifest.provides ?? []).map((p) => (p.key ? `${p.slot.id}[${p.key}]` : p.slot.id)),
|
|
200
|
+
)
|
|
201
|
+
// A scaffold approves permissions before there's an install to hash (`integrity: null`), and that's filled in by `approve`.
|
|
202
|
+
const changed =
|
|
203
|
+
!!entry &&
|
|
204
|
+
(entry.integrity ?? null) !== integrity &&
|
|
205
|
+
!(entry.integrity == null && source === 'package')
|
|
206
|
+
const scanned =
|
|
207
|
+
dir && (full || !entry || !integrity || !entry.integrity || changed)
|
|
208
|
+
? audit(root, dir, c)
|
|
209
|
+
: null
|
|
210
|
+
const uses = scanned?.uses ?? []
|
|
211
|
+
// Without a scan the lock's own record stands: those are the same files.
|
|
212
|
+
const undeclared = scanned?.undeclared ?? entry?.undeclared ?? []
|
|
213
|
+
const added = permissions.filter((g) => !entry?.permissions?.includes(g))
|
|
214
|
+
const removed = (entry?.permissions ?? []).filter((g) => !permissions.includes(g))
|
|
215
|
+
const hidden = undeclared.filter((need) => !entry?.undeclared?.includes(need))
|
|
216
|
+
return {
|
|
217
|
+
name: c.name,
|
|
218
|
+
version: c.version ?? null,
|
|
219
|
+
source,
|
|
220
|
+
integrity,
|
|
221
|
+
permissions,
|
|
222
|
+
provides,
|
|
223
|
+
uses,
|
|
224
|
+
undeclared,
|
|
225
|
+
dependencies: scanned?.dependencies ?? [],
|
|
226
|
+
locked: !!entry,
|
|
227
|
+
lockedVersion: entry?.version ?? null,
|
|
228
|
+
added,
|
|
229
|
+
removed,
|
|
230
|
+
hidden,
|
|
231
|
+
integrityChanged: changed,
|
|
232
|
+
blocked: added.length > 0 || hidden.length > 0 || changed,
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
return {
|
|
236
|
+
lock: lockFile,
|
|
237
|
+
found: lock.found,
|
|
238
|
+
broken: lock.broken,
|
|
239
|
+
plugins,
|
|
240
|
+
stale: Object.keys(lock.plugins)
|
|
241
|
+
.filter((name) => !plugins.some((p) => p.name === name))
|
|
242
|
+
.sort(),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** P109 and P110 for what the lock doesn't cover. `trust: 'warn'` in the config makes them warnings. */
|
|
247
|
+
export function trustDiagnostics(root, loaded, status = trustStatus(root, loaded)) {
|
|
248
|
+
const severity = loaded.config.trust === 'warn' ? 'warning' : 'error'
|
|
249
|
+
const rel = loaded.file ? posix(relative(root, loaded.file)) : 'kerf.config.ts'
|
|
250
|
+
const text = loaded.file ? readFileSync(loaded.file, 'utf8') : ''
|
|
251
|
+
const out = []
|
|
252
|
+
const report = (code, name, why, help) =>
|
|
253
|
+
out.push(
|
|
254
|
+
diagnostic({ code, severity, owner: name, file: rel, why, help, ...locate(text, name) }),
|
|
255
|
+
)
|
|
256
|
+
if (status.broken)
|
|
257
|
+
out.push(
|
|
258
|
+
diagnostic({
|
|
259
|
+
code: 'P110',
|
|
260
|
+
severity,
|
|
261
|
+
file: lockFile,
|
|
262
|
+
why: `${lockFile} can't be read: ${status.broken}.`,
|
|
263
|
+
help: 'Fix it, or delete it and approve each plugin again.',
|
|
264
|
+
}),
|
|
265
|
+
)
|
|
266
|
+
for (const p of status.plugins) {
|
|
267
|
+
const approve = `Review it, then run \`kerf plugins approve ${p.name}\`.`
|
|
268
|
+
if (p.added.length)
|
|
269
|
+
report(
|
|
270
|
+
'P109',
|
|
271
|
+
p.name,
|
|
272
|
+
`\`${p.name}\` asks for permissions the lock doesn't give it: ${p.added.join(', ')}.`,
|
|
273
|
+
approve,
|
|
274
|
+
)
|
|
275
|
+
if (p.hidden.length)
|
|
276
|
+
report(
|
|
277
|
+
'P109',
|
|
278
|
+
p.name,
|
|
279
|
+
`\`${p.name}\` uses ${p.hidden.join(', ')} without declaring it (${p.uses.find((u) => p.hidden.includes(u.need))?.file ?? 'see `kerf plugins audit`'}).`,
|
|
280
|
+
`Have it declared, or review it and run \`kerf plugins approve ${p.name}\`.`,
|
|
281
|
+
)
|
|
282
|
+
if (p.integrityChanged)
|
|
283
|
+
report(
|
|
284
|
+
'P110',
|
|
285
|
+
p.name,
|
|
286
|
+
`\`${p.name}\`${p.lockedVersion && p.lockedVersion !== p.version ? ` went from ${p.lockedVersion} to ${p.version} and its` : `'s installed`} code isn't what the lock recorded.`,
|
|
287
|
+
`Reinstall the locked version, or review the update and run \`kerf plugins approve ${p.name}\`.`,
|
|
288
|
+
)
|
|
289
|
+
}
|
|
290
|
+
return out
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Stops `kerf dev` and `kerf build` before a blocked plugin's build hooks run. */
|
|
294
|
+
export function requireTrust(root, loaded) {
|
|
295
|
+
if (loaded.diagnostics.some((d) => d.code === 'P007')) return
|
|
296
|
+
const blocked = trustDiagnostics(root, loaded).filter((d) => d.severity === 'error')
|
|
297
|
+
if (blocked.length)
|
|
298
|
+
throw refused(blocked.map((d) => `${d.code} ${d.why} ${d.fix.text}`).join('\n'))
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** `kerf plugins audit --json`. */
|
|
302
|
+
export function auditCommand(root, loaded) {
|
|
303
|
+
const status = trustStatus(root, loaded, { full: true })
|
|
304
|
+
const diagnostics = trustDiagnostics(root, loaded, status)
|
|
305
|
+
return {
|
|
306
|
+
ok: !diagnostics.some((d) => d.severity === 'error'),
|
|
307
|
+
...status,
|
|
308
|
+
diagnostics: [...loaded.diagnostics, ...diagnostics],
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** `.kerf/` in a .gitignore hides the lock, and a lock nobody commits protects nobody. */
|
|
313
|
+
function unignoreLock(root) {
|
|
314
|
+
const file = join(root, '.gitignore')
|
|
315
|
+
if (!existsSync(file)) return false
|
|
316
|
+
const text = readFileSync(file, 'utf8')
|
|
317
|
+
const next = text.replace(/^\/?\.kerf\/?$/m, `.kerf/*\n!${lockFile}`)
|
|
318
|
+
if (next === text) return false
|
|
319
|
+
writeFileSync(file, next)
|
|
320
|
+
return true
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* `kerf plugins approve`: names, or `all`. With neither it only shows what's waiting, so approving is always something
|
|
325
|
+
* said out loud, one plugin at a time or all at once.
|
|
326
|
+
*/
|
|
327
|
+
export function approveCommand(root, loaded, { names = [], all = false } = {}) {
|
|
328
|
+
const failed = loaded.diagnostics.find((d) => d.code === 'P007')
|
|
329
|
+
if (failed) throw refused(`The config doesn't load, so nothing can be approved: ${failed.why}`)
|
|
330
|
+
const status = trustStatus(root, loaded, { full: true })
|
|
331
|
+
if (status.broken)
|
|
332
|
+
throw refused(
|
|
333
|
+
`${lockFile} can't be read: ${status.broken}. Fix it, or delete it and approve each plugin again.`,
|
|
334
|
+
)
|
|
335
|
+
const pending = status.plugins.filter((p) => p.blocked)
|
|
336
|
+
const unknown = names.filter((n) => !status.plugins.some((p) => p.name === n))
|
|
337
|
+
if (unknown.length)
|
|
338
|
+
throw Object.assign(new Error(`not in \`plugins\`: ${unknown.join(', ')}`), { code: 'E_USAGE' })
|
|
339
|
+
if (!all && !names.length)
|
|
340
|
+
return {
|
|
341
|
+
ok: !pending.length,
|
|
342
|
+
written: false,
|
|
343
|
+
lock: lockFile,
|
|
344
|
+
approved: [],
|
|
345
|
+
pending,
|
|
346
|
+
stale: status.stale,
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const chosen = status.plugins.filter((p) => all || names.includes(p.name))
|
|
350
|
+
const entries = { ...readLock(root).plugins }
|
|
351
|
+
if (all) for (const name of status.stale) delete entries[name]
|
|
352
|
+
for (const p of chosen)
|
|
353
|
+
entries[p.name] = {
|
|
354
|
+
version: p.version,
|
|
355
|
+
integrity: p.integrity,
|
|
356
|
+
permissions: p.permissions,
|
|
357
|
+
provides: p.provides,
|
|
358
|
+
undeclared: p.undeclared,
|
|
359
|
+
}
|
|
360
|
+
const ordered = Object.fromEntries(
|
|
361
|
+
Object.keys(entries)
|
|
362
|
+
.sort()
|
|
363
|
+
.map((name) => [name, entries[name]]),
|
|
364
|
+
)
|
|
365
|
+
mkdirSync(join(root, '.kerf'), { recursive: true })
|
|
366
|
+
writeFileSync(
|
|
367
|
+
join(root, lockFile),
|
|
368
|
+
`${JSON.stringify({ version: 1, plugins: ordered }, null, 2)}\n`,
|
|
369
|
+
)
|
|
370
|
+
return {
|
|
371
|
+
ok: true,
|
|
372
|
+
written: true,
|
|
373
|
+
lock: lockFile,
|
|
374
|
+
approved: chosen,
|
|
375
|
+
pending: pending.filter((p) => !chosen.includes(p)),
|
|
376
|
+
stale: all ? [] : status.stale,
|
|
377
|
+
gitignore: unignoreLock(root),
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** One line per plugin for the terminal. */
|
|
382
|
+
export function formatTrust(p) {
|
|
383
|
+
const parts = [
|
|
384
|
+
...p.added.map((g) => `+${g}`),
|
|
385
|
+
...p.removed.map((g) => `-${g}`),
|
|
386
|
+
...p.hidden.map((n) => `undeclared ${n}`),
|
|
387
|
+
...(p.integrityChanged
|
|
388
|
+
? [
|
|
389
|
+
`code changed${p.lockedVersion && p.lockedVersion !== p.version ? ` (${p.lockedVersion} → ${p.version})` : ''}`,
|
|
390
|
+
]
|
|
391
|
+
: []),
|
|
392
|
+
]
|
|
393
|
+
return `${p.blocked ? '!' : ' '} ${p.name}@${p.version ?? '?'} ${parts.join(' ') || (p.locked ? 'approved' : 'not locked, asks for nothing')}`
|
|
394
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// Scratch copies of a project for the gate, and vitest runs in them. A copy lives in `<root>/.kerf/gate/<id>`, so
|
|
2
|
+
// node_modules hoisted above the project still resolve; the project's own node_modules dirs are symlinked, not copied.
|
|
3
|
+
|
|
4
|
+
import { spawn } from 'node:child_process'
|
|
5
|
+
import {
|
|
6
|
+
existsSync,
|
|
7
|
+
lstatSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
readdirSync,
|
|
10
|
+
readFileSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
rmSync,
|
|
13
|
+
symlinkSync,
|
|
14
|
+
writeFileSync,
|
|
15
|
+
copyFileSync,
|
|
16
|
+
} from 'node:fs'
|
|
17
|
+
import { createRequire } from 'node:module'
|
|
18
|
+
import { dirname, join, relative } from 'node:path'
|
|
19
|
+
|
|
20
|
+
const skipDirs = new Set(['.git', '.kerf', 'dist', 'coverage', '.turbo', 'target'])
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Copy `root` into a new scratch dir. `exclude(rel)` leaves a path out (relative to root, posix).
|
|
24
|
+
* @returns {string} the copy's real path
|
|
25
|
+
*/
|
|
26
|
+
export function copyProject(
|
|
27
|
+
root,
|
|
28
|
+
{ exclude = () => false, id = Math.random().toString(36).slice(2, 8) } = {},
|
|
29
|
+
) {
|
|
30
|
+
const dest = join(root, '.kerf', 'gate', `${process.pid}-${id}`)
|
|
31
|
+
rmSync(dest, { recursive: true, force: true })
|
|
32
|
+
mkdirSync(dest, { recursive: true })
|
|
33
|
+
const go = (from, to, rel) => {
|
|
34
|
+
for (const e of readdirSync(from, { withFileTypes: true })) {
|
|
35
|
+
const r = rel ? `${rel}/${e.name}` : e.name
|
|
36
|
+
const src = join(from, e.name)
|
|
37
|
+
if (e.name === 'node_modules') {
|
|
38
|
+
symlinkSync(realpathSync(src), join(to, e.name))
|
|
39
|
+
continue
|
|
40
|
+
}
|
|
41
|
+
if (exclude(r)) continue
|
|
42
|
+
if (e.isDirectory()) {
|
|
43
|
+
if (skipDirs.has(e.name)) continue
|
|
44
|
+
mkdirSync(join(to, e.name))
|
|
45
|
+
go(src, join(to, e.name), r)
|
|
46
|
+
} else if (e.isFile()) copyFileSync(src, join(to, e.name))
|
|
47
|
+
else if (e.isSymbolicLink()) {
|
|
48
|
+
try {
|
|
49
|
+
symlinkSync(realpathSync(src), join(to, e.name))
|
|
50
|
+
} catch {}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
go(root, dest, '')
|
|
55
|
+
return realpathSync(dest)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Write (or with `null`, delete) files in a copy. */
|
|
59
|
+
export function writeFiles(dir, files) {
|
|
60
|
+
for (const [rel, text] of Object.entries(files)) {
|
|
61
|
+
const abs = join(dir, rel)
|
|
62
|
+
if (text == null) rmSync(abs, { force: true })
|
|
63
|
+
else {
|
|
64
|
+
mkdirSync(dirname(abs), { recursive: true })
|
|
65
|
+
writeFileSync(abs, text)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function removeCopy(dir) {
|
|
71
|
+
rmSync(dir, { recursive: true, force: true })
|
|
72
|
+
// Leave `.kerf/gate` behind only when something else is in it.
|
|
73
|
+
const gateDir = dirname(dir)
|
|
74
|
+
try {
|
|
75
|
+
if (!readdirSync(gateDir).length) rmSync(gateDir, { recursive: true })
|
|
76
|
+
} catch {}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The vitest CLI the project would run: its own, else the CLI's. */
|
|
80
|
+
export function vitestBin(root) {
|
|
81
|
+
for (const req of [createRequire(join(root, 'package.json')), createRequire(import.meta.url)]) {
|
|
82
|
+
try {
|
|
83
|
+
let dir = dirname(req.resolve('vitest'))
|
|
84
|
+
while (
|
|
85
|
+
!existsSync(join(dir, 'package.json')) ||
|
|
86
|
+
!/"name":\s*"vitest"/.test(readFileSync(join(dir, 'package.json'), 'utf8'))
|
|
87
|
+
) {
|
|
88
|
+
if (dirname(dir) === dir) throw new Error('no vitest')
|
|
89
|
+
dir = dirname(dir)
|
|
90
|
+
}
|
|
91
|
+
return join(dir, 'vitest.mjs')
|
|
92
|
+
} catch {}
|
|
93
|
+
}
|
|
94
|
+
throw Object.assign(new Error('vitest is not installed in this project'), { code: 'E_NO_VITEST' })
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Run vitest on `files` (relative to `dir`) and read its JSON report.
|
|
99
|
+
* @returns {Promise<{ tests: Map<string, { status: string, message?: string }>, loadErrors: Map<string, string>, timedOut: boolean, ms: number, stderr: string }>}
|
|
100
|
+
* `tests` is keyed `<file>::<suite > test>`; `loadErrors` by file, for files that failed before running a test.
|
|
101
|
+
*/
|
|
102
|
+
export function runVitest(dir, files, { bin, timeoutMs = 120_000, config } = {}) {
|
|
103
|
+
const report = join(dir, `.kerf-report-${Math.random().toString(36).slice(2, 8)}.json`)
|
|
104
|
+
const args = [
|
|
105
|
+
bin,
|
|
106
|
+
'run',
|
|
107
|
+
'--root',
|
|
108
|
+
dir,
|
|
109
|
+
'--reporter=json',
|
|
110
|
+
`--outputFile=${report}`,
|
|
111
|
+
'--passWithNoTests',
|
|
112
|
+
...(config ? ['--config', config] : []),
|
|
113
|
+
...files,
|
|
114
|
+
]
|
|
115
|
+
const started = performance.now()
|
|
116
|
+
return new Promise((resolve) => {
|
|
117
|
+
const child = spawn(process.execPath, args, {
|
|
118
|
+
cwd: dir,
|
|
119
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
120
|
+
env: { ...process.env, FORCE_COLOR: '0', CI: '1' },
|
|
121
|
+
detached: true,
|
|
122
|
+
})
|
|
123
|
+
let stderr = ''
|
|
124
|
+
child.stderr.on('data', (d) => {
|
|
125
|
+
if (stderr.length < 20_000) stderr += d
|
|
126
|
+
})
|
|
127
|
+
let timedOut = false
|
|
128
|
+
const timer = setTimeout(() => {
|
|
129
|
+
timedOut = true
|
|
130
|
+
try {
|
|
131
|
+
if (child.pid !== undefined) process.kill(-child.pid, 'SIGKILL')
|
|
132
|
+
} catch {}
|
|
133
|
+
}, timeoutMs)
|
|
134
|
+
child.on('close', () => {
|
|
135
|
+
clearTimeout(timer)
|
|
136
|
+
const tests = new Map(),
|
|
137
|
+
loadErrors = new Map()
|
|
138
|
+
if (!timedOut && existsSync(report)) {
|
|
139
|
+
let data
|
|
140
|
+
try {
|
|
141
|
+
data = JSON.parse(readFileSync(report, 'utf8'))
|
|
142
|
+
} catch {}
|
|
143
|
+
rmSync(report, { force: true })
|
|
144
|
+
for (const f of data?.testResults ?? []) {
|
|
145
|
+
const file = relative(dir, safeReal(f.name)).split('\\').join('/')
|
|
146
|
+
if (!f.assertionResults.length) {
|
|
147
|
+
loadErrors.set(file, firstLine(f.message) || 'the file failed to load')
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
for (const a of f.assertionResults)
|
|
151
|
+
tests.set(`${file}::${[...a.ancestorTitles, a.title].join(' > ')}`, {
|
|
152
|
+
status: a.status,
|
|
153
|
+
message: firstLine(a.failureMessages?.[0]),
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
resolve({ tests, loadErrors, timedOut, ms: Math.round(performance.now() - started), stderr })
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const safeReal = (p) => {
|
|
163
|
+
try {
|
|
164
|
+
return realpathSync(p)
|
|
165
|
+
} catch {
|
|
166
|
+
return p
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const firstLine = (s) =>
|
|
170
|
+
s
|
|
171
|
+
?.split('\n')
|
|
172
|
+
.find((l) => l.trim())
|
|
173
|
+
?.trim()
|
|
174
|
+
.slice(0, 300)
|
|
175
|
+
|
|
176
|
+
export const isDir = (p) => {
|
|
177
|
+
try {
|
|
178
|
+
return lstatSync(p).isDirectory()
|
|
179
|
+
} catch {
|
|
180
|
+
return false
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const { Engine } = require('../src/infer/engine.cjs')
|
|
2
|
+
const { languageHost } = require('../src/infer/hosts.cjs')
|
|
3
|
+
|
|
4
|
+
function createContext(ts, info, ls) {
|
|
5
|
+
const lsHost = info.languageServiceHost
|
|
6
|
+
const realSnapshot = lsHost.getScriptSnapshot.bind(lsHost)
|
|
7
|
+
const realVersion = lsHost.getScriptVersion.bind(lsHost)
|
|
8
|
+
const textOf = (f) => {
|
|
9
|
+
const s = realSnapshot(f)
|
|
10
|
+
return s && s.getText(0, s.getLength())
|
|
11
|
+
}
|
|
12
|
+
const engine = new Engine({ ts, read: textOf })
|
|
13
|
+
const gen = new Map()
|
|
14
|
+
const seen = new Map()
|
|
15
|
+
const sources = new Map()
|
|
16
|
+
|
|
17
|
+
lsHost.getScriptSnapshot = (f) => {
|
|
18
|
+
const m = engine.maps.get(f)
|
|
19
|
+
return m ? ts.ScriptSnapshot.fromString(m.text) : realSnapshot(f)
|
|
20
|
+
}
|
|
21
|
+
lsHost.getScriptVersion = (f) => `${realVersion(f)}.${gen.get(f) ?? 0}`
|
|
22
|
+
// tsserver only syncs a dirty project, so a new shadow has to dirty it.
|
|
23
|
+
const host = languageHost(
|
|
24
|
+
ts,
|
|
25
|
+
() => ls,
|
|
26
|
+
(files) => {
|
|
27
|
+
for (const f of files.keys()) gen.set(f, (gen.get(f) ?? 0) + 1)
|
|
28
|
+
info.project.markAsDirty()
|
|
29
|
+
},
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
let busy = false
|
|
33
|
+
function ensure() {
|
|
34
|
+
if (busy) return
|
|
35
|
+
let changed = false
|
|
36
|
+
for (const f of lsHost.getScriptFileNames()) {
|
|
37
|
+
if (!f.endsWith('.tsx')) continue
|
|
38
|
+
const v = realVersion(f)
|
|
39
|
+
if (seen.get(f) === v) continue
|
|
40
|
+
seen.set(f, v)
|
|
41
|
+
if (engine.set(f, textOf(f))) changed = true
|
|
42
|
+
}
|
|
43
|
+
if (!changed) return
|
|
44
|
+
busy = true
|
|
45
|
+
try {
|
|
46
|
+
engine.run(host)
|
|
47
|
+
} catch (e) {
|
|
48
|
+
info.project.projectService.logger.info(`kerf props: ${e.stack}`)
|
|
49
|
+
} finally {
|
|
50
|
+
busy = false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { engine, textOf, ensure, sources }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = { createContext }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// A tsserver plugin: the editor's language service sees each file's shadow, and every position in and out is mapped, so
|
|
2
|
+
// inferred props hover, complete and report at the source. TypeScript 7 doesn't load plugins and ignores the entry in
|
|
3
|
+
// tsconfig without a word, which is safe: research 44 §6 says what an editor on tsgo gets instead.
|
|
4
|
+
|
|
5
|
+
const { createContext } = require('./context.js')
|
|
6
|
+
const { createProxy } = require('./proxy.js')
|
|
7
|
+
|
|
8
|
+
function init({ typescript: ts }) {
|
|
9
|
+
function create(info) {
|
|
10
|
+
const ls = info.languageService
|
|
11
|
+
const context = createContext(ts, info, ls)
|
|
12
|
+
return createProxy(ts, ls, context)
|
|
13
|
+
}
|
|
14
|
+
return { create }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = init
|