Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(init): remove unmaintained resolve-package #3812

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"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 All @@ -67,7 +68,6 @@
"node-fetch": "^2.6.7",
"parse-author": "^2.0.0",
"rechoir": "^0.8.0",
"resolve-package": "^1.0.1",
"semver": "^7.2.1",
"source-map-support": "^0.5.13",
"sudo-prompt": "^9.1.1",
Expand Down
5 changes: 3 additions & 2 deletions packages/api/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
"@electron-forge/maker-squirrel": "7.6.0",
"@electron-forge/maker-wix": "7.6.0",
"@electron-forge/maker-zip": "7.6.0",
"@electron-forge/template-fixture": "file:./spec/fixture/electron-forge-template-fixture",
"@electron-forge/test-utils": "7.6.0",
"@types/interpret": "^1.1.1",
"@types/progress": "^2.0.5",
"@types/rechoir": "^0.6.1",
"cross-env": "^7.0.2",
"electron-forge-template-fixture-two": "file:./spec/fixture/electron-forge-template-fixture",
"electron-installer-common": "^0.10.2",
"vitest": "^2.1.6",
"yaml-hook": "^1.0.0"
Expand Down Expand Up @@ -49,14 +50,14 @@
"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",
"listr2": "^7.0.2",
"lodash": "^4.17.20",
"log-symbols": "^4.0.0",
"node-fetch": "^2.6.7",
"rechoir": "^0.8.0",
"resolve-package": "^1.0.1",
"semver": "^7.2.1",
"source-map-support": "^0.5.13",
"sudo-prompt": "^9.1.1",
Expand Down
57 changes: 57 additions & 0 deletions packages/api/core/spec/fast/find-template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from 'node:path';

import { describe, expect, it, vi } from 'vitest';

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

describe('findTemplate', () => {
/**
* Note: this test suite does not mock `require.resolve`. Instead, it uses
* fixture dependencies defined in this module's package.json file to
* actually resolve a local template.
*
* If you modify the fixtures, you may need to re-run `yarn install` in order
* for the fixtures to be installed in your local `node_modules`.
*/
describe('local modules', () => {
it('should find an @electron-forge/template based on name', async () => {
await expect(findTemplate('fixture')).resolves.toEqual({ name: 'electron-forge-template-fixture' });
});
it('should find an @electron-forge/template based on name', async () => {
await expect(findTemplate('fixture-two')).resolves.toEqual({ name: 'electron-forge-template-fixture' });
});
it('should find an @electron-forge/template based on name', async () => {
await expect(findTemplate('electron-forge-template-fixture-two')).resolves.toEqual({ name: 'electron-forge-template-fixture' });
});
});

/**
* 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', () => {
vi.mock(import('global-dirs'), async (importOriginal) => {
const mod = await importOriginal();
return {
default: {
...mod.default,
npm: {
...mod.default.npm,
packages: path.resolve(__dirname, '..', 'fixture', 'global-stub', 'node_modules'),
},
},
};
});
it('should find an @electron-forge/template based on name', async () => {
await expect(findTemplate('global')).resolves.toEqual({ name: 'electron-forge-template-fixture-global' });
});
it('should find an electron-forge-template based on name', async () => {
await expect(findTemplate('global-two')).resolves.toEqual({ name: 'electron-forge-template-fixture-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".');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { name: 'electron-forge-template-fixture' };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"main": "index.js",
"type": "commonjs",
"version": "13.3.7"
}

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.

32 changes: 19 additions & 13 deletions packages/api/core/src/api/init-scripts/find-template.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import { ForgeTemplate } from '@electron-forge/shared-types';
import debug from 'debug';
import resolvePackage from 'resolve-package';
import globalDirs from 'global-dirs';

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

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

export const findTemplate = async (dir: string, template: string): Promise<ForgeTemplate> => {
enum TemplateType {
global = 'global',
local = 'local',
}

export const findTemplate = async (template: string): Promise<ForgeTemplate> => {
let templateModulePath!: string;
const resolveTemplateTypes = [
['global', `electron-forge-template-${template}`],
['global', `@electron-forge/template-${template}`],
['local', `electron-forge-template-${template}`],
['local', `@electron-forge/template-${template}`],
['local', template],
[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],
];
let foundTemplate = false;
for (const [templateType, moduleName] of resolveTemplateTypes) {
try {
d(`Trying ${templateType} template: ${moduleName}`);
if (templateType === 'global') {
templateModulePath = await resolvePackage(moduleName);
if (templateType === TemplateType.global) {
templateModulePath = require.resolve(moduleName, {
paths: [globalDirs.npm.packages, globalDirs.yarn.packages],
});
} else {
// local
templateModulePath = require.resolve(moduleName);
}
foundTemplate = true;
Expand All @@ -37,8 +44,7 @@ export const findTemplate = async (dir: string, template: string): Promise<Forge

d(`found template module at: ${templateModulePath}`);

// eslint-disable-next-line @typescript-eslint/no-require-imports
const templateModule: PossibleModule<ForgeTemplate> = require(templateModulePath);
const templateModule: PossibleModule<ForgeTemplate> = await import(templateModulePath);

return templateModule.default || templateModule;
return templateModule.default ?? templateModule;
};
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 @@ -65,7 +65,7 @@ export default async ({ dir = process.cwd(), interactive = false, copyCIFiles =
{
title: `Locating custom template: "${template}"`,
task: async (ctx) => {
ctx.templateModule = await findTemplate(dir, template);
ctx.templateModule = await findTemplate(template);
},
},
{
Expand Down
4 changes: 0 additions & 4 deletions typings/resolve-package/index.d.ts

This file was deleted.

91 changes: 18 additions & 73 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,9 @@
dependencies:
fast-check "^3.12.0"

"@electron-forge/template-fixture@file:./packages/api/core/spec/fixture/electron-forge-template-fixture":
version "13.3.7"

"@electron/asar@^3.0.3", "@electron/asar@^3.2.1", "@electron/asar@^3.2.7":
version "3.2.10"
resolved "https://registry.yarnpkg.com/@electron/asar/-/asar-3.2.10.tgz#615cf346b734b23cafa4e0603551010bd0e50aa8"
Expand Down Expand Up @@ -5018,13 +5021,6 @@ cross-dirname@^0.1.0:
resolved "https://registry.yarnpkg.com/cross-dirname/-/cross-dirname-0.1.0.tgz#b899599f30a5389f59e78c150e19f957ad16a37c"
integrity sha512-+R08/oI0nl3vfPcqftZRpytksBXDzOUveBq/NBVx0sUp1axwzPQrKinNx5yd5sxPu8j1wIy8AfnVQ+5eFdha6Q==

cross-env@^7.0.2:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
dependencies:
cross-spawn "^7.0.1"

cross-spawn-windows-exe@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/cross-spawn-windows-exe/-/cross-spawn-windows-exe-1.2.0.tgz#46253b0f497676e766faf4a7061004618b5ac5ec"
Expand Down Expand Up @@ -5536,6 +5532,9 @@ ejs@^3.1.7:
dependencies:
jake "^10.8.5"

"electron-forge-template-fixture-two@file:./packages/api/core/spec/fixture/electron-forge-template-fixture":
version "13.3.7"

electron-installer-common@^0.10.0, electron-installer-common@^0.10.2:
version "0.10.3"
resolved "https://registry.yarnpkg.com/electron-installer-common/-/electron-installer-common-0.10.3.tgz#40f9db644ca60eb28673d545b67ee0113aef4444"
Expand Down Expand Up @@ -6688,13 +6687,6 @@ execa@^5.0.0, execa@^5.1.1:
signal-exit "^3.0.3"
strip-final-newline "^2.0.0"

expand-tilde@^2.0.0, expand-tilde@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/expand-tilde/-/expand-tilde-2.0.2.tgz#97e801aa052df02454de46b02bf621642cdc8502"
integrity sha1-l+gBqgUt8CRU3kawK/YhZCzchQI=
dependencies:
homedir-polyfill "^1.0.1"

expect-type@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-1.1.0.tgz#a146e414250d13dfc49eafcfd1344a4060fa4c75"
Expand Down Expand Up @@ -7351,13 +7343,6 @@ get-folder-size@^2.0.1:
gar "^1.0.4"
tiny-each-async "2.0.3"

get-installed-path@^2.0.3:
version "2.1.1"
resolved "https://registry.yarnpkg.com/get-installed-path/-/get-installed-path-2.1.1.tgz#a1f33dc6b8af542c9331084e8edbe37fe2634152"
integrity sha512-Qkn9eq6tW5/q9BDVdMpB8tOHljX9OSP0jRC5TRNVA4qRc839t4g8KQaR8t0Uv0EFVL0MlyG7m/ofjEgAROtYsA==
dependencies:
global-modules "1.0.0"

get-intrinsic@^1.0.2, get-intrinsic@^1.1.0, get-intrinsic@^1.1.1, get-intrinsic@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/get-intrinsic/-/get-intrinsic-1.1.3.tgz#063c84329ad93e83893c7f4f243ef63ffa351385"
Expand Down Expand Up @@ -7649,25 +7634,12 @@ global-agent@^3.0.0:
semver "^7.3.2"
serialize-error "^7.0.1"

[email protected], global-modules@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-1.0.0.tgz#6d770f0eb523ac78164d72b5e71a8877265cc3ea"
integrity sha512-sKzpEkf11GpOFuw0Zzjzmt4B4UZwjOcG757PPvrfhxcLFbq0wpsgpOqxpxtxFiCG4DtG93M6XRVbF2oGdev7bg==
dependencies:
global-prefix "^1.0.1"
is-windows "^1.0.1"
resolve-dir "^1.0.0"

global-prefix@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/global-prefix/-/global-prefix-1.0.2.tgz#dbf743c6c14992593c655568cb66ed32c0122ebe"
integrity sha1-2/dDxsFJklk8ZVVoy2btMsASLr4=
global-dirs@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-3.0.1.tgz#0c488971f066baceda21447aecb1a8b911d22485"
integrity sha512-NBcGGFbBA9s1VzD41QXDG+3++t9Mn5t1FpLdhESY6oKY4gYTFpX4wO3sqGUa0Srjtbfj3szX0RnemmrVRUdULA==
dependencies:
expand-tilde "^2.0.2"
homedir-polyfill "^1.0.1"
ini "^1.3.4"
is-windows "^1.0.1"
which "^1.2.14"
ini "2.0.0"

globals@^11.1.0:
version "11.12.0"
Expand Down Expand Up @@ -7907,13 +7879,6 @@ headers-polyfill@^4.0.2:
resolved "https://registry.yarnpkg.com/headers-polyfill/-/headers-polyfill-4.0.3.tgz#922a0155de30ecc1f785bcf04be77844ca95ad07"
integrity sha512-IScLbePpkvO846sIwOtOTDjutRMWdXdJmXdMvk6gCBHxFO8d+QKOQedyZSxFTTFYRSmlgSTDtXqqq4pcenBXLQ==

homedir-polyfill@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz#743298cef4e5af3e194161fbadcc2151d3a058e8"
integrity sha512-eSmmWE5bZTK2Nou4g0AI3zZ9rswp7GRKoKXS1BLUkvPviOqs4YTN1djQIqrXy9k5gEtdLPy86JjRwsNM9tnDcA==
dependencies:
parse-passwd "^1.0.0"

hosted-git-info@^2.1.4:
version "2.8.9"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
Expand Down Expand Up @@ -8229,7 +8194,12 @@ [email protected]:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==

ini@^1.3.2, ini@^1.3.4, ini@^1.3.8:
[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ini/-/ini-2.0.0.tgz#e5fd556ecdd5726be978fa1001862eacb0a94bc5"
integrity sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA==

ini@^1.3.2, ini@^1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==
Expand Down Expand Up @@ -8759,11 +8729,6 @@ is-weakset@^2.0.3:
call-bound "^1.0.3"
get-intrinsic "^1.2.6"

is-windows@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==

is-wsl@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-2.2.0.tgz#74a4c76e77ca9fd3f932f290c17ea326cd157271"
Expand Down Expand Up @@ -11004,11 +10969,6 @@ parse-json@^5.0.0, parse-json@^5.2.0:
json-parse-even-better-errors "^2.3.0"
lines-and-columns "^1.1.6"

parse-passwd@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6"
integrity sha1-bVuTSkVpk7I9N/QKOC1vFmao5cY=

parse-path@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/parse-path/-/parse-path-7.0.0.tgz#605a2d58d0a749c8594405d8cc3a2bf76d16099b"
Expand Down Expand Up @@ -11726,14 +11686,6 @@ resolve-cwd@^3.0.0:
dependencies:
resolve-from "^5.0.0"

resolve-dir@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-1.0.1.tgz#79a40644c362be82f26effe739c9bb5382046f43"
integrity sha1-eaQGRMNivoLybv/nOcm7U4IEb0M=
dependencies:
expand-tilde "^2.0.0"
global-modules "^1.0.0"

[email protected], resolve-from@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
Expand All @@ -11744,13 +11696,6 @@ resolve-from@^4.0.0:
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6"
integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==

resolve-package@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/resolve-package/-/resolve-package-1.0.1.tgz#686f70b188bd7d675f5bbc4282ccda060abb9d27"
integrity sha1-aG9wsYi9fWdfW7xCgszaBgq7nSc=
dependencies:
get-installed-path "^2.0.3"

resolve-pkg-maps@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz#616b3dc2c57056b5588c31cdf4b3d64db133720f"
Expand Down Expand Up @@ -13931,7 +13876,7 @@ which-typed-array@^1.1.9:
has-tostringtag "^1.0.0"
is-typed-array "^1.1.10"

which@^1.2.14, which@^1.2.4, which@^1.2.9:
which@^1.2.4, which@^1.2.9:
version "1.3.1"
resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==
Expand Down
Loading