Skip to content

Commit 4da07ac

Browse files
committed
refactor: update installOrRebuild and related functions to include projectDir parameter
1 parent 8bd1a10 commit 4da07ac

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

packages/app-builder-lib/src/packager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ export class Packager {
600600
if (config.buildDependenciesFromSource === true && platform.nodeName !== process.platform) {
601601
log.info({ reason: "platform is different and buildDependenciesFromSource is set to true" }, "skipped dependencies rebuild")
602602
} else {
603-
await installOrRebuild(config, this.appDir, {
603+
await installOrRebuild(config, this.appDir, this.projectDir, {
604604
frameworkInfo,
605605
platform: platform.nodeName,
606606
arch: Arch[arch],

packages/app-builder-lib/src/util/appFileCopier.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,18 @@ function validateFileSet(fileSet: ResolvedFileSet): ResolvedFileSet {
178178

179179
/** @internal */
180180
export async function computeNodeModuleFileSets(platformPackager: PlatformPackager<any>, mainMatcher: FileMatcher): Promise<Array<ResolvedFileSet>> {
181-
const deps = await getNodeModules(platformPackager.info.appDir)
181+
const projectDir = platformPackager.info.projectDir
182+
const appDir = platformPackager.info.appDir
183+
184+
let deps = await getNodeModules(appDir)
185+
if (projectDir !== appDir && deps.length === 0) {
186+
const packageJson = require(path.join(appDir, "package.json"))
187+
if (Object.keys(packageJson.dependencies).length > 0) {
188+
log.debug({ projectDir, appDir }, "no node_modules in app dir, trying to find in project dir")
189+
deps = await getNodeModules(projectDir)
190+
}
191+
}
192+
182193
log.debug({ nodeModules: deps }, "collected node modules")
183194

184195
const nodeModuleExcludedExts = getNodeModuleExcludedExts(platformPackager)

packages/app-builder-lib/src/util/yarn.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as electronRebuild from "@electron/rebuild"
2-
import { getProjectRootPath } from "@electron/rebuild/lib/search-module"
32
import { RebuildMode } from "@electron/rebuild/lib/types"
43
import { asArray, log, spawn } from "builder-util"
54
import { pathExists } from "fs-extra"
@@ -12,7 +11,7 @@ import { PM, detect, getPackageManagerVersion } from "../node-module-collector"
1211
import { NodeModuleDirInfo } from "./packageDependencies"
1312
import { rebuild as remoteRebuild } from "./rebuild/rebuild"
1413

15-
export async function installOrRebuild(config: Configuration, appDir: string, options: RebuildOptions, forceInstall = false) {
14+
export async function installOrRebuild(config: Configuration, appDir: string, projectDir: string, options: RebuildOptions, forceInstall = false) {
1615
const effectiveOptions: RebuildOptions = {
1716
buildFromSource: config.buildDependenciesFromSource === true,
1817
additionalArgs: asArray(config.npmArgs),
@@ -29,9 +28,9 @@ export async function installOrRebuild(config: Configuration, appDir: string, op
2928
}
3029

3130
if (forceInstall || !isDependenciesInstalled) {
32-
await installDependencies(config, appDir, effectiveOptions)
31+
await installDependencies(config, appDir, projectDir, effectiveOptions)
3332
} else {
34-
await rebuild(config, appDir, effectiveOptions)
33+
await rebuild(config, appDir, projectDir, effectiveOptions)
3534
}
3635
}
3736

@@ -91,12 +90,11 @@ async function checkYarnBerry(pm: PM) {
9190
return version.split(".")[0] >= "2"
9291
}
9392

94-
async function installDependencies(config: Configuration, appDir: string, options: RebuildOptions): Promise<any> {
93+
async function installDependencies(config: Configuration, appDir: string, projectDir: string, options: RebuildOptions): Promise<any> {
9594
const platform = options.platform || process.platform
9695
const arch = options.arch || process.arch
9796
const additionalArgs = options.additionalArgs
9897

99-
const projectDir = await getProjectRootPath(appDir)
10098
const pm = await detect({ cwd: projectDir })
10199
log.info({ pm, platform, arch, projectDir, appDir }, `installing production dependencies`)
102100
const execArgs = ["install"]
@@ -123,7 +121,7 @@ async function installDependencies(config: Configuration, appDir: string, option
123121

124122
// Some native dependencies no longer use `install` hook for building their native module, (yarn 3+ removed implicit link of `install` and `rebuild` steps)
125123
// https://github.com/electron-userland/electron-builder/issues/8024
126-
return rebuild(config, appDir, options)
124+
return rebuild(config, appDir, projectDir, options)
127125
}
128126

129127
export async function nodeGypRebuild(platform: NodeJS.Platform, arch: string, frameworkInfo: DesktopFrameworkInfo) {
@@ -171,7 +169,7 @@ export interface RebuildOptions {
171169
}
172170

173171
/** @internal */
174-
export async function rebuild(config: Configuration, appDir: string, options: RebuildOptions) {
172+
export async function rebuild(config: Configuration, appDir: string, projectDir: string, options: RebuildOptions) {
175173
const configuration = {
176174
dependencies: await options.productionDeps.value,
177175
nodeExecPath: process.execPath,
@@ -205,7 +203,7 @@ export async function rebuild(config: Configuration, appDir: string, options: Re
205203
arch,
206204
platform,
207205
buildFromSource,
208-
projectRootPath: await getProjectRootPath(appDir),
206+
projectRootPath: projectDir,
209207
mode: (config.nativeRebuilder as RebuildMode) || "sequential",
210208
disablePreGypCopy: true,
211209
}

packages/electron-builder/src/cli/install-app-deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export async function installAppDeps(args: any) {
5858
await installOrRebuild(
5959
config,
6060
appDir,
61+
projectDir,
6162
{
6263
frameworkInfo: { version, useCustomDist: true },
6364
platform: args.platform,

0 commit comments

Comments
 (0)