-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): allow mutating packageJSON on load
In order to override fields like "version" at build time this provides the ability to intercept forge loading your package.json file and modify the JS files we read
- Loading branch information
1 parent
ce36356
commit 1b7e411
Showing
18 changed files
with
146 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { ForgeConfig } from '@electron-forge/shared-types'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
export default async (dir: string) => | ||
import { runMutatingHook } from './hook'; | ||
|
||
export const readRawPackageJson = async (dir: string) => | ||
await fs.readJson(path.resolve(dir, 'package.json')); | ||
|
||
export const readMutatedPackageJson = async (dir: string, forgeConfig: ForgeConfig) => | ||
runMutatingHook(forgeConfig, 'readPackageJson', await readRawPackageJson(dir)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,46 @@ | ||
import { ForgeConfig } from '@electron-forge/shared-types'; | ||
import { expect } from 'chai'; | ||
import { stub } from 'sinon'; | ||
import { stub, SinonStub } from 'sinon'; | ||
|
||
import runHook from '../../src/util/hook'; | ||
import { runHook, runMutatingHook } from '../../src/util/hook'; | ||
|
||
const fakeConfig = { | ||
pluginInterface: { | ||
triggerHook: async () => false, | ||
triggerMutatingHook: async (_: any, item: any) => item, | ||
}, | ||
} as any as ForgeConfig; | ||
|
||
describe('runHook', () => { | ||
it('should not error when running non existent hooks', async () => { | ||
await runHook(Object.assign({}, fakeConfig), 'magic'); | ||
describe('hooks', () => { | ||
describe('runHook', () => { | ||
it('should not error when running non existent hooks', async () => { | ||
await runHook(Object.assign({}, fakeConfig), 'magic'); | ||
}); | ||
|
||
it('should not error when running a hook that is not a function', async () => { | ||
await runHook(Object.assign({ hooks: { myHook: 'abc' } }, fakeConfig), 'abc'); | ||
}); | ||
|
||
it('should run the hook if it is provided as a function', async () => { | ||
const myStub = stub(); | ||
myStub.returns(Promise.resolve()); | ||
await runHook(Object.assign({ hooks: { myHook: myStub } }, fakeConfig), 'myHook'); | ||
expect(myStub.callCount).to.equal(1); | ||
}); | ||
}); | ||
|
||
it('should not error when running a hook that is not a function', async () => { | ||
await runHook(Object.assign({ hooks: { myHook: 'abc' } }, fakeConfig), 'abc'); | ||
}); | ||
describe('runMutatingHook', () => { | ||
it('should return the input when running non existent hooks', async () => { | ||
expect(await runMutatingHook(Object.assign({}, fakeConfig), 'magic', 'input')).to.equal('input'); | ||
}); | ||
|
||
it('should run the hook if it is provided as a function', async () => { | ||
const myStub = stub(); | ||
myStub.returns(Promise.resolve()); | ||
await runHook(Object.assign({ hooks: { myHook: myStub } }, fakeConfig), 'myHook'); | ||
expect(myStub.callCount).to.equal(1); | ||
it('should return the mutated input when returned from a hook', async () => { | ||
fakeConfig.pluginInterface.triggerMutatingHook = stub().returnsArg(1); | ||
const myStub = stub(); | ||
myStub.returns(Promise.resolve('magneto')); | ||
const output = await runMutatingHook(Object.assign({ hooks: { myHook: myStub } }, fakeConfig), 'myHook', 'input'); | ||
expect(output).to.equal('magneto'); | ||
expect((fakeConfig.pluginInterface.triggerMutatingHook as SinonStub).firstCall.args[1]).to.equal('magneto'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.