fix(setup): shell rc files should be created (#4028)

close #4027
This commit is contained in:
Zoltan Kochan
2021-11-22 18:07:42 +02:00
committed by GitHub
parent af3fde6cc7
commit b847e03008
3 changed files with 51 additions and 10 deletions

View 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).

View File

@@ -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}`
}

View File

@@ -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()