mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-05 07:46:10 -04:00
6
.changeset/late-crews-speak.md
Normal file
6
.changeset/late-crews-speak.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@pnpm/plugin-commands-setup": patch
|
||||
"pnpm": patch
|
||||
---
|
||||
|
||||
`pnpm setup` should create shell rc files for pnpm path configuration if no such file exists prior [#4027](https://github.com/pnpm/pnpm/issues/4027).
|
||||
@@ -84,28 +84,34 @@ async function updateShell (currentShell: string | null, pnpmHomeDir: string): P
|
||||
}
|
||||
|
||||
async function setupShell (configFile: string, pnpmHomeDir: string): Promise<string> {
|
||||
if (!fs.existsSync(configFile)) return `Could not setup pnpm. No ${configFile} found`
|
||||
const content = `export PNPM_HOME="${pnpmHomeDir}"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`
|
||||
if (!fs.existsSync(configFile)) {
|
||||
await fs.promises.writeFile(configFile, content, 'utf8')
|
||||
return `Created ${configFile}`
|
||||
}
|
||||
const configContent = await fs.promises.readFile(configFile, 'utf8')
|
||||
if (configContent.includes('PNPM_HOME')) {
|
||||
return `PNPM_HOME is already in ${configFile}`
|
||||
}
|
||||
await fs.promises.writeFile(configFile, `${configContent}
|
||||
export PNPM_HOME="${pnpmHomeDir}"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`, 'utf8')
|
||||
await fs.promises.appendFile(configFile, `\n${content}`, 'utf8')
|
||||
return `Updated ${configFile}`
|
||||
}
|
||||
|
||||
async function setupFishShell (pnpmHomeDir: string): Promise<string> {
|
||||
const configFile = path.join(os.homedir(), '.config/fish/config.fish')
|
||||
if (!fs.existsSync(configFile)) return `Could not setup pnpm. No ${configFile} found`
|
||||
const content = `set -gx PNPM_HOME "${pnpmHomeDir}"
|
||||
set -gx PATH "$PNPM_HOME" $PATH
|
||||
`
|
||||
if (!fs.existsSync(configFile)) {
|
||||
await fs.promises.writeFile(configFile, content, 'utf8')
|
||||
return `Created ${configFile}`
|
||||
}
|
||||
const configContent = await fs.promises.readFile(configFile, 'utf8')
|
||||
if (configContent.includes('PNPM_HOME')) {
|
||||
return `PNPM_HOME is already in ${configFile}`
|
||||
}
|
||||
await fs.promises.writeFile(configFile, `${configContent}
|
||||
set -gx PNPM_HOME "${pnpmHomeDir}"
|
||||
set -gx PATH "$PNPM_HOME" $PATH
|
||||
`, 'utf8')
|
||||
await fs.promises.appendFile(configFile, `\n${content}`, 'utf8')
|
||||
return `Updated ${configFile}`
|
||||
}
|
||||
|
||||
@@ -28,6 +28,20 @@ export PATH="$PNPM_HOME:$PATH"
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is added to ~/.bashrc and .bashrc file created', async () => {
|
||||
process.env.SHELL = '/bin/bash'
|
||||
tempDir()
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
const output = await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
expect(output).toMatch(/^Created /)
|
||||
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
|
||||
expect(bashRCContent).toEqual(`export PNPM_HOME="${__dirname}"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is not added to ~/.bashrc if already present', async () => {
|
||||
process.env.SHELL = '/bin/bash'
|
||||
tempDir()
|
||||
@@ -99,6 +113,21 @@ set -gx PATH "$PNPM_HOME" $PATH
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is added to ~/.config/fish/config.fish and config.fish file created', async () => {
|
||||
process.env.SHELL = '/bin/fish'
|
||||
tempDir()
|
||||
fs.mkdirSync('.config/fish', { recursive: true })
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
const output = await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
expect(output).toMatch(/^Created /)
|
||||
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
|
||||
expect(bashRCContent).toEqual(`set -gx PNPM_HOME "${__dirname}"
|
||||
set -gx PATH "$PNPM_HOME" $PATH
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is not added to ~/.config/fish/config.fish if already present', async () => {
|
||||
process.env.SHELL = '/bin/fish'
|
||||
tempDir()
|
||||
|
||||
Reference in New Issue
Block a user