Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"find-up": "^5.0.0",
"form-data": "^4.0.0",
"fs-extra": "^10.0.0",
"global-dirs": "^3.0.0",
"got": "^11.8.5",
"html-webpack-plugin": "^5.5.3",
"interpret": "^3.1.1",
Expand Down
1 change: 0 additions & 1 deletion packages/api/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"filenamify": "^4.1.0",
"find-up": "^5.0.0",
"fs-extra": "^10.0.0",
"global-dirs": "^3.0.0",
"got": "^11.8.5",
"interpret": "^3.1.1",
"jiti": "^2.4.2",
Expand Down
42 changes: 1 addition & 41 deletions packages/api/core/spec/fast/find-template.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import path from 'node:path';

import globalDirs from 'global-dirs';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { describe, expect, it } from 'vitest';

import { findTemplate } from '../../src/api/init-scripts/find-template';

Expand Down Expand Up @@ -46,43 +43,6 @@ describe('findTemplate', () => {
});
});

/**
* For global modules, we re-route the global NPM `node_modules` directory to
* a folder in our fixture directory. Note that the folder _needs_ to be called
* `node_modules` in order for the `require.resolve` custom path to work.
*/
describe('global modules', () => {
beforeEach(() => {
vi.spyOn(globalDirs, 'npm', 'get').mockReturnValue({
binaries: '',
prefix: '',
packages: path.resolve(
__dirname,
'..',
'fixture',
'global-stub',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: let's delete this fixture folder since it's no longer used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the folder actually exists. IIRC I just had to wire up the mock this way so that npm had something to work with.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think VSCode code search failed me because of node_modules being in the path there. I'll fix that ASAP. 😓

'node_modules',
),
});
});
it('should find an @electron-forge/template based on name', async () => {
await expect(findTemplate('global')).resolves.toEqual(
expect.objectContaining({
template: { name: 'electron-forge-template-fixture-global' },
type: 'global',
}),
);
});
it('should find an electron-forge-template based on name', async () => {
await expect(findTemplate('global-two')).resolves.toEqual(
expect.objectContaining({
template: { name: 'electron-forge-template-fixture-global' },
type: 'global',
}),
);
});
});

it('should error if there are no valid templates', async () => {
await expect(findTemplate('non-existent-template')).rejects.toThrowError(
'Failed to locate custom template: "non-existent-template".',
Expand Down
31 changes: 6 additions & 25 deletions packages/api/core/src/api/init-scripts/find-template.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { ForgeTemplate } from '@electron-forge/shared-types';
import debug from 'debug';
import globalDirs from 'global-dirs';

import { PossibleModule } from '../../util/import-search';

const d = debug('electron-forge:init:find-template');

enum TemplateType {
global = 'global',
local = 'local',
}

export interface ForgeTemplateDetails {
name: string;
path: string;
template: ForgeTemplate;
type: TemplateType;
}

export const findTemplate = async (
Expand All @@ -24,27 +17,15 @@ export const findTemplate = async (
let foundTemplate: Omit<ForgeTemplateDetails, 'template'> | null = null;

const resolveTemplateTypes = [
[TemplateType.global, `electron-forge-template-${template}`],
[TemplateType.global, `@electron-forge/template-${template}`],
[TemplateType.local, `electron-forge-template-${template}`],
[TemplateType.local, `@electron-forge/template-${template}`],
[TemplateType.global, template],
[TemplateType.local, template],
`electron-forge-template-${template}`,
`@electron-forge/template-${template}`,
template,
] as const;
for (const [templateType, moduleName] of resolveTemplateTypes) {
for (const moduleName of resolveTemplateTypes) {
try {
d(`Trying ${templateType} template: ${moduleName}`);
let templateModulePath: string;
if (templateType === TemplateType.global) {
templateModulePath = require.resolve(moduleName, {
paths: [globalDirs.npm.packages, globalDirs.yarn.packages],
});
} else {
templateModulePath = require.resolve(moduleName);
}
d(`Trying template: ${moduleName}`);
foundTemplate = {
path: templateModulePath,
type: templateType,
path: require.resolve(moduleName),
name: moduleName,
};
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default async ({
task: async (ctx, task) => {
const tmpl = await findTemplate(template);
ctx.templateModule = tmpl.template;
task.output = `Using ${chalk.green(tmpl.name)} (${tmpl.type} module)`;
task.output = `Using ${chalk.green(tmpl.name)}`;
},
rendererOptions: { persistentOutput: true },
},
Expand Down
18 changes: 0 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,6 @@ __metadata:
filenamify: "npm:^4.1.0"
find-up: "npm:^5.0.0"
fs-extra: "npm:^10.0.0"
global-dirs: "npm:^3.0.0"
got: "npm:^11.8.5"
interpret: "npm:^3.1.1"
jiti: "npm:^2.4.2"
Expand Down Expand Up @@ -8153,7 +8152,6 @@ __metadata:
fork-ts-checker-webpack-plugin: "npm:^7.2.13"
form-data: "npm:^4.0.0"
fs-extra: "npm:^10.0.0"
global-dirs: "npm:^3.0.0"
got: "npm:^11.8.5"
html-webpack-plugin: "npm:^5.5.3"
husky: "npm:^7.0.1"
Expand Down Expand Up @@ -10745,15 +10743,6 @@ __metadata:
languageName: node
linkType: hard

"global-dirs@npm:^3.0.0":
version: 3.0.1
resolution: "global-dirs@npm:3.0.1"
dependencies:
ini: "npm:2.0.0"
checksum: 10c0/ef65e2241a47ff978f7006a641302bc7f4c03dfb98783d42bf7224c136e3a06df046e70ee3a010cf30214114755e46c9eb5eb1513838812fbbe0d92b14c25080
languageName: node
linkType: hard

"globals@npm:^13.19.0":
version: 13.20.0
resolution: "globals@npm:13.20.0"
Expand Down Expand Up @@ -11433,13 +11422,6 @@ __metadata:
languageName: node
linkType: hard

"ini@npm:2.0.0":
version: 2.0.0
resolution: "ini@npm:2.0.0"
checksum: 10c0/2e0c8f386369139029da87819438b20a1ff3fe58372d93fb1a86e9d9344125ace3a806b8ec4eb160a46e64cbc422fe68251869441676af49b7fc441af2389c25
languageName: node
linkType: hard

"ini@npm:^1.3.2, ini@npm:^1.3.8":
version: 1.3.8
resolution: "ini@npm:1.3.8"
Expand Down
Loading