-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(install): Implement a very basic hook system (#5293)
* feat(install): Implement a very basic hook system This PR adds a very basic and undocumented hook system. I plan to use it internally to get better stats on how Yarn performs, and how much time is spent on the linking step. * Adds tests * Improves the typing of callThroughHook * Lints
- Loading branch information
Showing
3 changed files
with
121 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* @flow */ | ||
|
||
import {runInstall} from './commands/_helpers.js'; | ||
|
||
test('install should call the resolveStep hook', async () => { | ||
global.experimentalYarnHooks = { | ||
resolveStep: jest.fn(cb => cb()), | ||
}; | ||
await runInstall({}, 'install-production', config => { | ||
expect(global.experimentalYarnHooks.resolveStep.mock.calls.length).toEqual(1); | ||
}); | ||
delete global.experimentalYarnHooks; | ||
}); | ||
|
||
test('install should call the fetchStep hook', async () => { | ||
global.experimentalYarnHooks = { | ||
fetchStep: jest.fn(cb => cb()), | ||
}; | ||
await runInstall({}, 'install-production', config => { | ||
expect(global.experimentalYarnHooks.fetchStep.mock.calls.length).toEqual(1); | ||
}); | ||
delete global.experimentalYarnHooks; | ||
}); | ||
|
||
test('install should call the linkStep hook', async () => { | ||
global.experimentalYarnHooks = { | ||
linkStep: jest.fn(cb => cb()), | ||
}; | ||
await runInstall({}, 'install-production', config => { | ||
expect(global.experimentalYarnHooks.linkStep.mock.calls.length).toEqual(1); | ||
}); | ||
delete global.experimentalYarnHooks; | ||
}); | ||
|
||
test('install should call the buildStep hook', async () => { | ||
global.experimentalYarnHooks = { | ||
buildStep: jest.fn(cb => cb()), | ||
}; | ||
await runInstall({}, 'install-production', config => { | ||
expect(global.experimentalYarnHooks.buildStep.mock.calls.length).toEqual(1); | ||
}); | ||
delete global.experimentalYarnHooks; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* @flow */ | ||
|
||
export type YarnHook = 'resolveStep' | 'fetchStep' | 'linkStep' | 'buildStep'; | ||
|
||
const YARN_HOOKS_KEY = 'experimentalYarnHooks'; | ||
|
||
export function callThroughHook<T>(type: YarnHook, fn: () => T): T { | ||
if (typeof global === 'undefined') { | ||
return fn(); | ||
} | ||
|
||
if (typeof global[YARN_HOOKS_KEY] !== 'object' || !global[YARN_HOOKS_KEY]) { | ||
return fn(); | ||
} | ||
|
||
const hook: (() => T) => T = global[YARN_HOOKS_KEY][type]; | ||
|
||
if (!hook) { | ||
return fn(); | ||
} | ||
|
||
return hook(fn); | ||
} |