diff --git a/packages/api/core/src/api/import.ts b/packages/api/core/src/api/import.ts index 9dfdc8e074..4631753b4a 100644 --- a/packages/api/core/src/api/import.ts +++ b/packages/api/core/src/api/import.ts @@ -7,6 +7,7 @@ import path from 'path'; import initGit from './init-scripts/init-git'; import { deps, devDeps, exactDevDeps } from './init-scripts/init-npm'; +import { updateElectronDependency } from '../util/electron-version'; import { setInitialForgeConfig } from '../util/forge-config'; import { info, warn } from '../util/messages'; import installDepList, { DepType, DepVersionRestriction } from '../util/install-dependencies'; @@ -15,25 +16,6 @@ import upgradeForgeConfig, { updateUpgradedForgeDevDeps } from'../util/upgrade-f const d = debug('electron-forge:import'); -function findElectronDep(dep: string): boolean { - return ['electron', 'electron-prebuilt', 'electron-prebuilt-compile'].includes(dep); -} - -function updateElectronDependency(packageJSON: any, dev: string[], exact: string[]): [string[], string[]] { - if (Object.keys(packageJSON.devDependencies).find(findElectronDep)) { - exact = exact.filter(dep => dep !== 'electron'); - } else { - const electronKey = Object.keys(packageJSON.dependencies).find(findElectronDep); - if (electronKey) { - d(`Moving ${electronKey} from dependencies to devDependencies`); - dev.push(`${electronKey}@${packageJSON.dependencies[electronKey]}`); - delete packageJSON.dependencies[electronKey]; - } - } - - return [dev, exact]; -} - export interface ImportOptions { /** * The path to the app to be imported diff --git a/packages/api/core/src/api/make.ts b/packages/api/core/src/api/make.ts index e4d2d0ec32..5f6617872c 100644 --- a/packages/api/core/src/api/make.ts +++ b/packages/api/core/src/api/make.ts @@ -12,7 +12,7 @@ import parseArchs from '../util/parse-archs'; import { readMutatedPackageJson } from '../util/read-package-json'; import resolveDir from '../util/resolve-dir'; import getCurrentOutDir from '../util/out-dir'; -import getElectronVersion from '../util/electron-version'; +import { getElectronVersion } from '../util/electron-version'; import requireSearch from '../util/require-search'; import packager from './package'; diff --git a/packages/api/core/src/api/package.ts b/packages/api/core/src/api/package.ts index 34b45306e1..b60ed0bb4b 100644 --- a/packages/api/core/src/api/package.ts +++ b/packages/api/core/src/api/package.ts @@ -16,7 +16,7 @@ import rebuildHook from '../util/rebuild'; import requireSearch from '../util/require-search'; import resolveDir from '../util/resolve-dir'; import getCurrentOutDir from '../util/out-dir'; -import getElectronVersion from '../util/electron-version'; +import { getElectronVersion } from '../util/electron-version'; const { host: hostArch }: { host: () => ForgeArch | 'all' } = require('electron-download/lib/arch'); diff --git a/packages/api/core/src/api/start.ts b/packages/api/core/src/api/start.ts index ce8e9a47fa..34a0a1f784 100644 --- a/packages/api/core/src/api/start.ts +++ b/packages/api/core/src/api/start.ts @@ -9,7 +9,7 @@ import rebuild from '../util/rebuild'; import resolveDir from '../util/resolve-dir'; import getForgeConfig from '../util/forge-config'; import { runHook } from '../util/hook'; -import getElectronVersion from '../util/electron-version'; +import { getElectronVersion } from '../util/electron-version'; export { StartOptions }; diff --git a/packages/api/core/src/util/electron-version.ts b/packages/api/core/src/util/electron-version.ts index 28c8dacc3f..bca17ab889 100644 --- a/packages/api/core/src/util/electron-version.ts +++ b/packages/api/core/src/util/electron-version.ts @@ -1,6 +1,40 @@ -export default (packageJSON: any) => { - if (!packageJSON.devDependencies) return null; - return (packageJSON.devDependencies['electron-prebuilt-compile'] - || packageJSON.devDependencies['electron-prebuilt'] - || packageJSON.devDependencies.electron); +import debug from 'debug'; + +const d = debug('electron-forge:electron-version'); + +const electronPackageNames = [ + 'electron-prebuilt-compile', + 'electron-prebuilt', + 'electron', +]; + +function findElectronDep(dep: string): boolean { + return electronPackageNames.includes(dep); +} + +export function getElectronVersion (packageJSON: any) { + if (!packageJSON.devDependencies) { + throw new Error('package.json for app does not have any devDependencies'.red); + } + const packageName = electronPackageNames.find(pkg => packageJSON.devDependencies[pkg]); + if (packageName === undefined) { + throw new Error('Could not find any Electron packages in devDependencies'); + } + return packageJSON.devDependencies[packageName]; }; + +export function updateElectronDependency(packageJSON: any, dev: string[], exact: string[]): [string[], string[]] { + if (Object.keys(packageJSON.devDependencies).find(findElectronDep)) { + exact = exact.filter(dep => dep !== 'electron'); + } else { + const electronKey = Object.keys(packageJSON.dependencies).find(findElectronDep); + if (electronKey) { + exact = exact.filter(dep => dep !== 'electron'); + d(`Moving ${electronKey} from dependencies to devDependencies`); + dev.push(`${electronKey}@${packageJSON.dependencies[electronKey]}`); + delete packageJSON.dependencies[electronKey]; + } + } + + return [dev, exact]; +} diff --git a/packages/api/core/src/util/resolve-dir.ts b/packages/api/core/src/util/resolve-dir.ts index 985885dbc1..4eea41f9ea 100644 --- a/packages/api/core/src/util/resolve-dir.ts +++ b/packages/api/core/src/util/resolve-dir.ts @@ -2,7 +2,7 @@ import debug from 'debug'; import fs from 'fs-extra'; import path from 'path'; import { readRawPackageJson } from './read-package-json'; -import getElectronVersion from './electron-version'; +import { getElectronVersion } from './electron-version'; const d = debug('electron-forge:project-resolver'); diff --git a/packages/api/core/test/fast/electron-version_spec.ts b/packages/api/core/test/fast/electron-version_spec.ts new file mode 100644 index 0000000000..c49eec4282 --- /dev/null +++ b/packages/api/core/test/fast/electron-version_spec.ts @@ -0,0 +1,61 @@ +import { expect } from 'chai'; +import { getElectronVersion, updateElectronDependency } from '../../src/util/electron-version'; +import { deps, devDeps, exactDevDeps } from '../../src/api/init-scripts/init-npm'; + +describe.only('updateElectronDependency', () => { + it('adds an Electron dep if one does not already exist', () => { + const packageJSON = { dependencies: {}, devDependencies: {} }; + const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps); + expect(dev).to.deep.equal(devDeps); + expect(exact).to.deep.equal(exactDevDeps); + }); + it('does not add an Electron dep if one already exists', () => { + const packageJSON = { + dependencies: {}, + devDependencies: { electron: '0.37.0' }, + }; + const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps); + expect(dev).to.deep.equal(devDeps); + expect(exact).to.deep.equal([]); + }); + it('moves an Electron dependency from dependencies to devDependencies', () => { + const packageJSON = { + dependencies: { electron: '0.37.0'}, + devDependencies: { }, + }; + const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps); + expect(dev.includes('electron@0.37.0')).to.equal(true); + expect(exact).to.deep.equal([]); + }); +}); + +describe('getElectronVersion', () => { + it('fails without devDependencies', () => { + expect(() => getElectronVersion({})).to.throw('does not have any devDependencies'); + }); + + it('fails without electron devDependencies', () => { + expect(() => getElectronVersion({ devDependencies: {} })).to.throw('Electron packages in devDependencies'); + }); + + it('works with electron-prebuilt-compile', () => { + const packageJSON = { + devDependencies: { 'electron-prebuilt-compile': '1.0.0' }, + }; + expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0'); + }); + + it('works with electron-prebuilt', () => { + const packageJSON = { + devDependencies: { 'electron-prebuilt': '1.0.0' }, + }; + expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0'); + }); + + it('works with electron', () => { + const packageJSON = { + devDependencies: { 'electron': '1.0.0' }, + }; + expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0'); + }); +});