feat(package): add support for async Forge configs (#3288)

* feat: add support for async Forge configs

* chore: improve typing for async fn

* build: check function after loading "default"

* build: improve typings

---------

Co-authored-by: Erick Zhao <erick@hotmail.ca>
This commit is contained in:
Keeley Hammond 2023-08-08 13:47:47 -07:00 коммит произвёл GitHub
Родитель bdc9108f17
Коммит f6dce52386
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 48 добавлений и 2 удалений

Просмотреть файл

@ -60,6 +60,13 @@ for versions 6.0.1 and up.
LINK_FORGE_DEPENDENCIES_ON_INIT=1 node path/to/forge/packages/api/cli/dist/electron-forge-init.js my-app
```
To link an existing project to your local Forge packages, use the `yarn link:prepare` command as listed
above, and then run the following command in your project:
```sh
yarn link @electron-forge/core --link-folder=path/to/forge/.links
```
Forge commands executed in your `my-app` sample project should reflect any changes in your local
Forge build. (Make sure to run `yarn build:fast` or `yarn build` between code changes.)

Просмотреть файл

@ -104,6 +104,7 @@ export function renderConfigTemplate(dir: string, templateObj: any, obj: any): v
}
type MaybeESM<T> = T | { default: T };
type AsyncForgeConfigGenerator = () => Promise<ForgeConfig>;
export default async (dir: string): Promise<ResolvedForgeConfig> => {
const packageJSON = await readRawPackageJson(dir);
@ -123,9 +124,11 @@ export default async (dir: string): Promise<ResolvedForgeConfig> => {
if (await forgeConfigIsValidFilePath(dir, forgeConfig)) {
try {
// The loaded "config" could potentially be a static forge config, ESM module or async function
// eslint-disable-next-line @typescript-eslint/no-var-requires
const loaded = require(path.resolve(dir, forgeConfig as string)) as MaybeESM<ForgeConfig>;
forgeConfig = 'default' in loaded ? loaded.default : loaded;
const loaded = require(path.resolve(dir, forgeConfig as string)) as MaybeESM<ForgeConfig | AsyncForgeConfigGenerator>;
const maybeForgeConfig = 'default' in loaded ? loaded.default : loaded;
forgeConfig = typeof maybeForgeConfig === 'function' ? await maybeForgeConfig() : maybeForgeConfig;
} catch (err) {
console.error(`Failed to load: ${path.resolve(dir, forgeConfig as string)}`);
throw err;

Просмотреть файл

@ -24,6 +24,22 @@ describe('forge-config', () => {
await expect(findConfig(path.resolve(__dirname, '../fixture/bad_external_forge_config'))).to.eventually.be.rejectedWith(/Unexpected token/);
});
it('should support async configs exported in forge.config.js', async () => {
const config = await findConfig(path.resolve(__dirname, '../fixture/async_forge_config'));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (config as any).pluginInterface;
expect(config).to.be.deep.equal({
...defaults,
makers: [
{
name: '@electron-forge/maker-zip',
platforms: ['darwin'],
},
],
packagerConfig: { foo: {} },
});
});
it('should be set to the defaults if no Forge config is specified in package.json', async () => {
const config = await findConfig(path.resolve(__dirname, '../fixture/no_forge_config'));
// eslint-disable-next-line @typescript-eslint/no-explicit-any

Просмотреть файл

@ -0,0 +1,12 @@
module.exports = async function () {
return {
packagerConfig: { foo: {} },
rebuildConfig: {},
makers: [
{
name: '@electron-forge/maker-zip',
platforms: ['darwin'],
},
],
};
};

Просмотреть файл

@ -0,0 +1,8 @@
{
"name": "async-test",
"devDependencies": {
"electron": "^1000.0.0",
"@electron-forge/cli": "6.0.0",
"@electron-forge/core": "6.0.0"
}
}