opencode-dev-framework 0.1.8 → 0.1.12
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 +30 -9
- package/bin/df +36 -1
- package/dist/config.js +16 -11
- package/dist/detect.d.ts +26 -0
- package/dist/detect.js +183 -0
- package/dist/index.js +9 -3
- package/dist/installer.d.ts +15 -0
- package/dist/installer.js +132 -0
- package/dist/lint.d.ts +6 -0
- package/dist/lint.js +14 -1
- package/dist/registry.d.ts +2 -0
- package/dist/rules.d.ts +21 -14
- package/dist/rules.js +109 -37
- package/dist/tools.js +6 -23
- package/dist/types.d.ts +12 -5
- package/package.json +1 -1
- package/templates/.opencode/opencode-dev-framework/rules/.gitkeep +0 -0
- package/templates/.opencode-dev-framework.yml +0 -35
package/README.md
CHANGED
|
@@ -9,13 +9,15 @@ reimplemented as an OpenCode-native plugin.
|
|
|
9
9
|
|
|
10
10
|
- **Constitution injection.** Adds a project constitution (quality bar, test
|
|
11
11
|
discipline, focus rules) to the system prompt at session start. Ships with a
|
|
12
|
-
bundled default split into numbered rule files
|
|
13
|
-
|
|
12
|
+
bundled default split into numbered rule files. Override it by dropping files
|
|
13
|
+
in `.opencode/opencode-dev-framework/rules/`, by setting explicit `rules`, or
|
|
14
|
+
by letting the plugin auto-discover `STYLE.md` / `CONTRIBUTING.md`.
|
|
14
15
|
- **Guardrails.** Blocks or warns on edits to protected paths (`.env` files,
|
|
15
16
|
`node_modules`, vendored code, your own globs) and on dangerous shell
|
|
16
17
|
commands (`git push`, `rm -rf`, `git reset --hard`, ...).
|
|
17
18
|
- **Per-edit lint.** Runs your configured linter on each file the agent edits
|
|
18
|
-
and reports failures loudly.
|
|
19
|
+
and reports failures loudly. Optionally delegates per-file linting to
|
|
20
|
+
`pre-commit run --files` when `precommit: auto` is set.
|
|
19
21
|
- **Completion gate.** When the session goes idle, runs typecheck, tests, and
|
|
20
22
|
(optionally) lint on changed files. In OpenCode versions with the
|
|
21
23
|
`experimental.session.stopping` hook, gate failures keep the session running
|
|
@@ -24,6 +26,9 @@ reimplemented as an OpenCode-native plugin.
|
|
|
24
26
|
- **Custom tools.** `dev_framework_init` scaffolds project-level agents,
|
|
25
27
|
skills, commands, and config; `dev_framework_set_profile` changes the profile
|
|
26
28
|
in-session without restarting.
|
|
29
|
+
- **CLI installer.** `df init` auto-detects your language and writes a config
|
|
30
|
+
with sensible commands; `df profile <name>` changes the profile from the
|
|
31
|
+
shell; `df status` / `df version` report template state and the version.
|
|
27
32
|
- **Slash-command templates.** `/df-verify` and `/df-profile` are installed
|
|
28
33
|
into `.opencode/commands/` by `df init` or the `dev_framework_init` tool.
|
|
29
34
|
(OpenCode does not load slash commands from plugin packages automatically.)
|
|
@@ -62,8 +67,10 @@ df init
|
|
|
62
67
|
|
|
63
68
|
`df init` is interactive by default: it asks before overwriting existing files.
|
|
64
69
|
Use `--skip-existing` or `--overwrite-existing` for non-interactive runs.
|
|
65
|
-
`df
|
|
66
|
-
|
|
70
|
+
`df init` auto-detects your project's language and writes a matching
|
|
71
|
+
`.opencode-dev-framework.yml` with sensible commands. `df status` shows what is
|
|
72
|
+
and isn't scaffolded; `df profile off|advisory|standard|strict` changes the
|
|
73
|
+
profile from the shell; `df version` prints the plugin version.
|
|
67
74
|
|
|
68
75
|
## Local development and testing
|
|
69
76
|
|
|
@@ -100,7 +107,9 @@ npm run build
|
|
|
100
107
|
|
|
101
108
|
## Configuration
|
|
102
109
|
|
|
103
|
-
Create `.opencode-dev-framework.yml` in your project root
|
|
110
|
+
Create `.opencode-dev-framework.yml` in your project root. The easiest way to
|
|
111
|
+
override the bundled constitution is to run `df init` and then add Markdown
|
|
112
|
+
files to `.opencode/opencode-dev-framework/rules/`.
|
|
104
113
|
|
|
105
114
|
```yaml
|
|
106
115
|
profile: standard
|
|
@@ -123,7 +132,16 @@ gate:
|
|
|
123
132
|
on_edit:
|
|
124
133
|
lint: true
|
|
125
134
|
|
|
126
|
-
|
|
135
|
+
# Optional: delegate per-file linting to pre-commit when available
|
|
136
|
+
# precommit: auto
|
|
137
|
+
|
|
138
|
+
# Optional: explicit rule files (replace bundled; use mode: append to extend)
|
|
139
|
+
# rules:
|
|
140
|
+
# - docs/team-rules.md
|
|
141
|
+
|
|
142
|
+
# Optional: project style guide appended to the constitution.
|
|
143
|
+
# Auto-discovers STYLE.md / CONTRIBUTING.md / docs/STYLE.md when not set.
|
|
144
|
+
# style_guide: STYLE.md
|
|
127
145
|
```
|
|
128
146
|
|
|
129
147
|
The legacy `.dev-framework.yml` flat-key format is read as a fallback for
|
|
@@ -149,8 +167,11 @@ suite manually, or `/df-profile strict` to change the profile.
|
|
|
149
167
|
- **Guardrails run inside OpenCode.** The `tool.execute.before` hook runs
|
|
150
168
|
after OpenCode's own permission system; it adds project rules on top, it
|
|
151
169
|
does not replace OpenCode permissions.
|
|
152
|
-
- **Formatting
|
|
153
|
-
|
|
170
|
+
- **Formatting and linting are delegated.** The plugin does not reimplement
|
|
171
|
+
formatters or linters; you declare your formatter and linter commands in
|
|
172
|
+
`.opencode-dev-framework.yml` and the plugin runs them on edited files and
|
|
173
|
+
at the completion gate. It does not automatically rewrite `opencode.json`
|
|
174
|
+
formatter/permission fragments.
|
|
154
175
|
|
|
155
176
|
## Development
|
|
156
177
|
|
package/bin/df
CHANGED
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
import { readFile } from "node:fs/promises";
|
|
6
6
|
import { dirname, join } from "node:path";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
installTemplates,
|
|
10
|
+
resolveTargetDir,
|
|
11
|
+
setProfileInFile,
|
|
12
|
+
statusTemplates,
|
|
13
|
+
writeDetectedConfig,
|
|
14
|
+
} from "../dist/installer.js";
|
|
9
15
|
|
|
10
16
|
async function getVersion() {
|
|
11
17
|
const packagePath = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
|
|
@@ -18,6 +24,7 @@ function printHelp() {
|
|
|
18
24
|
|
|
19
25
|
Usage:
|
|
20
26
|
df init [dir] Scaffold templates into a project (default: cwd)
|
|
27
|
+
df profile <name> [dir] Set profile to off, advisory, standard, or strict
|
|
21
28
|
df status [dir] Show which template files are missing/present/different
|
|
22
29
|
df version Print the plugin version
|
|
23
30
|
df help Show this help
|
|
@@ -49,6 +56,22 @@ async function main() {
|
|
|
49
56
|
return;
|
|
50
57
|
}
|
|
51
58
|
|
|
59
|
+
if (command === "profile") {
|
|
60
|
+
const profile = positional[0];
|
|
61
|
+
const profileDir = resolveTargetDir(positional[1]);
|
|
62
|
+
const valid = ["off", "advisory", "standard", "strict"];
|
|
63
|
+
if (!profile || !valid.includes(profile)) {
|
|
64
|
+
console.error(`Invalid profile: ${profile ?? "(missing)"}`);
|
|
65
|
+
console.error(`Valid profiles: ${valid.join(", ")}`);
|
|
66
|
+
process.exitCode = 1;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const configPath = join(profileDir, ".opencode-dev-framework.yml");
|
|
70
|
+
await setProfileInFile(configPath, profile);
|
|
71
|
+
console.log(`Profile set to "${profile}" in ${configPath}`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
52
75
|
if (command !== "init" && command !== "status") {
|
|
53
76
|
console.error(`Unknown command: ${command}`);
|
|
54
77
|
printHelp();
|
|
@@ -60,6 +83,12 @@ async function main() {
|
|
|
60
83
|
const positional = args.slice(1).filter((arg) => !arg.startsWith("--"));
|
|
61
84
|
const targetDir = resolveTargetDir(positional[0]);
|
|
62
85
|
|
|
86
|
+
if (command === "init" && flags.has("--skip-existing") && flags.has("--overwrite-existing")) {
|
|
87
|
+
console.error("Cannot use both --skip-existing and --overwrite-existing");
|
|
88
|
+
process.exitCode = 1;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
63
92
|
if (command === "status") {
|
|
64
93
|
const status = await statusTemplates(targetDir);
|
|
65
94
|
console.log(`Template status for ${targetDir}:`);
|
|
@@ -78,6 +107,11 @@ async function main() {
|
|
|
78
107
|
skipExisting: flags.has("--skip-existing"),
|
|
79
108
|
});
|
|
80
109
|
|
|
110
|
+
const configResult = await writeDetectedConfig(targetDir, {
|
|
111
|
+
overwriteExisting: flags.has("--overwrite-existing"),
|
|
112
|
+
skipExisting: flags.has("--skip-existing"),
|
|
113
|
+
});
|
|
114
|
+
|
|
81
115
|
console.log(`Installed templates into ${targetDir}:`);
|
|
82
116
|
for (const line of [
|
|
83
117
|
...formatList("Created", result.created),
|
|
@@ -86,6 +120,7 @@ async function main() {
|
|
|
86
120
|
]) {
|
|
87
121
|
console.log(line);
|
|
88
122
|
}
|
|
123
|
+
console.log(`\nConfig: ${configResult.action}`);
|
|
89
124
|
}
|
|
90
125
|
|
|
91
126
|
main().catch((error) => {
|
package/dist/config.js
CHANGED
|
@@ -60,9 +60,17 @@ const configSchema = z.object({
|
|
|
60
60
|
})
|
|
61
61
|
.optional(),
|
|
62
62
|
exclude: z.array(z.string()).optional(),
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
rules: z
|
|
64
|
+
.union([
|
|
65
|
+
z.array(z.string()),
|
|
66
|
+
z.object({
|
|
67
|
+
mode: z.enum(["replace", "append"]),
|
|
68
|
+
files: z.array(z.string()),
|
|
69
|
+
}),
|
|
70
|
+
])
|
|
71
|
+
.optional(),
|
|
65
72
|
style_guide: z.string().optional(),
|
|
73
|
+
precommit: z.enum(["auto", "off"]).optional(),
|
|
66
74
|
});
|
|
67
75
|
/** Find the highest-precedence config file in a directory, if any. */
|
|
68
76
|
export function findConfigFile(directory) {
|
|
@@ -164,9 +172,6 @@ export function mapFlatConfig(flat) {
|
|
|
164
172
|
case "test_changed":
|
|
165
173
|
commands.test_changed = String(value);
|
|
166
174
|
break;
|
|
167
|
-
case "precommit":
|
|
168
|
-
// Ignored in MVP.
|
|
169
|
-
break;
|
|
170
175
|
case "format_on_edit":
|
|
171
176
|
onEdit.format = toBoolean(value);
|
|
172
177
|
break;
|
|
@@ -209,15 +214,15 @@ export function mapFlatConfig(flat) {
|
|
|
209
214
|
case "exclude":
|
|
210
215
|
config.exclude = splitGlobs(value);
|
|
211
216
|
break;
|
|
212
|
-
case "constitution":
|
|
213
|
-
config.constitution = String(value);
|
|
214
|
-
break;
|
|
215
217
|
case "rules":
|
|
216
|
-
config.rules = splitGlobs(value);
|
|
218
|
+
config.rules = { mode: "replace", files: splitGlobs(value) };
|
|
217
219
|
break;
|
|
218
220
|
case "style_guide":
|
|
219
221
|
config.style_guide = String(value);
|
|
220
222
|
break;
|
|
223
|
+
case "precommit":
|
|
224
|
+
config.precommit = value === "auto" || value === true ? "auto" : "off";
|
|
225
|
+
break;
|
|
221
226
|
default:
|
|
222
227
|
// Unknown flat keys are ignored for forward compatibility.
|
|
223
228
|
break;
|
|
@@ -278,9 +283,9 @@ export function resolveConfig(raw, configPath) {
|
|
|
278
283
|
lint: raw.on_edit?.lint ?? defaults.on_edit_lint,
|
|
279
284
|
},
|
|
280
285
|
exclude: raw.exclude ?? [],
|
|
281
|
-
|
|
282
|
-
rules: raw.rules,
|
|
286
|
+
rules: Array.isArray(raw.rules) ? { mode: "replace", files: raw.rules } : raw.rules,
|
|
283
287
|
style_guide: raw.style_guide,
|
|
288
|
+
precommit: raw.precommit ?? "off",
|
|
284
289
|
};
|
|
285
290
|
}
|
|
286
291
|
function validateConfig(data, configPath) {
|
package/dist/detect.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Project auto-detection for `df init`.
|
|
3
|
+
*
|
|
4
|
+
* Detects the dominant language, test/typecheck/format/lint commands, and
|
|
5
|
+
* pre-commit availability by inspecting files in the project root. All
|
|
6
|
+
* detection is synchronous and side-effect free; callers decide what to do
|
|
7
|
+
* with the result.
|
|
8
|
+
*/
|
|
9
|
+
import type { CommandsConfig } from "./types.js";
|
|
10
|
+
export interface DetectionResult {
|
|
11
|
+
/** Dominant language or framework detected from root files. */
|
|
12
|
+
language: string | null;
|
|
13
|
+
/** Suggested commands config for the project. */
|
|
14
|
+
commands: CommandsConfig;
|
|
15
|
+
/** True when a `.pre-commit-config.yaml` file is present. */
|
|
16
|
+
hasPreCommitConfig: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Detect project tooling by scanning the directory root.
|
|
20
|
+
*/
|
|
21
|
+
export declare function detectProject(directory: string): DetectionResult;
|
|
22
|
+
/**
|
|
23
|
+
* Build a YAML config string from a detection result. Only includes keys that
|
|
24
|
+
* have a detected value.
|
|
25
|
+
*/
|
|
26
|
+
export declare function detectedConfigYaml(language: string | null, commands: CommandsConfig): string;
|
package/dist/detect.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Project auto-detection for `df init`.
|
|
3
|
+
*
|
|
4
|
+
* Detects the dominant language, test/typecheck/format/lint commands, and
|
|
5
|
+
* pre-commit availability by inspecting files in the project root. All
|
|
6
|
+
* detection is synchronous and side-effect free; callers decide what to do
|
|
7
|
+
* with the result.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
function fileExists(path) {
|
|
12
|
+
try {
|
|
13
|
+
return existsSync(path);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function readJsonSafe(path) {
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function hasAny(files, names) {
|
|
28
|
+
return names.some((name) => files.includes(name));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Detect project tooling by scanning the directory root.
|
|
32
|
+
*/
|
|
33
|
+
export function detectProject(directory) {
|
|
34
|
+
const files = readdirSync(directory);
|
|
35
|
+
const has = {
|
|
36
|
+
packageJson: fileExists(join(directory, "package.json")),
|
|
37
|
+
goMod: fileExists(join(directory, "go.mod")),
|
|
38
|
+
cargoToml: fileExists(join(directory, "Cargo.toml")),
|
|
39
|
+
pyProject: fileExists(join(directory, "pyproject.toml")),
|
|
40
|
+
requirements: fileExists(join(directory, "requirements.txt")),
|
|
41
|
+
gemfile: fileExists(join(directory, "Gemfile")),
|
|
42
|
+
composerJson: fileExists(join(directory, "composer.json")),
|
|
43
|
+
pomXml: fileExists(join(directory, "pom.xml")),
|
|
44
|
+
gradle: hasAny(files, ["build.gradle", "build.gradle.kts"]),
|
|
45
|
+
mavenWrapper: fileExists(join(directory, "mvnw")),
|
|
46
|
+
gradleWrapper: fileExists(join(directory, "gradlew")),
|
|
47
|
+
tsConfig: fileExists(join(directory, "tsconfig.json")),
|
|
48
|
+
};
|
|
49
|
+
const result = {
|
|
50
|
+
language: null,
|
|
51
|
+
commands: {},
|
|
52
|
+
hasPreCommitConfig: fileExists(join(directory, ".pre-commit-config.yaml")),
|
|
53
|
+
};
|
|
54
|
+
if (has.packageJson) {
|
|
55
|
+
result.language = "node";
|
|
56
|
+
const pkg = readJsonSafe(join(directory, "package.json"));
|
|
57
|
+
const scripts = pkg?.scripts ?? {};
|
|
58
|
+
if (typeof scripts.test === "string") {
|
|
59
|
+
result.commands.test = "npm test";
|
|
60
|
+
}
|
|
61
|
+
if (has.tsConfig) {
|
|
62
|
+
result.commands.typecheck = "npx tsc --noEmit";
|
|
63
|
+
}
|
|
64
|
+
const devDeps = {
|
|
65
|
+
...(pkg?.devDependencies ?? {}),
|
|
66
|
+
...(pkg?.dependencies ?? {}),
|
|
67
|
+
};
|
|
68
|
+
if ("prettier" in devDeps) {
|
|
69
|
+
result.commands.format = "npx prettier --write {file}";
|
|
70
|
+
}
|
|
71
|
+
if ("eslint" in devDeps) {
|
|
72
|
+
result.commands.lint = "npx eslint {file}";
|
|
73
|
+
}
|
|
74
|
+
else if ("biome" in devDeps) {
|
|
75
|
+
result.commands.lint = "npx biome lint {file}";
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
if (has.goMod) {
|
|
80
|
+
result.language = "go";
|
|
81
|
+
result.commands.test = "go test ./...";
|
|
82
|
+
result.commands.typecheck = "go build ./...";
|
|
83
|
+
result.commands.format = "gofumpt -w {file}";
|
|
84
|
+
result.commands.lint = "golangci-lint run {file}";
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
if (has.pyProject || has.requirements) {
|
|
88
|
+
result.language = "python";
|
|
89
|
+
result.commands.test = "pytest";
|
|
90
|
+
if (has.pyProject) {
|
|
91
|
+
result.commands.typecheck = "mypy .";
|
|
92
|
+
}
|
|
93
|
+
result.commands.format = "black {file}";
|
|
94
|
+
result.commands.lint = "flake8 {file}";
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
if (has.cargoToml) {
|
|
98
|
+
result.language = "rust";
|
|
99
|
+
result.commands.test = "cargo test";
|
|
100
|
+
result.commands.typecheck = "cargo check";
|
|
101
|
+
result.commands.format = "cargo fmt";
|
|
102
|
+
result.commands.lint = "cargo clippy";
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
if (has.gemfile) {
|
|
106
|
+
result.language = "ruby";
|
|
107
|
+
result.commands.test = "bundle exec rake test";
|
|
108
|
+
result.commands.format = "rubocop -A {file}";
|
|
109
|
+
result.commands.lint = "rubocop {file}";
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
if (has.composerJson) {
|
|
113
|
+
result.language = "php";
|
|
114
|
+
result.commands.test = "composer test";
|
|
115
|
+
result.commands.format = "php-cs-fixer fix {file}";
|
|
116
|
+
result.commands.lint = "phpstan analyse {file}";
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
if (has.pomXml || has.gradle) {
|
|
120
|
+
result.language = "java";
|
|
121
|
+
if (has.mavenWrapper) {
|
|
122
|
+
result.commands.test = "./mvnw test";
|
|
123
|
+
result.commands.typecheck = "./mvnw compile";
|
|
124
|
+
}
|
|
125
|
+
else if (has.gradleWrapper) {
|
|
126
|
+
result.commands.test = "./gradlew test";
|
|
127
|
+
result.commands.typecheck = "./gradlew compileJava";
|
|
128
|
+
}
|
|
129
|
+
else if (has.pomXml) {
|
|
130
|
+
result.commands.test = "mvn test";
|
|
131
|
+
result.commands.typecheck = "mvn compile";
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
result.commands.test = "gradle test";
|
|
135
|
+
result.commands.typecheck = "gradle compileJava";
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Build a YAML config string from a detection result. Only includes keys that
|
|
143
|
+
* have a detected value.
|
|
144
|
+
*/
|
|
145
|
+
export function detectedConfigYaml(language, commands) {
|
|
146
|
+
const lines = [];
|
|
147
|
+
lines.push("profile: standard");
|
|
148
|
+
if (language !== null) {
|
|
149
|
+
lines.push(`# Detected language: ${language}`);
|
|
150
|
+
}
|
|
151
|
+
if (Object.keys(commands).length > 0) {
|
|
152
|
+
lines.push("commands:");
|
|
153
|
+
if (commands.test !== undefined)
|
|
154
|
+
lines.push(` test: ${commands.test}`);
|
|
155
|
+
if (commands.typecheck !== undefined)
|
|
156
|
+
lines.push(` typecheck: ${commands.typecheck}`);
|
|
157
|
+
if (commands.format !== undefined) {
|
|
158
|
+
if (typeof commands.format === "string") {
|
|
159
|
+
lines.push(` format: ${commands.format}`);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
lines.push(" format:");
|
|
163
|
+
for (const [ext, cmd] of Object.entries(commands.format)) {
|
|
164
|
+
lines.push(` ${ext}: ${cmd}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (commands.lint !== undefined) {
|
|
169
|
+
if (typeof commands.lint === "string") {
|
|
170
|
+
lines.push(` lint: ${commands.lint}`);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
lines.push(" lint:");
|
|
174
|
+
for (const [ext, cmd] of Object.entries(commands.lint)) {
|
|
175
|
+
lines.push(` ${ext}: ${cmd}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (commands.test_changed !== undefined)
|
|
180
|
+
lines.push(` test_changed: ${commands.test_changed}`);
|
|
181
|
+
}
|
|
182
|
+
return lines.join("\n");
|
|
183
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
import { loadConfig } from "./config.js";
|
|
13
13
|
import { createChangedFileTracker, runGate, summarizeGate, } from "./gate.js";
|
|
14
14
|
import { runCommand } from "./host.js";
|
|
15
|
-
import { isLintFailure, lintFile, summarizeLint } from "./lint.js";
|
|
15
|
+
import { detectPreCommitAvailability, isLintFailure, lintFile, summarizeLint } from "./lint.js";
|
|
16
16
|
import { createLogger } from "./logger.js";
|
|
17
17
|
import { checkToolCall } from "./protect.js";
|
|
18
18
|
import { clearSessionDirectory, getDirectoryForSession, getHookState, setHookState, setSessionDirectory, } from "./registry.js";
|
|
@@ -53,7 +53,7 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
|
|
|
53
53
|
}
|
|
54
54
|
},
|
|
55
55
|
"tool.execute.before": async (input, output) => {
|
|
56
|
-
const state = getHookState();
|
|
56
|
+
const state = getHookState(getDirectoryForSession(input.sessionID) ?? undefined);
|
|
57
57
|
if (!state) {
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
@@ -111,7 +111,9 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
|
|
|
111
111
|
clearSessionDirectory(event.properties.info.id);
|
|
112
112
|
return;
|
|
113
113
|
}
|
|
114
|
-
const state = getHookState(
|
|
114
|
+
const state = getHookState(event.type === "session.idle"
|
|
115
|
+
? (getDirectoryForSession(event.properties.sessionID) ?? undefined)
|
|
116
|
+
: undefined);
|
|
115
117
|
if (!state) {
|
|
116
118
|
return;
|
|
117
119
|
}
|
|
@@ -121,9 +123,13 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
|
|
|
121
123
|
if (!state.config.on_edit.lint) {
|
|
122
124
|
return;
|
|
123
125
|
}
|
|
126
|
+
if (state.config.precommit === "auto" && state.precommitAvailable === undefined) {
|
|
127
|
+
state.precommitAvailable = await detectPreCommitAvailability(state.run, state.directory);
|
|
128
|
+
}
|
|
124
129
|
const outcome = await lintFile(state.run, state.config, filePath, {
|
|
125
130
|
cwd: state.directory,
|
|
126
131
|
timeout: state.config.gate?.timeout,
|
|
132
|
+
precommitAvailable: state.precommitAvailable,
|
|
127
133
|
});
|
|
128
134
|
if (outcome.skipped) {
|
|
129
135
|
await state.log("debug", summarizeLint(outcome), { filePath, reason: outcome.reason });
|
package/dist/installer.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Profile } from "./types.js";
|
|
1
2
|
/** Directory containing the project templates shipped with the package. */
|
|
2
3
|
export declare const TEMPLATES_DIR: string;
|
|
3
4
|
export type OverwriteDecision = "overwrite" | "skip" | "overwrite-all" | "skip-all";
|
|
@@ -31,5 +32,19 @@ export declare function installTemplates(targetDir: string, options?: InstallOpt
|
|
|
31
32
|
* Compare the target project against the bundled templates.
|
|
32
33
|
*/
|
|
33
34
|
export declare function statusTemplates(targetDir: string): Promise<TemplateStatus>;
|
|
35
|
+
export interface ConfigWriteResult {
|
|
36
|
+
action: "created" | "skipped" | "overwritten";
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Write a project-level `.opencode-dev-framework.yml` based on detected
|
|
40
|
+
* tooling. Respects the same overwrite/skip flags and prompt as
|
|
41
|
+
* `installTemplates`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function writeDetectedConfig(targetDir: string, options?: InstallOptions): Promise<ConfigWriteResult>;
|
|
34
44
|
/** Resolve a user-supplied directory (default cwd) to an absolute path. */
|
|
35
45
|
export declare function resolveTargetDir(dir?: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Set the top-level `profile:` key in a config file. Edits the raw text
|
|
48
|
+
* instead of re-serializing the YAML so user comments and formatting survive.
|
|
49
|
+
*/
|
|
50
|
+
export declare function setProfileInFile(configPath: string, profile: Profile): Promise<void>;
|
package/dist/installer.js
CHANGED
|
@@ -2,6 +2,7 @@ import { promises as fs } from "node:fs";
|
|
|
2
2
|
import { dirname, join, resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { createInterface } from "node:readline";
|
|
5
|
+
import { detectProject } from "./detect.js";
|
|
5
6
|
/** Directory containing the project templates shipped with the package. */
|
|
6
7
|
export const TEMPLATES_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "templates");
|
|
7
8
|
/** List all template files relative to TEMPLATES_DIR. */
|
|
@@ -149,7 +150,138 @@ export async function statusTemplates(targetDir) {
|
|
|
149
150
|
}
|
|
150
151
|
return status;
|
|
151
152
|
}
|
|
153
|
+
const DEFAULT_PROTECT = [".env*", "**/.env*", "**/node_modules/**", "**/vendor/**"];
|
|
154
|
+
function buildDefaultConfigYaml(language, commands, hasPreCommitConfig) {
|
|
155
|
+
const lines = ["# opencode-dev-framework configuration"];
|
|
156
|
+
lines.push("# Edit the commands to match your project's toolchain.");
|
|
157
|
+
lines.push("");
|
|
158
|
+
lines.push("profile: standard");
|
|
159
|
+
if (language !== null) {
|
|
160
|
+
lines.push(`# Detected language: ${language}`);
|
|
161
|
+
}
|
|
162
|
+
if (Object.keys(commands).length > 0) {
|
|
163
|
+
lines.push("commands:");
|
|
164
|
+
if (commands.test !== undefined)
|
|
165
|
+
lines.push(` test: ${commands.test}`);
|
|
166
|
+
if (commands.typecheck !== undefined)
|
|
167
|
+
lines.push(` typecheck: ${commands.typecheck}`);
|
|
168
|
+
if (commands.format !== undefined) {
|
|
169
|
+
if (typeof commands.format === "string") {
|
|
170
|
+
lines.push(` format: ${commands.format}`);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
lines.push(" format:");
|
|
174
|
+
for (const [ext, cmd] of Object.entries(commands.format)) {
|
|
175
|
+
lines.push(` ${ext}: ${cmd}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (commands.lint !== undefined) {
|
|
180
|
+
if (typeof commands.lint === "string") {
|
|
181
|
+
lines.push(` lint: ${commands.lint}`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
lines.push(" lint:");
|
|
185
|
+
for (const [ext, cmd] of Object.entries(commands.lint)) {
|
|
186
|
+
lines.push(` ${ext}: ${cmd}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (commands.test_changed !== undefined)
|
|
191
|
+
lines.push(` test_changed: ${commands.test_changed}`);
|
|
192
|
+
}
|
|
193
|
+
if (hasPreCommitConfig) {
|
|
194
|
+
lines.push("");
|
|
195
|
+
lines.push("# Pre-commit config detected; use it for per-file linting.");
|
|
196
|
+
lines.push("precommit: auto");
|
|
197
|
+
}
|
|
198
|
+
lines.push("");
|
|
199
|
+
lines.push("protect:");
|
|
200
|
+
for (const pattern of DEFAULT_PROTECT) {
|
|
201
|
+
lines.push(` - "${pattern}"`);
|
|
202
|
+
}
|
|
203
|
+
lines.push("");
|
|
204
|
+
lines.push("exclude: []");
|
|
205
|
+
lines.push("");
|
|
206
|
+
lines.push("gate:");
|
|
207
|
+
lines.push(" run_typecheck: true");
|
|
208
|
+
lines.push(" run_tests: true");
|
|
209
|
+
lines.push(" block_on_failure: true");
|
|
210
|
+
lines.push(" skip_unchanged: true");
|
|
211
|
+
lines.push(" scope: all");
|
|
212
|
+
lines.push(" lint_changed: false");
|
|
213
|
+
lines.push(" timeout: 300");
|
|
214
|
+
lines.push(" max_blocks: 3");
|
|
215
|
+
lines.push("");
|
|
216
|
+
lines.push("on_edit:");
|
|
217
|
+
lines.push(" format: true");
|
|
218
|
+
lines.push(" lint: true");
|
|
219
|
+
return lines.join("\n");
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Write a project-level `.opencode-dev-framework.yml` based on detected
|
|
223
|
+
* tooling. Respects the same overwrite/skip flags and prompt as
|
|
224
|
+
* `installTemplates`.
|
|
225
|
+
*/
|
|
226
|
+
export async function writeDetectedConfig(targetDir, options = {}) {
|
|
227
|
+
const { language, commands, hasPreCommitConfig } = detectProject(targetDir);
|
|
228
|
+
const content = buildDefaultConfigYaml(language, commands, hasPreCommitConfig);
|
|
229
|
+
const destination = join(targetDir, ".opencode-dev-framework.yml");
|
|
230
|
+
const relativePath = ".opencode-dev-framework.yml";
|
|
231
|
+
const exists = await fileExists(destination);
|
|
232
|
+
if (!exists) {
|
|
233
|
+
await fs.writeFile(destination, content, "utf8");
|
|
234
|
+
return { action: "created" };
|
|
235
|
+
}
|
|
236
|
+
const existing = await readFileSafe(destination);
|
|
237
|
+
if (existing === content) {
|
|
238
|
+
return { action: "skipped" };
|
|
239
|
+
}
|
|
240
|
+
const prompt = options.prompt ?? defaultPrompt;
|
|
241
|
+
let decision;
|
|
242
|
+
if (options.skipExisting) {
|
|
243
|
+
decision = "skip";
|
|
244
|
+
}
|
|
245
|
+
else if (options.overwriteExisting) {
|
|
246
|
+
decision = "overwrite";
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
decision = await prompt(relativePath);
|
|
250
|
+
}
|
|
251
|
+
if (decision === "overwrite-all") {
|
|
252
|
+
decision = "overwrite";
|
|
253
|
+
}
|
|
254
|
+
else if (decision === "skip-all") {
|
|
255
|
+
decision = "skip";
|
|
256
|
+
}
|
|
257
|
+
if (decision === "overwrite") {
|
|
258
|
+
await fs.writeFile(destination, content, "utf8");
|
|
259
|
+
return { action: "overwritten" };
|
|
260
|
+
}
|
|
261
|
+
return { action: "skipped" };
|
|
262
|
+
}
|
|
152
263
|
/** Resolve a user-supplied directory (default cwd) to an absolute path. */
|
|
153
264
|
export function resolveTargetDir(dir) {
|
|
154
265
|
return resolve(dir ?? process.cwd());
|
|
155
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Set the top-level `profile:` key in a config file. Edits the raw text
|
|
269
|
+
* instead of re-serializing the YAML so user comments and formatting survive.
|
|
270
|
+
*/
|
|
271
|
+
export async function setProfileInFile(configPath, profile) {
|
|
272
|
+
let content = "";
|
|
273
|
+
try {
|
|
274
|
+
content = await fs.readFile(configPath, "utf8");
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
// File does not exist yet; it will be created below.
|
|
278
|
+
}
|
|
279
|
+
const line = `profile: ${profile}`;
|
|
280
|
+
if (/^profile\s*:/m.test(content)) {
|
|
281
|
+
content = content.replace(/^profile\s*:.*$/m, line);
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
content = content.trim() === "" ? `${line}\n` : `${content.trimEnd()}\n${line}\n`;
|
|
285
|
+
}
|
|
286
|
+
await fs.writeFile(configPath, content, "utf8");
|
|
287
|
+
}
|
package/dist/lint.d.ts
CHANGED
|
@@ -30,6 +30,11 @@ export declare function matchExclude(config: ResolvedConfig, filePath: string, d
|
|
|
30
30
|
* command is configured at all.
|
|
31
31
|
*/
|
|
32
32
|
export declare function resolveLintCommand(config: ResolvedConfig, filePath: string): string[] | undefined;
|
|
33
|
+
export declare function resolvePreCommitCommand(filePath: string): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Check whether the `pre-commit` binary is available in the project.
|
|
36
|
+
*/
|
|
37
|
+
export declare function detectPreCommitAvailability(run: RunCommand, directory?: string): Promise<boolean>;
|
|
33
38
|
/**
|
|
34
39
|
* Lint a single file. Excluded files and file types without a configured
|
|
35
40
|
* linter are skipped; everything else runs through the injected runner.
|
|
@@ -37,6 +42,7 @@ export declare function resolveLintCommand(config: ResolvedConfig, filePath: str
|
|
|
37
42
|
export declare function lintFile(run: RunCommand, config: ResolvedConfig, filePath: string, options?: {
|
|
38
43
|
cwd?: string;
|
|
39
44
|
timeout?: number;
|
|
45
|
+
precommitAvailable?: boolean;
|
|
40
46
|
}): Promise<LintOutcome>;
|
|
41
47
|
/** True when a lint outcome represents a failure (non-zero exit or timeout). */
|
|
42
48
|
export declare function isLintFailure(outcome: LintOutcome): boolean;
|
package/dist/lint.js
CHANGED
|
@@ -45,6 +45,16 @@ export function resolveLintCommand(config, filePath) {
|
|
|
45
45
|
}
|
|
46
46
|
return splitCommand(template).map((token) => token.replaceAll("{file}", filePath));
|
|
47
47
|
}
|
|
48
|
+
export function resolvePreCommitCommand(filePath) {
|
|
49
|
+
return ["pre-commit", "run", "--files", filePath];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check whether the `pre-commit` binary is available in the project.
|
|
53
|
+
*/
|
|
54
|
+
export async function detectPreCommitAvailability(run, directory) {
|
|
55
|
+
const result = await run(["pre-commit", "--version"], { cwd: directory, timeout: 10 });
|
|
56
|
+
return result.exitCode === 0;
|
|
57
|
+
}
|
|
48
58
|
/**
|
|
49
59
|
* Lint a single file. Excluded files and file types without a configured
|
|
50
60
|
* linter are skipped; everything else runs through the injected runner.
|
|
@@ -54,7 +64,10 @@ export async function lintFile(run, config, filePath, options) {
|
|
|
54
64
|
if (excluded !== undefined) {
|
|
55
65
|
return { skipped: true, reason: `excluded by "${excluded}"`, filePath };
|
|
56
66
|
}
|
|
57
|
-
const
|
|
67
|
+
const usePreCommit = config.precommit === "auto" && options?.precommitAvailable === true;
|
|
68
|
+
const command = usePreCommit
|
|
69
|
+
? resolvePreCommitCommand(filePath)
|
|
70
|
+
: resolveLintCommand(config, filePath);
|
|
58
71
|
if (command === undefined) {
|
|
59
72
|
return { skipped: true, reason: "no lint command configured for this file type", filePath };
|
|
60
73
|
}
|
package/dist/registry.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export interface HookState {
|
|
|
13
13
|
constitution: string | null;
|
|
14
14
|
/** Per-session count of completion-gate blocks (keyed by sessionID). */
|
|
15
15
|
blockCounts: Map<string, number>;
|
|
16
|
+
/** Cached result of checking whether `pre-commit` is available. */
|
|
17
|
+
precommitAvailable?: boolean;
|
|
16
18
|
}
|
|
17
19
|
export declare function setHookState(directory: string, state: HookState): void;
|
|
18
20
|
export declare function getHookState(directoryHint?: string): HookState | null;
|
package/dist/rules.d.ts
CHANGED
|
@@ -2,32 +2,39 @@ import type { ResolvedConfig } from "./types.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* Directory containing the constitution bundled with the plugin package.
|
|
4
4
|
* Built code lives in `dist/`, so `../rules` resolves to the package root at
|
|
5
|
-
* runtime; during tests (ts source) it resolves the same way relative to
|
|
6
|
-
* `src/`.
|
|
5
|
+
* runtime; during tests (ts source) it resolves the same way relative to `src/`.
|
|
7
6
|
*/
|
|
8
7
|
export declare const BUNDLED_CONSTITUTION_DIR: string;
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export declare const BUNDLED_CONSTITUTION_PATH: string;
|
|
8
|
+
/** Namespaced directory inside the project for local rule overrides. */
|
|
9
|
+
export declare const LOCAL_RULES_DIR = ".opencode/opencode-dev-framework/rules";
|
|
10
|
+
/** Default style-guide filenames the plugin auto-discovers. */
|
|
11
|
+
export declare const STYLE_GUIDE_CANDIDATES: string[];
|
|
14
12
|
export interface ConstitutionResult {
|
|
15
13
|
/** Constitution text to inject, or null when nothing should be injected. */
|
|
16
14
|
constitution: string | null;
|
|
17
15
|
source: "custom" | "bundled" | null;
|
|
18
|
-
/** Human-readable warning when a configured
|
|
16
|
+
/** Human-readable warning when a configured rule file could not be read. */
|
|
19
17
|
warning?: string;
|
|
20
18
|
}
|
|
21
19
|
/**
|
|
22
20
|
* Loads the constitution to inject for a session.
|
|
23
21
|
*
|
|
24
22
|
* - `off` profile never injects anything.
|
|
25
|
-
* -
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* -
|
|
30
|
-
*
|
|
23
|
+
* - Explicit `rules` config wins:
|
|
24
|
+
* - `rules` as an array (or `mode: replace`) loads only those rule files.
|
|
25
|
+
* - `rules: { mode: append, files: [...] }` loads the bundled/local rules
|
|
26
|
+
* first, then the listed files.
|
|
27
|
+
* - If no explicit `rules` config is set, the plugin auto-discovers
|
|
28
|
+
* `.opencode/opencode-dev-framework/rules/*.md` in the project root. If that
|
|
29
|
+
* directory exists and contains markdown files, it replaces the bundled
|
|
30
|
+
* rules.
|
|
31
|
+
* - If no local override exists, the bundled `rules/*.md` files are used.
|
|
32
|
+
* - Missing rule files produce warnings but do not stop other files from
|
|
33
|
+
* loading.
|
|
34
|
+
* - `style_guide` config always wins. When it is not set, the plugin
|
|
35
|
+
* auto-discovers `STYLE.md`, `CONTRIBUTING.md`, `docs/STYLE.md`, or
|
|
36
|
+
* `docs/CONTRIBUTING.md` in the project root and appends it under a
|
|
37
|
+
* "Style Guide" heading.
|
|
31
38
|
*/
|
|
32
39
|
export declare function loadConstitution(config: ResolvedConfig, directory?: string): Promise<ConstitutionResult>;
|
|
33
40
|
/**
|
package/dist/rules.js
CHANGED
|
@@ -4,16 +4,19 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
/**
|
|
5
5
|
* Directory containing the constitution bundled with the plugin package.
|
|
6
6
|
* Built code lives in `dist/`, so `../rules` resolves to the package root at
|
|
7
|
-
* runtime; during tests (ts source) it resolves the same way relative to
|
|
8
|
-
* `src/`.
|
|
7
|
+
* runtime; during tests (ts source) it resolves the same way relative to `src/`.
|
|
9
8
|
*/
|
|
10
9
|
export const BUNDLED_CONSTITUTION_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "rules");
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
/** Namespaced directory inside the project for local rule overrides. */
|
|
11
|
+
export const LOCAL_RULES_DIR = ".opencode/opencode-dev-framework/rules";
|
|
12
|
+
/** Default style-guide filenames the plugin auto-discovers. */
|
|
13
|
+
export const STYLE_GUIDE_CANDIDATES = [
|
|
14
|
+
"STYLE.md",
|
|
15
|
+
"docs/STYLE.md",
|
|
16
|
+
"CONTRIBUTING.md",
|
|
17
|
+
"docs/CONTRIBUTING.md",
|
|
18
|
+
];
|
|
19
|
+
async function readRuleFile(path) {
|
|
17
20
|
try {
|
|
18
21
|
const content = await readFile(path, "utf8");
|
|
19
22
|
return content.trim() === "" ? null : content;
|
|
@@ -27,7 +30,7 @@ async function readConstitution(path) {
|
|
|
27
30
|
* separated by blank lines. Returns null when the directory is missing or
|
|
28
31
|
* contains no non-empty markdown files.
|
|
29
32
|
*/
|
|
30
|
-
async function
|
|
33
|
+
async function readRuleDir(dir) {
|
|
31
34
|
try {
|
|
32
35
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
33
36
|
const files = entries
|
|
@@ -50,51 +53,120 @@ async function readConstitutionDir(dir) {
|
|
|
50
53
|
return null;
|
|
51
54
|
}
|
|
52
55
|
}
|
|
56
|
+
async function readRulesFiles(paths, directory) {
|
|
57
|
+
const parts = [];
|
|
58
|
+
const warnings = [];
|
|
59
|
+
for (const rawPath of paths) {
|
|
60
|
+
const path = isAbsolute(rawPath) ? rawPath : resolve(directory, rawPath);
|
|
61
|
+
const content = await readRuleFile(path);
|
|
62
|
+
if (content !== null) {
|
|
63
|
+
parts.push(content);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
warnings.push(`Could not read rule file ${path}; skipping.`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { content: parts.length === 0 ? null : parts.join("\n\n"), warnings };
|
|
70
|
+
}
|
|
71
|
+
async function discoverStyleGuide(directory) {
|
|
72
|
+
for (const candidate of STYLE_GUIDE_CANDIDATES) {
|
|
73
|
+
const content = await readRuleFile(resolve(directory, candidate));
|
|
74
|
+
if (content !== null) {
|
|
75
|
+
return content;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
53
80
|
/**
|
|
54
81
|
* Loads the constitution to inject for a session.
|
|
55
82
|
*
|
|
56
83
|
* - `off` profile never injects anything.
|
|
57
|
-
* -
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* -
|
|
62
|
-
*
|
|
84
|
+
* - Explicit `rules` config wins:
|
|
85
|
+
* - `rules` as an array (or `mode: replace`) loads only those rule files.
|
|
86
|
+
* - `rules: { mode: append, files: [...] }` loads the bundled/local rules
|
|
87
|
+
* first, then the listed files.
|
|
88
|
+
* - If no explicit `rules` config is set, the plugin auto-discovers
|
|
89
|
+
* `.opencode/opencode-dev-framework/rules/*.md` in the project root. If that
|
|
90
|
+
* directory exists and contains markdown files, it replaces the bundled
|
|
91
|
+
* rules.
|
|
92
|
+
* - If no local override exists, the bundled `rules/*.md` files are used.
|
|
93
|
+
* - Missing rule files produce warnings but do not stop other files from
|
|
94
|
+
* loading.
|
|
95
|
+
* - `style_guide` config always wins. When it is not set, the plugin
|
|
96
|
+
* auto-discovers `STYLE.md`, `CONTRIBUTING.md`, `docs/STYLE.md`, or
|
|
97
|
+
* `docs/CONTRIBUTING.md` in the project root and appends it under a
|
|
98
|
+
* "Style Guide" heading.
|
|
63
99
|
*/
|
|
64
100
|
export async function loadConstitution(config, directory) {
|
|
65
101
|
if (config.profile === "off") {
|
|
66
102
|
return { constitution: null, source: null };
|
|
67
103
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
104
|
+
const cwd = directory ?? process.cwd();
|
|
105
|
+
const warnings = [];
|
|
106
|
+
const parts = [];
|
|
107
|
+
let source = null;
|
|
108
|
+
if (config.rules && config.rules.files.length > 0) {
|
|
109
|
+
const rulesResult = await readRulesFiles(config.rules.files, cwd);
|
|
110
|
+
if (rulesResult.content !== null) {
|
|
111
|
+
parts.push(rulesResult.content);
|
|
112
|
+
source = "custom";
|
|
75
113
|
}
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
114
|
+
warnings.push(...rulesResult.warnings);
|
|
115
|
+
if (config.rules.mode === "append") {
|
|
116
|
+
const local = await readRuleDir(resolve(cwd, LOCAL_RULES_DIR));
|
|
117
|
+
const bundled = await readRuleDir(BUNDLED_CONSTITUTION_DIR);
|
|
118
|
+
const base = local ?? bundled;
|
|
119
|
+
if (base !== null) {
|
|
120
|
+
parts.unshift(base);
|
|
121
|
+
source = local !== null ? "custom" : "bundled";
|
|
122
|
+
}
|
|
83
123
|
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const local = await readRuleDir(resolve(cwd, LOCAL_RULES_DIR));
|
|
127
|
+
if (local !== null) {
|
|
128
|
+
parts.push(local);
|
|
129
|
+
source = "custom";
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const bundled = await readRuleDir(BUNDLED_CONSTITUTION_DIR);
|
|
133
|
+
if (bundled !== null) {
|
|
134
|
+
parts.push(bundled);
|
|
135
|
+
source = "bundled";
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (config.style_guide) {
|
|
140
|
+
const stylePath = isAbsolute(config.style_guide)
|
|
141
|
+
? config.style_guide
|
|
142
|
+
: resolve(cwd, config.style_guide);
|
|
143
|
+
const style = await readRuleFile(stylePath);
|
|
144
|
+
if (style !== null) {
|
|
145
|
+
parts.push(`# Style Guide\n\n${style}`);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
warnings.push(`Could not read style guide at ${stylePath}; skipping.`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const discovered = await discoverStyleGuide(cwd);
|
|
153
|
+
if (discovered !== null) {
|
|
154
|
+
parts.push(`# Style Guide\n\n${discovered}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (parts.length === 0) {
|
|
84
158
|
return {
|
|
85
159
|
constitution: null,
|
|
86
160
|
source: null,
|
|
87
|
-
warning:
|
|
161
|
+
warning: warnings.length > 0
|
|
162
|
+
? warnings.join("\n")
|
|
163
|
+
: "Could not read the bundled constitution; no constitution injected.",
|
|
88
164
|
};
|
|
89
165
|
}
|
|
90
|
-
const bundled = await readConstitutionDir(BUNDLED_CONSTITUTION_DIR);
|
|
91
|
-
if (bundled !== null) {
|
|
92
|
-
return { constitution: bundled, source: "bundled" };
|
|
93
|
-
}
|
|
94
166
|
return {
|
|
95
|
-
constitution:
|
|
96
|
-
source
|
|
97
|
-
warning:
|
|
167
|
+
constitution: parts.join("\n\n"),
|
|
168
|
+
source,
|
|
169
|
+
warning: warnings.length > 0 ? warnings.join("\n") : undefined,
|
|
98
170
|
};
|
|
99
171
|
}
|
|
100
172
|
/**
|
package/dist/tools.js
CHANGED
|
@@ -1,35 +1,13 @@
|
|
|
1
|
-
import { readFile, writeFile } from "node:fs/promises";
|
|
2
1
|
import { join } from "node:path";
|
|
3
2
|
import { tool } from "@opencode-ai/plugin";
|
|
4
3
|
import { loadConfig, clearConfigCache } from "./config.js";
|
|
5
|
-
import { installTemplates } from "./installer.js";
|
|
4
|
+
import { installTemplates, setProfileInFile, writeDetectedConfig } from "./installer.js";
|
|
6
5
|
import { getHookState, updateHookState } from "./registry.js";
|
|
7
6
|
import { loadConstitution } from "./rules.js";
|
|
8
7
|
const PROFILES = ["off", "advisory", "standard", "strict"];
|
|
9
8
|
function isProfile(value) {
|
|
10
9
|
return PROFILES.includes(value);
|
|
11
10
|
}
|
|
12
|
-
/**
|
|
13
|
-
* Set the top-level `profile:` key in the config file. Edits the raw text
|
|
14
|
-
* instead of re-serializing the YAML so user comments and formatting survive.
|
|
15
|
-
*/
|
|
16
|
-
async function setProfileInFile(configPath, profile) {
|
|
17
|
-
let content = "";
|
|
18
|
-
try {
|
|
19
|
-
content = await readFile(configPath, "utf8");
|
|
20
|
-
}
|
|
21
|
-
catch {
|
|
22
|
-
// File does not exist yet; it will be created below.
|
|
23
|
-
}
|
|
24
|
-
const line = `profile: ${profile}`;
|
|
25
|
-
if (/^profile\s*:/m.test(content)) {
|
|
26
|
-
content = content.replace(/^profile\s*:.*$/m, line);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
content = content.trim() === "" ? `${line}\n` : `${content.trimEnd()}\n${line}\n`;
|
|
30
|
-
}
|
|
31
|
-
await writeFile(configPath, content, "utf8");
|
|
32
|
-
}
|
|
33
11
|
export function buildTools(_ctx) {
|
|
34
12
|
return {
|
|
35
13
|
dev_framework_init: tool({
|
|
@@ -50,11 +28,16 @@ export function buildTools(_ctx) {
|
|
|
50
28
|
overwriteExisting: args.overwrite ?? false,
|
|
51
29
|
skipExisting: !(args.overwrite ?? false),
|
|
52
30
|
});
|
|
31
|
+
const configResult = await writeDetectedConfig(targetDir, {
|
|
32
|
+
overwriteExisting: args.overwrite ?? false,
|
|
33
|
+
skipExisting: !(args.overwrite ?? false),
|
|
34
|
+
});
|
|
53
35
|
const lines = [
|
|
54
36
|
`Installed opencode-dev-framework templates into ${targetDir}.`,
|
|
55
37
|
`Created: ${result.created.length} file(s)`,
|
|
56
38
|
`Overwritten: ${result.overwritten.length} file(s)`,
|
|
57
39
|
`Skipped: ${result.skipped.length} file(s)`,
|
|
40
|
+
`Config: ${configResult.action}`,
|
|
58
41
|
];
|
|
59
42
|
if (result.created.length > 0) {
|
|
60
43
|
lines.push("", "Created files:", ...result.created.map((file) => `- ${file}`));
|
package/dist/types.d.ts
CHANGED
|
@@ -38,6 +38,12 @@ export interface OnEditConfig {
|
|
|
38
38
|
format: boolean;
|
|
39
39
|
lint: boolean;
|
|
40
40
|
}
|
|
41
|
+
export interface RulesConfig {
|
|
42
|
+
/** Default is `replace` for the bundled constitution. */
|
|
43
|
+
mode: "replace" | "append";
|
|
44
|
+
/** Paths to Markdown rule files (absolute or relative to project root). */
|
|
45
|
+
files: string[];
|
|
46
|
+
}
|
|
41
47
|
/**
|
|
42
48
|
* Raw config as parsed from a config file. Every field is optional; defaults
|
|
43
49
|
* are applied during resolution (see resolveConfig in config.ts).
|
|
@@ -51,10 +57,11 @@ export interface Config {
|
|
|
51
57
|
gate?: Partial<GateConfig>;
|
|
52
58
|
on_edit?: Partial<OnEditConfig>;
|
|
53
59
|
exclude?: string[];
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
rules?: string[];
|
|
60
|
+
rules?: string[] | RulesConfig;
|
|
61
|
+
/** Path to a project style guide (absolute or relative to project root). */
|
|
57
62
|
style_guide?: string;
|
|
63
|
+
/** Use pre-commit for per-file linting when available (`auto`) or never (`off`). */
|
|
64
|
+
precommit?: "auto" | "off";
|
|
58
65
|
}
|
|
59
66
|
/**
|
|
60
67
|
* Fully resolved config with profile defaults applied. All consumers of the
|
|
@@ -71,7 +78,7 @@ export interface ResolvedConfig {
|
|
|
71
78
|
gate: GateConfig;
|
|
72
79
|
on_edit: OnEditConfig;
|
|
73
80
|
exclude: string[];
|
|
74
|
-
|
|
75
|
-
rules?: string[];
|
|
81
|
+
rules?: RulesConfig;
|
|
76
82
|
style_guide?: string;
|
|
83
|
+
precommit: "auto" | "off";
|
|
77
84
|
}
|
package/package.json
CHANGED
|
File without changes
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# Default opencode-dev-framework configuration.
|
|
2
|
-
# Edit the commands to match your project's toolchain.
|
|
3
|
-
|
|
4
|
-
profile: standard
|
|
5
|
-
|
|
6
|
-
commands:
|
|
7
|
-
typecheck: ""
|
|
8
|
-
test: ""
|
|
9
|
-
test_changed: ""
|
|
10
|
-
lint:
|
|
11
|
-
default: ""
|
|
12
|
-
format:
|
|
13
|
-
default: ""
|
|
14
|
-
|
|
15
|
-
protect:
|
|
16
|
-
- ".env*"
|
|
17
|
-
- "**/.env*"
|
|
18
|
-
- "**/node_modules/**"
|
|
19
|
-
- "**/vendor/**"
|
|
20
|
-
|
|
21
|
-
exclude: []
|
|
22
|
-
|
|
23
|
-
gate:
|
|
24
|
-
run_typecheck: true
|
|
25
|
-
run_tests: true
|
|
26
|
-
block_on_failure: true
|
|
27
|
-
skip_unchanged: true
|
|
28
|
-
scope: all
|
|
29
|
-
lint_changed: false
|
|
30
|
-
timeout: 300
|
|
31
|
-
max_blocks: 3
|
|
32
|
-
|
|
33
|
-
on_edit:
|
|
34
|
-
format: true
|
|
35
|
-
lint: true
|