mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: app.getAppPath() returning default-app path for files or directories without package.json (#18763) (#18895)
This commit is contained in:
committed by
Shelley Vohr
parent
65c53cc4fa
commit
a568e13642
@@ -86,6 +86,7 @@ function loadApplicationPackage (packagePath) {
|
||||
// Override app name and version.
|
||||
packagePath = path.resolve(packagePath)
|
||||
const packageJsonPath = path.join(packagePath, 'package.json')
|
||||
let appPath
|
||||
if (fs.existsSync(packageJsonPath)) {
|
||||
let packageJson
|
||||
try {
|
||||
@@ -105,11 +106,12 @@ function loadApplicationPackage (packagePath) {
|
||||
}
|
||||
app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
|
||||
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
|
||||
app.setAppPath(packagePath)
|
||||
appPath = packagePath
|
||||
}
|
||||
|
||||
try {
|
||||
Module._resolveFilename(packagePath, module, true)
|
||||
const filePath = Module._resolveFilename(packagePath, module, true)
|
||||
app.setAppPath(appPath || path.dirname(filePath))
|
||||
} catch (e) {
|
||||
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
|
||||
return
|
||||
|
||||
@@ -6,6 +6,7 @@ const https = require('https')
|
||||
const net = require('net')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const cp = require('child_process')
|
||||
const { ipcRenderer, remote } = require('electron')
|
||||
const { emittedOnce } = require('./events-helpers')
|
||||
const { closeWindow } = require('./window-helpers')
|
||||
@@ -556,6 +557,28 @@ describe('app module', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAppPath', () => {
|
||||
it('works for directories with package.json', async () => {
|
||||
const { appPath } = await runTestApp('app-path')
|
||||
expect(appPath).to.equal(path.resolve(__dirname, 'fixtures/api/app-path'))
|
||||
})
|
||||
|
||||
it('works for directories with index.js', async () => {
|
||||
const { appPath } = await runTestApp('app-path/lib')
|
||||
expect(appPath).to.equal(path.resolve(__dirname, 'fixtures/api/app-path/lib'))
|
||||
})
|
||||
|
||||
it('works for files without extension', async () => {
|
||||
const { appPath } = await runTestApp('app-path/lib/index')
|
||||
expect(appPath).to.equal(path.resolve(__dirname, 'fixtures/api/app-path/lib'))
|
||||
})
|
||||
|
||||
it('works for files', async () => {
|
||||
const { appPath } = await runTestApp('app-path/lib/index.js')
|
||||
expect(appPath).to.equal(path.resolve(__dirname, 'fixtures/api/app-path/lib'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPath(name)', () => {
|
||||
it('returns paths that exist', () => {
|
||||
const paths = [
|
||||
@@ -1125,3 +1148,16 @@ describe('app module', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
async function runTestApp (name, ...args) {
|
||||
const appPath = path.join(__dirname, 'fixtures', 'api', name)
|
||||
const electronPath = remote.getGlobal('process').execPath
|
||||
const appProcess = cp.spawn(electronPath, [appPath, ...args])
|
||||
|
||||
let output = ''
|
||||
appProcess.stdout.on('data', (data) => { output += data })
|
||||
|
||||
await emittedOnce(appProcess.stdout, 'end')
|
||||
|
||||
return JSON.parse(output)
|
||||
}
|
||||
|
||||
10
spec/fixtures/api/app-path/lib/index.js
vendored
Normal file
10
spec/fixtures/api/app-path/lib/index.js
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
const { app } = require('electron')
|
||||
|
||||
const payload = {
|
||||
appPath: app.getAppPath()
|
||||
}
|
||||
|
||||
process.stdout.write(JSON.stringify(payload))
|
||||
process.stdout.end()
|
||||
|
||||
process.exit()
|
||||
4
spec/fixtures/api/app-path/package.json
vendored
Normal file
4
spec/fixtures/api/app-path/package.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "app-path",
|
||||
"main": "lib/index.js"
|
||||
}
|
||||
Reference in New Issue
Block a user