From 56aeba4bae4f016773a049031f0313ac56b59423 Mon Sep 17 00:00:00 2001 From: chlorine Date: Wed, 21 Sep 2022 17:50:09 +0800 Subject: [PATCH] fix: error in pnpm --dir link --global (#5371) close #5368 --- .changeset/orange-apes-scream.md | 5 ++++ .changeset/stale-rice-nail.md | 5 ++++ .../plugin-commands-installation/src/link.ts | 2 +- .../plugin-commands-installation/test/link.ts | 26 +++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 .changeset/orange-apes-scream.md create mode 100644 .changeset/stale-rice-nail.md diff --git a/.changeset/orange-apes-scream.md b/.changeset/orange-apes-scream.md new file mode 100644 index 00000000000..a778928f502 --- /dev/null +++ b/.changeset/orange-apes-scream.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-installation": patch +--- + +add path join and add test case for pnpm --dir link global diff --git a/.changeset/stale-rice-nail.md b/.changeset/stale-rice-nail.md new file mode 100644 index 00000000000..d00ba5526a2 --- /dev/null +++ b/.changeset/stale-rice-nail.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-installation": patch +--- + +fix error in pnpm --dir link --global diff --git a/packages/plugin-commands-installation/src/link.ts b/packages/plugin-commands-installation/src/link.ts index d4915303e80..8972ccca7a5 100644 --- a/packages/plugin-commands-installation/src/link.ts +++ b/packages/plugin-commands-installation/src/link.ts @@ -116,7 +116,7 @@ export async function handler ( const { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.dir, opts) const newManifest = await addDependenciesToPackage( manifest ?? {}, - [`link:${cwd}`], + [`link:${opts.cliOptions?.dir ? path.join(cwd, opts.cliOptions.dir) : cwd}`], linkOpts ) await writeProjectManifest(newManifest) diff --git a/packages/plugin-commands-installation/test/link.ts b/packages/plugin-commands-installation/test/link.ts index cd2cdd69cb0..fe6c62eb8dd 100644 --- a/packages/plugin-commands-installation/test/link.ts +++ b/packages/plugin-commands-installation/test/link.ts @@ -72,6 +72,32 @@ test('link global bin', async function () { await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin')) }) +test('link --dir global bin', async function () { + prepare() + process.chdir('..') + + const globalDir = path.resolve('global') + const globalBin = path.join(globalDir, 'bin') + const oldPath = process.env[PATH] + process.env[PATH] = `${globalBin}${path.delimiter}${oldPath ?? ''}` + await fs.mkdir(globalBin, { recursive: true }) + + await writePkg('./dir/package-with-bin-in-dir', { name: 'package-with-bin-in-dir', version: '1.0.0', bin: 'bin.js' }) + await fs.writeFile('./dir/package-with-bin-in-dir/bin.js', '#!/usr/bin/env node\nconsole.log(/hi/)\n', 'utf8') + + await link.handler({ + ...DEFAULT_OPTS, + cliOptions: { + dir: './dir/package-with-bin-in-dir', + }, + bin: globalBin, + dir: globalDir, + }) + process.env[PATH] = oldPath + + await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin-in-dir')) +}) + test('relative link', async () => { const project = prepare({ dependencies: {