@sssb/tools 0.2.0 → 0.4.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/bin/sssb.js +14 -0
- package/package.json +1 -1
- package/src/app-port.js +3 -3
- package/src/commands/add-app.js +344 -0
- package/src/commands/doctor-checks.js +136 -16
- package/src/commands/doctor-settings.js +583 -0
- package/src/commands/doctor.js +94 -15
- package/src/commands/pin.js +411 -0
- package/src/commands/remove-items.js +191 -0
- package/src/kit.d.ts +6 -0
- package/src/kit.js +11 -0
- package/src/ports.d.ts +1 -1
- package/src/ports.js +5 -4
- 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/tick/tick.js +12 -10
- package/src/vitest.d.ts +17 -0
- package/src/vitest.js +28 -1
package/bin/sssb.js
CHANGED
|
@@ -63,10 +63,24 @@ const COMMANDS = {
|
|
|
63
63
|
summary:
|
|
64
64
|
"fail unless the app's SSSB is one copy, on its own seam, in step, and its conformance checks would run",
|
|
65
65
|
},
|
|
66
|
+
pin: {
|
|
67
|
+
load: () => import('../src/commands/pin.js'),
|
|
68
|
+
summary:
|
|
69
|
+
"raise the workflow's pin and the plugin marketplace's ref to the installed @sssb/kit's release commit",
|
|
70
|
+
},
|
|
66
71
|
'check-e2e': {
|
|
67
72
|
load: () => import('../src/commands/check-e2e.js'),
|
|
68
73
|
summary: 'fail when a Playwright spec reaches @sssb/kit other than through @sssb/kit/testing/e2e',
|
|
69
74
|
},
|
|
75
|
+
'remove-items': {
|
|
76
|
+
load: () => import('../src/commands/remove-items.js'),
|
|
77
|
+
summary: "take the starter's example Items slice out of the app (--drop-migrations for one never deployed)",
|
|
78
|
+
},
|
|
79
|
+
'add-app': {
|
|
80
|
+
load: () => import('../src/commands/add-app.js'),
|
|
81
|
+
summary:
|
|
82
|
+
"write a new app into this workspace from SSSB's starter, with a port slot of its own, and name it in sssb.apps",
|
|
83
|
+
},
|
|
70
84
|
ci: {
|
|
71
85
|
load: () => import('../src/commands/ci.js'),
|
|
72
86
|
summary: "a CI job's logic: lint-typecheck-unit, contract, matrix, e2e-shards, e2e, demo-static, synth",
|
package/package.json
CHANGED
package/src/app-port.js
CHANGED
|
@@ -38,9 +38,9 @@ export function appPortService(appDir) {
|
|
|
38
38
|
const declared = declaredPortService(appDir)
|
|
39
39
|
if (declared === undefined) {
|
|
40
40
|
throw new UsageError(
|
|
41
|
-
`sssb: ${path.join(appDir, 'package.json')} declares no port service. Add
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
`sssb: ${path.join(appDir, 'package.json')} declares no port service. Add a slot of the app's own, ` +
|
|
42
|
+
'"sssb": { "port": { "name": "<name>", "default": <port> } }, or a built-in service, "sssb": { "port": ' +
|
|
43
|
+
`"<service>" } (one of ${SERVICE_NAMES.join(', ')}), or name one: sssb port <service>`,
|
|
44
44
|
)
|
|
45
45
|
}
|
|
46
46
|
return declared
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `sssb add-app <slug> [--name "<Name>"] [--port <port>] [--slot <name>] [--create <dir>]`: a second app (or a third)
|
|
3
|
+
// in an existing workspace, written from SSSB's starter under apps/<slug>/ with the generator's renames, a port slot of
|
|
4
|
+
// its own, and each @sssb package taken as the workspace's first app takes it; then named in the root's `sssb.apps`,
|
|
5
|
+
// the one list of apps every SSSB tool reads. It prints what is left to the person (the root scripts and the CI
|
|
6
|
+
// workflow name one app on purpose), and `sssb doctor` names anything else.
|
|
7
|
+
//
|
|
8
|
+
// WHERE THE STARTER COMES FROM: the @sssb/create the workspace installs (`pnpm add -D -w @sssb/create@<version>`, at
|
|
9
|
+
// the @sssb packages' one version), which carries the starter as its packed template and the renames as its code
|
|
10
|
+
// (`@sssb/create/add-app`). Reached from the workspace root, as rule 4 reaches @sssb/kit from the app: no SSSB package
|
|
11
|
+
// depends on another (ADR-0014 part 10). `--create <dir>` names an unpacked @sssb/create instead.
|
|
12
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import { pathToFileURL } from 'node:url'
|
|
15
|
+
import { readJson, takeOption, UsageError, workspaceApps, workspaceRootOf } from '../app.js'
|
|
16
|
+
import { parsePortDeclaration, portForRoot, readPortDeclaration } from '../ports.js'
|
|
17
|
+
import { workspacePackages } from './doctor-checks.js'
|
|
18
|
+
|
|
19
|
+
const USAGE = 'usage: sssb add-app <slug> [--name "<Name>"] [--port <port>] [--slot <name>] [--create <dir>]'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `@sssb/create/add-app`'s `addApp`, as this command calls it (the generator is plain JavaScript with no types of its
|
|
23
|
+
* own, and no SSSB package depends on another).
|
|
24
|
+
* @typedef {(options: {
|
|
25
|
+
* templateDir: string,
|
|
26
|
+
* root: string,
|
|
27
|
+
* slug: string,
|
|
28
|
+
* name: string | null,
|
|
29
|
+
* port: { name: string, default: number },
|
|
30
|
+
* like: string,
|
|
31
|
+
* others: string[],
|
|
32
|
+
* finish: (appDir: string) => () => void,
|
|
33
|
+
* }) => { appDir: string, name: string, files: string[], notes: string[] }} AddApp
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** The first main-checkout port offered to a new app's slot, and the step between offers (3700, 3800, ...). */
|
|
37
|
+
const FIRST_DEFAULT = 3700
|
|
38
|
+
const DEFAULT_STEP = 100
|
|
39
|
+
const LAST_DEFAULT = 9900
|
|
40
|
+
|
|
41
|
+
/** @SSSB/tools' own version: the @sssb packages are one fixed group, released together at one version. */
|
|
42
|
+
const TOOLS_VERSION = readJson(path.resolve(import.meta.dirname, '..', '..', 'package.json')).version
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The @sssb/create to write the app with, checked: an unpacked package of that name, at @sssb/tools' version, holding
|
|
46
|
+
* its packed template.
|
|
47
|
+
* @param {string} root
|
|
48
|
+
* @param {string | undefined} option `--create <dir>`
|
|
49
|
+
* @returns {{ dir: string, templateDir: string, module: string }}
|
|
50
|
+
*/
|
|
51
|
+
export function createPackage(root, option) {
|
|
52
|
+
const dir = option === undefined ? path.join(root, 'node_modules', '@sssb', 'create') : path.resolve(option)
|
|
53
|
+
const manifestFile = path.join(dir, 'package.json')
|
|
54
|
+
if (!existsSync(manifestFile)) {
|
|
55
|
+
throw new UsageError(
|
|
56
|
+
option === undefined
|
|
57
|
+
? `sssb add-app: the workspace does not install @sssb/create, whose packed template is the app it writes. ` +
|
|
58
|
+
`Install it at your SSSB version, then run this again:\n pnpm add -D -w @sssb/create@${TOOLS_VERSION}`
|
|
59
|
+
: `sssb add-app: --create ${option} is not a package (no package.json at ${dir})`,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
const manifest = readJson(manifestFile)
|
|
63
|
+
if (manifest.name !== '@sssb/create')
|
|
64
|
+
throw new UsageError(`sssb add-app: ${dir} holds ${manifest.name ?? 'an unnamed package'}, not @sssb/create`)
|
|
65
|
+
if (manifest.version !== TOOLS_VERSION) {
|
|
66
|
+
throw new UsageError(
|
|
67
|
+
`sssb add-app: @sssb/create ${manifest.version} writes the starter of @sssb ${manifest.version}, and this ` +
|
|
68
|
+
`@sssb/tools is ${TOOLS_VERSION}: the @sssb packages are released together, so install ` +
|
|
69
|
+
`@sssb/create@${TOOLS_VERSION}`,
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
const entry = manifest.exports?.['./add-app']
|
|
73
|
+
if (typeof entry !== 'string')
|
|
74
|
+
throw new UsageError(`sssb add-app: @sssb/create ${manifest.version} exports no ./add-app`)
|
|
75
|
+
const templateDir = path.join(dir, 'template')
|
|
76
|
+
if (!existsSync(path.join(templateDir, 'meta.json'))) {
|
|
77
|
+
throw new UsageError(
|
|
78
|
+
`sssb add-app: ${dir} holds no packed template (template/meta.json): only a packed @sssb/create carries ` +
|
|
79
|
+
'the starter (a checkout of SSSB has none until `pnpm pack` builds it)',
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
return { dir, templateDir, module: path.join(dir, entry) }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The new app's port slot: `--slot` (else the slug) and `--port` (else the first of 3700, 3800, ... that no app of the
|
|
87
|
+
* workspace binds in a main checkout and SSSB keeps for nothing of its own), checked as `sssb doctor` checks slots.
|
|
88
|
+
* @param {{ root: string, apps: string[], slug: string, slot?: string, port?: string }} options `apps` absolute
|
|
89
|
+
* @returns {{ name: string, default: number }}
|
|
90
|
+
*/
|
|
91
|
+
export function newPortSlot({ root, apps, slug, slot, port }) {
|
|
92
|
+
const name = slot ?? slug
|
|
93
|
+
/** @type {Map<string, string>} */
|
|
94
|
+
const names = new Map()
|
|
95
|
+
/** @type {Map<number, string>} */
|
|
96
|
+
const defaults = new Map()
|
|
97
|
+
for (const app of apps) {
|
|
98
|
+
let declared
|
|
99
|
+
try {
|
|
100
|
+
declared = readPortDeclaration(app)
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw new UsageError(
|
|
103
|
+
`sssb add-app: ${error instanceof Error ? error.message.replace(/^ports: /, '') : error}`,
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
if (declared === undefined) continue
|
|
107
|
+
const where = path.relative(root, app)
|
|
108
|
+
names.set(typeof declared === 'string' ? declared : declared.name, where)
|
|
109
|
+
defaults.set(portForRoot(path.sep, declared, {}), where)
|
|
110
|
+
}
|
|
111
|
+
const holder = names.get(name)
|
|
112
|
+
if (holder !== undefined)
|
|
113
|
+
throw new UsageError(`sssb add-app: ${holder} already declares the port slot "${name}": pass --slot <name>`)
|
|
114
|
+
/**
|
|
115
|
+
* @param {number} value
|
|
116
|
+
* @returns {{ name: string, default: number } | UsageError}
|
|
117
|
+
*/
|
|
118
|
+
const check = (value) => {
|
|
119
|
+
try {
|
|
120
|
+
return /** @type {{ name: string, default: number }} */ (parsePortDeclaration({ name, default: value }))
|
|
121
|
+
} catch (error) {
|
|
122
|
+
return new UsageError(
|
|
123
|
+
`sssb add-app: ${error instanceof Error ? error.message.replace(/^ports: /, '') : String(error)}`,
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (port !== undefined) {
|
|
128
|
+
if (!/^\d+$/.test(port)) throw new UsageError(`sssb add-app: --port ${port} is not a port number`)
|
|
129
|
+
const value = Number(port)
|
|
130
|
+
const result = check(value)
|
|
131
|
+
if (result instanceof UsageError) throw result
|
|
132
|
+
if (defaults.has(value))
|
|
133
|
+
throw new UsageError(`sssb add-app: ${defaults.get(value)} binds port ${value} in a main checkout already`)
|
|
134
|
+
return result
|
|
135
|
+
}
|
|
136
|
+
/** @type {UsageError | undefined} */
|
|
137
|
+
let first
|
|
138
|
+
for (let value = FIRST_DEFAULT; value <= LAST_DEFAULT; value += DEFAULT_STEP) {
|
|
139
|
+
if (defaults.has(value)) continue
|
|
140
|
+
const result = check(value)
|
|
141
|
+
if (!(result instanceof UsageError)) return result
|
|
142
|
+
first ??= result
|
|
143
|
+
}
|
|
144
|
+
// Every offer refused: a name the slot cannot carry refuses them all, so the first refusal says why.
|
|
145
|
+
throw new UsageError(
|
|
146
|
+
first === undefined
|
|
147
|
+
? 'sssb add-app: no free default port from 3700 to 9900 (pass --port <port>)'
|
|
148
|
+
: `${first.message} (pass --slot <name>)`,
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The indent a JSON file is written with (its first indented line's), so the root package.json keeps its own.
|
|
154
|
+
* @param {string} text
|
|
155
|
+
*/
|
|
156
|
+
const indentOf = (text) => /\n([ \t]+)"/.exec(text)?.[1] ?? ' '
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* What is left to the person once the app is written: what the workspace names one app in by design (the root's
|
|
160
|
+
* scripts, the CI workflow's synth) and what does not cover the new one.
|
|
161
|
+
* @param {string} root
|
|
162
|
+
* @param {{ slug: string, appDir: string, like: string }} added
|
|
163
|
+
* @returns {string[]}
|
|
164
|
+
*/
|
|
165
|
+
export function leftToDo(root, { slug, appDir, like }) {
|
|
166
|
+
/** @type {string[]} */
|
|
167
|
+
const left = []
|
|
168
|
+
const rel = (/** @type {string} */ dir) => path.relative(root, dir).split(path.sep).join('/')
|
|
169
|
+
const packages = new Set(workspacePackages(root))
|
|
170
|
+
const uncovered = [appDir, path.join(appDir, 'infra'), path.join(appDir, 'src', 'app-config')].filter(
|
|
171
|
+
(dir) => existsSync(path.join(dir, 'package.json')) && !packages.has(dir),
|
|
172
|
+
)
|
|
173
|
+
if (uncovered.length > 0)
|
|
174
|
+
left.push(`pnpm-workspace.yaml's packages do not cover ${uncovered.map(rel).join(', ')}: add globs that do`)
|
|
175
|
+
|
|
176
|
+
const likeName = readJson(path.join(like, 'package.json')).name
|
|
177
|
+
const scripts = /** @type {Record<string, string>} */ (readJson(path.join(root, 'package.json')).scripts ?? {})
|
|
178
|
+
const naming = Object.entries(scripts)
|
|
179
|
+
.filter(([, text]) => typeof text === 'string')
|
|
180
|
+
.filter(
|
|
181
|
+
([, text]) =>
|
|
182
|
+
new RegExp(`--filter[= ](['"]?)${likeName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\1(\\s|$)`).test(
|
|
183
|
+
text,
|
|
184
|
+
) ||
|
|
185
|
+
(rel(like) !== '' && text.includes(`./${rel(like)}`)),
|
|
186
|
+
)
|
|
187
|
+
.map(([script]) => script)
|
|
188
|
+
if (naming.length > 0) {
|
|
189
|
+
left.push(
|
|
190
|
+
`the root's ${naming.join(', ')} script(s) run ${likeName} alone; add ones for ${slug} if you want them ` +
|
|
191
|
+
`(say, "dev:${slug}": "pnpm --filter ${slug} dev")`,
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const workflows = path.join(root, '.github', 'workflows')
|
|
196
|
+
if (existsSync(workflows)) {
|
|
197
|
+
for (const file of readdirSync(workflows).filter((name) => /\.ya?ml$/.test(name))) {
|
|
198
|
+
const text = readFileSync(path.join(workflows, file), 'utf8')
|
|
199
|
+
const synth = /^\s*synth:\s*['"]?(\S+?)['"]?\s*(?:#.*)?$/m.exec(text)?.[1]
|
|
200
|
+
if (synth && rel(like) !== '' && synth.includes(rel(like)) && !synth.includes('*')) {
|
|
201
|
+
left.push(
|
|
202
|
+
`.github/workflows/${file} synthesizes ${synth} alone: make its \`synth\` a selector that takes ` +
|
|
203
|
+
"every added app's infra/ too (./apps/*/infra takes every app's)",
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
// jscpd measures duplication across every app's src/ together, and the new app is the starter: as long as another
|
|
209
|
+
// app still holds the starter's code, the copy fails its threshold. That is jscpd being right, so it is said, not
|
|
210
|
+
// silenced.
|
|
211
|
+
const jscpd = path.join(root, '.jscpd.json')
|
|
212
|
+
if (existsSync(jscpd) && /apps\/\*/.test(readFileSync(jscpd, 'utf8'))) {
|
|
213
|
+
left.push(
|
|
214
|
+
`\`pnpm jscpd\` fails while ${rel(appDir)} is the starter and ${rel(like) || 'your first app'} still holds ` +
|
|
215
|
+
`starter code: make the apps your own, or, while you do, add "${rel(appDir)}/src/**" to .jscpd.json's ` +
|
|
216
|
+
'`ignore` and take it out again once the app is its own',
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
const manifest = readJson(path.join(root, 'package.json'))
|
|
220
|
+
if (manifest.devDependencies?.['@sssb/create'] ?? manifest.dependencies?.['@sssb/create']) {
|
|
221
|
+
left.push(
|
|
222
|
+
'`pnpm knip` calls @sssb/create unused (nothing imports it): `pnpm remove -w @sssb/create` once your apps ' +
|
|
223
|
+
"are written, or add it to knip.json's root ignoreDependencies to keep it",
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
return left
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** @param {string[]} args */
|
|
230
|
+
export async function run(args) {
|
|
231
|
+
const name = takeOption(args, '--name')
|
|
232
|
+
const port = takeOption(args, '--port')
|
|
233
|
+
const slot = takeOption(args, '--slot')
|
|
234
|
+
const create = takeOption(args, '--create')
|
|
235
|
+
const [slug, ...rest] = args
|
|
236
|
+
if (slug === undefined || slug.startsWith('-') || rest.length > 0)
|
|
237
|
+
throw new UsageError(
|
|
238
|
+
`sssb add-app: ${slug === undefined ? 'name the app' : `unexpected ${rest[0] ?? slug}`}\n${USAGE}`,
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
const root = workspaceRootOf(process.cwd())
|
|
242
|
+
const apps = workspaceApps(root).map((app) => path.join(root, app))
|
|
243
|
+
const like = apps.find((app) => {
|
|
244
|
+
const manifest = readJson(path.join(app, 'package.json'))
|
|
245
|
+
return Boolean(manifest.dependencies?.['@sssb/kit'] ?? manifest.devDependencies?.['@sssb/kit'])
|
|
246
|
+
})
|
|
247
|
+
if (like === undefined) {
|
|
248
|
+
throw new UsageError(
|
|
249
|
+
`sssb add-app: ${root} has no app that takes @sssb/kit (\`sssb.apps\` in its package.json, or apps/<name>) ` +
|
|
250
|
+
'to add one beside: a new workspace is `pnpm create @sssb <dir>`',
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
const { templateDir, module } = createPackage(root, create)
|
|
254
|
+
const generator =
|
|
255
|
+
/** @type {{ addApp: AddApp, checkSlug: (slug: string) => void, signalsSettled: () => Promise<void> }} */ (
|
|
256
|
+
await import(pathToFileURL(module).href)
|
|
257
|
+
)
|
|
258
|
+
/** @type {<T>(work: () => T) => T} the generator's refusals, as this command's */
|
|
259
|
+
const asUsage = (work) => {
|
|
260
|
+
try {
|
|
261
|
+
return work()
|
|
262
|
+
} catch (error) {
|
|
263
|
+
if (error instanceof Error && error.name === 'CreateError')
|
|
264
|
+
throw new UsageError(`sssb add-app: ${error.message}`)
|
|
265
|
+
throw error
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
asUsage(() => generator.checkSlug(slug))
|
|
269
|
+
const appDir = path.join(root, 'apps', slug)
|
|
270
|
+
// The new app's package names may be neither a package of the workspace nor a dependency one of them takes: pnpm
|
|
271
|
+
// mistakes a workspace package for the dependency of the same name (the generator refuses its own template's
|
|
272
|
+
// dependencies; these are the workspace's own, the adopter's additions among them).
|
|
273
|
+
const packages = workspacePackages(root)
|
|
274
|
+
const manifests = [root, ...packages]
|
|
275
|
+
.map((dir) => path.join(dir, 'package.json'))
|
|
276
|
+
.filter((file) => existsSync(file))
|
|
277
|
+
.map((file) => readJson(file))
|
|
278
|
+
const names = new Set(manifests.map((manifest) => manifest.name))
|
|
279
|
+
const dependencies = new Set(
|
|
280
|
+
manifests.flatMap((manifest) =>
|
|
281
|
+
['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'].flatMap((field) =>
|
|
282
|
+
Object.keys(manifest[field] ?? {}),
|
|
283
|
+
),
|
|
284
|
+
),
|
|
285
|
+
)
|
|
286
|
+
for (const taken of [slug, `${slug}-infra`, `${slug}-app-config`]) {
|
|
287
|
+
if (names.has(taken))
|
|
288
|
+
throw new UsageError(`sssb add-app: the workspace already has a package named ${taken}: pick another slug`)
|
|
289
|
+
if (dependencies.has(taken))
|
|
290
|
+
throw new UsageError(
|
|
291
|
+
`sssb add-app: a package of the workspace depends on ${taken}, which the new app's package would be ` +
|
|
292
|
+
'named: pnpm mistakes one for the other. Pick another slug',
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
// Nor the slug of an app already here, whatever its packages are called: the slug names the session cookie, the
|
|
296
|
+
// contract database and the deployment's stack.
|
|
297
|
+
for (const app of apps) {
|
|
298
|
+
const config = path.join(app, 'config', 'app.ts')
|
|
299
|
+
const declared = existsSync(config)
|
|
300
|
+
? /export const APP_SLUG = '([^']+)'/.exec(readFileSync(config, 'utf8'))?.[1]
|
|
301
|
+
: undefined
|
|
302
|
+
if (declared === slug)
|
|
303
|
+
throw new UsageError(
|
|
304
|
+
`sssb add-app: ${path.relative(root, app)} is the app "${slug}" (its config/app.ts APP_SLUG), and the ` +
|
|
305
|
+
'slug names the session cookie, the contract database and the stack: pick another slug',
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
const portSlot = newPortSlot({ root, apps, slug, slot, port })
|
|
309
|
+
// The one list of apps: named in `sssb.apps` when the root keeps one (else apps/* is the list, and holds it). Written
|
|
310
|
+
// by the generator's `finish`, under its signal hold, so an interrupted run takes the entry back with the app.
|
|
311
|
+
const manifestFile = path.join(root, 'package.json')
|
|
312
|
+
const entry = path.relative(root, appDir).split(path.sep).join('/')
|
|
313
|
+
let listed = false
|
|
314
|
+
const finish = () => {
|
|
315
|
+
const text = readFileSync(manifestFile, 'utf8')
|
|
316
|
+
const manifest = JSON.parse(text)
|
|
317
|
+
const apps = manifest.sssb?.apps
|
|
318
|
+
if (!Array.isArray(apps) || apps.includes(entry)) return () => {}
|
|
319
|
+
manifest.sssb.apps = [...apps, entry]
|
|
320
|
+
writeFileSync(manifestFile, `${JSON.stringify(manifest, null, indentOf(text))}\n`)
|
|
321
|
+
listed = true
|
|
322
|
+
return () => writeFileSync(manifestFile, text)
|
|
323
|
+
}
|
|
324
|
+
const others = [root, ...packages].filter((dir) => dir !== like)
|
|
325
|
+
const added = asUsage(() =>
|
|
326
|
+
generator.addApp({ templateDir, root, slug, name: name ?? null, port: portSlot, like, others, finish }),
|
|
327
|
+
)
|
|
328
|
+
// A Ctrl-C that came while the app was written is dispatched now, undoes it and ends the process, before anything
|
|
329
|
+
// says it was added.
|
|
330
|
+
await generator.signalsSettled()
|
|
331
|
+
|
|
332
|
+
const left = [...added.notes, ...leftToDo(root, { slug, appDir, like })]
|
|
333
|
+
console.log(`
|
|
334
|
+
Added ${added.name} in ${entry}/: ${added.files.length} files, from @sssb/create ${TOOLS_VERSION}'s template.
|
|
335
|
+
Its port slot is "${portSlot.name}": port ${portSlot.default} in a main checkout, ${portSlot.name
|
|
336
|
+
.toUpperCase()
|
|
337
|
+
.replaceAll('-', '_')}_PORT overrides it.${listed ? ` The root's sssb.apps names it.` : ''}
|
|
338
|
+
|
|
339
|
+
Next:
|
|
340
|
+
pnpm install
|
|
341
|
+
pnpm exec sssb doctor (it names anything else the workspace still needs for ${slug})
|
|
342
|
+
${left.length > 0 ? `\nLeft to you:\n${left.map((line) => ` - ${line}`).join('\n')}\n` : ''}`)
|
|
343
|
+
return 0
|
|
344
|
+
}
|
|
@@ -998,7 +998,7 @@ export function cutoverReport(rows, checklist) {
|
|
|
998
998
|
|
|
999
999
|
/**
|
|
1000
1000
|
* PORT SLOTS: two apps of one workspace must not bind one port. An app declares its slot in its package.json
|
|
1001
|
-
* (`"sssb": { "port": "starter" }`, or a slot of its own, `{ "name": "ledger", "default":
|
|
1001
|
+
* (`"sssb": { "port": "starter" }`, or a slot of its own, `{ "name": "ledger", "default": 3800 }`), the slot fixes the
|
|
1002
1002
|
* port its dev server, its Playwright config and its ticker use, so two apps on one slot start on one port: the second
|
|
1003
1003
|
* server fails to bind, or, under Playwright's `reuseExistingServer`, the second app's suite ADOPTS the first app's
|
|
1004
1004
|
* server and tests the wrong app (docs/build-notes.md). Fails: a declaration that is not valid, two apps on one slot
|
|
@@ -1053,6 +1053,112 @@ export function portSlotProblems(root, apps) {
|
|
|
1053
1053
|
return { problems, slots }
|
|
1054
1054
|
}
|
|
1055
1055
|
|
|
1056
|
+
/**
|
|
1057
|
+
* JSONC as JSON: its comments and trailing commas taken out, strings (a `$schema` URL's `//` among them) left whole.
|
|
1058
|
+
* @param {string} text
|
|
1059
|
+
*/
|
|
1060
|
+
function withoutJsonComments(text) {
|
|
1061
|
+
let out = ''
|
|
1062
|
+
// A comma is held until the next token: dropped before a `}` or `]` (a trailing comma), written before anything else.
|
|
1063
|
+
let comma = ''
|
|
1064
|
+
const token = (/** @type {string} */ value) => {
|
|
1065
|
+
out += /^[}\]]/.test(value) ? comma.slice(1) : comma
|
|
1066
|
+
comma = ''
|
|
1067
|
+
out += value
|
|
1068
|
+
}
|
|
1069
|
+
for (let i = 0; i < text.length; i++) {
|
|
1070
|
+
const char = /** @type {string} */ (text[i])
|
|
1071
|
+
if (char === '"') {
|
|
1072
|
+
let end = i + 1
|
|
1073
|
+
while (end < text.length && text[end] !== '"') end += text[end] === '\\' ? 2 : 1
|
|
1074
|
+
token(text.slice(i, end + 1))
|
|
1075
|
+
i = end
|
|
1076
|
+
} else if (char === '/' && text[i + 1] === '/') {
|
|
1077
|
+
while (i + 1 < text.length && text[i + 1] !== '\n') i++
|
|
1078
|
+
} else if (char === '/' && text[i + 1] === '*') {
|
|
1079
|
+
const end = text.indexOf('*/', i + 2)
|
|
1080
|
+
i = end < 0 ? text.length : end + 1
|
|
1081
|
+
} else if (char === ',') {
|
|
1082
|
+
token('')
|
|
1083
|
+
comma = ','
|
|
1084
|
+
} else if (/\s/.test(char)) {
|
|
1085
|
+
if (comma) comma += char
|
|
1086
|
+
else out += char
|
|
1087
|
+
} else token(char)
|
|
1088
|
+
}
|
|
1089
|
+
return out + comma
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/** Where knip looks for its JSON config at a workspace root, in its own order. */
|
|
1093
|
+
const KNIP_JSON = ['knip.json', 'knip.jsonc', '.knip.json', '.knip.jsonc']
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* KNIP'S WORKSPACES against the workspace's apps: a generated workspace's knip.json gives each app, and its seam, an
|
|
1097
|
+
* entry of its own (their entry files, and the dependencies SSSB's tools load from the app), so an app it does not name
|
|
1098
|
+
* is read with knip's defaults, which flag the seam's modules (the framework reads them from node_modules, which knip
|
|
1099
|
+
* does not follow) and the presets' dependencies as unused. Reported, never failed: knip's own run is the gate.
|
|
1100
|
+
* It also says when the root installs `@sssb/create` (as `sssb add-app` asks) and knip's root config does not ignore it:
|
|
1101
|
+
* nothing imports it (the command loads it from node_modules), so knip calls it an unused dependency.
|
|
1102
|
+
* Returns null when the root has no knip JSON config; `missing` is null when it has no `workspaces` (one config for
|
|
1103
|
+
* every package).
|
|
1104
|
+
* @param {string} root
|
|
1105
|
+
* @param {readonly string[]} apps absolute
|
|
1106
|
+
* @param {readonly string[]} packages the workspace's packages, absolute (workspacePackages)
|
|
1107
|
+
* @returns {{ file: string, missing: string[] | null, generator: boolean } | { file: string, unread: string } | null}
|
|
1108
|
+
*/
|
|
1109
|
+
export function knipGaps(root, apps, packages) {
|
|
1110
|
+
const file = KNIP_JSON.find((name) => existsSync(path.join(root, name)))
|
|
1111
|
+
if (file === undefined) return null
|
|
1112
|
+
let config
|
|
1113
|
+
try {
|
|
1114
|
+
// As knip reads it: a byte-order mark dropped, and comments and trailing commas allowed in every one of its names
|
|
1115
|
+
// (it falls back to JSONC for a `.json` too). Plain JSON passes through unchanged.
|
|
1116
|
+
const text = readFileSync(path.join(root, file), 'utf8').replace(/^\uFEFF/, '')
|
|
1117
|
+
config = JSON.parse(withoutJsonComments(text))
|
|
1118
|
+
} catch (error) {
|
|
1119
|
+
return { file, unread: error instanceof Error ? error.message : String(error) }
|
|
1120
|
+
}
|
|
1121
|
+
const workspaces = config?.workspaces
|
|
1122
|
+
const hasWorkspaces = workspaces !== null && typeof workspaces === 'object'
|
|
1123
|
+
const rootConfig = hasWorkspaces ? workspaces['.'] : config
|
|
1124
|
+
const ignored = /** @type {unknown[]} */ (
|
|
1125
|
+
[config?.ignoreDependencies, rootConfig?.ignoreDependencies].flatMap((list) =>
|
|
1126
|
+
Array.isArray(list) ? list : [],
|
|
1127
|
+
)
|
|
1128
|
+
)
|
|
1129
|
+
const manifestFile = path.join(root, 'package.json')
|
|
1130
|
+
const rootManifest = existsSync(manifestFile) ? readJson(manifestFile) : {}
|
|
1131
|
+
const installs = ['dependencies', 'devDependencies'].some((field) => rootManifest?.[field]?.['@sssb/create'])
|
|
1132
|
+
// knip reads an entry with a regular expression's characters as one (its `toRegexOrString`).
|
|
1133
|
+
const ignores = (/** @type {unknown} */ entry) => {
|
|
1134
|
+
if (typeof entry !== 'string') return false
|
|
1135
|
+
if (entry === '@sssb/create') return true
|
|
1136
|
+
if (!/[*+\\(|{^$]/.test(entry)) return false
|
|
1137
|
+
try {
|
|
1138
|
+
return new RegExp(entry).test('@sssb/create')
|
|
1139
|
+
} catch {
|
|
1140
|
+
return false
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
const generator = Boolean(installs) && !ignored.some(ignores)
|
|
1144
|
+
if (!hasWorkspaces) return { file, missing: null, generator }
|
|
1145
|
+
const keys = Object.keys(workspaces)
|
|
1146
|
+
const named = (/** @type {string} */ rel) =>
|
|
1147
|
+
keys.some((key) => key === rel || path.posix.matchesGlob(rel, key.replace(/\/+$/, '')))
|
|
1148
|
+
/** @type {string[]} */
|
|
1149
|
+
const missing = []
|
|
1150
|
+
for (const app of apps) {
|
|
1151
|
+
const rel = path.relative(root, app).split(path.sep).join('/')
|
|
1152
|
+
// An app at the root is knip's `.` workspace, which a config with workspaces always has a say over; an app that
|
|
1153
|
+
// is no workspace package is part of the root's for knip, and needs no key of its own.
|
|
1154
|
+
if (rel !== '' && packages.includes(app) && !named(rel)) missing.push(rel)
|
|
1155
|
+
const seam = path.join(app, 'src', 'app-config')
|
|
1156
|
+
const seamRel = path.relative(root, seam).split(path.sep).join('/')
|
|
1157
|
+
if (packages.includes(seam) && !named(seamRel)) missing.push(seamRel)
|
|
1158
|
+
}
|
|
1159
|
+
return { file, missing, generator }
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1056
1162
|
/** A full commit SHA, the only pin that cannot move. */
|
|
1057
1163
|
const COMMIT = /^[0-9a-f]{40}$/
|
|
1058
1164
|
|
|
@@ -1072,8 +1178,17 @@ export function githubRepositoryOf(repository) {
|
|
|
1072
1178
|
*/
|
|
1073
1179
|
|
|
1074
1180
|
/**
|
|
1075
|
-
*
|
|
1076
|
-
*
|
|
1181
|
+
* One `uses:` line that calls a reusable workflow (`uses: <owner>/<name>/.github/workflows/<file>@<ref>`), cut so a
|
|
1182
|
+
* caller can read or rewrite the ref (`sssb pin`): 1 everything up to and including the `@`, 2 the `owner/name`, 3 the
|
|
1183
|
+
* workflow's path, 4 the ref, 5 whatever follows it (a trailing comment, a carriage return). One regex for doctor's
|
|
1184
|
+
* report and the command that fixes what it reports, so they cannot disagree about what a pin is.
|
|
1185
|
+
*/
|
|
1186
|
+
export const WORKFLOW_USES =
|
|
1187
|
+
/^(\s*(?:-\s+)?uses:\s*['"]?([\w.-]+\/[\w.-]+)\/(\.github\/workflows\/[^@\s'"]+)@)([^\s'"#]+)([\s\S]*)$/
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Every call of a reusable workflow of `repository` in the workspace's workflow files, the owner and name compared as
|
|
1191
|
+
* GitHub compares them, without case.
|
|
1077
1192
|
* @param {string} repository `owner/name`, lowercased
|
|
1078
1193
|
* @param {{ file: string, text: string }[]} workflows
|
|
1079
1194
|
* @returns {Pin[]}
|
|
@@ -1081,17 +1196,28 @@ export function githubRepositoryOf(repository) {
|
|
|
1081
1196
|
function workflowPins(repository, workflows) {
|
|
1082
1197
|
/** @type {Pin[]} */
|
|
1083
1198
|
const pins = []
|
|
1084
|
-
const uses = /^\s*(?:-\s+)?uses:\s*['"]?([\w.-]+\/[\w.-]+)\/(\.github\/workflows\/[^@\s'"]+)@([^\s'"#]+)/
|
|
1085
1199
|
for (const { file, text } of workflows) {
|
|
1086
1200
|
for (const line of text.split('\n')) {
|
|
1087
|
-
const match =
|
|
1088
|
-
if (match?.[
|
|
1089
|
-
pins.push({ where: `${file} (${match[
|
|
1201
|
+
const match = WORKFLOW_USES.exec(line)
|
|
1202
|
+
if (match?.[2]?.toLowerCase() === repository)
|
|
1203
|
+
pins.push({ where: `${file} (${match[3]})`, ref: /** @type {string} */ (match[4]) })
|
|
1090
1204
|
}
|
|
1091
1205
|
}
|
|
1092
1206
|
return pins
|
|
1093
1207
|
}
|
|
1094
1208
|
|
|
1209
|
+
/**
|
|
1210
|
+
* The `owner/name` a marketplace's `source` names on GitHub, lowercased: a `github` source's `repo`, or a `git` source's
|
|
1211
|
+
* `url`. Null for any other source.
|
|
1212
|
+
* @param {any} source an `extraKnownMarketplaces.<name>.source` value
|
|
1213
|
+
* @returns {string | null}
|
|
1214
|
+
*/
|
|
1215
|
+
export function sourceRepository(source) {
|
|
1216
|
+
if (source?.source === 'github' && typeof source.repo === 'string')
|
|
1217
|
+
return source.repo.toLowerCase().replace(/\.git$/, '')
|
|
1218
|
+
return source?.source === 'git' ? githubRepositoryOf(source.url) : null
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1095
1221
|
/**
|
|
1096
1222
|
* Every marketplace `.claude/settings.json` declares from `repository` (`extraKnownMarketplaces.<name>.source`, a
|
|
1097
1223
|
* `github` source naming it as `repo`, or a `git` one whose `url` is it), with its `ref`; and, when there is one, a
|
|
@@ -1123,13 +1249,7 @@ function marketplacePins(repository, file, text) {
|
|
|
1123
1249
|
const marketplaces = settings?.extraKnownMarketplaces
|
|
1124
1250
|
for (const [name, entry] of Object.entries(typeof marketplaces === 'object' && marketplaces ? marketplaces : {})) {
|
|
1125
1251
|
const source = entry?.source
|
|
1126
|
-
|
|
1127
|
-
source?.source === 'github' && typeof source.repo === 'string'
|
|
1128
|
-
? source.repo.toLowerCase().replace(/\.git$/, '')
|
|
1129
|
-
: source?.source === 'git'
|
|
1130
|
-
? githubRepositoryOf(source.url)
|
|
1131
|
-
: null
|
|
1132
|
-
if (repo === repository) {
|
|
1252
|
+
if (sourceRepository(source) === repository) {
|
|
1133
1253
|
names.push(name)
|
|
1134
1254
|
pins.push({
|
|
1135
1255
|
where: `${file} (marketplace ${name})`,
|
|
@@ -1161,7 +1281,7 @@ function marketplacePins(repository, file, text) {
|
|
|
1161
1281
|
* The commit a kit's release stamp (the text of its dist/release.json) names, or null for none or one unreadable.
|
|
1162
1282
|
* @param {string | null} stamp
|
|
1163
1283
|
*/
|
|
1164
|
-
function stampedCommit(stamp) {
|
|
1284
|
+
export function stampedCommit(stamp) {
|
|
1165
1285
|
try {
|
|
1166
1286
|
const parsed = stamp === null ? null : JSON.parse(stamp)
|
|
1167
1287
|
return typeof parsed?.commit === 'string' && COMMIT.test(parsed.commit) ? parsed.commit : null
|
|
@@ -1270,7 +1390,7 @@ export function pinReport({ inRepo, version, stamp, repository, workflows, setti
|
|
|
1270
1390
|
: (differing.length > 0
|
|
1271
1391
|
? `${differing.length} pin(s) differ from @sssb/kit ${version}'s release commit ${commit}`
|
|
1272
1392
|
: `not every pin is there to compare with @sssb/kit ${version}'s release commit ${commit}`) +
|
|
1273
|
-
" (reported, not failed: the pins are in this workspace's own files;
|
|
1393
|
+
" (reported, not failed: the pins are in this workspace's own files; `pnpm exec sssb pin` raises both to the release commit)"),
|
|
1274
1394
|
lines,
|
|
1275
1395
|
}
|
|
1276
1396
|
}
|