From f445309e8afe6c99f6481e80b1fbb7e7749a0f1b Mon Sep 17 00:00:00 2001 From: JiangWeixian Date: Tue, 8 Feb 2022 14:21:42 +0800 Subject: [PATCH] fix: failed on pnpm --- index.js | 7 ++++--- package.json | 3 +++ yarn.lock | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4f68f4f..ca55a0d 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const fs = require("fs"); const path = require("path"); +const resolve = require('resolve/sync') function findMainPackageJson(entryPath, packageName) { entryPath = entryPath.replace(/\//g, path.sep); @@ -38,11 +39,11 @@ function getSelfReferencePath(packageName) { } } -function getPackageJson(packageName) { +function getPackageJson(packageName, basedir) { // Require `package.json` from the package, both from exported `exports` field // in ESM packages, or directly from the file itself in CommonJS packages. try { - return require(`${packageName}/package.json`); + return require(resolve(`${packageName}/package.json`, { basedir })); } catch (requireError) { if (requireError.code === "MODULE_NOT_FOUND") { throw requireError; @@ -116,7 +117,7 @@ module.exports = (request, options) => { const selfReferencePath = getSelfReferencePath(packageName); if(selfReferencePath) packageName = selfReferencePath - const packageJson = getPackageJson(packageName); + const packageJson = getPackageJson(packageName, options.basedir); if (!packageJson) { console.error(`Failed to find package.json for ${packageName}`); diff --git a/package.json b/package.json index ada0bb7..cfc364c 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ ], "devDependencies": { "prettier": "^2.5.1" + }, + "dependencies": { + "resolve": "^1.22.0" } } diff --git a/yarn.lock b/yarn.lock index 8ebb669..518a522 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,7 +2,45 @@ # yarn lockfile v1 -"prettier@^2.5.1": - "integrity" "sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==" - "resolved" "https://registry.npmjs.org/prettier/-/prettier-2.5.1.tgz" - "version" "2.5.1" +function-bind@^1.1.1: + version "1.1.1" + resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" + integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A== + +has@^1.0.3: + version "1.0.3" + resolved "https://registry.npmjs.org/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796" + integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw== + dependencies: + function-bind "^1.1.1" + +is-core-module@^2.8.1: + version "2.8.1" + resolved "https://registry.npmjs.org/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211" + integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA== + dependencies: + has "^1.0.3" + +path-parse@^1.0.7: + version "1.0.7" + resolved "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735" + integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw== + +prettier@^2.5.1: + version "2.5.1" + resolved "https://registry.npmjs.org/prettier/-/prettier-2.5.1.tgz" + integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg== + +resolve@^1.22.0: + version "1.22.0" + resolved "https://registry.npmjs.org/resolve/-/resolve-1.22.0.tgz#5e0b8c67c15df57a89bdbabe603a002f21731198" + integrity sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw== + dependencies: + is-core-module "^2.8.1" + path-parse "^1.0.7" + supports-preserve-symlinks-flag "^1.0.0" + +supports-preserve-symlinks-flag@^1.0.0: + version "1.0.0" + resolved "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09" + integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==