Skip to content

Commit c6a35cf

Browse files
tablackburnclaude
andcommitted
fix(init): preserve existing README.md / CHANGELOG.md on re-run
The README and CHANGELOG generation steps used Move-Item -Force, which silently overwrites the destination. The top-of-script "already initialized" check is just a warning the user can dismiss, so a re-run after customization (e.g., user re-runs init to redo something, or template files reappear via git checkout) could clobber their work. Guard each move on the destination not existing. If both source and destination are present, warn and leave both in place — manual resolution is safer than picking a side automatically. Addresses CodeRabbit feedback on PR #16. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 997c47f commit c6a35cf

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

Initialize-Template.ps1

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,34 @@ if (Test-Path -Path $docsFolder) {
281281
}
282282

283283
# Replace template-facing README.md with the module-facing README.template.md
284-
# (placeholders inside README.template.md were already substituted by the file-processing loop above)
284+
# (placeholders inside README.template.md were already substituted by the file-processing loop above).
285+
# If the destination already exists (e.g., the user is re-running init after customizing it),
286+
# leave both files in place and warn — manually resolving is safer than overwriting customizations.
285287
$readmeTemplate = Join-Path -Path $PSScriptRoot -ChildPath 'README.template.md'
286288
$readmePath = Join-Path -Path $PSScriptRoot -ChildPath 'README.md'
287289
if (Test-Path -Path $readmeTemplate) {
288-
Move-Item -Path $readmeTemplate -Destination $readmePath -Force
289-
Write-Host ' Generated module README.md from template' -ForegroundColor Green
290+
if (Test-Path -Path $readmePath) {
291+
Write-Warning ' README.md already exists; leaving it in place. Resolve README.template.md manually.'
292+
}
293+
else {
294+
Move-Item -Path $readmeTemplate -Destination $readmePath
295+
Write-Host ' Generated module README.md from template' -ForegroundColor Green
296+
}
290297
}
291298

292299
# Replace template-facing CHANGELOG.md with the module-facing CHANGELOG.template.md
293-
# (placeholders inside CHANGELOG.template.md were already substituted by the file-processing loop above)
300+
# (placeholders inside CHANGELOG.template.md were already substituted by the file-processing loop above).
301+
# Same guard as README above — preserve any existing CHANGELOG.md rather than clobber it.
294302
$changelogTemplate = Join-Path -Path $PSScriptRoot -ChildPath 'CHANGELOG.template.md'
295303
$changelogPath = Join-Path -Path $PSScriptRoot -ChildPath 'CHANGELOG.md'
296304
if (Test-Path -Path $changelogTemplate) {
297-
Move-Item -Path $changelogTemplate -Destination $changelogPath -Force
298-
Write-Host ' Generated module CHANGELOG.md from template' -ForegroundColor Green
305+
if (Test-Path -Path $changelogPath) {
306+
Write-Warning ' CHANGELOG.md already exists; leaving it in place. Resolve CHANGELOG.template.md manually.'
307+
}
308+
else {
309+
Move-Item -Path $changelogTemplate -Destination $changelogPath
310+
Write-Host ' Generated module CHANGELOG.md from template' -ForegroundColor Green
311+
}
299312
}
300313

301314
# Initialize Git repository if requested

0 commit comments

Comments
 (0)