From 7dc14306c2952170b1b7b6946c5e4d335b86af6b Mon Sep 17 00:00:00 2001 From: ShanArosh <96517136+ShanArosh@users.noreply.github.com> Date: Tue, 23 Jan 2024 12:55:25 +0530 Subject: [PATCH 1/3] fix: fixed package resolving in pnpm & monorepo setups --- src/cli-scripts/add.ts | 8 ++------ src/cli-scripts/common.ts | 10 +++++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/cli-scripts/add.ts b/src/cli-scripts/add.ts index faea402..0c7b522 100644 --- a/src/cli-scripts/add.ts +++ b/src/cli-scripts/add.ts @@ -10,12 +10,8 @@ import { readJSON, runExec, writePrettyJSON } from './common'; export async function doAdd(taskInfoMessageProvider: TaskInfoProvider): Promise { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const usersProjectDir = process.env.CAPACITOR_ROOT_DIR!; - const platformNodeModuleTemplateTar = join( - usersProjectDir, - 'node_modules', - '@capacitor-community', - 'electron', - 'template.tar.gz' + const platformNodeModuleTemplateTar = require.resolve( + ['@capacitor-community', 'electron', 'template.tar.gz'].join('/') ); const destDir = join(usersProjectDir, 'electron'); let usersProjectCapConfigFile: string | undefined = undefined; diff --git a/src/cli-scripts/common.ts b/src/cli-scripts/common.ts index 9ddb73f..3f64891 100644 --- a/src/cli-scripts/common.ts +++ b/src/cli-scripts/common.ts @@ -91,8 +91,7 @@ export function getDependencies(packageJson: PackageJson): string[] { export async function resolvePlugin(name: string): Promise { try { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const usersProjectDir = process.env.CAPACITOR_ROOT_DIR!; - const packagePath = resolveNode(usersProjectDir, name, 'package.json'); + const packagePath = resolveNode( name); if (!packagePath) { console.error( `\nUnable to find ${chalk.bold(`node_modules/${name}`)}.\n` + `Are you sure ${chalk.bold(name)} is installed?` @@ -101,7 +100,7 @@ export async function resolvePlugin(name: string): Promise { } const rootPath = dirname(packagePath); - const meta = await readJSON(packagePath); + const meta = await readJSON([packagePath, 'package.json'].join('/')); if (!meta) { return null; } @@ -121,9 +120,10 @@ export async function resolvePlugin(name: string): Promise { return null; } -export function resolveNode(root: string, ...pathSegments: string[]): string | null { + +export function resolveNode(pkg_name:string): string | null { try { - const t = require.resolve(pathSegments.join('/'), { paths: [root] }); + const t = require.resolve(pkg_name); //console.log(t); return t; } catch (e) { From 14022e11b46e4aaa50e8244d2aa951f4fa5517f6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 23 Jan 2024 09:11:54 +0000 Subject: [PATCH 2/3] fix ERR_PACKAGE_PATH_NOT_EXPORTED error --- src/cli-scripts/common.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cli-scripts/common.ts b/src/cli-scripts/common.ts index 3f64891..b3c6fd6 100644 --- a/src/cli-scripts/common.ts +++ b/src/cli-scripts/common.ts @@ -6,6 +6,7 @@ import { readFileSync, existsSync, writeFileSync } from 'fs'; import type { Ora } from 'ora'; import ora from 'ora'; import { dirname, join, parse, resolve } from 'path'; +import path from "node:path" const enum PluginType { Core, @@ -91,7 +92,8 @@ export function getDependencies(packageJson: PackageJson): string[] { export async function resolvePlugin(name: string): Promise { try { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const packagePath = resolveNode( name); + const packagePath = resolveNode([name, 'package.json'].join('/')); + // const packagePath = resolveNode(name); if (!packagePath) { console.error( `\nUnable to find ${chalk.bold(`node_modules/${name}`)}.\n` + `Are you sure ${chalk.bold(name)} is installed?` @@ -100,7 +102,8 @@ export async function resolvePlugin(name: string): Promise { } const rootPath = dirname(packagePath); - const meta = await readJSON([packagePath, 'package.json'].join('/')); + const meta = await readJSON(packagePath); + // const meta = await readJSON([packagePath, 'package.json'].join('/')); if (!meta) { return null; } @@ -120,11 +123,10 @@ export async function resolvePlugin(name: string): Promise { return null; } - -export function resolveNode(pkg_name:string): string | null { +export function resolveNode(pkg: string): string | null { try { - const t = require.resolve(pkg_name); - //console.log(t); + const t = path.resolve(pkg); + // console.log(t); return t; } catch (e) { return null; From 87f840fe68eb43a548cf1aaa2f07bc390d4a5687 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 23 Jan 2024 10:05:39 +0000 Subject: [PATCH 3/3] fix resolving package.json in edge cases --- src/cli-scripts/common.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cli-scripts/common.ts b/src/cli-scripts/common.ts index b3c6fd6..58ac241 100644 --- a/src/cli-scripts/common.ts +++ b/src/cli-scripts/common.ts @@ -6,7 +6,6 @@ import { readFileSync, existsSync, writeFileSync } from 'fs'; import type { Ora } from 'ora'; import ora from 'ora'; import { dirname, join, parse, resolve } from 'path'; -import path from "node:path" const enum PluginType { Core, @@ -92,8 +91,7 @@ export function getDependencies(packageJson: PackageJson): string[] { export async function resolvePlugin(name: string): Promise { try { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const packagePath = resolveNode([name, 'package.json'].join('/')); - // const packagePath = resolveNode(name); + const packagePath = resolveNode(name); if (!packagePath) { console.error( `\nUnable to find ${chalk.bold(`node_modules/${name}`)}.\n` + `Are you sure ${chalk.bold(name)} is installed?` @@ -103,7 +101,6 @@ export async function resolvePlugin(name: string): Promise { const rootPath = dirname(packagePath); const meta = await readJSON(packagePath); - // const meta = await readJSON([packagePath, 'package.json'].join('/')); if (!meta) { return null; } @@ -125,10 +122,17 @@ export async function resolvePlugin(name: string): Promise { export function resolveNode(pkg: string): string | null { try { - const t = path.resolve(pkg); + const t = require.resolve([pkg, 'package.json'].join('/')); // console.log(t); return t; } catch (e) { + try { + const entrypoint = require.resolve(pkg); + const resolved = entrypoint.slice(0, entrypoint.search(pkg) + pkg.length); + return [resolved, 'package.json'].join('/'); + } catch { + return null; + } return null; } }