Skip to content

Commit 5a4cd62

Browse files
tablackburnclaude
andauthored
fix(init): restore first-run overwrite of README/CHANGELOG (#17)
The c6a35cf guard against overwriting customized README.md/CHANGELOG.md on re-runs broke the first-run flow. On the un-initialized template, both the template-facing *.md files AND their *.template.md sources are present — file presence alone can't distinguish "first init" from "re-init with template files reappearing." The guard fired on first init, leaving the template-facing README.md/CHANGELOG.md in place AND leaving CHANGELOG.template.md on disk, which then makes downstream CI read is_template=true and skip every job on the user's brand-new module. Use the existing $templateModuleFolder check as the disambiguator: if {{ModuleName}}/ existed at script start, this is a first run and overwrite is correct; if it didn't, the user already initialized once and is re-running, so preserve any customized destination. Restore -Force on Move-Item since the first-run path now legitimately needs to overwrite the template-facing files. Smoke test on a fresh worktree of main with this fix verifies: - README.md / CHANGELOG.md replaced with module-facing content - README.template.md / CHANGELOG.template.md removed from disk - workflow guard sees is_template=false on the resulting module Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent c8f5586 commit 5a4cd62

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

Initialize-Template.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,15 @@ Write-Host ' PowerShell Module Template Setup' -ForegroundColor Cyan
119119
Write-Host '========================================' -ForegroundColor Cyan
120120
Write-Host ''
121121

122-
# Check if template has already been initialized
122+
# Check if template has already been initialized.
123+
# The {{ModuleName}} folder is the most reliable signal: it only exists pre-init.
124+
# Capture the answer here so the README/CHANGELOG move blocks below can distinguish
125+
# a first-run (template-facing README.md present alongside README.template.md;
126+
# overwrite is correct) from a re-run with template files reappearing (user-customized
127+
# README.md present alongside a re-introduced README.template.md; preserve user content).
123128
$templateModuleFolder = Join-Path -Path $PSScriptRoot -ChildPath '{{ModuleName}}'
124-
if (-not (Test-Path -Path $templateModuleFolder)) {
129+
$wasAlreadyInitialized = -not (Test-Path -Path $templateModuleFolder)
130+
if ($wasAlreadyInitialized) {
125131
Write-Warning 'This template appears to have already been initialized.'
126132
Write-Warning 'The {{ModuleName}} folder was not found.'
127133
$continue = Read-Host -Prompt 'Continue anyway? (y/N)'
@@ -282,31 +288,32 @@ if (Test-Path -Path $docsFolder) {
282288

283289
# Replace template-facing README.md with the module-facing README.template.md
284290
# (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.
291+
# On a first init both files exist (template-facing README.md and module-facing
292+
# README.template.md) — overwrite is correct. Only preserve the destination when this
293+
# is a re-run AND the destination is present, since that means the user has customized it.
287294
$readmeTemplate = Join-Path -Path $PSScriptRoot -ChildPath 'README.template.md'
288295
$readmePath = Join-Path -Path $PSScriptRoot -ChildPath 'README.md'
289296
if (Test-Path -Path $readmeTemplate) {
290-
if (Test-Path -Path $readmePath) {
297+
if ($wasAlreadyInitialized -and (Test-Path -Path $readmePath)) {
291298
Write-Warning ' README.md already exists; leaving it in place. Resolve README.template.md manually.'
292299
}
293300
else {
294-
Move-Item -Path $readmeTemplate -Destination $readmePath
301+
Move-Item -Path $readmeTemplate -Destination $readmePath -Force
295302
Write-Host ' Generated module README.md from template' -ForegroundColor Green
296303
}
297304
}
298305

299306
# Replace template-facing CHANGELOG.md with the module-facing CHANGELOG.template.md
300307
# (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.
308+
# Same guard as README above — preserve only on a re-run with destination present.
302309
$changelogTemplate = Join-Path -Path $PSScriptRoot -ChildPath 'CHANGELOG.template.md'
303310
$changelogPath = Join-Path -Path $PSScriptRoot -ChildPath 'CHANGELOG.md'
304311
if (Test-Path -Path $changelogTemplate) {
305-
if (Test-Path -Path $changelogPath) {
312+
if ($wasAlreadyInitialized -and (Test-Path -Path $changelogPath)) {
306313
Write-Warning ' CHANGELOG.md already exists; leaving it in place. Resolve CHANGELOG.template.md manually.'
307314
}
308315
else {
309-
Move-Item -Path $changelogTemplate -Destination $changelogPath
316+
Move-Item -Path $changelogTemplate -Destination $changelogPath -Force
310317
Write-Host ' Generated module CHANGELOG.md from template' -ForegroundColor Green
311318
}
312319
}

0 commit comments

Comments
 (0)