opencastle 0.38.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -4
- package/bin/cli.mjs +20 -3
- package/dist/cli/adapters/antigravity.d.ts +1 -1
- package/dist/cli/adapters/antigravity.d.ts.map +1 -1
- package/dist/cli/adapters/claude-code.d.ts +8 -4
- package/dist/cli/adapters/claude-code.d.ts.map +1 -1
- package/dist/cli/adapters/claude-code.js +13 -7
- package/dist/cli/adapters/claude-code.js.map +1 -1
- package/dist/cli/adapters/codex.d.ts +1 -1
- package/dist/cli/adapters/codex.d.ts.map +1 -1
- package/dist/cli/adapters/cursor.d.ts +1 -1
- package/dist/cli/adapters/cursor.d.ts.map +1 -1
- package/dist/cli/adapters/opencode.d.ts +1 -1
- package/dist/cli/adapters/opencode.d.ts.map +1 -1
- package/dist/cli/adapters/rules-dir-base.d.ts +3 -2
- package/dist/cli/adapters/rules-dir-base.d.ts.map +1 -1
- package/dist/cli/adapters/rules-dir-base.js +13 -25
- package/dist/cli/adapters/rules-dir-base.js.map +1 -1
- package/dist/cli/adapters/single-file-base.d.ts +8 -1
- package/dist/cli/adapters/single-file-base.d.ts.map +1 -1
- package/dist/cli/adapters/single-file-base.js +43 -46
- package/dist/cli/adapters/single-file-base.js.map +1 -1
- package/dist/cli/adapters/vscode.d.ts +14 -3
- package/dist/cli/adapters/vscode.d.ts.map +1 -1
- package/dist/cli/adapters/vscode.js +63 -75
- package/dist/cli/adapters/vscode.js.map +1 -1
- package/dist/cli/adapters/windsurf.d.ts +1 -1
- package/dist/cli/adapters/windsurf.d.ts.map +1 -1
- package/dist/cli/baseline.d.ts +3 -0
- package/dist/cli/baseline.d.ts.map +1 -0
- package/dist/cli/baseline.js +280 -0
- package/dist/cli/baseline.js.map +1 -0
- package/dist/cli/ci.d.ts +18 -0
- package/dist/cli/ci.d.ts.map +1 -0
- package/dist/cli/ci.js +279 -0
- package/dist/cli/ci.js.map +1 -0
- package/dist/cli/command-namespace.d.ts +91 -0
- package/dist/cli/command-namespace.d.ts.map +1 -0
- package/dist/cli/command-namespace.js +218 -0
- package/dist/cli/command-namespace.js.map +1 -0
- package/dist/cli/command-namespace.test.d.ts +2 -0
- package/dist/cli/command-namespace.test.d.ts.map +1 -0
- package/dist/cli/command-namespace.test.js +356 -0
- package/dist/cli/command-namespace.test.js.map +1 -0
- package/dist/cli/copy.d.ts +1 -1
- package/dist/cli/copy.d.ts.map +1 -1
- package/dist/cli/copy.js +3 -2
- package/dist/cli/copy.js.map +1 -1
- package/dist/cli/dispatcher.test.js +2 -2
- package/dist/cli/dispatcher.test.js.map +1 -1
- package/dist/cli/docs-accuracy.test.js +5 -0
- package/dist/cli/docs-accuracy.test.js.map +1 -1
- package/dist/cli/doctor.d.ts +3 -3
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +66 -9
- package/dist/cli/doctor.js.map +1 -1
- package/dist/cli/doctor.test.js +2 -2
- package/dist/cli/doctor.test.js.map +1 -1
- package/dist/cli/explain.d.ts +3 -0
- package/dist/cli/explain.d.ts.map +1 -0
- package/dist/cli/explain.js +222 -0
- package/dist/cli/explain.js.map +1 -0
- package/dist/cli/fleet.d.ts +30 -0
- package/dist/cli/fleet.d.ts.map +1 -0
- package/dist/cli/fleet.js +149 -0
- package/dist/cli/fleet.js.map +1 -0
- package/dist/cli/github-report.d.ts +49 -0
- package/dist/cli/github-report.d.ts.map +1 -0
- package/dist/cli/github-report.js +161 -0
- package/dist/cli/github-report.js.map +1 -0
- package/dist/cli/github-report.test.d.ts +2 -0
- package/dist/cli/github-report.test.d.ts.map +1 -0
- package/dist/cli/github-report.test.js +146 -0
- package/dist/cli/github-report.test.js.map +1 -0
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +171 -96
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/init.test.js +35 -29
- package/dist/cli/init.test.js.map +1 -1
- package/dist/cli/layers.d.ts +152 -0
- package/dist/cli/layers.d.ts.map +1 -0
- package/dist/cli/layers.js +932 -0
- package/dist/cli/layers.js.map +1 -0
- package/dist/cli/layers.test.d.ts +2 -0
- package/dist/cli/layers.test.d.ts.map +1 -0
- package/dist/cli/layers.test.js +483 -0
- package/dist/cli/layers.test.js.map +1 -0
- package/dist/cli/lock.d.ts +130 -0
- package/dist/cli/lock.d.ts.map +1 -0
- package/dist/cli/lock.js +320 -0
- package/dist/cli/lock.js.map +1 -0
- package/dist/cli/managed-paths.d.ts +18 -1
- package/dist/cli/managed-paths.d.ts.map +1 -1
- package/dist/cli/managed-paths.js +72 -2
- package/dist/cli/managed-paths.js.map +1 -1
- package/dist/cli/manifest.d.ts.map +1 -1
- package/dist/cli/manifest.js +2 -0
- package/dist/cli/manifest.js.map +1 -1
- package/dist/cli/mcp-audit.d.ts +164 -0
- package/dist/cli/mcp-audit.d.ts.map +1 -0
- package/dist/cli/mcp-audit.js +504 -0
- package/dist/cli/mcp-audit.js.map +1 -0
- package/dist/cli/mcp-audit.test.d.ts +2 -0
- package/dist/cli/mcp-audit.test.d.ts.map +1 -0
- package/dist/cli/mcp-audit.test.js +215 -0
- package/dist/cli/mcp-audit.test.js.map +1 -0
- package/dist/cli/mcp-upgrade.test.d.ts +2 -0
- package/dist/cli/mcp-upgrade.test.d.ts.map +1 -0
- package/dist/cli/mcp-upgrade.test.js +118 -0
- package/dist/cli/mcp-upgrade.test.js.map +1 -0
- package/dist/cli/mcp.d.ts +74 -5
- package/dist/cli/mcp.d.ts.map +1 -1
- package/dist/cli/mcp.js +340 -32
- package/dist/cli/mcp.js.map +1 -1
- package/dist/cli/policy.d.ts +58 -0
- package/dist/cli/policy.d.ts.map +1 -0
- package/dist/cli/policy.js +194 -0
- package/dist/cli/policy.js.map +1 -0
- package/dist/cli/publish-workflow.test.js +55 -1
- package/dist/cli/publish-workflow.test.js.map +1 -1
- package/dist/cli/remove.d.ts.map +1 -1
- package/dist/cli/remove.js +25 -6
- package/dist/cli/remove.js.map +1 -1
- package/dist/cli/review.d.ts +25 -0
- package/dist/cli/review.d.ts.map +1 -0
- package/dist/cli/review.js +437 -0
- package/dist/cli/review.js.map +1 -0
- package/dist/cli/status.d.ts +2 -0
- package/dist/cli/status.d.ts.map +1 -1
- package/dist/cli/status.js +25 -4
- package/dist/cli/status.js.map +1 -1
- package/dist/cli/status.test.js +5 -3
- package/dist/cli/status.test.js.map +1 -1
- package/dist/cli/sync-check.d.ts +16 -1
- package/dist/cli/sync-check.d.ts.map +1 -1
- package/dist/cli/sync-check.js +241 -17
- package/dist/cli/sync-check.js.map +1 -1
- package/dist/cli/sync-check.test.js +14 -8
- package/dist/cli/sync-check.test.js.map +1 -1
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +2 -0
- package/dist/cli/sync.js.map +1 -1
- package/dist/cli/team-ci.test.d.ts +2 -0
- package/dist/cli/team-ci.test.d.ts.map +1 -0
- package/dist/cli/team-ci.test.js +169 -0
- package/dist/cli/team-ci.test.js.map +1 -0
- package/dist/cli/team-commands.test.d.ts +2 -0
- package/dist/cli/team-commands.test.d.ts.map +1 -0
- package/dist/cli/team-commands.test.js +233 -0
- package/dist/cli/team-commands.test.js.map +1 -0
- package/dist/cli/team-config.d.ts +88 -0
- package/dist/cli/team-config.d.ts.map +1 -0
- package/dist/cli/team-config.js +261 -0
- package/dist/cli/team-config.js.map +1 -0
- package/dist/cli/team-health.d.ts +49 -0
- package/dist/cli/team-health.d.ts.map +1 -0
- package/dist/cli/team-health.js +205 -0
- package/dist/cli/team-health.js.map +1 -0
- package/dist/cli/team-sources.test.d.ts +2 -0
- package/dist/cli/team-sources.test.d.ts.map +1 -0
- package/dist/cli/team-sources.test.js +542 -0
- package/dist/cli/team-sources.test.js.map +1 -0
- package/dist/cli/types.d.ts +48 -2
- package/dist/cli/types.d.ts.map +1 -1
- package/dist/cli/types.js.map +1 -1
- package/dist/cli/update.d.ts.map +1 -1
- package/dist/cli/update.js +218 -44
- package/dist/cli/update.js.map +1 -1
- package/dist/cli/version-range.d.ts +24 -0
- package/dist/cli/version-range.d.ts.map +1 -0
- package/dist/cli/version-range.js +157 -0
- package/dist/cli/version-range.js.map +1 -0
- package/dist/cli/website-plugins.test.d.ts +2 -0
- package/dist/cli/website-plugins.test.d.ts.map +1 -0
- package/dist/cli/website-plugins.test.js +34 -0
- package/dist/cli/website-plugins.test.js.map +1 -0
- package/dist/orchestrator/plugins/chrome-devtools/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/chrome-devtools/config.js +11 -1
- package/dist/orchestrator/plugins/chrome-devtools/config.js.map +1 -1
- package/dist/orchestrator/plugins/contentful/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/contentful/config.js +11 -1
- package/dist/orchestrator/plugins/contentful/config.js.map +1 -1
- package/dist/orchestrator/plugins/convex/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/convex/config.js +11 -1
- package/dist/orchestrator/plugins/convex/config.js.map +1 -1
- package/dist/orchestrator/plugins/coolify/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/coolify/config.js +14 -1
- package/dist/orchestrator/plugins/coolify/config.js.map +1 -1
- package/dist/orchestrator/plugins/figma/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/figma/config.js +24 -13
- package/dist/orchestrator/plugins/figma/config.js.map +1 -1
- package/dist/orchestrator/plugins/linear/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/linear/config.js +27 -17
- package/dist/orchestrator/plugins/linear/config.js.map +1 -1
- package/dist/orchestrator/plugins/netlify/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/netlify/config.js +23 -9
- package/dist/orchestrator/plugins/netlify/config.js.map +1 -1
- package/dist/orchestrator/plugins/nextjs/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/nextjs/config.js +11 -1
- package/dist/orchestrator/plugins/nextjs/config.js.map +1 -1
- package/dist/orchestrator/plugins/nx/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/nx/config.js +11 -1
- package/dist/orchestrator/plugins/nx/config.js.map +1 -1
- package/dist/orchestrator/plugins/pins.test.d.ts +2 -0
- package/dist/orchestrator/plugins/pins.test.d.ts.map +1 -0
- package/dist/orchestrator/plugins/pins.test.js +78 -0
- package/dist/orchestrator/plugins/pins.test.js.map +1 -0
- package/dist/orchestrator/plugins/playwright/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/playwright/config.js +11 -1
- package/dist/orchestrator/plugins/playwright/config.js.map +1 -1
- package/dist/orchestrator/plugins/prisma/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/prisma/config.js +17 -2
- package/dist/orchestrator/plugins/prisma/config.js.map +1 -1
- package/dist/orchestrator/plugins/resend/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/resend/config.js +17 -1
- package/dist/orchestrator/plugins/resend/config.js.map +1 -1
- package/dist/orchestrator/plugins/sentry/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/sentry/config.js +13 -1
- package/dist/orchestrator/plugins/sentry/config.js.map +1 -1
- package/dist/orchestrator/plugins/slack/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/slack/config.js +20 -1
- package/dist/orchestrator/plugins/slack/config.js.map +1 -1
- package/dist/orchestrator/plugins/strapi/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/strapi/config.js +11 -1
- package/dist/orchestrator/plugins/strapi/config.js.map +1 -1
- package/dist/orchestrator/plugins/trello/config.d.ts.map +1 -1
- package/dist/orchestrator/plugins/trello/config.js +21 -1
- package/dist/orchestrator/plugins/trello/config.js.map +1 -1
- package/dist/orchestrator/plugins/types.d.ts +22 -0
- package/dist/orchestrator/plugins/types.d.ts.map +1 -1
- package/package.json +4 -2
- package/src/cli/adapters/claude-code.ts +13 -7
- package/src/cli/adapters/rules-dir-base.ts +23 -44
- package/src/cli/adapters/single-file-base.ts +65 -50
- package/src/cli/adapters/vscode.ts +75 -77
- package/src/cli/baseline.ts +289 -0
- package/src/cli/ci.ts +289 -0
- package/src/cli/command-namespace.test.ts +417 -0
- package/src/cli/command-namespace.ts +222 -0
- package/src/cli/copy.ts +6 -2
- package/src/cli/dispatcher.test.ts +2 -2
- package/src/cli/docs-accuracy.test.ts +5 -0
- package/src/cli/doctor.test.ts +2 -2
- package/src/cli/doctor.ts +92 -6
- package/src/cli/explain.ts +222 -0
- package/src/cli/fleet.ts +176 -0
- package/src/cli/github-report.test.ts +158 -0
- package/src/cli/github-report.ts +179 -0
- package/src/cli/init.test.ts +36 -31
- package/src/cli/init.ts +174 -96
- package/src/cli/layers.test.ts +525 -0
- package/src/cli/layers.ts +1089 -0
- package/src/cli/lock.ts +395 -0
- package/src/cli/managed-paths.ts +73 -3
- package/src/cli/manifest.ts +2 -0
- package/src/cli/mcp-audit.test.ts +259 -0
- package/src/cli/mcp-audit.ts +619 -0
- package/src/cli/mcp-upgrade.test.ts +135 -0
- package/src/cli/mcp.ts +373 -34
- package/src/cli/policy.ts +207 -0
- package/src/cli/publish-workflow.test.ts +60 -1
- package/src/cli/remove.ts +28 -6
- package/src/cli/review.ts +457 -0
- package/src/cli/status.test.ts +5 -3
- package/src/cli/status.ts +29 -4
- package/src/cli/sync-check.test.ts +16 -8
- package/src/cli/sync-check.ts +264 -17
- package/src/cli/sync.ts +2 -0
- package/src/cli/team-ci.test.ts +195 -0
- package/src/cli/team-commands.test.ts +253 -0
- package/src/cli/team-config.ts +289 -0
- package/src/cli/team-health.ts +234 -0
- package/src/cli/team-sources.test.ts +578 -0
- package/src/cli/types.ts +51 -1
- package/src/cli/update.ts +229 -44
- package/src/cli/version-range.ts +150 -0
- package/src/cli/website-plugins.test.ts +41 -0
- package/src/dashboard/node_modules/.vite/deps/_metadata.json +6 -6
- package/src/orchestrator/plugins/chrome-devtools/config.ts +11 -1
- package/src/orchestrator/plugins/contentful/config.ts +11 -1
- package/src/orchestrator/plugins/convex/config.ts +11 -1
- package/src/orchestrator/plugins/coolify/config.ts +14 -1
- package/src/orchestrator/plugins/figma/SKILL.md +18 -14
- package/src/orchestrator/plugins/figma/config.ts +24 -13
- package/src/orchestrator/plugins/linear/SKILL.md +6 -14
- package/src/orchestrator/plugins/linear/config.ts +27 -17
- package/src/orchestrator/plugins/netlify/config.ts +23 -9
- package/src/orchestrator/plugins/nextjs/config.ts +11 -1
- package/src/orchestrator/plugins/nx/config.ts +11 -1
- package/src/orchestrator/plugins/pins.test.ts +80 -0
- package/src/orchestrator/plugins/playwright/config.ts +11 -1
- package/src/orchestrator/plugins/prisma/config.ts +17 -2
- package/src/orchestrator/plugins/resend/config.ts +17 -1
- package/src/orchestrator/plugins/sentry/config.ts +13 -1
- package/src/orchestrator/plugins/slack/config.ts +20 -1
- package/src/orchestrator/plugins/strapi/config.ts +11 -1
- package/src/orchestrator/plugins/trello/config.ts +21 -1
- package/src/orchestrator/plugins/types.ts +24 -0
- package/src/orchestrator/prompts/bootstrap-customizations.prompt.md +4 -4
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { mkdir, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { isAbsolute, join, relative, resolve } from 'node:path'
|
|
5
|
+
import { resolveSources, materialize, cliVersionOf, formatIssues, hasErrors } from './layers.js'
|
|
6
|
+
import { contentReport, tokensOf } from './lock.js'
|
|
7
|
+
import { CONFIG_SCHEMA_URL } from './team-config.js'
|
|
8
|
+
import { c } from './prompt.js'
|
|
9
|
+
import type { CliContext } from './types.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `opencastle baseline`: the organisation's standard as a package.
|
|
13
|
+
*
|
|
14
|
+
* A baseline is an ordinary npm package that says so in its package.json. Every
|
|
15
|
+
* repository adds it as a devDependency and names it in `extends`; the
|
|
16
|
+
* package manager does the fetching, the lockfile does the pinning, and an
|
|
17
|
+
* upgrade bot does the rollout — one pull request per repository, each with
|
|
18
|
+
* `opencastle review` explaining what it changes.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const HELP = `
|
|
22
|
+
opencastle baseline <init|check> [dir] [options]
|
|
23
|
+
|
|
24
|
+
init [dir] Scaffold a baseline package (default: ./opencastle-baseline):
|
|
25
|
+
a layer with example instructions, a skill and a policy,
|
|
26
|
+
a README on publishing and adopting it, and a CI check.
|
|
27
|
+
check [dir] Validate a baseline the way every repository extending it
|
|
28
|
+
will: its config, its content, its policy and its packaging.
|
|
29
|
+
|
|
30
|
+
Options:
|
|
31
|
+
--name <pkg> Package name for init (default: @your-org/opencastle-baseline)
|
|
32
|
+
--json Machine-readable output for check
|
|
33
|
+
--help, -h Show this help
|
|
34
|
+
`
|
|
35
|
+
|
|
36
|
+
function files(name: string, cliVersion: string): Record<string, string> {
|
|
37
|
+
return {
|
|
38
|
+
'package.json':
|
|
39
|
+
JSON.stringify(
|
|
40
|
+
{
|
|
41
|
+
name,
|
|
42
|
+
version: '0.1.0',
|
|
43
|
+
description: "Our AI assistant standard, compiled by OpenCastle into every assistant in every repository that extends it.",
|
|
44
|
+
license: 'UNLICENSED',
|
|
45
|
+
files: ['layer/'],
|
|
46
|
+
opencastle: { baseline: 'layer' },
|
|
47
|
+
scripts: { check: 'opencastle baseline check' },
|
|
48
|
+
devDependencies: { opencastle: `^${cliVersion}` },
|
|
49
|
+
publishConfig: { access: 'restricted' },
|
|
50
|
+
},
|
|
51
|
+
null,
|
|
52
|
+
2,
|
|
53
|
+
) + '\n',
|
|
54
|
+
'layer/config.json': `{
|
|
55
|
+
"$schema": "${CONFIG_SCHEMA_URL}",
|
|
56
|
+
// Every repository that extends this baseline is held to this policy.
|
|
57
|
+
// A repository can tighten it in its own .opencastle/config.json, never relax it.
|
|
58
|
+
"policy": {
|
|
59
|
+
"mcp": {
|
|
60
|
+
// The MCP servers an assistant may start. Integration servers not listed here
|
|
61
|
+
// are left out of the generated config, and a hand-added one fails doctor and CI.
|
|
62
|
+
"allow": ["chrome-devtools", "Playwright", "Linear", "Sentry"],
|
|
63
|
+
// Remote servers may only connect to these hosts.
|
|
64
|
+
"remoteHosts": ["mcp.linear.app"],
|
|
65
|
+
// Every local server runs an exact version, so every laptop runs the same code.
|
|
66
|
+
"requirePinned": true
|
|
67
|
+
},
|
|
68
|
+
// Items no repository may exclude or replace.
|
|
69
|
+
"require": ["instructions/engineering-standards"],
|
|
70
|
+
// Tokens every assistant may load before it reads the task.
|
|
71
|
+
"contextBudget": 8000
|
|
72
|
+
},
|
|
73
|
+
// Servers every repository gets. Write variables as \${NAME}; OpenCastle
|
|
74
|
+
// spells them the way each assistant expects.
|
|
75
|
+
"mcpServers": {}
|
|
76
|
+
}
|
|
77
|
+
`,
|
|
78
|
+
'layer/instructions/engineering-standards.md': `# Engineering standards
|
|
79
|
+
|
|
80
|
+
These apply to every repository in the organisation, and every assistant reads
|
|
81
|
+
them before every task. Keep this file short: detail belongs in a skill, which
|
|
82
|
+
loads only when a task needs it.
|
|
83
|
+
|
|
84
|
+
- Write tests for behaviour you change, and run them before you say you are done.
|
|
85
|
+
- Never write credentials into files; read them from the environment.
|
|
86
|
+
- Keep changes small and focused; one pull request, one purpose.
|
|
87
|
+
`,
|
|
88
|
+
'layer/skills/code-review/SKILL.md': `---
|
|
89
|
+
name: code-review
|
|
90
|
+
description: "How we review code: what to check, what to block on, and how to word feedback. Use when reviewing a pull request or preparing one for review."
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
# Code review
|
|
94
|
+
|
|
95
|
+
Block on: correctness, security, missing tests for changed behaviour, and
|
|
96
|
+
anything that makes the next change harder. Comment, do not block, on style
|
|
97
|
+
a formatter could fix.
|
|
98
|
+
|
|
99
|
+
Word feedback as a question or a suggestion with the reason attached.
|
|
100
|
+
`,
|
|
101
|
+
'README.md': `# ${name}
|
|
102
|
+
|
|
103
|
+
Our AI assistant standard, as an [OpenCastle](https://www.opencastle.dev) baseline.
|
|
104
|
+
Every repository that extends it gets the same instructions, skills and MCP
|
|
105
|
+
servers in every assistant its developers use — Claude Code, Cursor, Copilot,
|
|
106
|
+
OpenCode, Windsurf, Codex CLI and Antigravity — and is held to the same policy.
|
|
107
|
+
|
|
108
|
+
## What is in it
|
|
109
|
+
|
|
110
|
+
- \`layer/instructions/\` — loaded by every assistant before every task
|
|
111
|
+
- \`layer/skills/<name>/SKILL.md\` — loaded when a task matches its description
|
|
112
|
+
- \`layer/agents/*.agent.md\`, \`layer/prompts/\`, \`layer/workflows/\` — optional
|
|
113
|
+
- \`layer/config.json\` — MCP servers every repository gets, and the policy
|
|
114
|
+
(allowed servers and hosts, pinning, required items, context budget)
|
|
115
|
+
|
|
116
|
+
## Adopting it in a repository
|
|
117
|
+
|
|
118
|
+
\`\`\`sh
|
|
119
|
+
npm i -D ${name} opencastle
|
|
120
|
+
\`\`\`
|
|
121
|
+
|
|
122
|
+
\`\`\`jsonc
|
|
123
|
+
// .opencastle/config.json
|
|
124
|
+
{ "extends": ["${name}"] }
|
|
125
|
+
\`\`\`
|
|
126
|
+
|
|
127
|
+
\`\`\`sh
|
|
128
|
+
npx opencastle sync # compile it into every assistant
|
|
129
|
+
npx opencastle ci # fail pull requests on drift or policy breaks
|
|
130
|
+
\`\`\`
|
|
131
|
+
|
|
132
|
+
The version comes from package.json and the lockfile, like any dependency.
|
|
133
|
+
|
|
134
|
+
## Changing it
|
|
135
|
+
|
|
136
|
+
1. Edit \`layer/\`, then run \`npm run check\` — it validates the baseline the way
|
|
137
|
+
every repository extending it will.
|
|
138
|
+
2. Bump the version and publish.
|
|
139
|
+
3. Let your upgrade bot (Renovate, Dependabot) open the pull requests. In each,
|
|
140
|
+
CI runs \`opencastle sync --check\` and adds \`opencastle review\`'s summary of
|
|
141
|
+
what the new version changes for that repository's assistants.
|
|
142
|
+
`,
|
|
143
|
+
'.github/workflows/check.yml': `name: Check baseline
|
|
144
|
+
on: [pull_request, push]
|
|
145
|
+
permissions:
|
|
146
|
+
contents: read
|
|
147
|
+
jobs:
|
|
148
|
+
check:
|
|
149
|
+
runs-on: ubuntu-latest
|
|
150
|
+
steps:
|
|
151
|
+
- uses: actions/checkout@v4
|
|
152
|
+
- uses: actions/setup-node@v4
|
|
153
|
+
with:
|
|
154
|
+
node-version: 22
|
|
155
|
+
- run: npm install
|
|
156
|
+
- run: npx --no opencastle baseline check
|
|
157
|
+
`,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function init(pkgRoot: string, dir: string, name: string): Promise<void> {
|
|
162
|
+
const root = resolve(dir)
|
|
163
|
+
if (existsSync(join(root, 'package.json'))) {
|
|
164
|
+
console.error(`\n ${c.red('✗')} ${dir} already has a package.json — pick an empty directory.\n`)
|
|
165
|
+
process.exit(1)
|
|
166
|
+
}
|
|
167
|
+
for (const [rel, text] of Object.entries(files(name, cliVersionOf(pkgRoot)))) {
|
|
168
|
+
const abs = join(root, rel)
|
|
169
|
+
await mkdir(resolve(abs, '..'), { recursive: true })
|
|
170
|
+
await writeFile(abs, text)
|
|
171
|
+
}
|
|
172
|
+
console.log(`\n ${c.green('✓')} Created the baseline ${c.bold(name)} in ${dir}/`)
|
|
173
|
+
console.log(` ${c.dim('Next: edit layer/, run')} ${c.cyan(`npx opencastle baseline check ${dir}`)}${c.dim(', then publish it.')}`)
|
|
174
|
+
console.log(` ${c.dim('Repositories adopt it with')} ${c.cyan(`npm i -D ${name}`)} ${c.dim('and')} ${c.cyan(`"extends": ["${name}"]`)}\n`)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface CheckReport {
|
|
178
|
+
baseline: string
|
|
179
|
+
version?: string
|
|
180
|
+
errors: string[]
|
|
181
|
+
warnings: string[]
|
|
182
|
+
contributes: { skills: number; agents: number; instructions: number; prompts: number; workflows: number; mcpServers: number }
|
|
183
|
+
contextTokens: number
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function check(pkgRoot: string, dir: string): CheckReport {
|
|
187
|
+
const root = resolve(dir)
|
|
188
|
+
const errors: string[] = []
|
|
189
|
+
const warnings: string[] = []
|
|
190
|
+
let layerRoot = root
|
|
191
|
+
let name = dir
|
|
192
|
+
let version: string | undefined
|
|
193
|
+
const pkgFile = join(root, 'package.json')
|
|
194
|
+
if (existsSync(pkgFile)) {
|
|
195
|
+
let pkg: { name?: string; version?: string; files?: string[]; opencastle?: { baseline?: string } } = {}
|
|
196
|
+
try {
|
|
197
|
+
pkg = JSON.parse(readFileSync(pkgFile, 'utf8'))
|
|
198
|
+
} catch (err) {
|
|
199
|
+
errors.push(`package.json: is not valid JSON — ${(err as Error).message}`)
|
|
200
|
+
}
|
|
201
|
+
name = pkg.name ?? name
|
|
202
|
+
version = pkg.version
|
|
203
|
+
if (typeof pkg.opencastle?.baseline !== 'string') {
|
|
204
|
+
errors.push('package.json: has no "opencastle": { "baseline": "<dir>" }, so no repository can extend it')
|
|
205
|
+
} else {
|
|
206
|
+
layerRoot = resolve(root, pkg.opencastle.baseline)
|
|
207
|
+
const rel = relative(root, layerRoot).split('\\').join('/')
|
|
208
|
+
// Consumers refuse a layer outside the package, so this must too — or
|
|
209
|
+
// it would pass here and fail in every repository that extends it.
|
|
210
|
+
if (rel.startsWith('..') || isAbsolute(rel)) {
|
|
211
|
+
errors.push(`package.json: "opencastle.baseline" points outside the package (${pkg.opencastle.baseline}), which no repository extending it will accept`)
|
|
212
|
+
}
|
|
213
|
+
// `files` decides what `npm publish` ships. A baseline whose layer is not
|
|
214
|
+
// in it publishes as an empty package, and every repository extending it
|
|
215
|
+
// compiles nothing from it — with no error anywhere.
|
|
216
|
+
if (pkg.files && rel !== '' && !pkg.files.some((f) => f.replace(/\/$/, '') === rel || rel.startsWith(f.replace(/\/$/, '') + '/'))) {
|
|
217
|
+
errors.push(`package.json: "files" does not include ${rel}/, so npm publish would ship a baseline with nothing in it`)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (!existsSync(layerRoot)) errors.push(`${relative(process.cwd(), layerRoot) || '.'}: the layer directory does not exist`)
|
|
222
|
+
|
|
223
|
+
const contributes = { skills: 0, agents: 0, instructions: 0, prompts: 0, workflows: 0, mcpServers: 0 }
|
|
224
|
+
let contextTokens = 0
|
|
225
|
+
if (errors.length === 0) {
|
|
226
|
+
// Resolved exactly as a repository extending it would resolve it.
|
|
227
|
+
const project = mkdtempSync(join(tmpdir(), 'opencastle-baseline-check-'))
|
|
228
|
+
try {
|
|
229
|
+
mkdirSync(join(project, '.opencastle'), { recursive: true })
|
|
230
|
+
const spec = relative(join(project, '.opencastle'), layerRoot).split('\\').join('/')
|
|
231
|
+
writeFileSync(join(project, '.opencastle', 'config.json'), JSON.stringify({ extends: [spec.startsWith('.') ? spec : `./${spec}`] }))
|
|
232
|
+
const resolved = resolveSources({ pkgRoot, projectRoot: project, stack: { ides: [], techTools: [], teamTools: [] } })
|
|
233
|
+
const shown = (text: string): string => text.split(project).join('<repository>').replace(/(\.\.\/)+/g, '')
|
|
234
|
+
for (const line of formatIssues(resolved.issues.filter((i) => i.level === 'error'))) errors.push(shown(line))
|
|
235
|
+
for (const line of formatIssues(resolved.issues.filter((i) => i.level === 'warning'))) warnings.push(shown(line))
|
|
236
|
+
for (const item of resolved.items.values()) {
|
|
237
|
+
if (item.layer !== 'opencastle') contributes[item.kind]++
|
|
238
|
+
}
|
|
239
|
+
contributes.mcpServers = resolved.servers.size
|
|
240
|
+
if (!hasErrors(resolved)) {
|
|
241
|
+
const source = materialize(resolved, pkgRoot)
|
|
242
|
+
try {
|
|
243
|
+
const r = contentReport(source)
|
|
244
|
+
contextTokens = tokensOf(r.instructionChars + r.indexChars)
|
|
245
|
+
} finally {
|
|
246
|
+
source.dispose()
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} finally {
|
|
250
|
+
rmSync(project, { recursive: true, force: true })
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return { baseline: name, version, errors, warnings, contributes, contextTokens }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export default async function baseline({ pkgRoot, args }: CliContext): Promise<void> {
|
|
257
|
+
const [sub, ...rest] = args
|
|
258
|
+
if (!sub || args.includes('--help') || args.includes('-h') || !['init', 'check'].includes(sub)) {
|
|
259
|
+
console.log(HELP)
|
|
260
|
+
if (sub && !['init', 'check', '--help', '-h'].includes(sub)) process.exit(1)
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
const nameAt = rest.indexOf('--name')
|
|
264
|
+
if (nameAt !== -1 && (!rest[nameAt + 1] || rest[nameAt + 1].startsWith('--'))) {
|
|
265
|
+
console.error(` ${c.red('✗')} --name needs a package name, e.g. --name @acme/opencastle-baseline`)
|
|
266
|
+
process.exit(1)
|
|
267
|
+
}
|
|
268
|
+
const positional = rest.filter((a, i) => !a.startsWith('--') && !(nameAt !== -1 && i === nameAt + 1))
|
|
269
|
+
|
|
270
|
+
if (sub === 'init') {
|
|
271
|
+
await init(pkgRoot, positional[0] ?? 'opencastle-baseline', nameAt !== -1 ? rest[nameAt + 1] : '@your-org/opencastle-baseline')
|
|
272
|
+
return
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const report = check(pkgRoot, positional[0] ?? '.')
|
|
276
|
+
if (rest.includes('--json')) {
|
|
277
|
+
console.log(JSON.stringify(report, null, 2))
|
|
278
|
+
} else {
|
|
279
|
+
console.log(`\n 🏰 ${c.bold('Baseline')} ${report.baseline}${report.version ? c.dim(` ${report.version}`) : ''}\n`)
|
|
280
|
+
const k = report.contributes
|
|
281
|
+
const parts = Object.entries(k).filter(([, n]) => n > 0).map(([kind, n]) => `${n} ${kind === 'mcpServers' ? 'MCP server(s)' : kind}`)
|
|
282
|
+
console.log(` Contributes: ${parts.length > 0 ? parts.join(', ') : c.dim('nothing yet')}`)
|
|
283
|
+
if (report.contextTokens) console.log(` A repository extending it loads ~${report.contextTokens} tokens up front, with OpenCastle's own content`)
|
|
284
|
+
for (const e of report.errors) console.log(` ${c.red('✗')} ${e}`)
|
|
285
|
+
for (const w of report.warnings) console.log(` ${c.yellow('!')} ${c.dim(w)}`)
|
|
286
|
+
console.log(report.errors.length === 0 ? `\n ${c.green('✓')} Ready to publish.\n` : `\n ${c.red(`${report.errors.length} problem(s) to fix before publishing.`)}\n`)
|
|
287
|
+
}
|
|
288
|
+
if (report.errors.length > 0) process.exit(1)
|
|
289
|
+
}
|
package/src/cli/ci.ts
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { mkdir, writeFile, appendFile } from 'node:fs/promises'
|
|
4
|
+
import { basename, dirname, join, relative, sep } from 'node:path'
|
|
5
|
+
import { readManifest } from './manifest.js'
|
|
6
|
+
import { cliVersionOf, resolveSources } from './layers.js'
|
|
7
|
+
import { resolveStack } from './stack-config.js'
|
|
8
|
+
import { LOCK_REL } from './lock.js'
|
|
9
|
+
import { TEAM_CONFIG_REL } from './team-config.js'
|
|
10
|
+
import { c } from './prompt.js'
|
|
11
|
+
import type { CliContext } from './types.js'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `opencastle ci`: the checks a team needs, in the team's CI, in one command.
|
|
15
|
+
*
|
|
16
|
+
* `sync --check` has always been the gate; wiring it up was left to each team,
|
|
17
|
+
* and most never did — which is how generated config drifts. This writes a
|
|
18
|
+
* workflow that installs the project's dependencies (baselines come from
|
|
19
|
+
* there), runs the project's own OpenCastle version, fails on drift, and on a
|
|
20
|
+
* pull request writes what the change does to the assistants beside it. With
|
|
21
|
+
* `--owners`, CODEOWNERS routes every change to what assistants are given —
|
|
22
|
+
* all of which moves `.opencastle/lock.json` — to the people who own it.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const HELP = `
|
|
26
|
+
opencastle ci [options]
|
|
27
|
+
|
|
28
|
+
Write a GitHub Actions workflow that fails a pull request when generated
|
|
29
|
+
assistant config differs from its sources or breaks the team's policy, and
|
|
30
|
+
adds a summary of what the change does to every assistant.
|
|
31
|
+
|
|
32
|
+
Options:
|
|
33
|
+
--owners <team> Also route changes to .opencastle/lock.json and
|
|
34
|
+
.opencastle/config.json to these reviewers in CODEOWNERS
|
|
35
|
+
(e.g. @acme/platform)
|
|
36
|
+
--force Replace an existing workflow file
|
|
37
|
+
--dry-run Print what would be written, and write nothing
|
|
38
|
+
--help, -h Show this help
|
|
39
|
+
`
|
|
40
|
+
|
|
41
|
+
type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun'
|
|
42
|
+
|
|
43
|
+
function git(cwd: string, args: string[]): string | null {
|
|
44
|
+
try {
|
|
45
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
|
|
46
|
+
} catch {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function packageManagerOf(root: string): PackageManager {
|
|
52
|
+
if (existsSync(join(root, 'pnpm-lock.yaml'))) return 'pnpm'
|
|
53
|
+
if (existsSync(join(root, 'yarn.lock'))) return 'yarn'
|
|
54
|
+
if (existsSync(join(root, 'bun.lockb')) || existsSync(join(root, 'bun.lock'))) return 'bun'
|
|
55
|
+
return 'npm'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface Plan {
|
|
59
|
+
workflowPath: string
|
|
60
|
+
workflow: string
|
|
61
|
+
codeowners?: { path: string; block: string }
|
|
62
|
+
pinned: boolean
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The lockfile lives at the project, or — in a workspace — above it. */
|
|
66
|
+
function findUp(start: string, names: string[], stop: string): string | null {
|
|
67
|
+
let dir = start
|
|
68
|
+
for (;;) {
|
|
69
|
+
if (names.some((n) => existsSync(join(dir, n)))) return dir
|
|
70
|
+
if (dir === stop) return null
|
|
71
|
+
const parent = dirname(dir)
|
|
72
|
+
if (parent === dir) return null
|
|
73
|
+
dir = parent
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `extraPaths`: directories outside the project the check depends on — a
|
|
79
|
+
* baseline kept elsewhere in the repository, whose change must run it too.
|
|
80
|
+
*/
|
|
81
|
+
export function planCi(projectRoot: string, cliVersion: string, owners?: string, extraPaths: string[] = []): Plan {
|
|
82
|
+
const repoRoot = git(projectRoot, ['rev-parse', '--show-toplevel']) ?? projectRoot
|
|
83
|
+
const prefix = (git(projectRoot, ['rev-parse', '--show-prefix']) ?? '').replace(/\/$/, '')
|
|
84
|
+
const branch = (git(projectRoot, ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD']) ?? 'origin/main').replace(/^origin\//, '')
|
|
85
|
+
|
|
86
|
+
const installRoot =
|
|
87
|
+
findUp(projectRoot, ['package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'bun.lockb', 'bun.lock'], repoRoot) ??
|
|
88
|
+
(existsSync(join(projectRoot, 'package.json')) ? projectRoot : null)
|
|
89
|
+
const pm = installRoot ? packageManagerOf(installRoot) : 'npm'
|
|
90
|
+
|
|
91
|
+
let pkg: { dependencies?: Record<string, string>; devDependencies?: Record<string, string> } = {}
|
|
92
|
+
try {
|
|
93
|
+
pkg = JSON.parse(readFileSync(join(projectRoot, 'package.json'), 'utf8'))
|
|
94
|
+
} catch {
|
|
95
|
+
pkg = {}
|
|
96
|
+
}
|
|
97
|
+
// The project's own copy when it has one: then every laptop and CI run the
|
|
98
|
+
// version its lockfile pins, and nobody's global install decides the output.
|
|
99
|
+
const pinned = Boolean(pkg.devDependencies?.opencastle ?? pkg.dependencies?.opencastle)
|
|
100
|
+
const run = pinned
|
|
101
|
+
? pm === 'pnpm'
|
|
102
|
+
? 'pnpm exec opencastle'
|
|
103
|
+
: pm === 'yarn'
|
|
104
|
+
? 'yarn opencastle'
|
|
105
|
+
: 'npx --no opencastle'
|
|
106
|
+
: `npx -y opencastle@${cliVersion}`
|
|
107
|
+
|
|
108
|
+
const install: string[] = []
|
|
109
|
+
let installRel = ''
|
|
110
|
+
if (installRoot) {
|
|
111
|
+
installRel = installRoot === repoRoot ? '' : installRoot.slice(repoRoot.length + 1).split('\\').join('/')
|
|
112
|
+
// Always explicit in a subdirectory project: the job's default directory is
|
|
113
|
+
// the project, and a lockfile at the repository root installs from there —
|
|
114
|
+
// `npm ci` in a subdirectory that is not a workspace finds no lockfile.
|
|
115
|
+
const at = prefix || installRel ? `\n working-directory: ${installRel || '.'}` : ''
|
|
116
|
+
// The workspace root's `packageManager`, or the project's own.
|
|
117
|
+
let manager = ''
|
|
118
|
+
for (const dir of [installRoot, projectRoot]) {
|
|
119
|
+
try {
|
|
120
|
+
manager = (JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) as { packageManager?: string }).packageManager ?? ''
|
|
121
|
+
} catch {
|
|
122
|
+
manager = ''
|
|
123
|
+
}
|
|
124
|
+
if (manager) break
|
|
125
|
+
}
|
|
126
|
+
if (pm === 'pnpm') {
|
|
127
|
+
// The action needs a version, from `packageManager` or here.
|
|
128
|
+
install.push(' - uses: pnpm/action-setup@v4')
|
|
129
|
+
if (!manager.startsWith('pnpm@')) install.push(' with:', ' version: 10 # match the pnpm your lockfile was written with')
|
|
130
|
+
install.push(` - run: pnpm install --frozen-lockfile${at}`)
|
|
131
|
+
} else if (pm === 'yarn') {
|
|
132
|
+
// Yarn 1 ignores --immutable; --frozen-lockfile is its spelling.
|
|
133
|
+
const classic = !manager.startsWith('yarn@') || manager.startsWith('yarn@1')
|
|
134
|
+
install.push(' - run: corepack enable', ` - run: yarn install ${classic ? '--frozen-lockfile' : '--immutable'}${at}`)
|
|
135
|
+
} else if (pm === 'bun') {
|
|
136
|
+
install.push(' - uses: oven-sh/setup-bun@v2', ` - run: bun install --frozen-lockfile${at}`)
|
|
137
|
+
} else {
|
|
138
|
+
install.push(` - run: ${existsSync(join(installRoot, 'package-lock.json')) ? 'npm ci' : 'npm install'}${at}`)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const name = prefix ? `opencastle-${basename(prefix)}` : 'opencastle'
|
|
143
|
+
const workflowPath = join(repoRoot, '.github', 'workflows', `${name}.yml`)
|
|
144
|
+
// A subdirectory project runs only when something it depends on changes:
|
|
145
|
+
// its own files, the lockfile a baseline upgrade moves, and this workflow.
|
|
146
|
+
const paths = prefix
|
|
147
|
+
? [
|
|
148
|
+
`${prefix}/**`,
|
|
149
|
+
...(installRel !== prefix ? [installRel ? `${installRel}/package.json` : 'package.json'] : []),
|
|
150
|
+
...['package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'bun.lock', 'bun.lockb']
|
|
151
|
+
.filter((f) => installRoot && existsSync(join(installRoot, f)))
|
|
152
|
+
.map((f) => (installRel ? `${installRel}/${f}` : f)),
|
|
153
|
+
...extraPaths.map((p) => `${p}/**`),
|
|
154
|
+
`.github/workflows/${name}.yml`,
|
|
155
|
+
]
|
|
156
|
+
: []
|
|
157
|
+
const pathFilter = paths.length > 0 ? [` paths: [${[...new Set(paths)].map((p) => `'${p}'`).join(', ')}]`] : []
|
|
158
|
+
const lines = [
|
|
159
|
+
'# Written by `opencastle ci`. Fails a pull request when the AI assistant config',
|
|
160
|
+
"# every developer gets differs from its sources or breaks the team's policy,",
|
|
161
|
+
'# and summarises what the change does to the assistants.',
|
|
162
|
+
`name: AI assistant config${prefix ? ` (${prefix})` : ''}`,
|
|
163
|
+
'',
|
|
164
|
+
'on:',
|
|
165
|
+
' pull_request:',
|
|
166
|
+
...pathFilter,
|
|
167
|
+
' push:',
|
|
168
|
+
` branches: [${branch}]`,
|
|
169
|
+
...pathFilter,
|
|
170
|
+
'',
|
|
171
|
+
'permissions:',
|
|
172
|
+
' contents: read',
|
|
173
|
+
'',
|
|
174
|
+
'jobs:',
|
|
175
|
+
' opencastle:',
|
|
176
|
+
' runs-on: ubuntu-latest',
|
|
177
|
+
...(prefix ? [' defaults:', ' run:', ` working-directory: ${prefix}`] : []),
|
|
178
|
+
' steps:',
|
|
179
|
+
' # Pin actions to a commit SHA if your organisation requires it.',
|
|
180
|
+
' - uses: actions/checkout@v4',
|
|
181
|
+
' with:',
|
|
182
|
+
' fetch-depth: 0',
|
|
183
|
+
' - uses: actions/setup-node@v4',
|
|
184
|
+
' with:',
|
|
185
|
+
' node-version: 22',
|
|
186
|
+
...install,
|
|
187
|
+
' - name: Generated config matches its sources and policy',
|
|
188
|
+
` run: ${run} sync --check`,
|
|
189
|
+
' - name: What this pull request changes',
|
|
190
|
+
" if: github.event_name == 'pull_request'",
|
|
191
|
+
` run: ${run} review --base origin/\${{ github.base_ref }}`,
|
|
192
|
+
'',
|
|
193
|
+
]
|
|
194
|
+
|
|
195
|
+
const plan: Plan = { workflowPath, workflow: lines.join('\n'), pinned }
|
|
196
|
+
if (owners) {
|
|
197
|
+
const at = prefix ? `/${prefix}/` : '/'
|
|
198
|
+
// GitHub reads the first CODEOWNERS it finds — .github/, then the root,
|
|
199
|
+
// then docs/ — and ignores the rest. Creating .github/CODEOWNERS beside an
|
|
200
|
+
// existing one would switch every other ownership rule off.
|
|
201
|
+
const existing = ['.github/CODEOWNERS', 'CODEOWNERS', 'docs/CODEOWNERS'].map((p) => join(repoRoot, p)).find((p) => existsSync(p))
|
|
202
|
+
plan.codeowners = {
|
|
203
|
+
path: existing ?? join(repoRoot, '.github', 'CODEOWNERS'),
|
|
204
|
+
block: [
|
|
205
|
+
'',
|
|
206
|
+
'# What every AI assistant is given (opencastle ci). Every change to it moves',
|
|
207
|
+
'# the lock, so these two lines route all of them to the owners.',
|
|
208
|
+
`${at}${LOCK_REL} ${owners}`,
|
|
209
|
+
`${at}${TEAM_CONFIG_REL} ${owners}`,
|
|
210
|
+
'',
|
|
211
|
+
].join('\n'),
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return plan
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export default async function ci({ pkgRoot, args }: CliContext): Promise<void> {
|
|
218
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
219
|
+
console.log(HELP)
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
const projectRoot = process.cwd()
|
|
223
|
+
const ownersAt = args.indexOf('--owners')
|
|
224
|
+
const owners = ownersAt !== -1 ? args[ownersAt + 1] : undefined
|
|
225
|
+
if (ownersAt !== -1 && (!owners || owners.startsWith('--'))) {
|
|
226
|
+
console.error(` ${c.red('✗')} --owners needs a team or user, e.g. --owners @acme/platform`)
|
|
227
|
+
process.exit(1)
|
|
228
|
+
}
|
|
229
|
+
if (!(await readManifest(projectRoot))) {
|
|
230
|
+
console.error(`\n ${c.red('✗')} OpenCastle is not set up here — run opencastle init first.\n`)
|
|
231
|
+
process.exit(1)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const manifest = await readManifest(projectRoot)
|
|
235
|
+
const manifestVersion = manifest?.version
|
|
236
|
+
// Baselines kept elsewhere in this repository: a change there must run the check.
|
|
237
|
+
const repoRoot = git(projectRoot, ['rev-parse', '--show-toplevel']) ?? projectRoot
|
|
238
|
+
const extra: string[] = []
|
|
239
|
+
try {
|
|
240
|
+
const resolved = resolveSources({ pkgRoot, projectRoot, stack: resolveStack(manifest!), repoInfo: manifest?.repoInfo })
|
|
241
|
+
for (const l of resolved.layers) {
|
|
242
|
+
if (l.kind !== 'baseline' || l.root.includes(`${sep}node_modules${sep}`)) continue
|
|
243
|
+
const rel = relative(repoRoot, l.root)
|
|
244
|
+
if (rel && !rel.startsWith('..') && relative(projectRoot, l.root).startsWith('..')) extra.push(rel.split(sep).join('/'))
|
|
245
|
+
}
|
|
246
|
+
} catch {
|
|
247
|
+
// The workflow is still right without them; sync --check will name the problem.
|
|
248
|
+
}
|
|
249
|
+
const plan = planCi(projectRoot, manifestVersion ?? cliVersionOf(pkgRoot), owners, extra)
|
|
250
|
+
const rel = (p: string): string => p.slice((git(projectRoot, ['rev-parse', '--show-toplevel']) ?? projectRoot).length + 1)
|
|
251
|
+
|
|
252
|
+
if (args.includes('--dry-run')) {
|
|
253
|
+
console.log(`\n ${c.dim(`[dry-run] ${rel(plan.workflowPath)}:`)}\n`)
|
|
254
|
+
console.log(plan.workflow.split('\n').map((l) => ` ${l}`).join('\n'))
|
|
255
|
+
if (plan.codeowners) {
|
|
256
|
+
console.log(` ${c.dim(`[dry-run] appended to ${rel(plan.codeowners.path)}:`)}`)
|
|
257
|
+
console.log(plan.codeowners.block.split('\n').map((l) => ` ${l}`).join('\n'))
|
|
258
|
+
}
|
|
259
|
+
console.log(` ${c.dim('No files were written.')}\n`)
|
|
260
|
+
return
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (existsSync(plan.workflowPath) && !args.includes('--force')) {
|
|
264
|
+
console.error(`\n ${c.red('✗')} ${rel(plan.workflowPath)} already exists. Pass --force to replace it.\n`)
|
|
265
|
+
process.exit(1)
|
|
266
|
+
}
|
|
267
|
+
await mkdir(dirname(plan.workflowPath), { recursive: true })
|
|
268
|
+
await writeFile(plan.workflowPath, plan.workflow)
|
|
269
|
+
console.log(`\n ${c.green('✓')} Wrote ${rel(plan.workflowPath)}`)
|
|
270
|
+
|
|
271
|
+
if (plan.codeowners) {
|
|
272
|
+
const existing = existsSync(plan.codeowners.path) ? readFileSync(plan.codeowners.path, 'utf8') : ''
|
|
273
|
+
const lockLine = plan.codeowners.block.split('\n').find((l) => l.includes(LOCK_REL)) ?? ''
|
|
274
|
+
if (existing.includes(lockLine.split(' ')[0])) {
|
|
275
|
+
console.log(` ${c.dim(`${rel(plan.codeowners.path)} already routes ${LOCK_REL}; left as it is`)}`)
|
|
276
|
+
} else {
|
|
277
|
+
await mkdir(dirname(plan.codeowners.path), { recursive: true })
|
|
278
|
+
await appendFile(plan.codeowners.path, plan.codeowners.block)
|
|
279
|
+
console.log(` ${c.green('✓')} Added ${owners} as owners of the lock and the team config in ${rel(plan.codeowners.path)}`)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (!plan.pinned) {
|
|
283
|
+
console.log(
|
|
284
|
+
` ${c.yellow('!')} ${c.dim('OpenCastle is not in devDependencies, so CI pins the version that compiled this project.')}`,
|
|
285
|
+
)
|
|
286
|
+
console.log(` ${c.dim('Add it (npm i -D opencastle) so laptops and CI run one version, then re-run with --force.')}`)
|
|
287
|
+
}
|
|
288
|
+
console.log(` ${c.dim('Commit it; the next pull request gets the check and a summary of what it changes.')}\n`)
|
|
289
|
+
}
|