Skip to content

Commit

Permalink
chore: make templates work in v6
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Oct 22, 2018
1 parent cddfb1f commit 22549d9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 30 deletions.
6 changes: 3 additions & 3 deletions packages/api/core/src/api/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions packages/api/core/src/api/init-scripts/init-custom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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';
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');

Expand Down Expand Up @@ -41,15 +43,15 @@ export default async (dir: string, template: string) => {
}
});

let templateModule = require(templateModulePath);
let templateModule: PossibleModule<ForgeTemplate> = require(templateModulePath);

templateModule = templateModule.default || templateModule;

await asyncOra('Installing Template Dependencies', async () => {
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 () => {
Expand All @@ -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));
}
};
8 changes: 4 additions & 4 deletions packages/api/core/src/api/init-scripts/init-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
});
};
26 changes: 18 additions & 8 deletions packages/api/core/src/util/install-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@ 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();
}
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 {
Expand Down
14 changes: 7 additions & 7 deletions packages/api/core/test/fast/install-dependencies_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
});
});
Expand All @@ -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']);
});
});
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"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"
},
"engines": {
"node": ">= 6.0"
}
}
}
10 changes: 8 additions & 2 deletions packages/utils/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -116,3 +115,10 @@ export interface StartOptions {
*/
inspect?: boolean;
}

export interface ForgeTemplate {
dependencies?: string[];
devDependencies?: string[];
templateDirectory?: string;
postCopy?: (dir: string) => void;
}

0 comments on commit 22549d9

Please sign in to comment.