Skip to content

Commit 503da64

Browse files
trop[bot]codebytere
authored andcommitted
fix: app.getAppPath() returning default-app path for files or directories without package.json (electron#18888)
1 parent bd48dfe commit 503da64

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

default_app/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ function loadApplicationPackage (packagePath: string) {
8585
// Override app name and version.
8686
packagePath = path.resolve(packagePath)
8787
const packageJsonPath = path.join(packagePath, 'package.json')
88+
let appPath
8889
if (fs.existsSync(packageJsonPath)) {
8990
let packageJson
9091
try {
@@ -102,11 +103,12 @@ function loadApplicationPackage (packagePath: string) {
102103
} else if (packageJson.name) {
103104
app.setName(packageJson.name)
104105
}
105-
app._setDefaultAppPaths(packagePath)
106+
appPath = packagePath
106107
}
107108

108109
try {
109-
Module._resolveFilename(packagePath, module, true)
110+
const filePath = Module._resolveFilename(packagePath, module, true)
111+
app._setDefaultAppPaths(appPath || path.dirname(filePath))
110112
} catch (e) {
111113
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
112114
return

spec-main/api-app-spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,28 @@ describe('app module', () => {
649649
})
650650
})
651651

652+
describe('getAppPath', () => {
653+
it('works for directories with package.json', async () => {
654+
const { appPath } = await runTestApp('app-path')
655+
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path'))
656+
})
657+
658+
it('works for directories with index.js', async () => {
659+
const { appPath } = await runTestApp('app-path/lib')
660+
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'))
661+
})
662+
663+
it('works for files without extension', async () => {
664+
const { appPath } = await runTestApp('app-path/lib/index')
665+
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'))
666+
})
667+
668+
it('works for files', async () => {
669+
const { appPath } = await runTestApp('app-path/lib/index.js')
670+
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'))
671+
})
672+
})
673+
652674
describe('getPath(name)', () => {
653675
it('returns paths that exist', () => {
654676
const paths = [
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { app } = require('electron')
2+
3+
const payload = {
4+
appPath: app.getAppPath()
5+
}
6+
7+
process.stdout.write(JSON.stringify(payload))
8+
process.stdout.end()
9+
10+
process.exit()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "app-path",
3+
"main": "lib/index.js"
4+
}

0 commit comments

Comments
 (0)