-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
- Loading branch information
There are no files selected for viewing
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; | ||
} |
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; | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../typings |