-
-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): better error detection for electron-prebuilt-compile (#2268)
- Loading branch information
Showing
17 changed files
with
229 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import logSymbols from 'log-symbols'; | ||
import path from 'path'; | ||
|
||
import { getElectronModulePath } from './electron-version'; | ||
|
||
type PackageJSON = Record<string, unknown>; | ||
type Dependencies = Record<string, string>; | ||
|
||
export function pluginCompileExists(packageJSON: PackageJSON): boolean { | ||
if (!packageJSON.devDependencies) { | ||
return false; | ||
} | ||
|
||
const pluginCompileName = '@electron-forge/plugin-compile'; | ||
const findPluginCompile = (packageName: string): boolean => packageName === pluginCompileName; | ||
|
||
if (Object.keys(packageJSON.devDependencies as Dependencies).find(findPluginCompile)) { | ||
return true; | ||
} | ||
|
||
if (Object.keys(packageJSON.dependencies as Dependencies || {}).find(findPluginCompile)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(logSymbols.warning, `${pluginCompileName} was detected in dependencies, it should be in devDependencies`.yellow); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default async function locateElectronExecutable( | ||
dir: string, | ||
packageJSON: PackageJSON, | ||
): Promise<string> { | ||
let electronModulePath: string | undefined = await getElectronModulePath(dir, packageJSON); | ||
if (electronModulePath?.endsWith('electron-prebuilt-compile') && !pluginCompileExists(packageJSON)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(logSymbols.warning, 'WARNING: found electron-prebuilt-compile without the Electron Forge compile plugin. Please remove the deprecated electron-prebuilt-compile from your devDependencies.'.yellow); | ||
electronModulePath = undefined; | ||
} | ||
|
||
// eslint-disable-next-line import/no-dynamic-require, global-require | ||
let electronExecPath = require(electronModulePath || path.resolve(dir, 'node_modules/electron')); | ||
|
||
if (typeof electronExecPath !== 'string') { | ||
// eslint-disable-next-line no-console | ||
console.warn(logSymbols.warning, 'Returned Electron executable path is not a string, defaulting to a hardcoded location. Value:', electronExecPath); | ||
// eslint-disable-next-line import/no-dynamic-require, global-require | ||
electronExecPath = require(path.resolve(dir, 'node_modules/electron')); | ||
} | ||
|
||
return electronExecPath; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/api/core/test/fast/electron-executable_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import chai, { expect } from 'chai'; | ||
import path from 'path'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
import locateElectronExecutable, { pluginCompileExists } from '../../src/util/electron-executable'; | ||
|
||
chai.use(sinonChai); | ||
|
||
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'electron-executable'); | ||
|
||
describe('locateElectronExecutable', () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
sandbox.spy(console, 'warn'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('returns the correct path to electron', async () => { | ||
const appFixture = path.join(fixtureDir, 'electron_app'); | ||
const packageJSON = { | ||
devDependencies: { electron: '^100.0.0' }, | ||
}; | ||
|
||
await expect(locateElectronExecutable(appFixture, packageJSON)).to.eventually.equal('execPath'); | ||
|
||
// eslint-disable-next-line no-console, no-unused-expressions | ||
expect(console.warn).to.not.have.been.called; | ||
}); | ||
|
||
it('warns and returns a hardcoded path to electron if another electron module does not export a string', async () => { | ||
const appFixture = path.join(fixtureDir, 'bad-export'); | ||
const packageJSON = { | ||
devDependencies: { | ||
'@electron-forge/plugin-compile': '^6.0.0-beta.1', | ||
'electron-prebuilt-compile': '^1.4.0', | ||
}, | ||
}; | ||
|
||
await expect(locateElectronExecutable(appFixture, packageJSON)).to.eventually.equal('execPath'); | ||
|
||
// eslint-disable-next-line no-console, no-unused-expressions | ||
expect(console.warn).to.have.been.calledOnce; | ||
}); | ||
|
||
it('warns if prebuilt-compile exists without the corresponding plugin', async () => { | ||
const packageJSON = { | ||
devDependencies: { 'electron-prebuilt-compile': '1.0.0' }, | ||
}; | ||
const compileFixture = path.join(fixtureDir, 'prebuilt-compile'); | ||
|
||
await expect(locateElectronExecutable(compileFixture, packageJSON)).to.eventually.be.rejected; | ||
|
||
// eslint-disable-next-line no-console, no-unused-expressions | ||
expect(console.warn).to.have.been.calledOnce; | ||
}); | ||
|
||
it('does not warn if prebuilt-compile exists with the corresponding plugin', async () => { | ||
const packageJSON = { | ||
devDependencies: { | ||
'@electron-forge/plugin-compile': '^6.0.0-beta.1', | ||
'electron-prebuilt-compile': '1.0.0', | ||
}, | ||
}; | ||
|
||
const compileFixture = path.join(fixtureDir, 'prebuilt-compile'); | ||
// eslint-disable-next-line no-unused-expressions | ||
await expect(locateElectronExecutable(compileFixture, packageJSON)).to.eventually.be.rejected; | ||
|
||
// eslint-disable-next-line no-console, no-unused-expressions | ||
expect(console.warn).to.not.have.been.called; | ||
}); | ||
}); | ||
|
||
describe('pluginCompileExists', () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
sandbox.spy(console, 'warn'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('returns false if there is no devDependencies', () => { | ||
expect(pluginCompileExists({})).to.equal(false); | ||
}); | ||
|
||
it('returns false if the plugin is not found in devDependencies', () => { | ||
expect(pluginCompileExists({ devDependencies: {} })).to.equal(false); | ||
}); | ||
|
||
it('returns true if the plugin is found in devDependencies', () => { | ||
const packageJSON = { | ||
devDependencies: { '@electron-forge/plugin-compile': '^6.0.0-beta.1' }, | ||
}; | ||
|
||
expect(pluginCompileExists(packageJSON)).to.equal(true); | ||
|
||
// eslint-disable-next-line no-console, no-unused-expressions | ||
expect(console.warn).to.not.have.been.called; | ||
}); | ||
|
||
it('warns and returns true if the plugin is found in dependencies', () => { | ||
const packageJSON = { | ||
dependencies: { '@electron-forge/plugin-compile': '^6.0.0-beta.1' }, | ||
devDependencies: {}, | ||
}; | ||
|
||
expect(pluginCompileExists(packageJSON)).to.equal(true); | ||
|
||
// eslint-disable-next-line no-console, no-unused-expressions | ||
expect(console.warn).to.have.been.calledOnce; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...st/fixture/electron-executable/bad-export/node_modules/electron-prebuilt-compile/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...ixture/electron-executable/bad-export/node_modules/electron-prebuilt-compile/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/api/core/test/fixture/electron-executable/bad-export/node_modules/electron/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...s/api/core/test/fixture/electron-executable/bad-export/node_modules/electron/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...ges/api/core/test/fixture/electron-executable/electron_app/node_modules/electron/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...api/core/test/fixture/electron-executable/electron_app/node_modules/electron/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
.../electron-executable/prebuilt-compile/node_modules/electron-prebuilt-compile/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../typings |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters