mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-05 07:46:10 -04:00
feat: a new command for setting up pnpm (#3456)
This commit is contained in:
5
.changeset/dirty-gifts-share.md
Normal file
5
.changeset/dirty-gifts-share.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/plugin-commands-setup": minor
|
||||
---
|
||||
|
||||
Project created.
|
||||
15
packages/plugin-commands-setup/README.md
Normal file
15
packages/plugin-commands-setup/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# @pnpm/plugin-commands-setup
|
||||
|
||||
> pnpm commands for setting up pnpm
|
||||
|
||||
[](https://www.npmjs.com/package/@pnpm/plugin-commands-setup)
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
<pnpm|npm|yarn> add @pnpm/plugin-commands-setup
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
4
packages/plugin-commands-setup/jest.config.js
Normal file
4
packages/plugin-commands-setup/jest.config.js
Normal file
@@ -0,0 +1,4 @@
|
||||
const config = require('../../jest.config.js')
|
||||
|
||||
module.exports = config
|
||||
|
||||
39
packages/plugin-commands-setup/package.json
Normal file
39
packages/plugin-commands-setup/package.json
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "@pnpm/plugin-commands-setup",
|
||||
"version": "0.0.0",
|
||||
"description": "pnpm commands for setting up pnpm",
|
||||
"main": "lib/index.js",
|
||||
"typings": "lib/index.d.ts",
|
||||
"files": [
|
||||
"lib",
|
||||
"!*.map"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=12.17"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint -c ../../eslint.json src/**/*.ts test/**/*.ts",
|
||||
"_test": "jest",
|
||||
"test": "pnpm run compile && pnpm run _test",
|
||||
"prepublishOnly": "pnpm run compile",
|
||||
"compile": "rimraf lib tsconfig.tsbuildinfo && tsc --build && pnpm run lint -- --fix"
|
||||
},
|
||||
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/plugin-commands-setup",
|
||||
"keywords": [
|
||||
"pnpm",
|
||||
"setup"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pnpm/pnpm/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/plugin-commands-setup#readme",
|
||||
"dependencies": {
|
||||
"@pnpm/cli-utils": "workspace:0.6.4",
|
||||
"render-help": "^1.0.1"
|
||||
},
|
||||
"funding": "https://opencollective.com/pnpm",
|
||||
"devDependencies": {
|
||||
"@pnpm/prepare": "workspace:0.0.23"
|
||||
}
|
||||
}
|
||||
3
packages/plugin-commands-setup/src/index.ts
Normal file
3
packages/plugin-commands-setup/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import * as setup from './setup'
|
||||
|
||||
export { setup }
|
||||
68
packages/plugin-commands-setup/src/setup.ts
Normal file
68
packages/plugin-commands-setup/src/setup.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import fs from 'fs'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { docsUrl } from '@pnpm/cli-utils'
|
||||
import renderHelp from 'render-help'
|
||||
|
||||
export const rcOptionsTypes = () => ({})
|
||||
|
||||
export const cliOptionsTypes = () => ({})
|
||||
|
||||
export const shorthands = {}
|
||||
|
||||
export const commandNames = ['setup']
|
||||
|
||||
export function help () {
|
||||
return renderHelp({
|
||||
description: 'Sets up pnpm',
|
||||
descriptionLists: [
|
||||
],
|
||||
url: docsUrl('setup'),
|
||||
usages: ['pnpm setup'],
|
||||
})
|
||||
}
|
||||
|
||||
export async function handler (
|
||||
opts: {
|
||||
pnpmHomeDir: string
|
||||
}
|
||||
) {
|
||||
const currentShell = process.env.SHELL ? path.basename(process.env.SHELL) : null
|
||||
switch (currentShell) {
|
||||
case 'bash': {
|
||||
const configFile = path.join(os.homedir(), '.bashrc')
|
||||
return setupShell(configFile, opts.pnpmHomeDir)
|
||||
}
|
||||
case 'zsh': {
|
||||
const configFile = path.join(os.homedir(), '.zshrc')
|
||||
return setupShell(configFile, opts.pnpmHomeDir)
|
||||
}
|
||||
case 'fish': {
|
||||
return setupFishShell(opts.pnpmHomeDir)
|
||||
}
|
||||
}
|
||||
return 'Could not infer shell type.'
|
||||
}
|
||||
|
||||
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 ''
|
||||
await fs.promises.writeFile(configFile, `${configContent}
|
||||
export PNPM_HOME="${pnpmHomeDir}"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`, 'utf8')
|
||||
return ''
|
||||
}
|
||||
|
||||
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 ''
|
||||
await fs.promises.writeFile(configFile, `${configContent}
|
||||
set -gx PNPM_HOME "${pnpmHomeDir}"
|
||||
set -gx PATH "$PNPM_HOME" $PATH
|
||||
`, 'utf8')
|
||||
return ''
|
||||
}
|
||||
114
packages/plugin-commands-setup/test/setup.test.ts
Normal file
114
packages/plugin-commands-setup/test/setup.test.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import fs from 'fs'
|
||||
|
||||
import { homedir } from 'os'
|
||||
import { tempDir } from '@pnpm/prepare'
|
||||
import { setup } from '@pnpm/plugin-commands-setup'
|
||||
|
||||
jest.mock('os', () => {
|
||||
const os = jest.requireActual('os')
|
||||
return {
|
||||
...os,
|
||||
homedir: jest.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
test('PNPM_HOME is added to ~/.bashrc', async () => {
|
||||
process.env.SHELL = '/bin/bash'
|
||||
tempDir()
|
||||
fs.writeFileSync('.bashrc', '', 'utf8')
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
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()
|
||||
fs.writeFileSync('.bashrc', `
|
||||
export PNPM_HOME="pnpm_home"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`, 'utf8')
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
|
||||
expect(bashRCContent).toEqual(`
|
||||
export PNPM_HOME="pnpm_home"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is added to ~/.zshrc', async () => {
|
||||
process.env.SHELL = '/bin/zsh'
|
||||
tempDir()
|
||||
fs.writeFileSync('.zshrc', '', 'utf8')
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
const bashRCContent = fs.readFileSync('.zshrc', 'utf8')
|
||||
expect(bashRCContent).toEqual(`
|
||||
export PNPM_HOME="${__dirname}"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is not added to ~/.zshrc if already present', async () => {
|
||||
process.env.SHELL = '/bin/zsh'
|
||||
tempDir()
|
||||
fs.writeFileSync('.zshrc', `
|
||||
export PNPM_HOME="pnpm_home"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`, 'utf8')
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
const bashRCContent = fs.readFileSync('.zshrc', 'utf8')
|
||||
expect(bashRCContent).toEqual(`
|
||||
export PNPM_HOME="pnpm_home"
|
||||
export PATH="$PNPM_HOME:$PATH"
|
||||
`)
|
||||
})
|
||||
|
||||
test('PNPM_HOME is added to ~/.config/fish/config.fish', async () => {
|
||||
process.env.SHELL = '/bin/fish'
|
||||
tempDir()
|
||||
fs.mkdirSync('.config/fish', { recursive: true })
|
||||
fs.writeFileSync('.config/fish/config.fish', '', 'utf8')
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
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()
|
||||
fs.mkdirSync('.config/fish', { recursive: true })
|
||||
fs.writeFileSync('.config/fish/config.fish', `
|
||||
set -gx PNPM_HOME "pnpm_home"
|
||||
set -gx PATH "$PNPM_HOME" $PATH
|
||||
`, 'utf8')
|
||||
homedir['mockReturnValue'](process.cwd())
|
||||
await setup.handler({
|
||||
pnpmHomeDir: __dirname,
|
||||
})
|
||||
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
|
||||
expect(bashRCContent).toEqual(`
|
||||
set -gx PNPM_HOME "pnpm_home"
|
||||
set -gx PATH "$PNPM_HOME" $PATH
|
||||
`)
|
||||
})
|
||||
16
packages/plugin-commands-setup/tsconfig.json
Normal file
16
packages/plugin-commands-setup/tsconfig.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "@pnpm/tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "lib",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"../../typings/**/*.d.ts"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "../cli-utils"
|
||||
}
|
||||
]
|
||||
}
|
||||
8
packages/plugin-commands-setup/tsconfig.lint.json
Normal file
8
packages/plugin-commands-setup/tsconfig.lint.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"test/**/*.ts",
|
||||
"../../typings/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
@@ -48,6 +48,7 @@
|
||||
"@pnpm/plugin-commands-rebuild": "workspace:4.0.3",
|
||||
"@pnpm/plugin-commands-script-runners": "workspace:3.1.5",
|
||||
"@pnpm/plugin-commands-server": "workspace:3.0.8",
|
||||
"@pnpm/plugin-commands-setup": "workspace:^0.0.0",
|
||||
"@pnpm/plugin-commands-store": "workspace:3.0.8",
|
||||
"@pnpm/prepare": "workspace:0.0.23",
|
||||
"@pnpm/read-package-json": "workspace:5.0.1",
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
test,
|
||||
} from '@pnpm/plugin-commands-script-runners'
|
||||
import { server } from '@pnpm/plugin-commands-server'
|
||||
import { setup } from '@pnpm/plugin-commands-setup'
|
||||
import { store } from '@pnpm/plugin-commands-store'
|
||||
import * as R from 'ramda'
|
||||
import { PnpmOptions } from '../types'
|
||||
@@ -81,6 +82,7 @@ const commands: Array<{
|
||||
root,
|
||||
run,
|
||||
server,
|
||||
setup,
|
||||
store,
|
||||
test,
|
||||
unlink,
|
||||
|
||||
@@ -99,6 +99,9 @@
|
||||
{
|
||||
"path": "../plugin-commands-server"
|
||||
},
|
||||
{
|
||||
"path": "../plugin-commands-setup"
|
||||
},
|
||||
{
|
||||
"path": "../plugin-commands-store"
|
||||
},
|
||||
|
||||
15
pnpm-lock.yaml
generated
15
pnpm-lock.yaml
generated
@@ -2277,6 +2277,19 @@ importers:
|
||||
'@types/ramda': 0.27.39
|
||||
'@types/signal-exit': 3.0.0
|
||||
|
||||
packages/plugin-commands-setup:
|
||||
specifiers:
|
||||
'@pnpm/cli-utils': workspace:0.6.4
|
||||
'@pnpm/plugin-commands-setup': 'link:'
|
||||
'@pnpm/prepare': workspace:0.0.23
|
||||
render-help: ^1.0.1
|
||||
dependencies:
|
||||
'@pnpm/cli-utils': link:../cli-utils
|
||||
render-help: 1.0.2
|
||||
devDependencies:
|
||||
'@pnpm/plugin-commands-setup': 'link:'
|
||||
'@pnpm/prepare': link:../../privatePackages/prepare
|
||||
|
||||
packages/plugin-commands-store:
|
||||
specifiers:
|
||||
'@pnpm/assert-store': workspace:*
|
||||
@@ -2383,6 +2396,7 @@ importers:
|
||||
'@pnpm/plugin-commands-rebuild': workspace:4.0.3
|
||||
'@pnpm/plugin-commands-script-runners': workspace:3.1.5
|
||||
'@pnpm/plugin-commands-server': workspace:3.0.8
|
||||
'@pnpm/plugin-commands-setup': workspace:^0.0.0
|
||||
'@pnpm/plugin-commands-store': workspace:3.0.8
|
||||
'@pnpm/prepare': workspace:0.0.23
|
||||
'@pnpm/read-package-json': workspace:5.0.1
|
||||
@@ -2476,6 +2490,7 @@ importers:
|
||||
'@pnpm/plugin-commands-rebuild': link:../plugin-commands-rebuild
|
||||
'@pnpm/plugin-commands-script-runners': link:../plugin-commands-script-runners
|
||||
'@pnpm/plugin-commands-server': link:../plugin-commands-server
|
||||
'@pnpm/plugin-commands-setup': link:../plugin-commands-setup
|
||||
'@pnpm/plugin-commands-store': link:../plugin-commands-store
|
||||
'@pnpm/prepare': link:../../privatePackages/prepare
|
||||
'@pnpm/read-package-json': link:../read-package-json
|
||||
|
||||
Reference in New Issue
Block a user