diff --git a/default_app/main.js b/default_app/main.js index bb7f79c7d081f..422cf1c2b2f20 100644 --- a/default_app/main.js +++ b/default_app/main.js @@ -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 { @@ -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 diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index ed2842b307465..d80fc4b19a2ea 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -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(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 = [ diff --git a/spec/fixtures/api/app-path/lib/index.js b/spec/fixtures/api/app-path/lib/index.js new file mode 100644 index 0000000000000..d1a5732edca51 --- /dev/null +++ b/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() diff --git a/spec/fixtures/api/app-path/package.json b/spec/fixtures/api/app-path/package.json new file mode 100644 index 0000000000000..8f9e09dbdabb2 --- /dev/null +++ b/spec/fixtures/api/app-path/package.json @@ -0,0 +1,4 @@ +{ + "name": "app-path", + "main": "lib/index.js" +}