fix: pnpm setup should print a summary

This commit is contained in:
Zoltan Kochan
2021-06-06 01:27:03 +03:00
parent 17fc653268
commit 6a64c1ff58
3 changed files with 25 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-setup": patch
---
A summary should be printed.

View File

@@ -47,22 +47,26 @@ export async function handler (
async function setupShell (configFile: string, pnpmHomeDir: string) {
if (!fs.existsSync(configFile)) return `Could not setup pnpm. No ${configFile} found`
const configContent = await fs.promises.readFile(configFile, 'utf8')
if (configContent.includes('PNPM_HOME')) return ''
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')
return ''
return `Updated ${configFile}`
}
async function setupFishShell (pnpmHomeDir: string) {
const configFile = path.join(os.homedir(), '.config/fish/config.fish')
if (!fs.existsSync(configFile)) return `Could not setup pnpm. No ${configFile} found`
const configContent = await fs.promises.readFile(configFile, 'utf8')
if (configContent.includes('PNPM_HOME')) return ''
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')
return ''
return `Updated ${configFile}`
}

View File

@@ -17,9 +17,10 @@ test('PNPM_HOME is added to ~/.bashrc', async () => {
tempDir()
fs.writeFileSync('.bashrc', '', 'utf8')
homedir['mockReturnValue'](process.cwd())
await setup.handler({
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
expect(output).toMatch(/^Updated /)
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
expect(bashRCContent).toEqual(`
export PNPM_HOME="${__dirname}"
@@ -35,9 +36,10 @@ export PNPM_HOME="pnpm_home"
export PATH="$PNPM_HOME:$PATH"
`, 'utf8')
homedir['mockReturnValue'](process.cwd())
await setup.handler({
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
expect(output).toMatch(/^PNPM_HOME is already in /)
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
expect(bashRCContent).toEqual(`
export PNPM_HOME="pnpm_home"
@@ -50,9 +52,10 @@ test('PNPM_HOME is added to ~/.zshrc', async () => {
tempDir()
fs.writeFileSync('.zshrc', '', 'utf8')
homedir['mockReturnValue'](process.cwd())
await setup.handler({
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
expect(output).toMatch(/^Updated /)
const bashRCContent = fs.readFileSync('.zshrc', 'utf8')
expect(bashRCContent).toEqual(`
export PNPM_HOME="${__dirname}"
@@ -68,9 +71,10 @@ export PNPM_HOME="pnpm_home"
export PATH="$PNPM_HOME:$PATH"
`, 'utf8')
homedir['mockReturnValue'](process.cwd())
await setup.handler({
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
expect(output).toMatch(/^PNPM_HOME is already in /)
const bashRCContent = fs.readFileSync('.zshrc', 'utf8')
expect(bashRCContent).toEqual(`
export PNPM_HOME="pnpm_home"
@@ -84,9 +88,10 @@ test('PNPM_HOME is added to ~/.config/fish/config.fish', async () => {
fs.mkdirSync('.config/fish', { recursive: true })
fs.writeFileSync('.config/fish/config.fish', '', 'utf8')
homedir['mockReturnValue'](process.cwd())
await setup.handler({
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
expect(output).toMatch(/^Updated /)
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
expect(bashRCContent).toEqual(`
set -gx PNPM_HOME "${__dirname}"
@@ -103,9 +108,10 @@ set -gx PNPM_HOME "pnpm_home"
set -gx PATH "$PNPM_HOME" $PATH
`, 'utf8')
homedir['mockReturnValue'](process.cwd())
await setup.handler({
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
expect(output).toMatch(/^PNPM_HOME is already in /)
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
expect(bashRCContent).toEqual(`
set -gx PNPM_HOME "pnpm_home"