diff --git a/packages/api/core/spec/fast/util/forge-config.spec.ts b/packages/api/core/spec/fast/util/forge-config.spec.ts index 1e82889105..7573abf6fb 100644 --- a/packages/api/core/spec/fast/util/forge-config.spec.ts +++ b/packages/api/core/spec/fast/util/forge-config.spec.ts @@ -6,7 +6,6 @@ import { describe, expect, it, vi } from 'vitest'; import findConfig, { forgeConfigIsValidFilePath, registerForgeConfigForDirectory, - renderConfigTemplate, unregisterForgeConfigForDirectory, } from '../../../src/util/forge-config'; @@ -78,7 +77,7 @@ describe('findConfig', () => { '../../fixture/bad_forge_config', ); const err = - 'Expected packageJSON.config.forge to be an object or point to a requirable JS file'; + 'Expected `config.forge` in package.json to be an object or point to a Forge config file'; await expect(findConfig(fixturePath)).rejects.toThrow(err); }); @@ -90,7 +89,7 @@ describe('findConfig', () => { __dirname, '../../fixture/bad_external_forge_config', ); - const err = /Unexpected token/; + const err = /Failed to parse/; await expect(findConfig(fixturePath)).rejects.toThrow(err); spy.mockRestore(); }); @@ -294,43 +293,32 @@ describe('findConfig', () => { }); }); - describe('alternate config formats', () => { - it('should resolve the yml config from forge.config.yml specified in config.forge', async () => { + describe('TypeScript', () => { + it('should resolve forge.config.ts', async () => { const fixturePath = path.resolve( __dirname, - '../../fixture/dummy_ts_conf', + '../../fixture/dummy_default_ts_conf', ); const conf = await findConfig(fixturePath); - expect(conf.buildIdentifier).toEqual('yml'); + expect(conf.buildIdentifier).toEqual('typescript'); }); - describe('TypeScript', () => { - it('should resolve forge.config.ts', async () => { - const fixturePath = path.resolve( - __dirname, - '../../fixture/dummy_default_ts_conf', - ); - const conf = await findConfig(fixturePath); - expect(conf.buildIdentifier).toEqual('typescript'); - }); - - it('should resolve forge.config.cts', async () => { - const fixturePath = path.resolve( - __dirname, - '../../fixture/dummy_default_cts_conf', - ); - const conf = await findConfig(fixturePath); - expect(conf.buildIdentifier).toEqual('typescript-commonjs'); - }); + it('should resolve forge.config.cts', async () => { + const fixturePath = path.resolve( + __dirname, + '../../fixture/dummy_default_cts_conf', + ); + const conf = await findConfig(fixturePath); + expect(conf.buildIdentifier).toEqual('typescript-commonjs'); + }); - it('should resolve forge.config.mts', async () => { - const fixturePath = path.resolve( - __dirname, - '../../fixture/dummy_default_mts_conf', - ); - const conf = await findConfig(fixturePath); - expect(conf.buildIdentifier).toEqual('typescript-esm'); - }); + it('should resolve forge.config.mts', async () => { + const fixturePath = path.resolve( + __dirname, + '../../fixture/dummy_default_mts_conf', + ); + const conf = await findConfig(fixturePath); + expect(conf.buildIdentifier).toEqual('typescript-esm'); }); }); }); @@ -366,28 +354,13 @@ describe('forgeConfigIsValidFilePath', () => { const fixturePath = path.resolve(__dirname, '../../fixture/dummy_js_conf/'); await expect( forgeConfigIsValidFilePath(fixturePath, 'forge.different.config'), - ).resolves.toEqual(true); + ).toEqual(true); }); it('fails when a file is nonexistent', async () => { const fixturePath = path.resolve(__dirname, '../../fixture/dummy_js_conf/'); await expect( forgeConfigIsValidFilePath(fixturePath, 'forge.nonexistent.config'), - ).resolves.toEqual(false); - }); -}); - -describe('renderConfigTemplate', () => { - it('should import a JS file when a string starts with "require:"', () => { - const dir = path.resolve(__dirname, '../../fixture/dummy_js_conf'); - const config = { - foo: 'require:foo', - }; - renderConfigTemplate(dir, {}, config); - expect(config.foo).toEqual({ - bar: { - baz: 'quux', - }, - }); + ).toEqual(false); }); }); diff --git a/packages/api/core/spec/fixture/dummy_ts_conf/forge.config.yml b/packages/api/core/spec/fixture/dummy_ts_conf/forge.config.yml deleted file mode 100644 index ee69896efa..0000000000 --- a/packages/api/core/spec/fixture/dummy_ts_conf/forge.config.yml +++ /dev/null @@ -1 +0,0 @@ -buildIdentifier: 'yml' diff --git a/packages/api/core/spec/fixture/dummy_ts_conf/package.json b/packages/api/core/spec/fixture/dummy_ts_conf/package.json deleted file mode 100644 index fe524262eb..0000000000 --- a/packages/api/core/spec/fixture/dummy_ts_conf/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "", - "productName": "", - "version": "1.0.0", - "description": "", - "main": "src/index.js", - "scripts": { - "start": "electron-forge start" - }, - "keywords": [], - "author": "", - "license": "MIT", - "config": { - "forge": "./forge.config.yml" - }, - "devDependencies": { - "@electron-forge/shared-types": "*", - "electron": "99.99.99" - } -} diff --git a/packages/api/core/src/util/forge-config.ts b/packages/api/core/src/util/forge-config.ts index 91608300fb..251160d063 100644 --- a/packages/api/core/src/util/forge-config.ts +++ b/packages/api/core/src/util/forge-config.ts @@ -2,10 +2,7 @@ import path from 'node:path'; import { ForgeConfig, ResolvedForgeConfig } from '@electron-forge/shared-types'; import fs from 'fs-extra'; -import * as interpret from 'interpret'; import { createJiti } from 'jiti'; -import { template } from 'lodash'; -import * as rechoir from 'rechoir'; // eslint-disable-next-line n/no-missing-import import { dynamicImportMaybe } from '../../helper/dynamic-import.js'; @@ -126,36 +123,17 @@ export function fromBuildIdentifier( }; } -export async function forgeConfigIsValidFilePath( +export function forgeConfigIsValidFilePath( dir: string, forgeConfig: string | ForgeConfig, -): Promise { +): boolean { return ( typeof forgeConfig === 'string' && - ((await fs.pathExists(path.resolve(dir, forgeConfig))) || - fs.pathExists(path.resolve(dir, `${forgeConfig}.js`))) + (fs.existsSync(path.resolve(dir, forgeConfig)) || + fs.existsSync(path.resolve(dir, `${forgeConfig}.js`))) ); } -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function renderConfigTemplate( - dir: string, - templateObj: any, - obj: any, -): void { - for (const [key, value] of Object.entries(obj)) { - if (typeof value === 'object' && value !== null) { - renderConfigTemplate(dir, templateObj, value); - } else if (typeof value === 'string') { - obj[key] = template(value)(templateObj); - if (obj[key].startsWith('require:')) { - // eslint-disable-next-line @typescript-eslint/no-require-imports - obj[key] = require(path.resolve(dir, obj[key].substr(8))); - } - } - } -} - type MaybeESM = T | { default: T }; type AsyncForgeConfigGenerator = () => Promise; @@ -172,18 +150,9 @@ export default async (dir: string): Promise => { } if (!forgeConfig || typeof forgeConfig === 'string') { - // interpret.extensions doesn't support `.mts` files - for (const extension of [ - '.js', - '.mts', - ...Object.keys(interpret.extensions), - ]) { + for (const extension of ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts']) { const pathToConfig = path.resolve(dir, `forge.config${extension}`); - if (await fs.pathExists(pathToConfig)) { - // Use rechoir to parse alternative syntaxes (except for TypeScript where we use jiti) - if (!['.cts', '.mts', '.ts'].includes(extension)) { - rechoir.prepare(interpret.extensions, pathToConfig, dir); - } + if (fs.existsSync(pathToConfig)) { forgeConfig = `forge.config${extension}`; break; } @@ -191,8 +160,11 @@ export default async (dir: string): Promise => { } forgeConfig = forgeConfig || ({} as ForgeConfig); - if (await forgeConfigIsValidFilePath(dir, forgeConfig)) { - const forgeConfigPath = path.resolve(dir, forgeConfig as string); + if ( + typeof forgeConfig === 'string' && + forgeConfigIsValidFilePath(dir, forgeConfig) + ) { + const forgeConfigPath = path.resolve(dir, forgeConfig); try { let loadFn; if (['.cts', '.mts', '.ts'].includes(path.extname(forgeConfigPath))) { @@ -216,7 +188,7 @@ export default async (dir: string): Promise => { } } else if (typeof forgeConfig !== 'object') { throw new Error( - 'Expected packageJSON.config.forge to be an object or point to a requirable JS file', + 'Expected `config.forge` in package.json to be an object or point to a Forge config file', ); } const defaultForgeConfig = { @@ -233,9 +205,6 @@ export default async (dir: string): Promise => { pluginInterface: null as any, }; - const templateObj = { ...packageJSON, year: new Date().getFullYear() }; - renderConfigTemplate(dir, templateObj, resolvedForgeConfig); - resolvedForgeConfig.pluginInterface = await PluginInterface.create( dir, resolvedForgeConfig,