@sssb/tools 0.0.0 → 0.3.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 +21 -0
- package/bin/sssb.js +116 -0
- package/package.json +89 -5
- package/src/app-port.js +77 -0
- package/src/app.js +142 -0
- package/src/assembly-checks.d.ts +57 -0
- package/src/assembly-checks.js +259 -0
- package/src/commands/add-app.js +344 -0
- package/src/commands/check-demo-size.js +80 -0
- package/src/commands/check-doc-paths.js +48 -0
- package/src/commands/check-e2e.js +611 -0
- package/src/commands/check-fences.js +51 -0
- package/src/commands/check-real-env.js +165 -0
- package/src/commands/check-tmpdir-leak.js +78 -0
- package/src/commands/ci.js +350 -0
- package/src/commands/dev-tick.js +25 -0
- package/src/commands/doctor-checks.js +1396 -0
- package/src/commands/doctor-settings.js +583 -0
- package/src/commands/doctor-vitest.js +262 -0
- package/src/commands/doctor.js +1033 -0
- package/src/commands/e2e-profile.js +94 -0
- package/src/commands/guide.js +108 -0
- package/src/commands/launch-json.js +55 -0
- package/src/commands/pin.js +411 -0
- package/src/commands/port.js +22 -0
- package/src/commands/remove-items.js +191 -0
- package/src/commands/serve-standalone.js +78 -0
- package/src/commands/tick.js +27 -0
- package/src/doc-paths.d.ts +106 -0
- package/src/doc-paths.js +328 -0
- package/src/framework-guard.js +35 -0
- package/src/kit.d.ts +49 -0
- package/src/kit.js +216 -0
- package/src/next.d.ts +16 -0
- package/src/next.js +55 -0
- package/src/ports.d.ts +72 -0
- package/src/ports.js +307 -0
- package/src/prune-mantine-css.js +171 -0
- package/src/remove-items/code.patch +798 -0
- package/src/remove-items/database.patch +218 -0
- package/src/remove-items/exact-patch.js +122 -0
- package/src/testing/tmp-dir-marker.js +9 -0
- package/src/testing/tmp-dir-setup.d.ts +2 -0
- package/src/testing/tmp-dir-setup.js +9 -0
- package/src/testing/tmp-dir.d.ts +20 -0
- package/src/testing/tmp-dir.js +174 -0
- package/src/tick/dev-tick.js +128 -0
- package/src/tick/tick.js +317 -0
- package/src/tick/ticker-marker.d.ts +17 -0
- package/src/tick/ticker-marker.js +83 -0
- package/src/tick/ticker.d.ts +36 -0
- package/src/tick/ticker.js +142 -0
- package/src/vite.d.ts +38 -0
- package/src/vite.js +64 -0
- package/src/vitest.d.ts +38 -0
- package/src/vitest.js +82 -0
- package/tsconfig.base.json +33 -0
- package/README.md +0 -3
package/src/doc-paths.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// WHICH MARKDOWN A REPOSITORY HOLDS TO ITS PATH CITATIONS, and the parser that reads them. Nothing here names this
|
|
3
|
+
// repository: the roots, the doc set, the exemptions and the reference apps are the adopter's `sssb.docPaths` in the
|
|
4
|
+
// root package.json, so the gate is theirs the day the framework leaves their tree.
|
|
5
|
+
//
|
|
6
|
+
// Two callers need the same answer and must never drift apart: the `check-doc-paths` gate, which fails the build
|
|
7
|
+
// when a doctrine doc cites a path that does not exist, and an adoption rewriter, which repoints those citations
|
|
8
|
+
// when an app is renamed or ejected. A second list would mean the rewriter could miss a file the gate checks, a
|
|
9
|
+
// broken gate on the adopter's first command.
|
|
10
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Build outputs are never checked. A doc may legitimately name one (the static demo's `dist-demo/index.html` is the
|
|
15
|
+
* artifact you hand someone) but whether it exists on disk depends on whether you happened to run a build, which
|
|
16
|
+
* would make the gate pass locally and fail on a clean checkout. A gate whose verdict depends on build state is not
|
|
17
|
+
* a gate.
|
|
18
|
+
*/
|
|
19
|
+
const DEFAULT_BUILD_OUTPUTS = [
|
|
20
|
+
'dist-demo',
|
|
21
|
+
'.next',
|
|
22
|
+
'coverage',
|
|
23
|
+
'playwright-report',
|
|
24
|
+
'test-results',
|
|
25
|
+
'node_modules',
|
|
26
|
+
'.data',
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
/** Backtick-quoted spans, which is how a doc set cites every path. */
|
|
30
|
+
const BACKTICKED = /`([^`\n]+)`/g
|
|
31
|
+
|
|
32
|
+
/** The `apps/<name>/` prefix of a citation, with globs and metavariables already excluded. */
|
|
33
|
+
const APP_PREFIX = /^apps\/[^/<*{]+\//
|
|
34
|
+
|
|
35
|
+
/** @param {unknown} value @returns {value is string[]} */
|
|
36
|
+
const isStringList = (value) => Array.isArray(value) && value.every((entry) => typeof entry === 'string')
|
|
37
|
+
|
|
38
|
+
/** @type {import('./doc-paths.d.ts').readDocPathsConfig} */
|
|
39
|
+
export function readDocPathsConfig(root) {
|
|
40
|
+
const manifest = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8'))
|
|
41
|
+
const block = manifest?.sssb?.docPaths
|
|
42
|
+
if (block === undefined)
|
|
43
|
+
throw new Error(`sssb: ${root}/package.json has no \`sssb.docPaths\`: the doc-paths gate needs it`)
|
|
44
|
+
if (typeof block !== 'object' || block === null || Array.isArray(block))
|
|
45
|
+
throw new Error('sssb: `sssb.docPaths` in the root package.json must be an object')
|
|
46
|
+
for (const key of ['roots', 'include', 'exempt']) {
|
|
47
|
+
if (!isStringList(block[key])) throw new Error(`sssb: \`sssb.docPaths.${key}\` must be a list of strings`)
|
|
48
|
+
}
|
|
49
|
+
for (const key of ['referenceApps', 'buildOutputs']) {
|
|
50
|
+
if (block[key] !== undefined && !isStringList(block[key]))
|
|
51
|
+
throw new Error(`sssb: \`sssb.docPaths.${key}\` must be a list of strings when present`)
|
|
52
|
+
}
|
|
53
|
+
const bases = block.relativeTo
|
|
54
|
+
if (
|
|
55
|
+
bases !== undefined &&
|
|
56
|
+
(typeof bases !== 'object' ||
|
|
57
|
+
bases === null ||
|
|
58
|
+
Array.isArray(bases) ||
|
|
59
|
+
!Object.values(bases).every((base) => typeof base === 'string'))
|
|
60
|
+
)
|
|
61
|
+
throw new Error('sssb: `sssb.docPaths.relativeTo` must map doc paths to directories (strings) when present')
|
|
62
|
+
return block
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} root
|
|
67
|
+
* @param {string} rel
|
|
68
|
+
* @param {import('./doc-paths.d.ts').DocPathsConfig} config
|
|
69
|
+
* @param {string[]} out
|
|
70
|
+
*/
|
|
71
|
+
function collectDocs(root, rel, config, out) {
|
|
72
|
+
for (const entry of readdirSync(path.join(root, rel)).sort()) {
|
|
73
|
+
const child = rel === '' ? entry : `${rel}/${entry}`
|
|
74
|
+
if (config.exempt.some((exempt) => child.startsWith(exempt))) continue
|
|
75
|
+
if (statSync(path.join(root, child)).isDirectory()) collectDocs(root, child, config, out)
|
|
76
|
+
else if (path.extname(child) === '.md') out.push(child)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* `text` without its leading and trailing characters that are in `chars`. A loop, not `/^x+|x+$/` or `/x+$/`: a
|
|
82
|
+
* backtracking regex anchored at the end retries from every position of a long run of `x` that is followed by
|
|
83
|
+
* anything else, so it takes time quadratic in the run's length, and the text here comes from the docs and the
|
|
84
|
+
* configuration a caller hands in.
|
|
85
|
+
* @param {string} text
|
|
86
|
+
* @param {string} chars
|
|
87
|
+
* @param {{ start?: boolean }} [options] `start: false` trims the end only
|
|
88
|
+
*/
|
|
89
|
+
function trimChars(text, chars, { start = true } = {}) {
|
|
90
|
+
let from = 0
|
|
91
|
+
let to = text.length
|
|
92
|
+
if (start) while (from < to && chars.includes(/** @type {string} */ (text[from]))) from++
|
|
93
|
+
while (to > from && chars.includes(/** @type {string} */ (text[to - 1]))) to--
|
|
94
|
+
return text.slice(from, to)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** `text` without its trailing slashes. @param {string} text */
|
|
98
|
+
const trimTrailingSlashes = (text) => trimChars(text, '/', { start: false })
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The root-level entries are listed rather than globbed because a repo root accumulates markdown that is NOT
|
|
102
|
+
* doctrine (a stray NOTES.md should not become a build gate). SECURITY.md earns its place in SSSB by citing the
|
|
103
|
+
* code that backs its claims, an RLS proof suite, a fail-closed guard.
|
|
104
|
+
*
|
|
105
|
+
* Be precise about what that buys: `resolves()` below keeps a FILE citation alive on its parent directory
|
|
106
|
+
* existing, so gating SECURITY.md catches a moved DIRECTORY, not a moved file. That is a deliberate trade (recipes
|
|
107
|
+
* cite files you are meant to create), and it is measured: 22 citations across SSSB's doc set resolve only by that
|
|
108
|
+
* fallback, nearly all of them legitimately unbuilt.
|
|
109
|
+
*
|
|
110
|
+
* @type {import('./doc-paths.d.ts').docFiles}
|
|
111
|
+
*/
|
|
112
|
+
export function docFiles(root, config) {
|
|
113
|
+
/** @type {string[]} */
|
|
114
|
+
const out = []
|
|
115
|
+
for (const entry of config.include) {
|
|
116
|
+
const rel = trimTrailingSlashes(entry)
|
|
117
|
+
if (config.exempt.some((exempt) => rel.startsWith(exempt))) continue
|
|
118
|
+
const abs = path.join(root, rel)
|
|
119
|
+
if (!existsSync(abs)) continue
|
|
120
|
+
if (statSync(abs).isDirectory()) collectDocs(root, rel, config, out)
|
|
121
|
+
else out.push(rel)
|
|
122
|
+
}
|
|
123
|
+
return out
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The `include` entries that name nothing in the tree. A typo there would silently shrink the gate (`"doc"` for
|
|
128
|
+
* `"docs"` checks no doc at all and passes), so the gate fails on one instead.
|
|
129
|
+
*
|
|
130
|
+
* @type {import('./doc-paths.d.ts').missingIncludes}
|
|
131
|
+
*/
|
|
132
|
+
export function missingIncludes(root, config) {
|
|
133
|
+
return config.include
|
|
134
|
+
.map(trimTrailingSlashes)
|
|
135
|
+
.filter((rel) => !config.exempt.some((exempt) => rel.startsWith(exempt)) && !existsSync(path.join(root, rel)))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The path-like tokens inside one backticked span.
|
|
140
|
+
*
|
|
141
|
+
* Usually the span IS the path, `apps/showcase/src/app-config`, and that stays one citation. But a doc set also
|
|
142
|
+
* cites paths INSIDE backticked commands, and such a span starts with `pnpm` or `git`, not with a repo root. Reading
|
|
143
|
+
* whole spans only, the gate could not see those at all, and two had rotted unnoticed: a `fixtures/llm/` and a
|
|
144
|
+
* `tests/e2e/a11y.spec.ts` that had both moved under `apps/showcase/`. A gate blind to a whole citation FORM is not
|
|
145
|
+
* a gate over that form.
|
|
146
|
+
*
|
|
147
|
+
* @param {string} raw
|
|
148
|
+
* @param {string[]} roots
|
|
149
|
+
* @returns {string[]}
|
|
150
|
+
*/
|
|
151
|
+
function pathTokens(raw, roots) {
|
|
152
|
+
if (roots.some((root) => raw.startsWith(root))) return [raw]
|
|
153
|
+
return raw
|
|
154
|
+
.split(/\s+/)
|
|
155
|
+
.map((token) =>
|
|
156
|
+
trimChars(
|
|
157
|
+
trimChars(token, `'"`) // `pnpm --filter './apps/*'`
|
|
158
|
+
.replace(/^\.\//, ''), // ./apps/... is the same path as apps/...
|
|
159
|
+
',;)',
|
|
160
|
+
{ start: false },
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
.filter((token) => roots.some((root) => token.startsWith(root)))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** @type {import('./doc-paths.d.ts').citationsIn} */
|
|
167
|
+
export function citationsIn(text, config) {
|
|
168
|
+
const buildOutputs = config.buildOutputs ?? DEFAULT_BUILD_OUTPUTS
|
|
169
|
+
/** @type {import('./doc-paths.d.ts').Citation[]} */
|
|
170
|
+
const found = []
|
|
171
|
+
text.split('\n').forEach((lineText, index) => {
|
|
172
|
+
for (const match of lineText.matchAll(BACKTICKED)) {
|
|
173
|
+
const raw = (match[1] ?? '').trim()
|
|
174
|
+
for (const token of pathTokens(raw, config.roots)) {
|
|
175
|
+
// A trailing `:12` (or `:12:5`) is a LINE REFERENCE into the file, not part of its name:
|
|
176
|
+
// `docs/build-notes.md:52` asserts that document exists, and read literally it asserts a file
|
|
177
|
+
// nothing could ever create.
|
|
178
|
+
const located = token.replace(/:\d+(?::\d+)?$/, '')
|
|
179
|
+
// A glob, a brace expansion or a `<metavariable>` placeholder only asserts its static prefix
|
|
180
|
+
// exists: `packages/kit/src/adapters/<real|fake>/<name>.ts` is a template, and
|
|
181
|
+
// `messages/{en,es}.json` names two files, neither of them spelled that way.
|
|
182
|
+
const stop = ['*', '<', '{'].map((char) => located.indexOf(char)).filter((i) => i >= 0)
|
|
183
|
+
const stripped = stop.length ? located.slice(0, Math.min(...stop)) : located
|
|
184
|
+
const cited = trimTrailingSlashes(stripped)
|
|
185
|
+
if (!cited) continue
|
|
186
|
+
if (cited.split('/').some((segment) => buildOutputs.includes(segment))) continue
|
|
187
|
+
found.push({ path: cited, raw, line: index + 1 })
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
return found
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A doc named in `relativeTo` cites paths relative to a directory, not to the root: the app guide `@sssb/kit` ships
|
|
196
|
+
* says `src/app-config/` and `config/params.ts` because an adopter reads it about their own app, so SSSB checks it
|
|
197
|
+
* against `apps/starter`. Its citations come back joined onto that directory, so `resolves`, the reference-app rule
|
|
198
|
+
* and every message see the path as it sits in this tree. In such a doc EVERY citation is relative to the directory,
|
|
199
|
+
* so an `apps/…` or `packages/…` path in it fails, which is right: an adopter's app holds neither.
|
|
200
|
+
*
|
|
201
|
+
* @type {import('./doc-paths.d.ts').citedPaths}
|
|
202
|
+
*/
|
|
203
|
+
export function citedPaths(file, config, root) {
|
|
204
|
+
const found = citationsIn(readFileSync(path.resolve(root, file), 'utf8'), config)
|
|
205
|
+
const relative = config.relativeTo?.[file.replace(/^\.\//, '')]
|
|
206
|
+
if (relative === undefined) return found
|
|
207
|
+
const base = trimTrailingSlashes(relative)
|
|
208
|
+
return found.map((citation) => ({ ...citation, path: path.posix.join(base, citation.path) }))
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The reference apps a citation may name while absent, as a regex built from the config; null when none are
|
|
213
|
+
* configured, in which case the rule never applies.
|
|
214
|
+
*
|
|
215
|
+
* @param {import('./doc-paths.d.ts').DocPathsConfig} config
|
|
216
|
+
*/
|
|
217
|
+
function referenceAppPattern(config) {
|
|
218
|
+
const apps = config.referenceApps ?? []
|
|
219
|
+
if (apps.length === 0) return null
|
|
220
|
+
const names = apps.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
221
|
+
return new RegExp(`^apps/(${names.join('|')})(?:/|$)`)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* SSSB's two reference apps, which a doc may cite by name: every scaffold command points at `apps/showcase/...` for
|
|
226
|
+
* its worked example, and the adoption guide describes `apps/starter`.
|
|
227
|
+
*
|
|
228
|
+
* In the TEMPLATE both exist, so a citation of either is checked like any other and a broken one fails. In an
|
|
229
|
+
* ADOPTED tree they usually do not: a forked adopter deleted the showcase and renamed the starter, and a workspace
|
|
230
|
+
* `@sssb/create` writes has neither. The adoption script that came before the generator repointed the citations it
|
|
231
|
+
* found on the day it ran, but an upstream merge (ADR-0013) later brought in NEW text that named them again, and the
|
|
232
|
+
* gate went red on a citation nobody in the adopter's repo wrote, the first time a recipe cited the starter's
|
|
233
|
+
* sign-in page.
|
|
234
|
+
*
|
|
235
|
+
* So a citation under `apps/<reference app>/` whose app is not in this tree is a reference to SSSB's own app, which
|
|
236
|
+
* cannot be checked here and is not this repo's to fix. It resolves.
|
|
237
|
+
*
|
|
238
|
+
* "In this tree" means the app's `package.json` exists, NOT that its directory does. A merge adds any new file
|
|
239
|
+
* upstream puts under `apps/showcase/` even after you deleted the app (git reports a conflict for a file you
|
|
240
|
+
* deleted and upstream edited, and silently resurrects one that is new), so a directory holding a single stray file
|
|
241
|
+
* would otherwise switch this rule off and turn the gate red on the next doc. A local `apps/starter/node_modules`
|
|
242
|
+
* left behind by a rename does the same. The package manifest is what makes it an app, and a stray file cannot fake
|
|
243
|
+
* it. Everything else stays strict: the adopter's own app (`apps/console/...`), a path in `packages/kit`, and either
|
|
244
|
+
* reference app for as long as it is present. The reference apps are a closed list on purpose: an adopter who keeps
|
|
245
|
+
* the name `starter` has the directory, so their own citations of it are still checked.
|
|
246
|
+
*
|
|
247
|
+
* Here, and not in the test, so a rewriter that asks `resolves` which citations are gone before it generalizes them
|
|
248
|
+
* to `apps/<app>/...` (`generalizeAppCitations`) and the gate cannot disagree.
|
|
249
|
+
*
|
|
250
|
+
* @param {string} cited
|
|
251
|
+
* @param {string} root
|
|
252
|
+
* @param {import('./doc-paths.d.ts').DocPathsConfig} config
|
|
253
|
+
*/
|
|
254
|
+
function namesAbsentReferenceApp(cited, root, config) {
|
|
255
|
+
const app = referenceAppPattern(config)?.exec(cited)?.[1]
|
|
256
|
+
return app !== undefined && !existsSync(path.join(root, 'apps', app, 'package.json'))
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* A directory citation must resolve outright. A FILE citation may also pass on its parent directory existing,
|
|
261
|
+
* because a doc may legitimately name a file you are meant to create, as every recipe does
|
|
262
|
+
* (`packages/kit/src/adapters/real/esign.ts` is the recipe's output, not a file in the tree).
|
|
263
|
+
*
|
|
264
|
+
* The asymmetry matters: allowing the parent fallback for directories too would let `src/app-confgi` pass on the
|
|
265
|
+
* strength of `src/` existing, which defeats the point.
|
|
266
|
+
*
|
|
267
|
+
* @type {import('./doc-paths.d.ts').resolves}
|
|
268
|
+
*/
|
|
269
|
+
export function resolves(cited, root, config) {
|
|
270
|
+
if (namesAbsentReferenceApp(cited, root, config)) return true
|
|
271
|
+
if (existsSync(path.join(root, cited))) return true
|
|
272
|
+
const looksLikeFile = path.basename(cited).includes('.')
|
|
273
|
+
return looksLikeFile && existsSync(path.join(root, path.dirname(cited)))
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Generalize every citation of an app path that `isMissing` reports as gone into the metavariable form
|
|
278
|
+
* `apps/<app>/...`, which the gate reads as asserting only that `apps/` exists.
|
|
279
|
+
*
|
|
280
|
+
* An adoption rewriter is the caller: after a rename or an eject, docs cite files the surviving app does not have
|
|
281
|
+
* (the scaffold docs teach `src/jobs/`, `src/domain/`, `src/app/api/` because the SHOWCASE has them). Those
|
|
282
|
+
* sentences never meant "this app", they meant "your app".
|
|
283
|
+
*
|
|
284
|
+
* ONE SPAN CAN CARRY SEVERAL CITATIONS, and that is the whole subtlety this function exists for. A backticked
|
|
285
|
+
* command, `cp apps/showcase/src/jobs/x.ts apps/showcase/src/domain/y.ts`, yields TWO citations sharing one `raw`.
|
|
286
|
+
* Rewriting them one at a time cannot work: the first rewrite means the original span no longer appears in the
|
|
287
|
+
* document, so every later citation in it is silently skipped (and `String.replace(string, ...)` only ever replaced
|
|
288
|
+
* the first occurrence anyway). The result was a half-rewritten command with a dangling path, a doc-path gate red
|
|
289
|
+
* on the adopter's FIRST command, the exact failure the shared list at the top of this file exists to prevent. So:
|
|
290
|
+
* group by span, apply every replacement the span needs, then substitute the span once. Longest path first, so a
|
|
291
|
+
* shorter citation can never eat the prefix of a longer one.
|
|
292
|
+
*
|
|
293
|
+
* @type {import('./doc-paths.d.ts').generalizeAppCitations}
|
|
294
|
+
*/
|
|
295
|
+
export function generalizeAppCitations(text, isMissing, config) {
|
|
296
|
+
/** @type {Map<string, string[]>} */
|
|
297
|
+
const bySpan = new Map()
|
|
298
|
+
for (const { path: cited, raw } of citationsIn(text, config)) {
|
|
299
|
+
if (!APP_PREFIX.test(cited) || !isMissing(cited)) continue
|
|
300
|
+
const paths = bySpan.get(raw) ?? []
|
|
301
|
+
if (!paths.includes(cited)) paths.push(cited)
|
|
302
|
+
bySpan.set(raw, paths)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
let out = text
|
|
306
|
+
for (const [raw, paths] of bySpan) {
|
|
307
|
+
let span = raw
|
|
308
|
+
for (const cited of [...paths].sort((a, b) => b.length - a.length)) {
|
|
309
|
+
span = span.split(cited).join(cited.replace(APP_PREFIX, 'apps/<app>/'))
|
|
310
|
+
}
|
|
311
|
+
if (span !== raw) out = out.split(`\`${raw}\``).join(`\`${span}\``)
|
|
312
|
+
}
|
|
313
|
+
return out
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** @type {import('./doc-paths.d.ts').docPathProblems} */
|
|
317
|
+
export function docPathProblems(root, config) {
|
|
318
|
+
/** @type {string[]} */
|
|
319
|
+
const problems = missingIncludes(root, config).map((rel) => `${rel}: in sssb.docPaths.include, but not in the tree`)
|
|
320
|
+
const files = docFiles(root, config)
|
|
321
|
+
if (files.length === 0) problems.push('sssb.docPaths.include names no markdown file: the gate would check nothing')
|
|
322
|
+
for (const file of files) {
|
|
323
|
+
for (const { path: cited, line } of citedPaths(file, config, root)) {
|
|
324
|
+
if (!resolves(cited, root, config)) problems.push(`${file}:${line} cites \`${cited}\``)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return problems
|
|
328
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The guard the Vite and Vitest presets add (ADR-0014, rule 4): every module the app's build or test run loads that is
|
|
3
|
+
// framework source must be the app's own @sssb/kit instance.
|
|
4
|
+
//
|
|
5
|
+
// The presets pass the app's seam explicitly (`@sssb/app-config` to its `src/app-config`), and the alias applies to
|
|
6
|
+
// every module in the project. So if anything the app imports (a workspace package, a library) brings a framework
|
|
7
|
+
// instance of its own, that second instance would run on THIS app's seam beside the app's, silently, as a second
|
|
8
|
+
// copy of every framework class: the case pnpm's layout makes fail loudly everywhere else (an instance with no seam
|
|
9
|
+
// cannot load). The guard turns it into an error naming both. Framework source is recognised by its package, not its
|
|
10
|
+
// path (kit.js, isFrameworkFile), so a copy of the framework's tree reached by a relative path is caught too.
|
|
11
|
+
import { existsSync } from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { assertNoForeignFramework } from './kit.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} appDir
|
|
17
|
+
* @returns {import('vite').Plugin}
|
|
18
|
+
*/
|
|
19
|
+
export function frameworkGuard(appDir) {
|
|
20
|
+
/** @type {Set<string>} */
|
|
21
|
+
const checked = new Set()
|
|
22
|
+
return {
|
|
23
|
+
name: 'sssb:framework-guard',
|
|
24
|
+
enforce: 'pre',
|
|
25
|
+
load(id) {
|
|
26
|
+
const file = id.split('?', 1)[0] ?? id
|
|
27
|
+
// Virtual modules (`\0…`, `virtual:…`) and ids that are not files have no package to judge.
|
|
28
|
+
if (checked.has(file) || !path.isAbsolute(file) || !existsSync(file)) return null
|
|
29
|
+
// Remembered only once it passes, so a foreign file fails every test file that loads it, not just the first.
|
|
30
|
+
assertNoForeignFramework(appDir, file)
|
|
31
|
+
checked.add(file)
|
|
32
|
+
return null
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/kit.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// THE SHARED RESOLVER of ADR-0014's rule 4, for any SSSB package that must reach the framework from an app (the
|
|
2
|
+
// CLI, the presets, @sssb/cdk's construct). Each throws an Error whose message names the app when it cannot answer.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The real directory of the `@sssb/kit` an app resolves: the first `node_modules/@sssb/kit` walking up from the app's
|
|
6
|
+
* directory, as Node's lookup finds it, never through NODE_PATH (which pnpm's bin shims point at the hoisted store).
|
|
7
|
+
*/
|
|
8
|
+
export declare function kitDirOf(appDir: string): string
|
|
9
|
+
|
|
10
|
+
/** The version in the package.json of the `@sssb/kit` an app resolves (`kitDirOf`). */
|
|
11
|
+
export declare function kitVersionOf(appDir: string): string
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The text of the release stamp, `dist/release.json`, in the `@sssb/kit` an app resolves: the commit the package was
|
|
15
|
+
* packed from. Null when it has none (SSSB's own source, a package packed before the stamp existed or by hand).
|
|
16
|
+
*/
|
|
17
|
+
export declare function kitReleaseStampOf(appDir: string): string | null
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The shared real-path check: `file` must be part of the `@sssb/kit` that `appDir` resolves. Throws for anything
|
|
21
|
+
* else (the hoisted copy, another app's instance, a framework source tree reached by a path). Returns the file's real
|
|
22
|
+
* path.
|
|
23
|
+
*/
|
|
24
|
+
export declare function assertAppsKitFile(appDir: string, file: string): string
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A published subpath of `@sssb/kit` (`i18n/request`, `adapters/real/migrate-handler`), resolved from the app's
|
|
28
|
+
* directory through the framework's `exports` map and checked with `assertAppsKitFile`. Returns the real path. Throws
|
|
29
|
+
* when the app has no framework, or when the framework does not publish the subpath.
|
|
30
|
+
*/
|
|
31
|
+
export declare function resolveAppsKit(appDir: string, subpath: string): string
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether `file` is framework source: its nearest named package.json is `@sssb/kit`'s, wherever it sits (an installed
|
|
35
|
+
* instance, a workspace link, or a copy of the framework's tree reached by a relative path).
|
|
36
|
+
*/
|
|
37
|
+
export declare function isFrameworkFile(file: string): boolean
|
|
38
|
+
|
|
39
|
+
/** Throws when `file` is framework source that is not the app's own instance; anything else passes. */
|
|
40
|
+
export declare function assertNoForeignFramework(appDir: string, file: string): void
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Another package the app installs, resolved from the app's directory (its own lookup chain, never NODE_PATH), so a
|
|
44
|
+
* preset configures the app's copy of a tool. Throws, saying what it is missing it for, when the app has none.
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveFromApp(appDir: string, specifier: string, why: string): string
|
|
47
|
+
|
|
48
|
+
/** Loads a package the app installs (see `resolveFromApp`) and returns its module namespace. */
|
|
49
|
+
export declare function importFromApp(appDir: string, specifier: string, why: string): Promise<Record<string, unknown>>
|
package/src/kit.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// THE ONE WAY @sssb/tools REACHES THE FRAMEWORK (ADR-0014, rule 4).
|
|
3
|
+
//
|
|
4
|
+
// @sssb/tools does not depend on @sssb/kit, and must never import it by a bare specifier: resolved from this
|
|
5
|
+
// package's own location, `@sssb/kit` finds whichever copy pnpm happened to hoist into `.pnpm/node_modules`,
|
|
6
|
+
// which in a workspace with two apps is the OTHER app's instance, running on the other app's seam. The
|
|
7
|
+
// independent review of ADR-0014 reproduced exactly that. So every tool that needs a framework file resolves it
|
|
8
|
+
// HERE, with the app's own directory as the base, and then runs the shared check: the real path of the file it
|
|
9
|
+
// got must sit inside the real directory of the `@sssb/kit` that the app itself resolves. The lint rule
|
|
10
|
+
// `sssb/no-bare-kit-import` keeps every other route out of this package's source.
|
|
11
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs'
|
|
12
|
+
import { builtinModules, createRequire } from 'node:module'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import { pathToFileURL } from 'node:url'
|
|
15
|
+
import { UsageError } from './app.js'
|
|
16
|
+
|
|
17
|
+
const KIT = '@sssb/kit'
|
|
18
|
+
|
|
19
|
+
/** @param {string} appDir */
|
|
20
|
+
function requireFrom(appDir) {
|
|
21
|
+
return createRequire(path.join(path.resolve(appDir), 'package.json'))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The real directory of the package `name` as Node's lexical lookup finds it from `fromDir`: the first
|
|
26
|
+
* `node_modules/<name>` walking up. Null when there is none. It never consults NODE_PATH (see kitDirOf).
|
|
27
|
+
* @param {string} fromDir
|
|
28
|
+
* @param {string} name
|
|
29
|
+
* @returns {string | null}
|
|
30
|
+
*/
|
|
31
|
+
function packageDirFrom(fromDir, name) {
|
|
32
|
+
for (let dir = path.resolve(fromDir); ; dir = path.dirname(dir)) {
|
|
33
|
+
const candidate = path.join(dir, 'node_modules', ...name.split('/'))
|
|
34
|
+
if (existsSync(path.join(candidate, 'package.json'))) return realpathSync(candidate)
|
|
35
|
+
if (path.dirname(dir) === dir) return null
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @param {string} specifier `@scope/name/sub` or `name/sub` */
|
|
40
|
+
const packageNameOf = (specifier) =>
|
|
41
|
+
specifier
|
|
42
|
+
.split('/')
|
|
43
|
+
.slice(0, specifier.startsWith('@') ? 2 : 1)
|
|
44
|
+
.join('/')
|
|
45
|
+
|
|
46
|
+
/** @type {Map<string, string | null>} the `name` of each directory's nearest named package.json */
|
|
47
|
+
const owningNames = new Map()
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The `name` in the nearest package.json at or above `dir` that has one (a nameless `{ "type": "module" }` is not a
|
|
51
|
+
* package), or null.
|
|
52
|
+
* @param {string} dir
|
|
53
|
+
* @returns {string | null}
|
|
54
|
+
*/
|
|
55
|
+
function owningPackageName(dir) {
|
|
56
|
+
const cached = owningNames.get(dir)
|
|
57
|
+
if (cached !== undefined) return cached
|
|
58
|
+
const manifest = path.join(dir, 'package.json')
|
|
59
|
+
/** @type {string | null} */
|
|
60
|
+
let name
|
|
61
|
+
// A package.json with no `name` (a nested `{ "type": "module" }`) does not make a package: keep walking, or a file
|
|
62
|
+
// under one inside a framework copy would read as nobody's and pass every guard.
|
|
63
|
+
/** @type {string | null} */
|
|
64
|
+
let named = null
|
|
65
|
+
if (existsSync(manifest)) {
|
|
66
|
+
try {
|
|
67
|
+
const parsed = JSON.parse(readFileSync(manifest, 'utf8'))
|
|
68
|
+
named = typeof parsed?.name === 'string' ? parsed.name : null
|
|
69
|
+
} catch {
|
|
70
|
+
named = null
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (named !== null) name = named
|
|
74
|
+
else {
|
|
75
|
+
const parent = path.dirname(dir)
|
|
76
|
+
name = parent === dir ? null : owningPackageName(parent)
|
|
77
|
+
}
|
|
78
|
+
owningNames.set(dir, name)
|
|
79
|
+
return name
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Whether `file` is framework source: its nearest named package.json is `@sssb/kit`'s, wherever it sits (an installed
|
|
84
|
+
* instance, a workspace link, or a copy of the framework's tree reached by a relative path). Recognised by the
|
|
85
|
+
* package, not by an `@sssb/kit` segment in the path, which a copy outside node_modules does not have.
|
|
86
|
+
*
|
|
87
|
+
* @type {import('./kit.d.ts').isFrameworkFile}
|
|
88
|
+
*/
|
|
89
|
+
export function isFrameworkFile(file) {
|
|
90
|
+
return owningPackageName(path.dirname(realpathSync(file))) === KIT
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The guard every loader runs on each file it loads for an app: framework source must be the app's own instance
|
|
95
|
+
* (`assertAppsKitFile`); anything else passes.
|
|
96
|
+
*
|
|
97
|
+
* @type {import('./kit.d.ts').assertNoForeignFramework}
|
|
98
|
+
*/
|
|
99
|
+
export function assertNoForeignFramework(appDir, file) {
|
|
100
|
+
if (isFrameworkFile(file)) assertAppsKitFile(appDir, file)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The real directory of the `@sssb/kit` an app resolves: the first `node_modules/@sssb/kit` walking up from the
|
|
105
|
+
* app's directory, which is where Node's own lookup finds it. Throws, naming the app, when there is none.
|
|
106
|
+
*
|
|
107
|
+
* WALKED, NOT `require.resolve`d: Node's resolver falls back to NODE_PATH when the app's own chain has no match, and
|
|
108
|
+
* pnpm's bin shims (which `pnpm exec <bin>` and package scripts run) set NODE_PATH to the workspace's hoisted `.pnpm/node_modules`. So for an app that
|
|
109
|
+
* installs no framework, `require.resolve('@sssb/kit/…')` would quietly return the hoisted copy, another app's
|
|
110
|
+
* instance: the slip rule 4 exists to prevent. The walk never consults NODE_PATH or the global folders.
|
|
111
|
+
*
|
|
112
|
+
* @type {import('./kit.d.ts').kitDirOf}
|
|
113
|
+
*/
|
|
114
|
+
export function kitDirOf(appDir) {
|
|
115
|
+
const dir = packageDirFrom(appDir, KIT)
|
|
116
|
+
if (dir !== null) return dir
|
|
117
|
+
throw new UsageError(
|
|
118
|
+
`sssb: ${path.resolve(appDir)} does not resolve ${KIT}: no node_modules/${KIT} in it or above it. Run this ` +
|
|
119
|
+
'from an app that depends on it, or name one with --app',
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @type {import('./kit.d.ts').kitVersionOf} */
|
|
124
|
+
export function kitVersionOf(appDir) {
|
|
125
|
+
const manifest = JSON.parse(readFileSync(path.join(kitDirOf(appDir), 'package.json'), 'utf8'))
|
|
126
|
+
if (typeof manifest.version !== 'string') {
|
|
127
|
+
throw new UsageError(
|
|
128
|
+
`sssb: the ${KIT} that ${path.resolve(appDir)} resolves has no version in its package.json`,
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
return manifest.version
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The text of the release stamp (`dist/release.json`) the app's `@sssb/kit` carries, or null when it has none. It is a
|
|
136
|
+
* file `@sssb/kit`'s prepack writes into the package, so it is read from the kit directory the app resolves, here.
|
|
137
|
+
*
|
|
138
|
+
* @type {import('./kit.d.ts').kitReleaseStampOf}
|
|
139
|
+
*/
|
|
140
|
+
export function kitReleaseStampOf(appDir) {
|
|
141
|
+
const stamp = path.join(kitDirOf(appDir), 'dist', 'release.json')
|
|
142
|
+
return existsSync(stamp) ? readFileSync(stamp, 'utf8') : null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The shared real-path check: `file` must be part of the `@sssb/kit` that `appDir` resolves. Anything else (the
|
|
147
|
+
* hoisted copy, another app's instance, this repository's source reached by a path) throws.
|
|
148
|
+
*
|
|
149
|
+
* @type {import('./kit.d.ts').assertAppsKitFile}
|
|
150
|
+
*/
|
|
151
|
+
export function assertAppsKitFile(appDir, file) {
|
|
152
|
+
const kitDir = kitDirOf(appDir)
|
|
153
|
+
const real = realpathSync(file)
|
|
154
|
+
if (real !== kitDir && !real.startsWith(kitDir + path.sep)) {
|
|
155
|
+
throw new UsageError(
|
|
156
|
+
`sssb: ${real} is not part of the ${KIT} that ${path.resolve(appDir)} resolves (${kitDir}). ` +
|
|
157
|
+
'A tool reached the framework some other way than from the app, so it may be running on another ' +
|
|
158
|
+
"app's instance and seam (ADR-0014, rule 4).",
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
return real
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* A published subpath of `@sssb/kit` (`i18n/request`, `adapters/real/index`), resolved with the app's directory
|
|
166
|
+
* as the base and checked. Returns the real path.
|
|
167
|
+
*
|
|
168
|
+
* @type {import('./kit.d.ts').resolveAppsKit}
|
|
169
|
+
*/
|
|
170
|
+
export function resolveAppsKit(appDir, subpath) {
|
|
171
|
+
let file
|
|
172
|
+
try {
|
|
173
|
+
file = requireFrom(appDir).resolve(`${KIT}/${subpath}`)
|
|
174
|
+
} catch (error) {
|
|
175
|
+
// Distinguish "no framework here" from "the framework does not publish that subpath".
|
|
176
|
+
kitDirOf(appDir)
|
|
177
|
+
throw new UsageError(
|
|
178
|
+
`sssb: the ${KIT} that ${path.resolve(appDir)} resolves does not publish \`${KIT}/${subpath}\``,
|
|
179
|
+
{ cause: error },
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
return assertAppsKitFile(appDir, file)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Another package the APP installs, resolved from the app's directory: a preset configures the app's own copy
|
|
187
|
+
* of a tool (next-intl's plugin, Vite's React plugin), never one of its own.
|
|
188
|
+
*
|
|
189
|
+
* @type {import('./kit.d.ts').resolveFromApp}
|
|
190
|
+
*/
|
|
191
|
+
export function resolveFromApp(appDir, specifier, why) {
|
|
192
|
+
const missing = () =>
|
|
193
|
+
specifier.startsWith('#')
|
|
194
|
+
? `sssb: ${path.resolve(appDir)} cannot resolve \`${specifier}\`, which ${why}: check the \`imports\` of the app's package.json.`
|
|
195
|
+
: `sssb: ${path.resolve(appDir)} does not install \`${specifier}\`, which ${why}. Add it to the app's devDependencies.`
|
|
196
|
+
// The package must be on the app's own lookup chain. Node's resolver would otherwise fall back to NODE_PATH, which
|
|
197
|
+
// pnpm's bin shims point at the hoisted store, and hand back a copy installed for another package (see kitDirOf).
|
|
198
|
+
// Node's built-ins and package imports (`#x`) are not packages on a lookup chain.
|
|
199
|
+
const bare = !specifier.startsWith('node:') && !specifier.startsWith('#') && !builtinModules.includes(specifier)
|
|
200
|
+
if (bare && packageDirFrom(appDir, packageNameOf(specifier)) === null) throw new UsageError(missing())
|
|
201
|
+
try {
|
|
202
|
+
return requireFrom(appDir).resolve(specifier)
|
|
203
|
+
} catch (error) {
|
|
204
|
+
throw new UsageError(missing(), { cause: error })
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Loads a package the APP installs (see resolveFromApp) and returns its module namespace. A CommonJS package's
|
|
210
|
+
* `module.exports` arrives as `default`, and its named exports as Node's CJS lexer finds them.
|
|
211
|
+
*
|
|
212
|
+
* @type {import('./kit.d.ts').importFromApp}
|
|
213
|
+
*/
|
|
214
|
+
export async function importFromApp(appDir, specifier, why) {
|
|
215
|
+
return await import(pathToFileURL(resolveFromApp(appDir, specifier, why)).href)
|
|
216
|
+
}
|
package/src/next.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NextConfig } from 'next'
|
|
2
|
+
|
|
3
|
+
export interface SssbNextOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The app's directory (`__dirname` in its next.config.ts). Defaults to the working directory, which is the app's
|
|
6
|
+
* when Next loads the config, but not when another tool does, so pass it.
|
|
7
|
+
*/
|
|
8
|
+
appDir?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The app's Next config with what every SSSB app needs: the framework transpiled, next-intl pointed at the
|
|
13
|
+
* framework's request config (resolved from the app), the standalone output, the native server packages external,
|
|
14
|
+
* and `next dev` kept hermetic. The app's own settings win where they overlap, and its lists are extended.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withSssb(nextConfig?: NextConfig, options?: SssbNextOptions): NextConfig
|