connore 0.1.0 → 0.1.1

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.
Files changed (128) hide show
  1. package/README.md +53 -46
  2. package/package.json +35 -33
  3. package/packs/CONTRIBUTING.md +32 -0
  4. package/packs/base/catalog/README.md +24 -0
  5. package/packs/base/catalog/api.md +46 -0
  6. package/packs/base/catalog/auth.md +46 -0
  7. package/packs/base/catalog/backend.md +46 -0
  8. package/packs/base/catalog/blueprints.md +46 -0
  9. package/packs/base/catalog/components.md +46 -0
  10. package/packs/base/catalog/database.md +46 -0
  11. package/packs/base/catalog/docs.md +46 -0
  12. package/packs/base/catalog/github.md +46 -0
  13. package/packs/base/catalog/golden.md +46 -0
  14. package/packs/base/catalog/motion.md +46 -0
  15. package/packs/base/catalog/plan.md +46 -0
  16. package/packs/base/catalog/screenshots.md +46 -0
  17. package/packs/base/catalog/skills.md +46 -0
  18. package/packs/base/catalog/tokens.md +47 -0
  19. package/packs/base/planning/START.md +28 -0
  20. package/packs/base/planning/blueprint-template.md +46 -0
  21. package/packs/base/planning/plan-template.md +45 -0
  22. package/packs/base/rules/anti-patterns.md +43 -0
  23. package/packs/base/rules/code-style.md +43 -0
  24. package/packs/base/rules/definition-of-done.md +47 -0
  25. package/packs/base/rules/dependency-policy.md +43 -0
  26. package/packs/base/rules/error-handling.md +43 -0
  27. package/packs/base/rules/security-baseline.md +43 -0
  28. package/packs/base/templates/bug-report.md +43 -0
  29. package/packs/base/templates/feature-spec.md +43 -0
  30. package/packs/base/templates/handoff.md +43 -0
  31. package/packs/base/templates/project-brief.md +43 -0
  32. package/packs/base/workflow/build-loop.md +48 -0
  33. package/packs/base/workflow/code-review.md +43 -0
  34. package/packs/base/workflow/debugging.md +43 -0
  35. package/packs/base/workflow/git-and-commits.md +43 -0
  36. package/packs/base/workflow/idea-to-spec.md +43 -0
  37. package/packs/base/workflow/session-handoff.md +43 -0
  38. package/packs/base/workflow/spec-to-plan.md +43 -0
  39. package/packs/nextjs-fullstack/backend/api-conventions.md +48 -0
  40. package/packs/nextjs-fullstack/backend/auth-and-roles.md +43 -0
  41. package/packs/nextjs-fullstack/backend/background-jobs.md +43 -0
  42. package/packs/nextjs-fullstack/backend/caching.md +43 -0
  43. package/packs/nextjs-fullstack/backend/database.md +43 -0
  44. package/packs/nextjs-fullstack/backend/file-uploads.md +43 -0
  45. package/packs/nextjs-fullstack/backend/logging.md +43 -0
  46. package/packs/nextjs-fullstack/backend/migrations.md +43 -0
  47. package/packs/nextjs-fullstack/backend/rate-limiting.md +43 -0
  48. package/packs/nextjs-fullstack/backend/secrets-and-env.md +43 -0
  49. package/packs/nextjs-fullstack/backend/validation.md +43 -0
  50. package/packs/nextjs-fullstack/blueprints/ai-app.md +84 -0
  51. package/packs/nextjs-fullstack/blueprints/internal-tool.md +83 -0
  52. package/packs/nextjs-fullstack/blueprints/saas-app.md +83 -0
  53. package/packs/nextjs-fullstack/build-deploy/ci.md +43 -0
  54. package/packs/nextjs-fullstack/build-deploy/deploy.md +43 -0
  55. package/packs/nextjs-fullstack/build-deploy/monitoring.md +43 -0
  56. package/packs/nextjs-fullstack/build-deploy/performance.md +43 -0
  57. package/packs/nextjs-fullstack/build-deploy/project-structure.md +43 -0
  58. package/packs/nextjs-fullstack/build-deploy/release-checklist.md +43 -0
  59. package/packs/nextjs-fullstack/build-deploy/testing.md +43 -0
  60. package/packs/nextjs-fullstack/evals/README.md +43 -0
  61. package/packs/nextjs-fullstack/examples/golden-app/README.md +43 -0
  62. package/packs/nextjs-fullstack/frontend/accessibility.md +43 -0
  63. package/packs/nextjs-fullstack/frontend/components.md +43 -0
  64. package/packs/nextjs-fullstack/frontend/forms.md +43 -0
  65. package/packs/nextjs-fullstack/frontend/layout.md +43 -0
  66. package/packs/nextjs-fullstack/frontend/loading-empty-error.md +43 -0
  67. package/packs/nextjs-fullstack/frontend/motion.md +43 -0
  68. package/packs/nextjs-fullstack/frontend/responsive.md +43 -0
  69. package/packs/nextjs-fullstack/frontend/seo.md +43 -0
  70. package/packs/nextjs-fullstack/frontend/tables.md +43 -0
  71. package/packs/nextjs-fullstack/frontend/tokens.md +43 -0
  72. package/packs/nextjs-fullstack/integrations/analytics.md +43 -0
  73. package/packs/nextjs-fullstack/integrations/email.md +43 -0
  74. package/packs/nextjs-fullstack/integrations/llm-apis.md +43 -0
  75. package/packs/nextjs-fullstack/integrations/oauth.md +43 -0
  76. package/packs/nextjs-fullstack/integrations/object-storage.md +43 -0
  77. package/packs/nextjs-fullstack/integrations/payments.md +43 -0
  78. package/packs/nextjs-fullstack/integrations/queues.md +43 -0
  79. package/packs/nextjs-fullstack/integrations/search.md +43 -0
  80. package/packs/nextjs-fullstack/integrations/webhooks.md +43 -0
  81. package/packs/nextjs-fullstack/playbooks/add-ai-feature.md +43 -0
  82. package/packs/nextjs-fullstack/playbooks/add-auth.md +43 -0
  83. package/packs/nextjs-fullstack/playbooks/add-billing.md +50 -0
  84. package/packs/nextjs-fullstack/playbooks/add-crud.md +43 -0
  85. package/packs/nextjs-fullstack/playbooks/add-email.md +43 -0
  86. package/packs/nextjs-fullstack/playbooks/add-file-upload.md +43 -0
  87. package/packs/nextjs-fullstack/playbooks/add-webhook.md +43 -0
  88. package/packs/nextjs-fullstack/playbooks/ship-to-prod.md +43 -0
  89. package/src/cli.js +288 -90
  90. package/src/commands/add.js +61 -57
  91. package/src/commands/catalog.js +21 -0
  92. package/src/commands/doctor.js +42 -0
  93. package/src/commands/hook.js +67 -50
  94. package/src/commands/init.js +69 -55
  95. package/src/commands/learn.js +39 -0
  96. package/src/commands/lint.js +5 -0
  97. package/src/commands/list.js +10 -10
  98. package/src/commands/log.js +25 -0
  99. package/src/commands/memory.js +14 -0
  100. package/src/commands/plan.js +41 -0
  101. package/src/commands/reindex.js +8 -8
  102. package/src/commands/review.js +31 -0
  103. package/src/commands/search.js +21 -0
  104. package/src/commands/stats.js +35 -0
  105. package/src/commands/undo.js +7 -0
  106. package/src/commands/update.js +16 -14
  107. package/src/lib/catalog.js +32 -0
  108. package/src/lib/config.js +64 -0
  109. package/src/lib/fs-safe.js +36 -11
  110. package/src/lib/gitlog.js +75 -0
  111. package/src/lib/ignore.js +47 -0
  112. package/src/lib/index.js +27 -12
  113. package/src/lib/lessons.js +173 -0
  114. package/src/lib/lint.js +67 -0
  115. package/src/lib/lock.js +47 -0
  116. package/src/lib/memory.js +49 -0
  117. package/src/lib/mutate.js +24 -0
  118. package/src/lib/packs.js +10 -0
  119. package/src/lib/paths.js +6 -2
  120. package/src/lib/plan.js +156 -0
  121. package/src/lib/pointers.js +3 -3
  122. package/src/lib/provider.js +27 -0
  123. package/src/lib/report.js +20 -4
  124. package/src/lib/run.js +73 -0
  125. package/src/lib/search.js +40 -0
  126. package/src/lib/secrets.js +25 -0
  127. package/src/lib/signals.js +55 -0
  128. package/src/lib/sync.js +20 -12
package/src/cli.js CHANGED
@@ -1,90 +1,288 @@
1
- import { add } from "./commands/add.js"
2
- import { printIndex } from "./commands/hook.js"
3
- import { init } from "./commands/init.js"
4
- import { list } from "./commands/list.js"
5
- import { reindex } from "./commands/reindex.js"
6
- import { update } from "./commands/update.js"
7
- import { createReport } from "./lib/report.js"
8
-
9
- const USAGE = `connore <command> [options]
10
-
11
- init [--tools all|claude,cursor,codex,copilot,windsurf,gemini] [--pack frontend] [--hooks] [--dry-run]
12
- add <path> [--type t] [--tags a,b] [--dry-run]
13
- update [--pack frontend] [--dry-run]
14
- list
15
- index [--dry-run]
16
- hook
17
- `
18
-
19
- function parse(argv) {
20
- const flags = { dryRun: false, hooks: false }
21
- const positionals = []
22
- for (let index = 0; index < argv.length; index += 1) {
23
- const arg = argv[index]
24
- if (arg === "--dry-run") flags.dryRun = true
25
- else if (arg === "--hooks") flags.hooks = true
26
- else if (arg === "--tools" || arg === "--pack" || arg === "--type" || arg === "--tags") {
27
- const value = argv[index + 1]
28
- if (!value || value.startsWith("--")) throw new Error(`Missing value for ${arg}.`)
29
- flags[arg.slice(2)] = value
30
- index += 1
31
- } else if (arg.startsWith("--")) throw new Error(`Unknown flag ${arg}.`)
32
- else positionals.push(arg)
33
- }
34
- return { command: positionals[0], args: positionals.slice(1), flags }
35
- }
36
-
37
- export async function run(argv, io = {}) {
38
- const stdout = io.stdout || process.stdout
39
- const stderr = io.stderr || process.stderr
40
- const cwd = io.cwd || process.cwd()
41
- try {
42
- const { command, args, flags } = parse(argv)
43
- if (!command) {
44
- stdout.write(USAGE)
45
- return 1
46
- }
47
- const report = createReport(flags.dryRun)
48
- if (command === "init") {
49
- if (args.length) throw new Error("init does not take a path. Run it inside the project.")
50
- await init(cwd, flags, report)
51
- report.print(stdout)
52
- return 0
53
- }
54
- if (flags.hooks) throw new Error("--hooks is only valid with init.")
55
- if (flags.tools) throw new Error("--tools is only valid with init.")
56
- if (command === "add") {
57
- if (flags.pack) throw new Error("--pack is only valid with init and update.")
58
- if (args.length !== 1) throw new Error("Usage: connore add <path>.")
59
- await add(cwd, args[0], flags, report)
60
- report.print(stdout)
61
- return 0
62
- }
63
- if (flags.type || flags.tags) throw new Error("--type and --tags are only valid with add.")
64
- if (command === "update") {
65
- if (args.length) throw new Error("update does not take a path.")
66
- await update(cwd, flags, report)
67
- report.print(stdout)
68
- return 0
69
- }
70
- if (flags.pack) throw new Error("--pack is only valid with init and update.")
71
- if (command === "list") {
72
- await list(cwd, stdout)
73
- if (flags.dryRun) report.print(stdout)
74
- return 0
75
- }
76
- if (command === "index") {
77
- await reindex(cwd, report)
78
- report.print(stdout)
79
- return 0
80
- }
81
- if (command === "hook") {
82
- await printIndex(cwd, stdout)
83
- return 0
84
- }
85
- throw new Error(`Unknown command "${command}".`)
86
- } catch (error) {
87
- stderr.write(`${error.message}\n`)
88
- return 1
89
- }
90
- }
1
+ import { join } from "node:path"
2
+ import { add } from "./commands/add.js"
3
+ import { catalog } from "./commands/catalog.js"
4
+ import { doctor } from "./commands/doctor.js"
5
+ import { printIndex } from "./commands/hook.js"
6
+ import { init } from "./commands/init.js"
7
+ import { learn } from "./commands/learn.js"
8
+ import { lint } from "./commands/lint.js"
9
+ import { list } from "./commands/list.js"
10
+ import { memory } from "./commands/memory.js"
11
+ import { log } from "./commands/log.js"
12
+ import { plan } from "./commands/plan.js"
13
+ import { search } from "./commands/search.js"
14
+ import { reindex } from "./commands/reindex.js"
15
+ import { review } from "./commands/review.js"
16
+ import { stats } from "./commands/stats.js"
17
+ import { undo } from "./commands/undo.js"
18
+ import { update } from "./commands/update.js"
19
+ import { ensureConfig } from "./lib/config.js"
20
+ import { readOptional } from "./lib/fs-safe.js"
21
+ import { withLock } from "./lib/lock.js"
22
+ import { packDir } from "./lib/packs.js"
23
+ import { parseTools } from "./lib/pointers.js"
24
+ import { createReport } from "./lib/report.js"
25
+ import { sealRun } from "./lib/run.js"
26
+
27
+ const USAGE = `connore <command> [options]
28
+
29
+ init [--tools all|claude,cursor,codex,copilot,windsurf,gemini] [--pack frontend] [--hooks] [--include-drafts] [--dry-run] [--json]
30
+ add <path> [--type t] [--tags a,b] [--dry-run] [--json]
31
+ update [--pack name] [--include-drafts] [--dry-run] [--json]
32
+ list [--json]
33
+ index [--include-drafts] [--dry-run] [--json]
34
+ hook [--json]
35
+ lint [--json]
36
+ doctor [--json]
37
+ undo [--json]
38
+ log <fix|decision|failure> "text" [--files a,b] [--json]
39
+ memory [project|architecture|decision|agent|preferences|task|knowledge] ["text"] [--json]
40
+ catalog [--json]
41
+ learn [--input file] [--yes] [--json]
42
+ review [accept|reject|edit <id>] [--mistake text] [--fix text] [--json]
43
+ stats [--json]
44
+ search "query" [--include-drafts] [--json]
45
+ plan init <blueprint> | status | next | done <task> [--json]
46
+ `
47
+
48
+ const VALUE_FLAGS = new Set(["--tools", "--pack", "--type", "--tags", "--files", "--mistake", "--fix", "--input"])
49
+ const WRITES = new Set(["init", "add", "update", "index", "undo", "log", "learn", "review", "plan", "memory"])
50
+
51
+ function usage(message) {
52
+ const error = new Error(message)
53
+ error.exitCode = 2
54
+ return error
55
+ }
56
+
57
+ function parse(argv) {
58
+ const flags = { dryRun: false, hooks: false, json: false, includeDrafts: false, yes: false }
59
+ const positionals = []
60
+ for (let index = 0; index < argv.length; index += 1) {
61
+ const arg = argv[index]
62
+ if (arg === "--dry-run") flags.dryRun = true
63
+ else if (arg === "--hooks") flags.hooks = true
64
+ else if (arg === "--json") flags.json = true
65
+ else if (arg === "--include-drafts") flags.includeDrafts = true
66
+ else if (arg === "--yes") flags.yes = true
67
+ else if (VALUE_FLAGS.has(arg)) {
68
+ const value = argv[index + 1]
69
+ if (!value || value.startsWith("--")) throw usage(`Missing value for ${arg}.`)
70
+ flags[arg.slice(2)] = value
71
+ index += 1
72
+ } else if (arg.startsWith("--")) throw usage(`Unknown flag ${arg}.`)
73
+ else positionals.push(arg)
74
+ }
75
+ return { command: positionals[0], args: positionals.slice(1), flags }
76
+ }
77
+
78
+ function printJson(stdout, payload) {
79
+ stdout.write(`${JSON.stringify(payload)}\n`)
80
+ }
81
+
82
+ export async function run(argv, io = {}) {
83
+ const stdout = io.stdout || process.stdout
84
+ const stderr = io.stderr || process.stderr
85
+ const cwd = io.cwd || process.cwd()
86
+ let flags = { json: argv.includes("--json") }
87
+ try {
88
+ const parsed = parse(argv)
89
+ flags = parsed.flags
90
+ const { command, args } = parsed
91
+ if (!command) {
92
+ if (flags.json) printJson(stdout, { ok: false, error: "Missing command." })
93
+ else stdout.write(USAGE)
94
+ return 2
95
+ }
96
+ if (command === "init") {
97
+ if (args.length) throw usage("init does not take a path. Run it inside the project.")
98
+ if (flags.tools) {
99
+ try {
100
+ parseTools(flags.tools)
101
+ } catch (error) {
102
+ error.exitCode = 2
103
+ throw error
104
+ }
105
+ }
106
+ try {
107
+ await packDir(flags.pack || "frontend")
108
+ } catch (error) {
109
+ error.exitCode = 2
110
+ throw error
111
+ }
112
+ }
113
+ const report = createReport(flags.dryRun, { json: flags.json })
114
+ const body = async () => {
115
+ if (command !== "init" && WRITES.has(command)) {
116
+ if (!(await readOptional(join(cwd, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
117
+ await ensureConfig(cwd, report)
118
+ }
119
+ if (command === "init") {
120
+ await init(cwd, flags, report)
121
+ report.print(stdout)
122
+ return
123
+ }
124
+ if (flags.hooks) throw usage("--hooks is only valid with init.")
125
+ if (flags.tools) throw usage("--tools is only valid with init.")
126
+ if (command === "add") {
127
+ if (flags.pack) throw usage("--pack is only valid with init and update.")
128
+ if (args.length !== 1) throw usage("Usage: connore add <path>.")
129
+ await add(cwd, args[0], flags, report)
130
+ report.print(stdout)
131
+ return
132
+ }
133
+ if (flags.type || flags.tags) throw usage("--type and --tags are only valid with add.")
134
+ if (command === "update") {
135
+ if (args.length) throw usage("update does not take a path.")
136
+ await update(cwd, flags, report)
137
+ report.print(stdout)
138
+ return
139
+ }
140
+ if (flags.pack) throw usage("--pack is only valid with init and update.")
141
+ if (command === "list") {
142
+ if (flags.json) {
143
+ const { collectEntries } = await import("./lib/index.js")
144
+ if (!(await readOptional(join(cwd, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
145
+ printJson(stdout, { ok: true, entries: await collectEntries(cwd, { includeDrafts: flags.includeDrafts }) })
146
+ } else await list(cwd, stdout)
147
+ return
148
+ }
149
+ if (command === "index") {
150
+ await reindex(cwd, flags, report)
151
+ report.print(stdout)
152
+ return
153
+ }
154
+ if (command === "hook") {
155
+ let output = ""
156
+ await printIndex(cwd, { write(chunk) { output += chunk } })
157
+ if (flags.json) printJson(stdout, { ok: true, output })
158
+ else stdout.write(output)
159
+ return
160
+ }
161
+ if (command === "lint") {
162
+ const problems = await lint()
163
+ printJsonish(stdout, flags, problems.length ? { ok: false, problems } : { ok: true, problems }, () => {
164
+ for (const problem of problems) stdout.write(`${problem}\n`)
165
+ stdout.write(problems.length ? `${problems.length} problems\n` : "lint clean\n")
166
+ })
167
+ if (problems.length) throw Object.assign(new Error("lint failed"), { exitCode: 1, handled: true })
168
+ return
169
+ }
170
+ if (command === "doctor") {
171
+ const problems = await doctor(cwd)
172
+ printJsonish(stdout, flags, { ok: problems.length === 0, problems }, () => {
173
+ for (const problem of problems) stdout.write(`${problem}\n`)
174
+ stdout.write(problems.length ? `${problems.length} problems\n` : "doctor clean\n")
175
+ })
176
+ if (problems.length) throw Object.assign(new Error("doctor failed"), { exitCode: 1, handled: true })
177
+ return
178
+ }
179
+ if (command === "undo") {
180
+ if (args.length) throw usage("undo does not take a path.")
181
+ await undo(cwd, report)
182
+ report.print(stdout)
183
+ return
184
+ }
185
+ if (command === "catalog") {
186
+ if (args.length) throw usage("catalog does not take a path.")
187
+ await catalog(cwd, flags, stdout)
188
+ return
189
+ }
190
+ if (command === "memory") {
191
+ await memory(cwd, args, report, stdout, flags)
192
+ if (args.length) report.print(stdout)
193
+ return
194
+ }
195
+ if (command === "log") {
196
+ if (args.length !== 2) throw usage('Usage: connore log <fix|decision|failure> "text".')
197
+ await log(cwd, args[0], args[1], flags, report)
198
+ report.print(stdout)
199
+ return
200
+ }
201
+ if (command === "learn") {
202
+ const result = await learn(cwd, flags, report, io)
203
+ if (flags.json) printJson(stdout, { ok: true, ...result, summary: countEvents(report) })
204
+ else {
205
+ if (!result.proposals) stdout.write("No pattern has 3 occurrences yet.\n")
206
+ else if (!result.wrote) stdout.write(`${result.preview}\nRe-run with --yes to write proposals.\n`)
207
+ report.print(stdout)
208
+ }
209
+ return
210
+ }
211
+ if (command === "review") {
212
+ const result = await review(cwd, args, flags, report)
213
+ if (!args[0]) {
214
+ if (flags.json) printJson(stdout, { ok: true, proposals: result.proposals })
215
+ else if (!result.proposals.length) stdout.write("No proposals.\n")
216
+ else {
217
+ for (const lesson of result.proposals) {
218
+ stdout.write(`${lesson.id}\t${lesson.evidence}\t${lesson.mistake}\n`)
219
+ }
220
+ }
221
+ return
222
+ }
223
+ report.print(stdout)
224
+ return
225
+ }
226
+ if (command === "search") {
227
+ if (!args.length) throw usage('Usage: connore search "query".')
228
+ const results = await search(cwd, args.join(" "), flags)
229
+ if (flags.json) printJson(stdout, { ok: true, results })
230
+ else if (!results.length) stdout.write("No entries match.\n")
231
+ else {
232
+ for (const hit of results) stdout.write(`${hit.score}\t${hit.path}\t${hit.description}\n`)
233
+ }
234
+ return
235
+ }
236
+ if (command === "plan") {
237
+ if (!args.length) throw usage("Usage: connore plan init <blueprint> | status | next | done <task>.")
238
+ const view = await plan(cwd, args, report)
239
+ if (flags.json) printJson(stdout, { ok: true, ...view })
240
+ else {
241
+ stdout.write(`Phase ${view.phase}\nNext: ${view.next}\n`)
242
+ if (view.failedGate) stdout.write(`Failed gate: ${view.failedGate}\n`)
243
+ stdout.write(`${view.done}/${view.total} done\n`)
244
+ }
245
+ return
246
+ }
247
+ if (command === "stats") {
248
+ const data = await stats(cwd)
249
+ if (flags.json) printJson(stdout, { ok: true, ...data })
250
+ else if (!data.weeks.length) stdout.write("No signals yet.\n")
251
+ else {
252
+ for (const week of data.weeks) {
253
+ stdout.write(`${week.week}\tsignals ${week.signals}\taccepted ${week.accepted}\trecurring ${week.recurring}\n`)
254
+ }
255
+ }
256
+ return
257
+ }
258
+ throw usage(`Unknown command "${command}".`)
259
+ }
260
+
261
+ if (!flags.dryRun && WRITES.has(command) && (command === "init" || (await readOptional(join(cwd, ".ctx", "PROCESS.md"))))) {
262
+ await withLock(cwd, async () => {
263
+ await body()
264
+ await sealRun(cwd, command, report)
265
+ })
266
+ } else {
267
+ await body()
268
+ if (!flags.dryRun && WRITES.has(command)) await sealRun(cwd, command, report)
269
+ }
270
+ return 0
271
+ } catch (error) {
272
+ if (error.handled) return error.exitCode || 1
273
+ const code = error.exitCode || 1
274
+ if (flags.json) printJson(stdout, { ok: false, error: error.message })
275
+ else stderr.write(`${error.message}\n`)
276
+ return code
277
+ }
278
+ }
279
+
280
+ function countEvents(report) {
281
+ const count = (kind) => report.events.filter((event) => event.kind === kind).length
282
+ return { created: count("created"), changed: count("changed"), skipped: count("skipped"), removed: count("removed") }
283
+ }
284
+
285
+ function printJsonish(stdout, flags, payload, text) {
286
+ if (flags.json) printJson(stdout, payload)
287
+ else text()
288
+ }
@@ -1,57 +1,61 @@
1
- import { readFile, stat } from "node:fs/promises"
2
- import { basename, join, relative, resolve } from "node:path"
3
- import { readOptional, walkFiles, writeAtomic } from "../lib/fs-safe.js"
4
- import { twinStub, today } from "../lib/frontmatter.js"
5
- import { rebuildIndex } from "../lib/index.js"
6
- import { ctxPath, fromRoot } from "../lib/paths.js"
7
-
8
- const TYPES = new Set(["design-token", "component", "rule", "screenshot", "flow", "doc"])
9
- const IMAGE = /\.(png|jpe?g|webp|gif|svg)$/i
10
- const CODE = /\.(tsx|jsx|ts|js|mjs|cjs|css)$/i
11
-
12
- function defaultType(name) {
13
- if (IMAGE.test(name)) return "screenshot"
14
- if (CODE.test(name)) return "component"
15
- return null
16
- }
17
-
18
- async function placeFile(source, target, root, flags, report) {
19
- const display = fromRoot(root, target)
20
- if (await readOptional(target)) report.add("skipped", display)
21
- else {
22
- if (!report.dryRun) await writeAtomic(target, await readFile(source))
23
- report.add("created", display)
24
- }
25
- const type = flags.type || defaultType(basename(target))
26
- if (!type || target.endsWith(".md")) return
27
- const twin = target.replace(/\.[^.]+$/, ".md")
28
- const twinDisplay = fromRoot(root, twin)
29
- if (await readOptional(twin)) {
30
- report.add("skipped", twinDisplay)
31
- return
32
- }
33
- const tags = flags.tags ? flags.tags.split(",").map((tag) => tag.trim()).filter(Boolean) : []
34
- const body = twinStub({ title: basename(twin, ".md"), type, tags, date: today() })
35
- if (!report.dryRun) await writeAtomic(twin, body)
36
- report.add("created", twinDisplay)
37
- }
38
-
39
- export async function add(root, sourceArg, flags, report) {
40
- if (flags.type && !TYPES.has(flags.type)) throw new Error(`Unknown type "${flags.type}".`)
41
- if (!(await readOptional(join(root, ".ctx", "INDEX.md"))) && !(await readOptional(join(root, ".ctx", "PROCESS.md")))) {
42
- throw new Error("No .ctx/ folder. Run connore init first.")
43
- }
44
- const source = resolve(root, sourceArg)
45
- let info
46
- try {
47
- info = await stat(source)
48
- } catch {
49
- throw new Error(`Cannot read ${sourceArg}.`)
50
- }
51
- const mine = ctxPath(root, "mine")
52
- if (info.isDirectory()) {
53
- const files = await walkFiles(source)
54
- for (const file of files) await placeFile(file, join(mine, basename(source), relative(source, file)), root, flags, report)
55
- } else await placeFile(source, join(mine, basename(source)), root, flags, report)
56
- await rebuildIndex(root, report)
57
- }
1
+ import { readFile, stat } from "node:fs/promises"
2
+ import { basename, join, relative, resolve } from "node:path"
3
+ import { readConfig } from "../lib/config.js"
4
+ import { readOptional, walkFiles } from "../lib/fs-safe.js"
5
+ import { twinStub, today } from "../lib/frontmatter.js"
6
+ import { ignoreNames } from "../lib/ignore.js"
7
+ import { rebuildIndex } from "../lib/index.js"
8
+ import { commitWrite } from "../lib/mutate.js"
9
+ import { ctxPath, fromRoot } from "../lib/paths.js"
10
+ import { secretHits } from "../lib/secrets.js"
11
+
12
+ const TYPES = new Set(["design-token", "component", "rule", "screenshot", "flow", "doc"])
13
+ const IMAGE = /\.(png|jpe?g|webp|gif|svg)$/i
14
+ const CODE = /\.(tsx|jsx|ts|js|mjs|cjs|css)$/i
15
+
16
+ function defaultType(name) {
17
+ if (IMAGE.test(name)) return "screenshot"
18
+ if (CODE.test(name)) return "component"
19
+ return null
20
+ }
21
+
22
+ async function placeFile(source, target, root, flags, report) {
23
+ const display = fromRoot(root, target)
24
+ const bytes = await readFile(source)
25
+ const hits = secretHits(bytes.toString("utf8"))
26
+ if (hits.length) throw new Error(`Refusing to copy ${display} because it looks like a secret (${hits.join(", ")}).`)
27
+ if (await readOptional(target)) report.add("skipped", display)
28
+ else await commitWrite(target, bytes, report, display)
29
+ const type = flags.type || defaultType(basename(target))
30
+ if (!type || target.endsWith(".md")) return
31
+ const twin = target.replace(/\.[^.]+$/, ".md")
32
+ const twinDisplay = fromRoot(root, twin)
33
+ if (await readOptional(twin)) {
34
+ report.add("skipped", twinDisplay)
35
+ return
36
+ }
37
+ const tags = flags.tags ? flags.tags.split(",").map((tag) => tag.trim()).filter(Boolean) : []
38
+ const body = twinStub({ title: basename(twin, ".md"), type, tags, date: today() })
39
+ await commitWrite(twin, body, report, twinDisplay)
40
+ }
41
+
42
+ export async function add(root, sourceArg, flags, report) {
43
+ if (flags.type && !TYPES.has(flags.type)) throw new Error(`Unknown type "${flags.type}".`)
44
+ if (!(await readOptional(join(root, ".ctx", "INDEX.md"))) && !(await readOptional(join(root, ".ctx", "PROCESS.md")))) {
45
+ throw new Error("No .ctx/ folder. Run connore init first.")
46
+ }
47
+ const source = resolve(root, sourceArg)
48
+ let info
49
+ try {
50
+ info = await stat(source)
51
+ } catch {
52
+ throw new Error(`Cannot read ${sourceArg}.`)
53
+ }
54
+ const mine = ctxPath(root, "mine")
55
+ const ignore = await ignoreNames(root, (await readConfig(root)).config)
56
+ if (info.isDirectory()) {
57
+ const files = await walkFiles(source, ignore)
58
+ for (const file of files) await placeFile(file, join(mine, basename(source), relative(source, file)), root, flags, report)
59
+ } else await placeFile(source, join(mine, basename(source)), root, flags, report)
60
+ await rebuildIndex(root, report, { includeDrafts: flags.includeDrafts })
61
+ }
@@ -0,0 +1,21 @@
1
+ import { join } from "node:path"
2
+ import { readOptional } from "../lib/fs-safe.js"
3
+ import { GROUPS, countGroup } from "../lib/catalog.js"
4
+
5
+ export async function catalog(root, flags, stdout) {
6
+ if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
7
+ const groups = []
8
+ for (const group of GROUPS) {
9
+ const count = await countGroup(root, group.id)
10
+ groups.push({ ...group, count, met: count >= group.target })
11
+ }
12
+ const started = groups.filter((group) => group.count > 0).length
13
+ if (flags.json) {
14
+ stdout.write(`${JSON.stringify({ ok: true, started, total: groups.length, groups })}\n`)
15
+ return
16
+ }
17
+ stdout.write(`${started}/${groups.length} groups started\n`)
18
+ for (const group of groups) {
19
+ stdout.write(`${group.count}/${group.target}\t${group.label}\t.ctx/mine/catalog/${group.id}\n`)
20
+ }
21
+ }
@@ -0,0 +1,42 @@
1
+ import { join } from "node:path"
2
+ import { readConfig } from "../lib/config.js"
3
+ import { exists, readOptional, walkFiles } from "../lib/fs-safe.js"
4
+ import { lockIsStale } from "../lib/lock.js"
5
+ import { secretHits } from "../lib/secrets.js"
6
+ import { readFile } from "node:fs/promises"
7
+
8
+ export async function doctor(root) {
9
+ const problems = []
10
+ if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) {
11
+ return ["No .ctx/ folder. Run connore init first."]
12
+ }
13
+ try {
14
+ const { config } = await readConfig(root)
15
+ if (!config) problems.push("missing .ctx/config.json")
16
+ else if (config.schemaVersion !== 1) problems.push(`schemaVersion is ${config.schemaVersion}`)
17
+ } catch (error) {
18
+ problems.push(error.message)
19
+ }
20
+ const lock = await readOptional(join(root, ".ctx", ".lock"))
21
+ if (lock) {
22
+ try {
23
+ const parsed = JSON.parse(lock.toString("utf8"))
24
+ if (!lockIsStale(parsed)) problems.push(`a run holds .ctx/.lock (pid ${parsed.pid})`)
25
+ } catch {
26
+ problems.push(".ctx/.lock is not valid JSON")
27
+ }
28
+ }
29
+ if (!(await exists(join(root, ".ctx", "vault")))) problems.push("missing .ctx/vault")
30
+ const gitignore = await readOptional(join(root, ".gitignore"))
31
+ if (!gitignore || !gitignore.toString("utf8").includes(".ctx/.signals/")) {
32
+ problems.push(".gitignore is missing the connore local-state block")
33
+ }
34
+ for (const dir of ["library", "mine"]) {
35
+ for (const file of await walkFiles(join(root, ".ctx", dir))) {
36
+ if (!file.endsWith(".md")) continue
37
+ const hits = secretHits(await readFile(file, "utf8"))
38
+ if (hits.length) problems.push(`${file} looks like a secret (${hits.join(", ")})`)
39
+ }
40
+ }
41
+ return problems
42
+ }