@sssb/tools 0.2.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/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/src/commands/doctor.js
CHANGED
|
@@ -38,9 +38,15 @@
|
|
|
38
38
|
// package.json scripts pass to vitest (doctor proves what each configuration would run, never what a CI step's own
|
|
39
39
|
// command line adds); which script runs each app's contract config (doctor lists the contract checks, it never runs
|
|
40
40
|
// them); `dedupePeerDependents: false` missing from pnpm-workspace.yaml (advice); framework cutover rows the
|
|
41
|
-
// workspace's checklist never mentions;
|
|
41
|
+
// workspace's checklist never mentions; an app, or an app's seam package, that the root's knip JSON config (`knip.json`, `knip.jsonc`, `.knip.json` or
|
|
42
|
+
// `.knip.jsonc`) gives no workspace, and a root `@sssb/create` that knip would call unused; and the commits the workspace pins SSSB's reusable workflow and the plugin's
|
|
42
43
|
// marketplace at (`.github/workflows/*.yml`, `.claude/settings.json`), against the release commit the installed
|
|
43
|
-
// @sssb/kit stamps into its package (`dist/release.json`), naming both commits where they differ
|
|
44
|
+
// @sssb/kit stamps into its package (`dist/release.json`), naming both commits where they differ; and the adopter's own
|
|
45
|
+
// settings (doctor-settings.js): on the workspace's GitHub repository, read-only through `gh`, whether `sssb /
|
|
46
|
+
// all-checks` is a required check on the default branch, private vulnerability reporting is on, and, for a private
|
|
47
|
+
// repository in an organization, its Actions policy lets it call SSSB's reusable workflow, and whether the plugin
|
|
48
|
+
// `.claude/settings.json` enables is installed on this machine. A `gh` that is missing, signed out, offline or refused
|
|
49
|
+
// reads "not checked", with the reason; none of it runs when `CI` is set.
|
|
44
50
|
import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs'
|
|
45
51
|
import { createRequire } from 'node:module'
|
|
46
52
|
import path from 'node:path'
|
|
@@ -54,6 +60,7 @@ import {
|
|
|
54
60
|
githubRepositoryOf,
|
|
55
61
|
importerSeamProblems,
|
|
56
62
|
kitPeers,
|
|
63
|
+
knipGaps,
|
|
57
64
|
missingTests,
|
|
58
65
|
nodeLinkerProblem,
|
|
59
66
|
ownTests,
|
|
@@ -68,6 +75,7 @@ import {
|
|
|
68
75
|
versionProblems,
|
|
69
76
|
vitestInvocations,
|
|
70
77
|
} from './doctor-checks.js'
|
|
78
|
+
import { settingsReports } from './doctor-settings.js'
|
|
71
79
|
import {
|
|
72
80
|
projectsRunning,
|
|
73
81
|
runKey,
|
|
@@ -201,6 +209,8 @@ export async function run(args) {
|
|
|
201
209
|
pins(root, kitDir, version, inRepo),
|
|
202
210
|
dedupeAdvice(root, inRepo),
|
|
203
211
|
cutover(root, appDir),
|
|
212
|
+
...knip(root, apps, workspace.packages),
|
|
213
|
+
...settings(root, kitDir),
|
|
204
214
|
]
|
|
205
215
|
return print(results)
|
|
206
216
|
}
|
|
@@ -883,25 +893,51 @@ async function guides(root, apps, inRepo) {
|
|
|
883
893
|
* @returns {Result}
|
|
884
894
|
*/
|
|
885
895
|
function pins(root, kitDir, version, inRepo) {
|
|
886
|
-
const
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
896
|
+
const settingsText = readIfThere(path.join(root, '.claude', 'settings.json'))
|
|
897
|
+
return pinReport({
|
|
898
|
+
inRepo,
|
|
899
|
+
version,
|
|
900
|
+
stamp: readIfThere(path.join(kitDir, 'dist', 'release.json')),
|
|
901
|
+
repository: githubRepositoryOf(readJson(path.join(kitDir, 'package.json')).repository),
|
|
902
|
+
workflows: workflowFiles(root),
|
|
903
|
+
settings: settingsText === null ? null : { file: '.claude/settings.json', text: settingsText },
|
|
904
|
+
})
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/** @param {string} file */
|
|
908
|
+
const readIfThere = (file) => (existsSync(file) ? readFileSync(file, 'utf8') : null)
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* The workspace root's `.github/workflows/*.{yml,yaml}`, sorted, with their text.
|
|
912
|
+
* @param {string} root
|
|
913
|
+
*/
|
|
914
|
+
function workflowFiles(root) {
|
|
915
|
+
const dir = path.join(root, '.github', 'workflows')
|
|
916
|
+
return existsSync(dir)
|
|
917
|
+
? readdirSync(dir)
|
|
890
918
|
.filter((name) => /\.ya?ml$/.test(name))
|
|
891
919
|
.sort()
|
|
892
920
|
.map((name) => ({
|
|
893
921
|
file: `.github/workflows/${name}`,
|
|
894
|
-
text: /** @type {string} */ (
|
|
922
|
+
text: /** @type {string} */ (readIfThere(path.join(dir, name))),
|
|
895
923
|
}))
|
|
896
924
|
: []
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* THE ADOPTER'S OWN SETTINGS, REPORTED, never failed and never written (doctor-settings.js): the workspace's GitHub
|
|
929
|
+
* repository (`sssb / all-checks` required, private vulnerability reporting, the Actions policy a private organization
|
|
930
|
+
* repository needs for SSSB's reusable workflow) read through `gh`, and the plugin `.claude/settings.json` enables
|
|
931
|
+
* against what this machine has installed. Skipped in CI, with a line saying so.
|
|
932
|
+
* @param {string} root
|
|
933
|
+
* @param {string} kitDir
|
|
934
|
+
* @returns {Result[]}
|
|
935
|
+
*/
|
|
936
|
+
function settings(root, kitDir) {
|
|
937
|
+
return settingsReports({
|
|
938
|
+
root,
|
|
939
|
+
workflows: workflowFiles(root),
|
|
940
|
+
kitRepository: githubRepositoryOf(readJson(path.join(kitDir, 'package.json')).repository),
|
|
905
941
|
})
|
|
906
942
|
}
|
|
907
943
|
|
|
@@ -924,6 +960,49 @@ function dedupeAdvice(root, inRepo) {
|
|
|
924
960
|
}
|
|
925
961
|
}
|
|
926
962
|
|
|
963
|
+
/**
|
|
964
|
+
* KNIP: each app, and each app's seam package, has a workspace of its own in the root's knip.json (knipGaps).
|
|
965
|
+
* Reported, never failed; nothing per app when the root's knip config has no `workspaces`, and nothing at all when
|
|
966
|
+
* the root has no knip JSON config.
|
|
967
|
+
* @param {string} root
|
|
968
|
+
* @param {readonly string[]} apps
|
|
969
|
+
* @param {readonly string[]} packages
|
|
970
|
+
* @returns {Result[]}
|
|
971
|
+
*/
|
|
972
|
+
function knip(root, apps, packages) {
|
|
973
|
+
const gaps = knipGaps(root, apps, packages)
|
|
974
|
+
if (gaps === null) return []
|
|
975
|
+
if ('unread' in gaps)
|
|
976
|
+
return [{ status: 'report', title: `knip: not checked: ${gaps.file} does not parse as JSON (${gaps.unread})` }]
|
|
977
|
+
/** @type {Result[]} */
|
|
978
|
+
const results = []
|
|
979
|
+
if (gaps.missing === null) {
|
|
980
|
+
// One config for every package: nothing per app to look for.
|
|
981
|
+
} else if (gaps.missing.length === 0)
|
|
982
|
+
results.push({
|
|
983
|
+
status: 'ok',
|
|
984
|
+
title: `knip: ${gaps.file} gives every app, and every app's seam package, a workspace`,
|
|
985
|
+
})
|
|
986
|
+
else
|
|
987
|
+
results.push({
|
|
988
|
+
status: 'report',
|
|
989
|
+
title:
|
|
990
|
+
`knip: ${gaps.file} gives ${gaps.missing.length} of the apps' packages no workspace, so knip reads them ` +
|
|
991
|
+
"with its defaults and calls the seam's modules and the presets' dependencies unused: add an entry for " +
|
|
992
|
+
"each, like the first app's",
|
|
993
|
+
lines: gaps.missing,
|
|
994
|
+
})
|
|
995
|
+
if (gaps.generator)
|
|
996
|
+
results.push({
|
|
997
|
+
status: 'report',
|
|
998
|
+
title:
|
|
999
|
+
`knip: the root installs @sssb/create, which nothing imports (\`sssb add-app\` loads it from ` +
|
|
1000
|
+
`node_modules), so knip calls it unused: \`pnpm remove -w @sssb/create\` once your apps are written, or ` +
|
|
1001
|
+
`add it to the root's ignoreDependencies in ${gaps.file}`,
|
|
1002
|
+
})
|
|
1003
|
+
return results
|
|
1004
|
+
}
|
|
1005
|
+
|
|
927
1006
|
/**
|
|
928
1007
|
* @param {string} root
|
|
929
1008
|
* @param {string} appDir
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `sssb pin [--app <dir>]`: raises both of a workspace's pins on SSSB to the installed `@sssb/kit`'s release commit
|
|
3
|
+
// (adoption review follow-up 7). An adopter keeps two pins on one commit of SSSB's repository: the reusable workflow's
|
|
4
|
+
// `uses: <owner>/<name>/.github/workflows/app-checks.yml@<sha>` in `.github/workflows/*.yml`, and the plugin marketplace's
|
|
5
|
+
// `ref` in `.claude/settings.json`. `sssb doctor` reports the pins that are not the commit the kit's stamp
|
|
6
|
+
// (`dist/release.json`) names; this command writes them, so an upgrade maintains one version, the npm one.
|
|
7
|
+
//
|
|
8
|
+
// - Idempotent: a pin already on the commit is left byte for byte as it was, and the run says so.
|
|
9
|
+
// - It says what it changed, file and line, with the commit it replaced.
|
|
10
|
+
// - It refuses, and writes nothing, when there is no commit to pin to (SSSB's own repository, where the kit is source,
|
|
11
|
+
// or a kit packed by hand without a stamp), or when the workspace's files are not the shape it can edit: no workflow
|
|
12
|
+
// calls SSSB's reusable workflow, `.claude/settings.json` is not JSON or declares no marketplace from SSSB's
|
|
13
|
+
// repository. Every pin is judged before the first file is written.
|
|
14
|
+
// - A `.claude/settings.json` that is missing is not a refusal, as it is not one for doctor: a workspace whose people do
|
|
15
|
+
// not use Claude Code has no plugin and so no second pin. Its workflow pin is raised and the run says so.
|
|
16
|
+
//
|
|
17
|
+
// The edit is made on the file's text, so what else is in the file, its formatting included, is untouched. The pin is a
|
|
18
|
+
// commit's SHA, never the release's tag, which can be moved (ADR-0014 part 7).
|
|
19
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'
|
|
20
|
+
import path from 'node:path'
|
|
21
|
+
import { readJson, takeAppDir, UsageError, workspaceApps, workspaceRootOf } from '../app.js'
|
|
22
|
+
import { kitDirOf, kitReleaseStampOf, kitVersionOf } from '../kit.js'
|
|
23
|
+
import { githubRepositoryOf, isInRepoKit, sourceRepository, stampedCommit, WORKFLOW_USES } from './doctor-checks.js'
|
|
24
|
+
|
|
25
|
+
const SETTINGS = '.claude/settings.json'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {{ file: string, line?: number, what: string, from: string | null, to: string, comment?: true }} Change one
|
|
29
|
+
* pin: where it is, and the ref it had (null: none) and has; `comment`: only the version in its trailing comment moved
|
|
30
|
+
* @typedef {{ file: string, text: string }} SourceFile a file of the workspace, by the path shown for it, and its text
|
|
31
|
+
* @typedef {{ file: string, text: string, changes: Change[] }} Edited a file with the pins in it, `text` as it will be
|
|
32
|
+
* (the text it was, when no pin in it moves)
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The version in a trailing comment after a workflow pin, in the forms the generator (`# @sssb/create 0.2.0`) and the
|
|
37
|
+
* earlier release notes (`# v0.2.0`) wrote, and `# @sssb/kit 0.2.0`, moved to `version`; any other comment, which is the adopter's, as it was.
|
|
38
|
+
* @param {string} rest what follows the ref on the `uses:` line
|
|
39
|
+
* @param {string} version
|
|
40
|
+
*/
|
|
41
|
+
function withVersionComment(rest, version) {
|
|
42
|
+
return rest.replace(/^(['"]?\s+#\s*(?:@sssb\/(?:create|kit)\s+|v))\d[\w.+-]*(\s*)$/, `$1${version}$2`)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The workflow files' pins of `repository`'s reusable workflows, moved to `commit`.
|
|
47
|
+
* @param {SourceFile[]} workflows
|
|
48
|
+
* @param {string} repository `owner/name`, lowercased
|
|
49
|
+
* @param {string} commit
|
|
50
|
+
* @param {string} version the kit's, for a comment that names it
|
|
51
|
+
* @returns {Edited[]}
|
|
52
|
+
*/
|
|
53
|
+
function raiseWorkflowPins(workflows, repository, commit, version) {
|
|
54
|
+
/** @type {Edited[]} */
|
|
55
|
+
const edited = []
|
|
56
|
+
for (const { file, text } of workflows) {
|
|
57
|
+
/** @type {Change[]} */
|
|
58
|
+
const changes = []
|
|
59
|
+
const lines = text.split('\n').map((line, index) => {
|
|
60
|
+
const match = WORKFLOW_USES.exec(line)
|
|
61
|
+
if (match?.[2]?.toLowerCase() !== repository) return line
|
|
62
|
+
const [, prefix = '', , workflow = '', ref = '', rest = ''] = match
|
|
63
|
+
const next = `${prefix}${commit}${withVersionComment(rest, version)}`
|
|
64
|
+
changes.push({
|
|
65
|
+
file,
|
|
66
|
+
line: index + 1,
|
|
67
|
+
what: workflow,
|
|
68
|
+
from: ref,
|
|
69
|
+
to: commit,
|
|
70
|
+
...(ref === commit && next !== line ? { comment: true } : {}),
|
|
71
|
+
})
|
|
72
|
+
return next
|
|
73
|
+
})
|
|
74
|
+
if (changes.length > 0) edited.push({ file, text: lines.join('\n'), changes })
|
|
75
|
+
}
|
|
76
|
+
return edited
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @typedef {{ kind: 'object', start: number, end: number, props: { key: string, keyStart: number, node: Node }[] }
|
|
81
|
+
* | { kind: 'array', start: number, end: number }
|
|
82
|
+
* | { kind: 'string', start: number, end: number, value: string }
|
|
83
|
+
* | { kind: 'other', start: number, end: number }} Node a JSON value and where its text sits
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Where each value of a JSON text sits (the offsets of its first and one past its last character), so a value can be
|
|
88
|
+
* replaced without printing the whole file again. The text has already been through `JSON.parse`, so it is valid.
|
|
89
|
+
* @param {string} text
|
|
90
|
+
* @returns {Node}
|
|
91
|
+
*/
|
|
92
|
+
function locate(text) {
|
|
93
|
+
let i = 0
|
|
94
|
+
const space = () => {
|
|
95
|
+
while (i < text.length && /\s/.test(text.charAt(i))) i++
|
|
96
|
+
}
|
|
97
|
+
/** @returns {Node & { kind: 'string' }} */
|
|
98
|
+
const string = () => {
|
|
99
|
+
const start = i
|
|
100
|
+
for (i++; text.charAt(i) !== '"'; i++) if (text.charAt(i) === '\\') i++
|
|
101
|
+
i++
|
|
102
|
+
return { kind: 'string', start, end: i, value: JSON.parse(text.slice(start, i)) }
|
|
103
|
+
}
|
|
104
|
+
/** @returns {Node} */
|
|
105
|
+
const value = () => {
|
|
106
|
+
space()
|
|
107
|
+
const start = i
|
|
108
|
+
const first = text.charAt(i)
|
|
109
|
+
if (first === '{') {
|
|
110
|
+
/** @type {(Node & { kind: 'object' })['props']} */
|
|
111
|
+
const props = []
|
|
112
|
+
i++
|
|
113
|
+
space()
|
|
114
|
+
while (text.charAt(i) !== '}') {
|
|
115
|
+
space()
|
|
116
|
+
const key = string()
|
|
117
|
+
space()
|
|
118
|
+
i++ // the colon
|
|
119
|
+
props.push({ key: key.value, keyStart: key.start, node: value() })
|
|
120
|
+
space()
|
|
121
|
+
if (text.charAt(i) === ',') i++
|
|
122
|
+
space()
|
|
123
|
+
}
|
|
124
|
+
i++
|
|
125
|
+
return { kind: 'object', start, end: i, props }
|
|
126
|
+
}
|
|
127
|
+
if (first === '[') {
|
|
128
|
+
i++
|
|
129
|
+
space()
|
|
130
|
+
while (text.charAt(i) !== ']') {
|
|
131
|
+
value()
|
|
132
|
+
space()
|
|
133
|
+
if (text.charAt(i) === ',') i++
|
|
134
|
+
space()
|
|
135
|
+
}
|
|
136
|
+
i++
|
|
137
|
+
return { kind: 'array', start, end: i }
|
|
138
|
+
}
|
|
139
|
+
if (first === '"') return string()
|
|
140
|
+
while (i < text.length && !/[\s,\]}]/.test(text.charAt(i))) i++
|
|
141
|
+
return { kind: 'other', start, end: i }
|
|
142
|
+
}
|
|
143
|
+
return value()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The property of an object node named `key`; of several, the last, which is the one `JSON.parse` keeps, and so the one
|
|
148
|
+
* Claude Code and `sssb doctor` read.
|
|
149
|
+
* @param {(Node & { kind: 'object' })['props']} props
|
|
150
|
+
* @param {string} key
|
|
151
|
+
*/
|
|
152
|
+
const lastProp = (props, key) => props.findLast((prop) => prop.key === key)
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The marketplaces `.claude/settings.json` declares from `repository`, their `ref` moved to `commit`: replaced where
|
|
156
|
+
* there is one, added to the `source` where there is none (a marketplace that follows the default branch).
|
|
157
|
+
* @param {SourceFile} settings
|
|
158
|
+
* @param {string} repository `owner/name`, lowercased
|
|
159
|
+
* @param {string} commit
|
|
160
|
+
* @returns {{ edited: Edited | null, refusal: string | null }}
|
|
161
|
+
*/
|
|
162
|
+
function raiseMarketplacePins({ file, text }, repository, commit) {
|
|
163
|
+
try {
|
|
164
|
+
JSON.parse(text)
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return {
|
|
167
|
+
edited: null,
|
|
168
|
+
refusal: `${file} is not JSON this command can read (${error instanceof Error ? error.message : error})`,
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const root = locate(text)
|
|
172
|
+
const marketplaces = root.kind === 'object' ? lastProp(root.props, 'extraKnownMarketplaces') : null
|
|
173
|
+
/** @type {{ start: number, end: number, insert: string }[]} */
|
|
174
|
+
const edits = []
|
|
175
|
+
/** @type {Change[]} */
|
|
176
|
+
const changes = []
|
|
177
|
+
if (marketplaces?.node.kind === 'object') {
|
|
178
|
+
for (const { key: name, node: entry } of marketplaces.node.props) {
|
|
179
|
+
const source = entry.kind === 'object' ? lastProp(entry.props, 'source')?.node : null
|
|
180
|
+
if (source?.kind !== 'object') continue
|
|
181
|
+
const valueOf = (/** @type {string} */ key) => {
|
|
182
|
+
const node = lastProp(source.props, key)?.node
|
|
183
|
+
return node?.kind === 'string' ? node.value : undefined
|
|
184
|
+
}
|
|
185
|
+
if (
|
|
186
|
+
sourceRepository({ source: valueOf('source'), repo: valueOf('repo'), url: valueOf('url') }) !==
|
|
187
|
+
repository
|
|
188
|
+
)
|
|
189
|
+
continue
|
|
190
|
+
const what = `marketplace ${name}`
|
|
191
|
+
const ref = lastProp(source.props, 'ref')
|
|
192
|
+
if (ref === undefined) {
|
|
193
|
+
const last = source.props.at(-1)
|
|
194
|
+
const first = source.props[0]
|
|
195
|
+
if (last === undefined || first === undefined) continue
|
|
196
|
+
const lineStart = text.lastIndexOf('\n', first.keyStart) + 1
|
|
197
|
+
const indent = text.slice(lineStart, first.keyStart)
|
|
198
|
+
const multiline = text.slice(source.start, first.keyStart).includes('\n') && /^\s*$/.test(indent)
|
|
199
|
+
edits.push({
|
|
200
|
+
start: last.node.end,
|
|
201
|
+
end: last.node.end,
|
|
202
|
+
insert: multiline
|
|
203
|
+
? `,${text.includes('\r\n') ? '\r\n' : '\n'}${indent}"ref": ${JSON.stringify(commit)}`
|
|
204
|
+
: `, "ref": ${JSON.stringify(commit)}`,
|
|
205
|
+
})
|
|
206
|
+
changes.push({ file, what, from: null, to: commit })
|
|
207
|
+
} else if (ref.node.kind !== 'string') {
|
|
208
|
+
return { edited: null, refusal: `${file} (${what}): its \`ref\` is not a string, so it is not a pin` }
|
|
209
|
+
} else {
|
|
210
|
+
if (ref.node.value !== commit)
|
|
211
|
+
edits.push({ start: ref.node.start, end: ref.node.end, insert: JSON.stringify(commit) })
|
|
212
|
+
changes.push({ file, what, from: ref.node.value, to: commit })
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (changes.length === 0)
|
|
217
|
+
return {
|
|
218
|
+
edited: null,
|
|
219
|
+
refusal: `${file} declares no marketplace from ${repository}: there is no plugin pin to raise`,
|
|
220
|
+
}
|
|
221
|
+
let next = text
|
|
222
|
+
for (const { start, end, insert } of edits.sort((a, b) => b.start - a.start))
|
|
223
|
+
next = next.slice(0, start) + insert + next.slice(end)
|
|
224
|
+
return { edited: { file, text: next, changes }, refusal: null }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Every pin of `repository` in the workspace's files, raised to `commit`; or what stops it. Pure: it reads nothing and
|
|
229
|
+
* writes nothing.
|
|
230
|
+
* @param {{
|
|
231
|
+
* commit: string,
|
|
232
|
+
* version: string,
|
|
233
|
+
* repository: string,
|
|
234
|
+
* workflows: SourceFile[],
|
|
235
|
+
* settings: SourceFile | null,
|
|
236
|
+
* }} input `version`: the installed kit's, which a workflow pin's comment may name; `settings`: null when the workspace
|
|
237
|
+
* has no `.claude/settings.json`
|
|
238
|
+
* @returns {{ files: Edited[], refusals: string[], notes: string[] }} `files`: each file that holds a pin, `text` as it
|
|
239
|
+
* will be; nothing is to be written when `refusals` is not empty
|
|
240
|
+
*/
|
|
241
|
+
export function planPins({ commit, version, repository, workflows, settings }) {
|
|
242
|
+
/** @type {string[]} */
|
|
243
|
+
const refusals = []
|
|
244
|
+
/** @type {string[]} */
|
|
245
|
+
const notes = []
|
|
246
|
+
const files = raiseWorkflowPins(workflows, repository, commit, version)
|
|
247
|
+
if (files.length === 0)
|
|
248
|
+
refusals.push(
|
|
249
|
+
`no workflow under .github/workflows calls a reusable workflow of ${repository}, so there is no workflow pin to raise`,
|
|
250
|
+
)
|
|
251
|
+
if (settings === null)
|
|
252
|
+
notes.push(
|
|
253
|
+
`no ${SETTINGS}: no plugin marketplace pin to raise (a workspace whose people do not use Claude Code has none)`,
|
|
254
|
+
)
|
|
255
|
+
else {
|
|
256
|
+
const { edited, refusal } = raiseMarketplacePins(settings, repository, commit)
|
|
257
|
+
if (refusal !== null) refusals.push(refusal)
|
|
258
|
+
if (edited !== null) files.push(edited)
|
|
259
|
+
}
|
|
260
|
+
return { files, refusals, notes }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The app whose kit's stamp is read, as `sssb guide` chooses it: `--app <dir>`; else the working directory when it
|
|
265
|
+
* resolves `@sssb/kit`; else, at a workspace root that takes no framework (a generated workspace's), the first of the
|
|
266
|
+
* workspace's apps that resolves one (`guide` and `doctor` take its first app, resolving or not).
|
|
267
|
+
* @param {string[]} args
|
|
268
|
+
* @param {string} [cwd]
|
|
269
|
+
* @returns {string}
|
|
270
|
+
*/
|
|
271
|
+
function pinApp(args, cwd = process.cwd()) {
|
|
272
|
+
if (args.includes('--app')) return takeAppDir(args, cwd)
|
|
273
|
+
const here = path.resolve(cwd)
|
|
274
|
+
const resolves = (/** @type {string} */ dir) => {
|
|
275
|
+
try {
|
|
276
|
+
kitDirOf(dir)
|
|
277
|
+
return true
|
|
278
|
+
} catch {
|
|
279
|
+
return false
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const isRoot = existsSync(path.join(here, 'pnpm-workspace.yaml')) && existsSync(path.join(here, 'package.json'))
|
|
283
|
+
if (isRoot && !resolves(here)) {
|
|
284
|
+
const app = workspaceApps(here)
|
|
285
|
+
.map((dir) => path.resolve(here, dir))
|
|
286
|
+
.find(resolves)
|
|
287
|
+
if (app === undefined) {
|
|
288
|
+
throw new UsageError(
|
|
289
|
+
`sssb pin: ${here} is a workspace root that takes no @sssb/kit, and none of its apps (\`sssb.apps\` in its ` +
|
|
290
|
+
'package.json, or apps/<name>) resolves one: pass --app <dir>',
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
return app
|
|
294
|
+
}
|
|
295
|
+
// Anything else: the app is the working directory, and kitDirOf's refusal says to name one with --app.
|
|
296
|
+
return takeAppDir(args, cwd)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** @param {string} file */
|
|
300
|
+
const readIfThere = (file) => (existsSync(file) ? readFileSync(file, 'utf8') : null)
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The workspace root's `.github/workflows/*.{yml,yaml}`, sorted, with their text (as doctor reads them).
|
|
304
|
+
* @param {string} root
|
|
305
|
+
* @returns {SourceFile[]}
|
|
306
|
+
*/
|
|
307
|
+
function workflowFiles(root) {
|
|
308
|
+
const dir = path.join(root, '.github', 'workflows')
|
|
309
|
+
if (!existsSync(dir)) return []
|
|
310
|
+
return readdirSync(dir)
|
|
311
|
+
.filter((name) => /\.ya?ml$/.test(name))
|
|
312
|
+
.sort()
|
|
313
|
+
.map((name) => ({
|
|
314
|
+
file: `.github/workflows/${name}`,
|
|
315
|
+
text: /** @type {string} */ (readIfThere(path.join(dir, name))),
|
|
316
|
+
}))
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** @param {string[]} args */
|
|
320
|
+
export async function run(args) {
|
|
321
|
+
const appDir = realpathSync(pinApp(args))
|
|
322
|
+
if (args.length > 0) throw new UsageError(`sssb pin: unknown argument ${args[0]} (usage: pin [--app <dir>])`)
|
|
323
|
+
const root = realpathSync(workspaceRootOf(appDir))
|
|
324
|
+
const kitDir = kitDirOf(appDir)
|
|
325
|
+
const version = kitVersionOf(appDir)
|
|
326
|
+
const kit = `@sssb/kit ${version}`
|
|
327
|
+
const stamp = kitReleaseStampOf(appDir)
|
|
328
|
+
|
|
329
|
+
if (isInRepoKit(kitDir, root)) {
|
|
330
|
+
throw new UsageError(
|
|
331
|
+
`sssb pin: ${kit} here is this workspace's own source, which carries no release stamp (dist/release.json), so ` +
|
|
332
|
+
'there is no release commit to pin SSSB to. Nothing was changed',
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
const commit = stampedCommit(stamp)
|
|
336
|
+
if (commit === null) {
|
|
337
|
+
throw new UsageError(
|
|
338
|
+
`sssb pin: ${kit} ` +
|
|
339
|
+
(stamp === null
|
|
340
|
+
? 'carries no release stamp (dist/release.json): it was packed by hand, or before ADR-0014 P13 stamped one'
|
|
341
|
+
: 'has a release stamp (dist/release.json) that names no commit') +
|
|
342
|
+
', so there is no release commit to pin SSSB to. Nothing was changed',
|
|
343
|
+
)
|
|
344
|
+
}
|
|
345
|
+
const repository = githubRepositoryOf(readJson(path.join(kitDir, 'package.json')).repository)
|
|
346
|
+
if (repository === null) {
|
|
347
|
+
throw new UsageError(
|
|
348
|
+
`sssb pin: ${kit}'s package.json names no GitHub repository, so this cannot tell which pins are SSSB's. Nothing was changed`,
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const settingsText = readIfThere(path.join(root, ...SETTINGS.split('/')))
|
|
353
|
+
const workflows = workflowFiles(root)
|
|
354
|
+
const plan = planPins({
|
|
355
|
+
commit,
|
|
356
|
+
version,
|
|
357
|
+
repository,
|
|
358
|
+
workflows,
|
|
359
|
+
settings: settingsText === null ? null : { file: SETTINGS, text: settingsText },
|
|
360
|
+
})
|
|
361
|
+
if (plan.refusals.length > 0) {
|
|
362
|
+
throw new UsageError(
|
|
363
|
+
[
|
|
364
|
+
`sssb pin: not raising any pin, because the workspace's files are not what this command edits (${kit}, release commit ${commit}):`,
|
|
365
|
+
...plan.refusals.map((refusal) => ` ${refusal}`),
|
|
366
|
+
'Nothing was changed. Edit the pins by hand, as the release notes say, or put the files back in the shape ' +
|
|
367
|
+
'`sssb doctor` reads, then run this again',
|
|
368
|
+
].join('\n'),
|
|
369
|
+
)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
console.log(`sssb pin: ${kit}, release commit ${commit}`)
|
|
373
|
+
const before = new Map(
|
|
374
|
+
[...workflows, ...(settingsText === null ? [] : [{ file: SETTINGS, text: settingsText }])].map((f) => [
|
|
375
|
+
f.file,
|
|
376
|
+
f.text,
|
|
377
|
+
]),
|
|
378
|
+
)
|
|
379
|
+
let moved = 0
|
|
380
|
+
/** @type {string[]} */
|
|
381
|
+
const marketplaces = []
|
|
382
|
+
for (const { file, text, changes } of plan.files) {
|
|
383
|
+
const changed = text !== before.get(file)
|
|
384
|
+
for (const change of changes) {
|
|
385
|
+
const where = `${file}${change.line === undefined ? '' : `:${change.line}`} (${change.what})`
|
|
386
|
+
const was = change.from === null ? 'no ref' : change.from
|
|
387
|
+
if (change.comment) {
|
|
388
|
+
console.log(` ${where}: already ${change.to}; the version in its comment is now ${version}`)
|
|
389
|
+
moved++
|
|
390
|
+
} else if (change.from === change.to) console.log(` ${where}: already ${change.to}`)
|
|
391
|
+
else {
|
|
392
|
+
console.log(` ${where}: ${was} -> ${change.to}`)
|
|
393
|
+
moved++
|
|
394
|
+
}
|
|
395
|
+
if (change.what.startsWith('marketplace ')) marketplaces.push(change.what.slice('marketplace '.length))
|
|
396
|
+
}
|
|
397
|
+
if (changed) writeFileSync(path.join(root, ...file.split('/')), text)
|
|
398
|
+
}
|
|
399
|
+
for (const note of plan.notes) console.log(` ${note}`)
|
|
400
|
+
if (moved === 0) console.log('Every pin was already the release commit: nothing changed.')
|
|
401
|
+
else {
|
|
402
|
+
console.log(`Raised ${moved} pin(s) to ${commit}. Review the diff, then commit it with the package versions.`)
|
|
403
|
+
if (marketplaces.length > 0) {
|
|
404
|
+
console.log(
|
|
405
|
+
'Then each person updates their installed plugin and restarts Claude Code: ' +
|
|
406
|
+
`\`claude plugin marketplace update ${marketplaces[0]}\`, then \`claude plugin update <plugin>@${marketplaces[0]} --scope project\``,
|
|
407
|
+
)
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return 0
|
|
411
|
+
}
|