Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app.getAppPath() returning default-app path when no package.json found #18894

Merged
merged 1 commit into from Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions default_app/main.js
Expand Up @@ -74,6 +74,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 {
Expand All @@ -91,11 +92,12 @@ function loadApplicationPackage (packagePath) {
} else if (packageJson.name) {
app.setName(packageJson.name)
}
app._setDefaultAppPaths(packagePath)
appPath = packagePath
}

try {
Module._resolveFilename(packagePath, module, true)
const filePath = Module._resolveFilename(packagePath, module, true)
app._setDefaultAppPaths(appPath || path.dirname(filePath))
} catch (e) {
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
return
Expand Down
22 changes: 22 additions & 0 deletions spec/api-app-spec.js
Expand Up @@ -617,6 +617,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 = [
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/api/app-path/lib/index.js
@@ -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 changes: 4 additions & 0 deletions spec/fixtures/api/app-path/package.json
@@ -0,0 +1,4 @@
{
"name": "app-path",
"main": "lib/index.js"
}