Skip to content
This repository was archived by the owner on Sep 26, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions os/env/path-extender-posix/path-extender-posix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,53 @@ case ":$PATH:" in
esac
# pnpm end`)
})
it('should update first pnpm block and remove duplicate blocks, preserving content between them', async () => {
fs.writeFileSync(configFile, `# user config
# pnpm
export PNPM_HOME="old_home"
case ":$PATH:" in
*":$PNPM_HOME:"*) ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac
# pnpm end
# content between blocks that must not be deleted
# pnpm
export PNPM_HOME="duplicate_block"
# pnpm end
# after blocks`, 'utf8')
const report = await addDirToPosixEnvPath(pnpmHomeDir, {
proxyVarName: 'PNPM_HOME',
overwrite: true,
configSectionName: 'pnpm',
})
expect(report.configFile.changeType).toBe('modified')
const configContent = fs.readFileSync(configFile, 'utf8')
expect(configContent).toContain('# content between blocks that must not be deleted')
expect(configContent).toContain(`export PNPM_HOME="${pnpmHomeDir}"`)
expect(configContent).not.toContain('duplicate_block')
expect(configContent.match(/^# pnpm$/gm) ?? []).toHaveLength(1)
expect(configContent.match(/^# pnpm end$/gm) ?? []).toHaveLength(1)
})
it('should detect settings from the first block only when duplicate blocks exist', async () => {
const firstBlockSettings = `export PNPM_HOME="${pnpmHomeDir}"
case ":$PATH:" in
*":$PNPM_HOME:"*) ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac`
fs.writeFileSync(configFile, `# pnpm
${firstBlockSettings}
# pnpm end
# unrelated content
# pnpm
export PNPM_HOME="duplicate"
# pnpm end`, 'utf8')
const report = await addDirToPosixEnvPath(pnpmHomeDir, {
proxyVarName: 'PNPM_HOME',
configSectionName: 'pnpm',
})
expect(report.configFile.changeType).toBe('skipped')
expect(report.oldSettings).toBe(firstBlockSettings)
})
})

describe('Zsh', () => {
Expand Down
9 changes: 7 additions & 2 deletions os/env/path-extender-posix/path-extender-posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ async function updateShellConfig (
}
}
const configContent = await fs.promises.readFile(configFile, 'utf8')
const match = new RegExp(`# ${opts.configSectionName}\n([\\s\\S]*)\n# ${opts.configSectionName} end`, 'g').exec(configContent)
const match = new RegExp(`# ${opts.configSectionName}\n([\\s\\S]*?)\n# ${opts.configSectionName} end`).exec(configContent)
if (!match) {
await fs.promises.appendFile(configFile, `\n${newContent}\n`, 'utf8')
return {
Expand Down Expand Up @@ -253,5 +253,10 @@ async function updateShellConfig (
}

function replaceSection (originalContent: string, newSection: string, sectionName: string): string {
return originalContent.replace(new RegExp(`# ${sectionName}[\\s\\S]*# ${sectionName} end`, 'g'), newSection)
const pattern = new RegExp(`# ${sectionName}[\\s\\S]*?# ${sectionName} end`, 'g')
let replaced = false
return originalContent.replace(pattern, () => {
if (!replaced) { replaced = true; return newSection }
return ''
})
}