Skip to content

Commit

Permalink
fix: app.getAppPath() returning default-app path for files or directo…
Browse files Browse the repository at this point in the history
…ries without package.json
  • Loading branch information
miniak committed Jun 16, 2019
1 parent 1d6e5e6 commit 7854810
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions default_app/main.ts
Expand Up @@ -85,7 +85,8 @@ function loadApplicationPackage (packagePath: string) {
// Override app name and version.
packagePath = path.resolve(packagePath)
const packageJsonPath = path.join(packagePath, 'package.json')
if (fs.existsSync(packageJsonPath)) {
const hasPackageJson = fs.existsSync(packageJsonPath)
if (hasPackageJson) {
let packageJson
try {
packageJson = require(packageJsonPath)
Expand All @@ -106,7 +107,10 @@ function loadApplicationPackage (packagePath: string) {
}

try {
Module._resolveFilename(packagePath, module, true)
const filePath = Module._resolveFilename(packagePath, module, true)
if (!hasPackageJson) {
app._setDefaultAppPaths(path.dirname(filePath))
}
} catch (e) {
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
return
Expand Down
26 changes: 26 additions & 0 deletions spec-main/api-app-spec.ts
Expand Up @@ -645,6 +645,32 @@ describe('app module', () => {
})
})

describe('getAppPath', () => {
it('works for directories with package.json', async () => {
const { appPath } = await runTestApp('app-path')
const expectedAppPath = path.resolve(fixturesPath, 'api/app-path')
expect(appPath).to.equal(expectedAppPath)
})

it('works for directories with index.js', async () => {
const { appPath } = await runTestApp('app-path/lib')
const expectedAppPath = path.resolve(fixturesPath, 'api/app-path/lib')
expect(appPath).to.equal(expectedAppPath)
})

it('works for files without extension', async () => {
const { appPath } = await runTestApp('app-path/lib/index')
const expectedAppPath = path.resolve(fixturesPath, 'api/app-path/lib')
expect(appPath).to.equal(expectedAppPath)
})

it('works for files', async () => {
const { appPath } = await runTestApp('app-path/lib/index.js')
const expectedAppPath = path.resolve(fixturesPath, 'api/app-path/lib')
expect(appPath).to.equal(expectedAppPath)
})
})

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"
}

0 comments on commit 7854810

Please sign in to comment.