From 566c7f6466470689578e89e196fd9e3ace7c2c55 Mon Sep 17 00:00:00 2001 From: lvqq Date: Sun, 9 Oct 2022 23:04:30 +0800 Subject: [PATCH 1/4] fix: remove WARN undefined has no binaries --- .changeset/heavy-tools-agree.md | 5 +++++ packages/core/src/install/index.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/heavy-tools-agree.md diff --git a/.changeset/heavy-tools-agree.md b/.changeset/heavy-tools-agree.md new file mode 100644 index 00000000000..be606f4f71d --- /dev/null +++ b/.changeset/heavy-tools-agree.md @@ -0,0 +1,5 @@ +--- +"@pnpm/core": patch +--- + +Fix WARN undefined has no binaries diff --git a/packages/core/src/install/index.ts b/packages/core/src/install/index.ts index 8fa8d6066ac..5cf756f03c9 100644 --- a/packages/core/src/install/index.ts +++ b/packages/core/src/install/index.ts @@ -1004,7 +1004,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => { const projectToInstall = projects[index] if (opts.global && projectToInstall.mutation.includes('install')) { projectToInstall.wantedDependencies.forEach(pkg => { - if (!linkedPackages?.includes(pkg.alias)) { + if (pkg.alias && !linkedPackages?.includes(pkg.alias)) { logger.warn({ message: `${pkg.alias} has no binaries`, prefix: opts.lockfileDir }) } }) From 4d745b84d412670161ae9ea4c8bdc8f98f8f006f Mon Sep 17 00:00:00 2001 From: lvqq Date: Sun, 9 Oct 2022 23:58:20 +0800 Subject: [PATCH 2/4] fix: add dir option support in link --global --- .changeset/few-ghosts-rule.md | 5 ++ .../plugin-commands-installation/src/link.ts | 8 +-- .../plugin-commands-installation/test/link.ts | 49 ++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 .changeset/few-ghosts-rule.md diff --git a/.changeset/few-ghosts-rule.md b/.changeset/few-ghosts-rule.md new file mode 100644 index 00000000000..ddda1b31239 --- /dev/null +++ b/.changeset/few-ghosts-rule.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-installation": patch +--- + +Add dir option support in link --global diff --git a/packages/plugin-commands-installation/src/link.ts b/packages/plugin-commands-installation/src/link.ts index bd98969c139..32170cddee1 100644 --- a/packages/plugin-commands-installation/src/link.ts +++ b/packages/plugin-commands-installation/src/link.ts @@ -113,6 +113,8 @@ export async function handler ( workspacePackages, }) + const linkCwdDir = opts.cliOptions?.dir && opts.cliOptions?.global ? path.resolve(opts.cliOptions.dir) : cwd + // pnpm link if ((params == null) || (params.length === 0)) { if (path.relative(linkOpts.dir, cwd) === '') { @@ -121,7 +123,7 @@ export async function handler ( const { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.dir, opts) const newManifest = await addDependenciesToPackage( manifest ?? {}, - [`link:${opts.cliOptions?.dir ? path.resolve(opts.cliOptions.dir) : cwd}`], + [`link:${linkCwdDir}`], linkOpts ) await writeProjectManifest(newManifest) @@ -179,7 +181,7 @@ export async function handler ( globalPkgNames.forEach((pkgName) => pkgPaths.push(path.join(globalPkgPath, 'node_modules', pkgName))) } - const { manifest, writeProjectManifest } = await readProjectManifest(cwd, opts) + const { manifest, writeProjectManifest } = await readProjectManifest(linkCwdDir, opts) const linkConfig = await getConfig( { ...opts.cliOptions, dir: cwd }, @@ -190,7 +192,7 @@ export async function handler ( } ) const storeL = await createOrConnectStoreControllerCached(storeControllerCache, linkConfig) - const newManifest = await link(pkgPaths, path.join(cwd, 'node_modules'), { + const newManifest = await link(pkgPaths, path.join(linkCwdDir, 'node_modules'), { ...linkConfig, targetDependenciesField: linkOpts.targetDependenciesField, storeController: storeL.ctrl, diff --git a/packages/plugin-commands-installation/test/link.ts b/packages/plugin-commands-installation/test/link.ts index 08fecf2b7f8..7563a3de1e7 100644 --- a/packages/plugin-commands-installation/test/link.ts +++ b/packages/plugin-commands-installation/test/link.ts @@ -64,6 +64,9 @@ test('link global bin', async function () { await link.handler({ ...DEFAULT_OPTS, + cliOptions: { + global: true, + }, bin: globalBin, dir: globalDir, }) @@ -72,7 +75,7 @@ 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 () { +test('link to global bin in dir', async function () { prepare() process.chdir('..') @@ -88,6 +91,7 @@ test('link --dir global bin', async function () { await link.handler({ ...DEFAULT_OPTS, cliOptions: { + global: true, dir: path.resolve('./dir/package-with-bin-in-dir'), }, bin: globalBin, @@ -98,6 +102,49 @@ test('link --dir global bin', async function () { await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin-in-dir')) }) +test('link from global package in dir', async function () { + const project = 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('global-package-with-bin', { name: 'global-package-with-bin', version: '1.0.0', bin: 'bin.js' }) + await fs.writeFile('global-package-with-bin/bin.js', '#!/usr/bin/env node\nconsole.log(/hi/)\n', 'utf8') + + process.chdir('global-package-with-bin') + + // link to global + await link.handler({ + ...DEFAULT_OPTS, + cliOptions: { + global: true, + }, + bin: globalBin, + dir: globalDir, + }) + + process.chdir('..') + + // link from global + await link.handler({ + ...DEFAULT_OPTS, + cliOptions: { + global: true, + dir: path.resolve('./project'), + }, + bin: globalBin, + dir: globalDir, + }, ['global-package-with-bin']) + + process.env[PATH] = oldPath + + await project.has('global-package-with-bin') +}) + test('relative link', async () => { const project = prepare({ dependencies: { From 1d675c268e75dfc354af4149fa8abe8dab8cb0c2 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Mon, 10 Oct 2022 04:09:42 +0300 Subject: [PATCH 3/4] refactor: link --- .changeset/few-ghosts-rule.md | 3 ++- packages/plugin-commands-installation/test/link.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.changeset/few-ghosts-rule.md b/.changeset/few-ghosts-rule.md index ddda1b31239..81230afe34a 100644 --- a/.changeset/few-ghosts-rule.md +++ b/.changeset/few-ghosts-rule.md @@ -1,5 +1,6 @@ --- "@pnpm/plugin-commands-installation": patch +"pnpm": patch --- -Add dir option support in link --global +`pnpm link --global` should work when a custom target directory is specified with the `--dir` CLI option [#5473](https://github.com/pnpm/pnpm/pull/5473). diff --git a/packages/plugin-commands-installation/test/link.ts b/packages/plugin-commands-installation/test/link.ts index 7563a3de1e7..94e6f5b30d7 100644 --- a/packages/plugin-commands-installation/test/link.ts +++ b/packages/plugin-commands-installation/test/link.ts @@ -75,7 +75,7 @@ test('link global bin', async function () { await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin')) }) -test('link to global bin in dir', async function () { +test('link to global bin from the specified directory', async function () { prepare() process.chdir('..') @@ -102,7 +102,7 @@ test('link to global bin in dir', async function () { await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin-in-dir')) }) -test('link from global package in dir', async function () { +test('link a global package to the specified directory', async function () { const project = prepare() process.chdir('..') From 8e9cf64173bc0f22bd32a30eb971baeeed7dae4f Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Mon, 10 Oct 2022 04:13:08 +0300 Subject: [PATCH 4/4] fix: warng about bins --- packages/core/src/install/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/install/index.ts b/packages/core/src/install/index.ts index 5cf756f03c9..580a16440cd 100644 --- a/packages/core/src/install/index.ts +++ b/packages/core/src/install/index.ts @@ -1004,8 +1004,8 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => { const projectToInstall = projects[index] if (opts.global && projectToInstall.mutation.includes('install')) { projectToInstall.wantedDependencies.forEach(pkg => { - if (pkg.alias && !linkedPackages?.includes(pkg.alias)) { - logger.warn({ message: `${pkg.alias} has no binaries`, prefix: opts.lockfileDir }) + if (!linkedPackages?.includes(pkg.alias)) { + logger.warn({ message: `${pkg.alias ?? pkg.pref} has no binaries`, prefix: opts.lockfileDir }) } }) }