Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/cli-scripts/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import { readJSON, runExec, writePrettyJSON } from './common';
export async function doAdd(taskInfoMessageProvider: TaskInfoProvider): Promise<void> {
// 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;
Expand Down
16 changes: 11 additions & 5 deletions src/cli-scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ export function getDependencies(packageJson: PackageJson): string[] {
export async function resolvePlugin(name: string): Promise<Plugin | null> {
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?`
Expand Down Expand Up @@ -121,12 +120,19 @@ export async function resolvePlugin(name: string): Promise<Plugin | null> {
return null;
}

export function resolveNode(root: string, ...pathSegments: string[]): string | null {
export function resolveNode(pkg: string): string | null {
try {
const t = require.resolve(pathSegments.join('/'), { paths: [root] });
//console.log(t);
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;
}
}
Expand Down