Skip to content

Commit

Permalink
feat: rename pnpm-prefix to global-dir
Browse files Browse the repository at this point in the history
PR #2121
ref 2088
  • Loading branch information
zkochan authored Oct 27, 2019
1 parent 4fc917e commit 21f09a4
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/config/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface Config {
filter: string[],
rawLocalConfig: Record<string, any>, // tslint:disable-line
rawConfig: Record<string, any>, // tslint:disable-line
globalPrefix: string,
globalBin: string,
dryRun?: boolean, // This option might be not supported ever
global?: boolean,
globalDir: string,
workingDir: string,
bin?: string,
ignoreScripts?: boolean
Expand Down Expand Up @@ -111,6 +111,7 @@ export interface Config {

export interface ConfigWithDeprecatedSettings extends Config {
frozenShrinkwrap?: boolean,
globalPrefix?: string,
lockfileDirectory?: string,
shrinkwrapDirectory?: string,
shrinkwrapOnly?: boolean,
Expand Down
7 changes: 4 additions & 3 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const types = Object.assign({
'filter': [String, Array],
'frozen-lockfile': Boolean,
'frozen-shrinkwrap': Boolean,
'global-dir': String,
'global-path': path,
'global-pnpmfile': String,
'hoist': Boolean,
Expand Down Expand Up @@ -208,7 +209,7 @@ export default async (
default: normalizeRegistry(pnpmConfig.rawConfig.registry),
...getScopeRegistries(pnpmConfig.rawConfig),
}
const npmGlobalPrefix: string = pnpmConfig.rawConfig['pnpm-prefix'] ||
const npmGlobalPrefix: string = pnpmConfig.globalDir ?? pnpmConfig.rawConfig['pnpm-prefix'] ??
(
process.platform !== 'win32'
? npmConfig.globalPrefix
Expand All @@ -217,7 +218,7 @@ export default async (
pnpmConfig.globalBin = process.platform === 'win32'
? npmGlobalPrefix
: path.resolve(npmGlobalPrefix, 'bin')
pnpmConfig.globalPrefix = path.join(npmGlobalPrefix, 'pnpm-global')
pnpmConfig.globalDir = pnpmConfig.globalDir ? npmGlobalPrefix : path.join(npmGlobalPrefix, 'pnpm-global')
pnpmConfig.lockfileDir = pnpmConfig.lockfileDir ?? pnpmConfig.lockfileDirectory ?? pnpmConfig.shrinkwrapDirectory
pnpmConfig.useLockfile = (() => {
if (typeof pnpmConfig['lockfile'] === 'boolean') return pnpmConfig['lockfile']
Expand All @@ -239,7 +240,7 @@ export default async (
: pnpmConfig['sharedWorkspaceLockfile']

if (cliArgs['global']) {
pnpmConfig.workingDir = path.join(pnpmConfig.globalPrefix, LAYOUT_VERSION.toString())
pnpmConfig.workingDir = path.join(pnpmConfig.globalDir, LAYOUT_VERSION.toString())
pnpmConfig.bin = pnpmConfig.globalBin
pnpmConfig.allowNew = true
pnpmConfig.ignoreCurrentPrefs = true
Expand Down
11 changes: 11 additions & 0 deletions packages/pnpm/src/cmd/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default function (input: string[]) {
const docsUrl = (cmd: string) => `https://pnpm.js.org/en/cli/${cmd}`

const OPTIONS = {
globalDir: {
description: 'Specify a custom directory to store global packages',
name: '--global-dir',
},
ignoreScripts: {
description: "Don't run lifecycle scripts",
name: '--ignore-scripts',
Expand Down Expand Up @@ -100,6 +104,7 @@ function getHelpText (command: string) {
OPTIONS.ignoreScripts,
OPTIONS.offline,
OPTIONS.preferOffline,
OPTIONS.globalDir,
{
description: "Packages in \`devDependencies\` won't be installed",
name: '--production, --only prod[uction]',
Expand Down Expand Up @@ -303,6 +308,7 @@ function getHelpText (command: string) {
OPTIONS.preferOffline,
OPTIONS.storeDir,
OPTIONS.virtualStoreDir,
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
Expand Down Expand Up @@ -347,6 +353,7 @@ function getHelpText (command: string) {
name: '--recursive',
shortAlias: '-r',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
Expand Down Expand Up @@ -433,6 +440,7 @@ function getHelpText (command: string) {
name: '--latest',
shortAlias: '-L',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
Expand Down Expand Up @@ -497,6 +505,7 @@ function getHelpText (command: string) {
description: 'Display only the dependency tree for packages in \`devDependencies\`',
name: '--dev',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
Expand Down Expand Up @@ -550,6 +559,7 @@ function getHelpText (command: string) {
description: 'Display only the dependency tree for packages in \`devDependencies\`',
name: '--dev',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
Expand Down Expand Up @@ -691,6 +701,7 @@ function getHelpText (command: string) {
description: 'Prints the outdated packages in a list. Good for small consoles',
name: '--no-table',
},
OPTIONS.globalDir,
...UNIVERSAL_OPTIONS,
],
},
Expand Down
6 changes: 3 additions & 3 deletions packages/pnpm/src/cmd/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export default async (

// pnpm link
if (!input || !input.length) {
const { manifest, writeImporterManifest } = await tryReadImporterManifest(opts.globalPrefix, opts)
const { manifest, writeImporterManifest } = await tryReadImporterManifest(opts.globalDir, opts)
const newManifest = await linkToGlobal(cwd, {
...linkOpts,
// A temporary workaround. global bin/prefix are always defined when --global is set
globalBin: linkOpts.globalBin!,
globalPrefix: linkOpts.globalPrefix!,
globalDir: linkOpts.globalDir!,
manifest: manifest || {},
})
await writeImporterManifest(newManifest)
Expand All @@ -78,7 +78,7 @@ export default async (
} else {
globalPkgNames = pkgNames
}
const globalPkgPath = pathAbsolute(opts.globalPrefix)
const globalPkgPath = pathAbsolute(opts.globalDir)
globalPkgNames.forEach((pkgName) => pkgPaths.push(path.join(globalPkgPath, 'node_modules', pkgName)))
}

Expand Down
9 changes: 9 additions & 0 deletions packages/pnpm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type CLI_OPTIONS = 'access'
| 'filter'
| 'force'
| 'frozen-lockfile'
| 'global-dir'
| 'global-pnpmfile'
| 'global'
| 'help'
Expand Down Expand Up @@ -140,6 +141,7 @@ const INSTALL_CLI_OPTIONS = new Set<CLI_OPTIONS>([
'engine-strict',
'frozen-lockfile',
'force',
'global-dir',
'global-pnpmfile',
'global',
'hoist',
Expand Down Expand Up @@ -186,6 +188,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
'dev',
'engine-strict',
'force',
'global-dir',
'global-pnpmfile',
'global',
'hoist',
Expand Down Expand Up @@ -236,6 +239,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
'install': INSTALL_CLI_OPTIONS,
'install-test': INSTALL_CLI_OPTIONS,
'link': new Set([
'global-dir',
'global',
'only',
'package-import-method',
Expand All @@ -250,6 +254,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
'list': new Set([
'depth',
'dev',
'global-dir',
'global',
'json',
'long',
Expand All @@ -261,6 +266,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
]),
'outdated': new Set([
'depth',
'global-dir',
'global',
'long',
'recursive',
Expand All @@ -287,6 +293,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
]),
'remove': new Set([
'force',
'global-dir',
'global-pnpmfile',
'global',
'lockfile-dir',
Expand Down Expand Up @@ -335,6 +342,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
'dev',
'engine-strict',
'force',
'global-dir',
'global-pnpmfile',
'global',
'ignore-pnpmfile',
Expand Down Expand Up @@ -367,6 +375,7 @@ const SUPPORTED_CLI_OPTIONS: Record<CANONICAL_COMMAND_NAMES, Set<CLI_OPTIONS>> =
]),
'why': new Set([
'dev',
'global-dir',
'global',
'json',
'long',
Expand Down
9 changes: 9 additions & 0 deletions packages/pnpm/test/install/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ test('global installation', async (t: tape.Test) => {
t.ok(typeof isNegative === 'function', 'isNegative() is available')
})

test('global installation to custom directory with --global-dir', async (t: tape.Test) => {
prepare(t)

await execPnpm('add', '--global', '--global-dir=../global', 'is-positive')

const isPositive = require(path.resolve('../global/3/node_modules/is-positive'))
t.ok(typeof isPositive === 'function', 'isPositive() is available')
})

test('always install latest when doing global installation without spec', async (t: tape.Test) => {
prepare(t)
await addDistTag('peer-c', '2.0.0', 'latest')
Expand Down
4 changes: 2 additions & 2 deletions packages/supi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Create a symbolic link from the specified package to the global `node_modules`.
**Arguments:**

* `linkFrom` - *String* - path to the package that should be linked.
* `globalPrefix` - *String* - path to the global directory.
* `globalDir` - *String* - path to the global directory.
* `options.reporter` - *Function* - A function that listens for logs.

### `supi.linkFromGlobal(pkgNames, linkTo, options)`
Expand All @@ -54,7 +54,7 @@ Create symbolic links from the global `pkgName`s to the `linkTo/node_modules` fo

* `pkgNames` - *String[]* - packages to link.
* `linkTo` - *String* - package to link to.
* `globalPrefix` - *String* - path to the global directory.
* `globalDir` - *String* - path to the global directory.
* `options.reporter` - *Function* - A function that listens for logs.

### `supi.storeStatus([options])`
Expand Down
12 changes: 6 additions & 6 deletions packages/supi/src/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ function addLinkToLockfile (
export async function linkFromGlobal (
pkgNames: string[],
linkTo: string,
maybeOpts: LinkOptions & {globalPrefix: string},
maybeOpts: LinkOptions & {globalDir: string},
) {
const reporter = maybeOpts?.reporter
if (reporter) {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
const globalPkgPath = pathAbsolute(maybeOpts.globalPrefix)
const globalPkgPath = pathAbsolute(maybeOpts.globalDir)
const linkFromPkgs = pkgNames.map((pkgName) => path.join(globalPkgPath, 'node_modules', pkgName))
const newManifest = await link(linkFromPkgs, path.join(linkTo, 'node_modules'), opts)

Expand All @@ -221,20 +221,20 @@ export async function linkToGlobal (
linkFrom: string,
maybeOpts: LinkOptions & {
globalBin: string,
globalPrefix: string,
globalDir: string,
},
) {
const reporter = maybeOpts?.reporter
if (reporter) {
streamParser.on('data', reporter)
}
maybeOpts.lockfileDir = maybeOpts.lockfileDir || maybeOpts.globalPrefix
maybeOpts.lockfileDir = maybeOpts.lockfileDir || maybeOpts.globalDir
const opts = await extendOptions(maybeOpts)
const globalPkgPath = pathAbsolute(maybeOpts.globalPrefix)
const globalPkgPath = pathAbsolute(maybeOpts.globalDir)
const newManifest = await link([linkFrom], path.join(globalPkgPath, 'node_modules'), {
...opts,
linkToBin: maybeOpts.globalBin,
workingDir: maybeOpts.globalPrefix,
workingDir: maybeOpts.globalDir,
})

if (reporter) {
Expand Down
12 changes: 6 additions & 6 deletions packages/supi/test/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ test('global link', async (t: tape.Test) => {
const opts = await testDefaults()

process.chdir(linkedPkgPath)
const globalPrefix = path.resolve('..', 'global')
const globalDir = path.resolve('..', 'global')
const globalBin = path.resolve('..', 'global', 'bin')
await linkToGlobal(process.cwd(), { ...opts, globalPrefix, globalBin, manifest: {} }) // tslint:disable-line:no-any
await linkToGlobal(process.cwd(), { ...opts, globalDir, globalBin, manifest: {} }) // tslint:disable-line:no-any

await isExecutable(t, path.join(globalBin, 'hello-world-js-bin'))

Expand All @@ -187,21 +187,21 @@ test('global link', async (t: tape.Test) => {

process.chdir(projectPath)

await linkFromGlobal([linkedPkgName], process.cwd(), { ...opts, globalPrefix, manifest: {} }) // tslint:disable-line:no-any
await linkFromGlobal([linkedPkgName], process.cwd(), { ...opts, globalDir, manifest: {} }) // tslint:disable-line:no-any

await project.isExecutable('.bin/hello-world-js-bin')
})

test('failed linking should not create empty folder', async (t: tape.Test) => {
prepareEmpty(t)

const globalPrefix = path.resolve('..', 'global')
const globalDir = path.resolve('..', 'global')

try {
await linkFromGlobal(['does-not-exist'], process.cwd(), await testDefaults({ globalPrefix, manifest: {} }))
await linkFromGlobal(['does-not-exist'], process.cwd(), await testDefaults({ globalDir, manifest: {} }))
t.fail('should have failed')
} catch (err) {
t.notOk(await exists(path.join(globalPrefix, 'node_modules', 'does-not-exist')))
t.notOk(await exists(path.join(globalDir, 'node_modules', 'does-not-exist')))
}
})

Expand Down

0 comments on commit 21f09a4

Please sign in to comment.