Skip to content

Commit 5d8e3c6

Browse files
christsoclaude
andauthored
fix(workspace): preserve existing CLAUDE.md and AGENTS.md on init (#63)
* fix(workspace): preserve existing CLAUDE.md and AGENTS.md on init When running `workspace init`, skip copying agent files from the template source if they already exist in the target directory. Existing files still receive WORKSPACE-RULES injection (append) without losing user content. Closes #62 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(test): run non-workspace e2e tests from temp cwd Tests asserting "non-workspace dir" behavior were inheriting the test runner's cwd which may contain .allagents/workspace.yaml, causing the "No plugins configured" path to be skipped. Set cwd to the temp directory so tests are isolated from the project's own workspace config. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(e2e): verify workspace init preserves existing CLAUDE.md and AGENTS.md Runs the built CLI binary with `workspace init --from` against a directory that already has CLAUDE.md/AGENTS.md and asserts the original content is preserved while WORKSPACE-RULES are appended. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * revert: remove redundant e2e test for workspace init The unit test in workspace-init-preserve.test.ts already calls initWorkspace() directly and covers the same code path without requiring a pre-built binary. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore(test): remove e2e tests that require pre-built binary These tests spawned `node dist/index.js` and required a build step before running. They were also brittle (cwd leaked project state into "non-workspace" assertions). Remove them; the enriched-help and metadata tests in tests/unit/ and tests/e2e/cli-enriched-help.test.ts cover the same logic by importing source directly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(unit): add unit tests for agent-help and json-output modules Replace removed e2e binary-spawning tests with unit tests that import source directly: - agent-help: flag extraction, command metadata completeness, options/positionals - json-output: flag extraction and position flexibility The --help output tests are not replaced since they tested cmd-ts framework behavior, not our code. cli-enriched-help.test.ts already covers our metadata structure. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 0e7c62b commit 5d8e3c6

7 files changed

Lines changed: 238 additions & 568 deletions

File tree

src/core/workspace.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ export async function initWorkspace(
182182
if (parsedUrl) {
183183
const basePath = parsedUrl.subpath || '';
184184
for (const agentFile of AGENT_FILES) {
185+
const targetFilePath = join(absoluteTarget, agentFile);
186+
// Skip if file already exists in target - don't overwrite user content
187+
if (existsSync(targetFilePath)) {
188+
copiedAgentFiles.push(agentFile);
189+
continue;
190+
}
185191
const filePath = basePath ? `${basePath}/${agentFile}` : agentFile;
186192
const content = await fetchFileFromGitHub(
187193
parsedUrl.owner,
@@ -190,7 +196,7 @@ export async function initWorkspace(
190196
parsedUrl.branch,
191197
);
192198
if (content) {
193-
await writeFile(join(absoluteTarget, agentFile), content, 'utf-8');
199+
await writeFile(targetFilePath, content, 'utf-8');
194200
copiedAgentFiles.push(agentFile);
195201
}
196202
}
@@ -199,10 +205,16 @@ export async function initWorkspace(
199205
// Copy agent files from local source
200206
const effectiveSourceDir = sourceDir ?? defaultTemplatePath;
201207
for (const agentFile of AGENT_FILES) {
208+
const targetFilePath = join(absoluteTarget, agentFile);
209+
// Skip if file already exists in target - don't overwrite user content
210+
if (existsSync(targetFilePath)) {
211+
copiedAgentFiles.push(agentFile);
212+
continue;
213+
}
202214
const sourcePath = join(effectiveSourceDir, agentFile);
203215
if (existsSync(sourcePath)) {
204216
const content = await readFile(sourcePath, 'utf-8');
205-
await writeFile(join(absoluteTarget, agentFile), content, 'utf-8');
217+
await writeFile(targetFilePath, content, 'utf-8');
206218
copiedAgentFiles.push(agentFile);
207219
}
208220
}

tests/e2e/cli-agent-help.test.ts

Lines changed: 0 additions & 224 deletions
This file was deleted.

0 commit comments

Comments
 (0)