-
-
Notifications
You must be signed in to change notification settings - Fork 513
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
feat(core): allow mutating packageJSON on load #513
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import debug from 'debug'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import readPackageJSON from './read-package-json'; | ||
import { readRawPackageJson } from './read-package-json'; | ||
import getElectronVersion from './electron-version'; | ||
|
||
const d = debug('electron-forge:project-resolver'); | ||
|
||
// FIXME: If we want getElectronVersion to be overridable by plugins | ||
// and / or forge config then we need to be able to resolve | ||
// the dir without calling getElectronVersion | ||
export default async (dir: string) => { | ||
let mDir = dir; | ||
let prevDir; | ||
|
@@ -14,8 +17,10 @@ export default async (dir: string) => { | |
const testPath = path.resolve(mDir, 'package.json'); | ||
d('searching for project in:', mDir); | ||
if (await fs.pathExists(testPath)) { | ||
const packageJSON = await readPackageJSON(mDir); | ||
const packageJSON = await readRawPackageJson(mDir); | ||
|
||
// TODO: Move this check to inside the forge config resolver and use | ||
// mutatedPackageJson reader | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this for a different PR then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup |
||
const electronVersion = getElectronVersion(packageJSON); | ||
if (electronVersion) { | ||
if (!/[0-9]/.test(electronVersion[0])) { | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why pass the forgeConfig into the hook first? This was a bit cryptic to find from the docs.