diff --git a/packages/api/core/src/api/import.ts b/packages/api/core/src/api/import.ts index 9eb38e1c0..fc131b0b1 100644 --- a/packages/api/core/src/api/import.ts +++ b/packages/api/core/src/api/import.ts @@ -8,7 +8,7 @@ import { deps, devDeps, exactDevDeps } from './init-scripts/init-npm'; import { setInitialForgeConfig } from '../util/forge-config'; import { info, warn } from '../util/messages'; -import installDepList from '../util/install-dependencies'; +import installDepList, { DepType, DepVersionRestriction } from '../util/install-dependencies'; import { readRawPackageJson } from '../util/read-package-json'; const d = debug('electron-forge:import'); @@ -155,10 +155,10 @@ export default async ({ await installDepList(dir, deps); d('installing devDependencies'); - await installDepList(dir, devDeps, true); + await installDepList(dir, devDeps, DepType.DEV); d('installing exactDevDependencies'); - await installDepList(dir, exactDevDeps, true, true); + await installDepList(dir, exactDevDeps, DepType.DEV, DepVersionRestriction.EXACT); }); packageJSON = await readRawPackageJson(dir); diff --git a/packages/api/core/src/api/init-scripts/init-custom.ts b/packages/api/core/src/api/init-scripts/init-custom.ts index fc0eab259..c3e81512b 100644 --- a/packages/api/core/src/api/init-scripts/init-custom.ts +++ b/packages/api/core/src/api/init-scripts/init-custom.ts @@ -1,4 +1,4 @@ -import { asyncOra, ora } from '@electron-forge/async-ora'; +import { asyncOra } from '@electron-forge/async-ora'; import debug from 'debug'; import fs from 'fs-extra'; import glob from 'glob'; @@ -6,7 +6,9 @@ import resolvePackage from 'resolve-package'; import path from 'path'; import { copy } from './init-starter-files'; -import installDepList from '../../util/install-dependencies'; +import installDepList, { DepType } from '../../util/install-dependencies'; +import { PossibleModule } from '../../util/require-search'; +import { ForgeTemplate } from '@electron-forge/shared-types'; const d = debug('electron-forge:init:custom'); @@ -41,7 +43,7 @@ export default async (dir: string, template: string) => { } }); - let templateModule = require(templateModulePath); + let templateModule: PossibleModule = require(templateModulePath); templateModule = templateModule.default || templateModule; @@ -49,7 +51,7 @@ export default async (dir: string, template: string) => { d('installing dependencies'); await installDepList(dir, templateModule.dependencies || []); d('installing devDependencies'); - await installDepList(dir, templateModule.devDependencies || [], true); + await installDepList(dir, templateModule.devDependencies || [], DepType.DEV); }); await asyncOra('Copying Template Files', async () => { @@ -71,6 +73,6 @@ export default async (dir: string, template: string) => { }); if (typeof templateModule.postCopy === 'function') { - await Promise.resolve(templateModule.postCopy(dir, ora)); + await Promise.resolve(templateModule.postCopy(dir)); } }; diff --git a/packages/api/core/src/api/init-scripts/init-npm.ts b/packages/api/core/src/api/init-scripts/init-npm.ts index 51b6c19cb..d9cfb60f0 100644 --- a/packages/api/core/src/api/init-scripts/init-npm.ts +++ b/packages/api/core/src/api/init-scripts/init-npm.ts @@ -5,7 +5,7 @@ import path from 'path'; import username from 'username'; import { setInitialForgeConfig } from '../../util/forge-config'; -import installDepList from '../../util/install-dependencies'; +import installDepList, { DepType, DepVersionRestriction } from '../../util/install-dependencies'; import { readRawPackageJson } from '../../util/read-package-json'; const d = debug('electron-forge:init:npm'); @@ -43,11 +43,11 @@ export default async (dir: string) => { await installDepList(dir, deps); d('installing devDependencies'); - await installDepList(dir, devDeps, true); + await installDepList(dir, devDeps, DepType.DEV); - d('installing exact dependencies'); + d('installing exact devDependencies'); for (const packageName of exactDevDeps) { - await installDepList(dir, [packageName], true, true); + await installDepList(dir, [packageName], DepType.DEV, DepVersionRestriction.EXACT); } }); }; diff --git a/packages/api/core/src/util/install-dependencies.ts b/packages/api/core/src/util/install-dependencies.ts index dd7e4b7aa..e11f5050c 100644 --- a/packages/api/core/src/util/install-dependencies.ts +++ b/packages/api/core/src/util/install-dependencies.ts @@ -3,13 +3,23 @@ import { yarnOrNpmSpawn, hasYarn } from './yarn-or-npm'; const d = debug('electron-forge:dependency-installer'); +export enum DepType { + PROD = 'PROD', + DEV = 'DEV', +} + +export enum DepVersionRestriction { + EXACT = 'EXACT', + RANGE = 'RANGE', +} + export default async ( dir: string, deps: string[], - areDev = false, - exact = false, + depType = DepType.PROD, + versionRestriction = DepVersionRestriction.RANGE, ) => { - d('installing', JSON.stringify(deps), 'in:', dir, `dev=${areDev},exact=${exact},withYarn=${hasYarn()}`); + d('installing', JSON.stringify(deps), 'in:', dir, `depType=${depType},versionRestriction=${versionRestriction},withYarn=${hasYarn()}`); if (deps.length === 0) { d('nothing to install, stopping immediately'); return Promise.resolve(); @@ -17,12 +27,12 @@ export default async ( let cmd = ['install'].concat(deps); if (hasYarn()) { cmd = ['add'].concat(deps); - if (areDev) cmd.push('--dev'); - if (exact) cmd.push('--exact'); + if (depType === DepType.DEV) cmd.push('--dev'); + if (versionRestriction === DepVersionRestriction.EXACT) cmd.push('--exact'); } else { - if (exact) cmd.push('--save-exact'); - if (areDev) cmd.push('--save-dev'); - if (!areDev) cmd.push('--save'); + if (versionRestriction === DepVersionRestriction.EXACT) cmd.push('--save-exact'); + if (depType === DepType.DEV) cmd.push('--save-dev'); + if (depType === DepType.PROD) cmd.push('--save'); } d('executing', JSON.stringify(cmd), 'in:', dir); try { diff --git a/packages/api/core/test/fast/install-dependencies_spec.ts b/packages/api/core/test/fast/install-dependencies_spec.ts index c5ee3427c..3827cc893 100644 --- a/packages/api/core/test/fast/install-dependencies_spec.ts +++ b/packages/api/core/test/fast/install-dependencies_spec.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import proxyquire from 'proxyquire'; import sinon, { SinonStub } from 'sinon'; -import installDependencies from '../../src/util/install-dependencies'; +import installDependencies, { DepType, DepVersionRestriction } from '../../src/util/install-dependencies'; describe('Install dependencies', () => { let install: typeof installDependencies; @@ -58,17 +58,17 @@ describe('Install dependencies', () => { }); it('should install dev deps', () => { - install('mydir', ['eslint'], true); + install('mydir', ['eslint'], DepType.DEV); expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'eslint', '--dev']); }); it('should install exact deps', () => { - install('mydir', ['react-dom'], false, true); + install('mydir', ['react-dom'], DepType.PROD, DepVersionRestriction.EXACT); expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react-dom', '--exact']); }); it('should install exact dev deps', () => { - install('mydir', ['mocha'], true, true); + install('mydir', ['mocha'], DepType.DEV, DepVersionRestriction.EXACT); expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'mocha', '--dev', '--exact']); }); }); @@ -84,17 +84,17 @@ describe('Install dependencies', () => { }); it('should install dev deps', () => { - install('mydir', ['eslint'], true); + install('mydir', ['eslint'], DepType.DEV); expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'eslint', '--save-dev']); }); it('should install exact deps', () => { - install('mydir', ['react-dom'], false, true); + install('mydir', ['react-dom'], DepType.PROD, DepVersionRestriction.EXACT); expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'react-dom', '--save-exact', '--save']); }); it('should install exact dev deps', () => { - install('mydir', ['mocha'], true, true); + install('mydir', ['mocha'], DepType.DEV, DepVersionRestriction.EXACT); expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'mocha', '--save-exact', '--save-dev']); }); }); diff --git a/packages/utils/types/package.json b/packages/utils/types/package.json index 569aa445f..1722aa011 100644 --- a/packages/utils/types/package.json +++ b/packages/utils/types/package.json @@ -11,6 +11,7 @@ "test": "echo No Tests For Shared Types" }, "dependencies": { + "@electron-forge/async-ora": "^6.0.0-beta.29", "@types/electron-packager": "^12.0.0", "electron-rebuild": "^1.6.0", "ora": "^3.0.0" @@ -18,4 +19,4 @@ "engines": { "node": ">= 6.0" } -} +} \ No newline at end of file diff --git a/packages/utils/types/src/index.ts b/packages/utils/types/src/index.ts index 5ad5a248f..069033ad5 100644 --- a/packages/utils/types/src/index.ts +++ b/packages/utils/types/src/index.ts @@ -1,5 +1,4 @@ -/* tslint:disable ter-indent */ - +import { OraImpl } from '@electron-forge/async-ora'; import { ChildProcess } from 'child_process'; import { Options } from 'electron-packager'; import { RebuildOptions } from 'electron-rebuild/lib/src/rebuild'; @@ -116,3 +115,10 @@ export interface StartOptions { */ inspect?: boolean; } + +export interface ForgeTemplate { + dependencies?: string[]; + devDependencies?: string[]; + templateDirectory?: string; + postCopy?: (dir: string) => void; +}