From 627d0cc2a183a6be43a9d05c1ae6da3d8edd5e2e Mon Sep 17 00:00:00 2001 From: bluelovers Date: Wed, 3 Nov 2021 15:56:15 +0800 Subject: [PATCH] refactor: avoid https://github.com/ezolenko/rollup-plugin-typescript2/issues/286 --- .../@yarn-tool/resolve-package/index.d.ts | 6 +++++ packages/@yarn-tool/resolve-package/index.js | 12 +++++++-- packages/@yarn-tool/resolve-package/index.ts | 23 +++++++++++++--- .../@yarn-tool/resolve-package/package.json | 27 ++++++++++++++++++- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/@yarn-tool/resolve-package/index.d.ts b/packages/@yarn-tool/resolve-package/index.d.ts index 009f3e505..1e291f8ec 100644 --- a/packages/@yarn-tool/resolve-package/index.d.ts +++ b/packages/@yarn-tool/resolve-package/index.d.ts @@ -1,9 +1,15 @@ import type { IPackageJson } from '@ts-type/package-dts'; export declare function resolvePackage(name: string, options?: { + /** + * @see RequireResolve + */ paths?: string[]; }): { name: string; pkgRoot: string; pkg: IPackageJson; + pkgConfigLocation: string; + entryPointLocation: string; + resolveLocation(path: string, ...paths: string[]): string; }; export default resolvePackage; diff --git a/packages/@yarn-tool/resolve-package/index.js b/packages/@yarn-tool/resolve-package/index.js index 4a43ecab8..82df6441d 100644 --- a/packages/@yarn-tool/resolve-package/index.js +++ b/packages/@yarn-tool/resolve-package/index.js @@ -2,12 +2,20 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.resolvePackage = void 0; const upath2_1 = require("upath2"); +const pkg_dir_1 = require("pkg-dir"); function resolvePackage(name, options) { - let pkgRoot = (0, upath2_1.dirname)(require.resolve(`${name}/package.json`, options)); + const entryPointLocation = require.resolve(name, options); + const pkgRoot = (0, pkg_dir_1.sync)(entryPointLocation); + const pkgConfigLocation = (0, upath2_1.resolve)(pkgRoot, 'package.json'); return { name, pkgRoot, - pkg: require(`${pkgRoot}/package.json`), + pkg: require(pkgConfigLocation), + pkgConfigLocation, + entryPointLocation, + resolveLocation(path, ...paths) { + return (0, upath2_1.resolve)(pkgRoot, path, ...paths); + }, }; } exports.resolvePackage = resolvePackage; diff --git a/packages/@yarn-tool/resolve-package/index.ts b/packages/@yarn-tool/resolve-package/index.ts index 399dbf0a5..27528cee8 100644 --- a/packages/@yarn-tool/resolve-package/index.ts +++ b/packages/@yarn-tool/resolve-package/index.ts @@ -1,14 +1,29 @@ -import { dirname } from 'upath2'; +import { dirname, normalize, sep, resolve } from 'upath2'; import type { IPackageJson } from '@ts-type/package-dts'; +import { sync as pkgDir } from 'pkg-dir'; -export function resolvePackage(name: string, options?: { paths?: string[]; }) +export function resolvePackage(name: string, options?: { + /** + * @see RequireResolve + */ + paths?: string[]; +}) { - let pkgRoot = dirname(require.resolve(`${name}/package.json`, options)); + const entryPointLocation = require.resolve(name, options); + const pkgRoot = pkgDir(entryPointLocation); + + const pkgConfigLocation: string = resolve(pkgRoot, 'package.json'); return { name, pkgRoot, - pkg: require(`${pkgRoot}/package.json`) as any as IPackageJson, + pkg: require(pkgConfigLocation) as any as IPackageJson, + pkgConfigLocation, + entryPointLocation, + resolveLocation(path: string, ...paths: string[]) + { + return resolve(pkgRoot, path, ...paths) + }, } } diff --git a/packages/@yarn-tool/resolve-package/package.json b/packages/@yarn-tool/resolve-package/package.json index 8b710c3be..71c43bab2 100644 --- a/packages/@yarn-tool/resolve-package/package.json +++ b/packages/@yarn-tool/resolve-package/package.json @@ -1,7 +1,31 @@ { "name": "@yarn-tool/resolve-package", "version": "1.0.35", - "description": "", + "description": "resolve package root and package.json paths", + "keywords": [ + "resolve", + "package", + "json", + "root", + "npm", + "entry", + "find", + "up", + "find-up", + "findup", + "look-up", + "look", + "file", + "search", + "match", + "parent", + "parents", + "folder", + "directory", + "walk", + "walking", + "path" + ], "homepage": "https://github.com/bluelovers/ws-yarn-workspaces/tree/master/packages/@yarn-tool/resolve-package#readme", "bugs": { "url": "https://github.com/bluelovers/ws-yarn-workspaces/issues" @@ -30,6 +54,7 @@ }, "dependencies": { "@ts-type/package-dts": "^1.0.51", + "pkg-dir": "< 6 >= 5", "tslib": "^2.3.1", "upath2": "^3.1.10" },