Browse Source

fix: app.getAppPath() returning default-app path for files or directories without package.json (#18888)

trop[bot] 5 years ago
parent
commit
503da640f7

+ 4 - 2
default_app/main.ts

@@ -85,6 +85,7 @@ function loadApplicationPackage (packagePath: string) {
     // 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 {
@@ -102,11 +103,12 @@ function loadApplicationPackage (packagePath: string) {
       } 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

+ 22 - 0
spec-main/api-app-spec.ts

@@ -649,6 +649,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(fixturesPath, 'api/app-path'))
+    })
+
+    it('works for directories with index.js', async () => {
+      const { appPath } = await runTestApp('app-path/lib')
+      expect(appPath).to.equal(path.resolve(fixturesPath, '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(fixturesPath, 'api/app-path/lib'))
+    })
+
+    it('works for files', async () => {
+      const { appPath } = await runTestApp('app-path/lib/index.js')
+      expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'))
+    })
+  })
+
   describe('getPath(name)', () => {
     it('returns paths that exist', () => {
       const paths = [

+ 10 - 0
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 - 0
spec/fixtures/api/app-path/package.json

@@ -0,0 +1,4 @@
+{
+  "name": "app-path",
+  "main": "lib/index.js"
+}