Skip to content
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
6 changes: 4 additions & 2 deletions src/resources/javascript/pnpm/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ export class Pnpm extends Resource<PnpmConfig> {
await fs.rm(pnpmHome?.trim() || expectedPnpmHome, { recursive: true, force: true });

const shellRc = Utils.getPrimaryShellRc();

await FileUtils.removeBlockFromFile(shellRc, '# pnpm', '# pnpm end')
await FileUtils.removeLineFromStartupFile(/^export PNPM_HOME=.*$/)
await FileUtils.removeLineFromStartupFile('# pnpm')
await FileUtils.removeLineFromStartupFile(`export PNPM_HOME="${expectedPnpmHome}"`)
await FileUtils.removeLineFromStartupFile('# pnpm end')
await FileUtils.removeFromFile(shellRc,
`case ":$PATH:" in
*":$PNPM_HOME/bin:"*) ;;
Expand All @@ -98,7 +101,6 @@ esac`)
*":$PNPM_HOME:"*) ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac`)
await FileUtils.removeLineFromStartupFile('# pnpm end')
} else if (isInstalledByNpm) {
await $.spawn('npm uninstall -g pnpm', { interactive: true });
} else if (isInstalledByHomebrew) {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ ${lines.join('\n')}`)
await fs.writeFile(filePath, newContents, 'utf8');
}

static async removeBlockFromFile(filePath: string, startMarker: string, endMarker: string): Promise<void> {
const file = await fs.readFile(filePath, 'utf8');
const lines = file.split('\n');

const start = lines.findIndex((l) => l.trim() === startMarker);
if (start === -1) {
return;
}

const relativeEnd = lines.slice(start + 1).findIndex((l) => l.trim() === endMarker);
if (relativeEnd === -1) {
return;
}

lines.splice(start, relativeEnd + 2);

await fs.writeFile(filePath, lines.join('\n'), 'utf8');
console.log(`Removed block ${startMarker} to ${endMarker} from ${filePath}`);
}


static async removeLineFromFile(filePath: string, search: RegExp | string): Promise<void> {
const file = await fs.readFile(filePath, 'utf8')
Expand Down
Loading