Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions .yarn/versions/5fa9d3ae.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": minor
"@yarnpkg/plugin-dlx": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-node-modules"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
90 changes: 90 additions & 0 deletions packages/yarnpkg-core/sources/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1484,12 +1484,47 @@ export class Configuration {
}
}

/**
* Will trigger a requested hook in each plugin that provides said hook.
* Hooks requested via this call can not return a Promise and must be sync.
*
* @param get Function that returns the hook wanted to be triggered from a plugin.
* @param args Arguments to provide the requested hook.
*/
triggerHookSync<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((...args: U) => Exclude<V, Promise<any>>) | undefined, ...args: U): void {
for (const plugin of this.plugins.values()) {
const hooks = plugin.hooks as HooksDefinition;
if (!hooks)
continue;

const hook = get(hooks);
if (!hook)
continue;

hook(...args);
}
}

async triggerMultipleHooks<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((...args: U) => V) | undefined, argsList: Array<U>): Promise<void> {
for (const args of argsList) {
await this.triggerHook(get, ...args);
}
}

/**
* Will trigger a requested hook in each plugin that provides said hook.
* Hooks will be triggered once for each set of arguments provided.
* Hooks requested via this call can not return a Promise and must be sync.
*
* @param get Function that returns the hook wanted to be triggered from a plugin.
* @param argsList Array of Arguments to provide the requested hook.
*/
triggerMultipleHooksSync<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((...args: U) => Exclude<V, Promise<any>>) | undefined, argsList: Array<U>): void {
for (const args of argsList) {
this.triggerHookSync(get, ...args);
}
}

async reduceHook<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((reduced: V, ...args: U) => Promise<V>) | undefined, initialValue: V, ...args: U): Promise<V> {
let value = initialValue;

Expand All @@ -1508,6 +1543,33 @@ export class Configuration {
return value;
}

/**
* Will trigger a requested hook that returns a value.
* Hooks requested via this call must accept an initialValue as the first parameter, set via by hook caller or a previous hook.
* Hooks requested via this call can not return a Promise and must be sync.
*
* @param get Function that returns the hook wanted to be triggered from a plugin.
* @param initialValue Value to return if no hooks are found or none want to return a value, this value is given to hooks unless one has updated it.
* @param args Arguments to provide the requested hook.
*/
reduceHookSync<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((reduced: V, ...args: U) => V) | undefined, initialValue: V, ...args: U): V {
let value = initialValue;

for (const plugin of this.plugins.values()) {
const hooks = plugin.hooks as HooksDefinition;
if (!hooks)
continue;

const hook = get(hooks);
if (!hook)
continue;

value = hook(value, ...args);
}

return value;
}

async firstHook<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((...args: U) => Promise<V>) | undefined, ...args: U): Promise<Exclude<V, void> | null> {
for (const plugin of this.plugins.values()) {
const hooks = plugin.hooks as HooksDefinition;
Expand All @@ -1528,6 +1590,34 @@ export class Configuration {
return null;
}

/**
* Will trigger a requested hook that returns a value and return the value of the first hook to return a non `undefined` value.
* No further hooks will be triggered once a return is given.
* Hooks requested via this call can not return a Promise and must be sync.
*
* @param get Function that returns the hook wanted to be triggered from a plugin.
* @param args Arguments to provide the requested hook.
*/
firstHookSync<U extends Array<any>, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((...args: U) => V) | undefined, ...args: U): Exclude<V, void> | null {
for (const plugin of this.plugins.values()) {
const hooks = plugin.hooks as HooksDefinition;
if (!hooks)
continue;

const hook = get(hooks);
if (!hook)
continue;

const ret = hook(...args);
if (typeof ret !== `undefined`) {
// @ts-expect-error
return ret;
}
}

return null;
}

/**
* @deprecated Prefer using formatUtils.pretty instead, which is type-safe
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.