diff --git a/.yarn/versions/2ea03f30.yml b/.yarn/versions/2ea03f30.yml new file mode 100644 index 000000000000..ba09e64996d7 --- /dev/null +++ b/.yarn/versions/2ea03f30.yml @@ -0,0 +1,32 @@ +releases: + "@yarnpkg/builder": patch + "@yarnpkg/cli": patch + "@yarnpkg/core": major + "@yarnpkg/doctor": patch + "@yarnpkg/extensions": patch + "@yarnpkg/nm": patch + "@yarnpkg/plugin-compat": major + "@yarnpkg/plugin-constraints": patch + "@yarnpkg/plugin-dlx": patch + "@yarnpkg/plugin-essentials": major + "@yarnpkg/plugin-exec": patch + "@yarnpkg/plugin-file": patch + "@yarnpkg/plugin-git": major + "@yarnpkg/plugin-github": major + "@yarnpkg/plugin-http": patch + "@yarnpkg/plugin-init": patch + "@yarnpkg/plugin-interactive-tools": patch + "@yarnpkg/plugin-link": patch + "@yarnpkg/plugin-nm": patch + "@yarnpkg/plugin-npm": major + "@yarnpkg/plugin-npm-cli": patch + "@yarnpkg/plugin-pack": major + "@yarnpkg/plugin-patch": major + "@yarnpkg/plugin-pnp": major + "@yarnpkg/plugin-pnpm": patch + "@yarnpkg/plugin-stage": major + "@yarnpkg/plugin-typescript": major + "@yarnpkg/plugin-version": patch + "@yarnpkg/plugin-workspace-tools": patch + "@yarnpkg/pnpify": patch + "@yarnpkg/sdks": patch diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d0581fd76a0..24adbd852b2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ The following changes only affect people writing Yarn plugins: - The `getCustomDataKey` function in `Installer` from `@yarnpkg/core` has been moved to `Linker`. +- All hooks now accept an object instead of positional parameters. + ### Compatibility - The patched filesystem now supports `ftruncate`. diff --git a/packages/gatsby/content/advanced/plugin-tutorial.mdx b/packages/gatsby/content/advanced/plugin-tutorial.mdx index 2b6f4781badf..318a1c4d65d8 100644 --- a/packages/gatsby/content/advanced/plugin-tutorial.mdx +++ b/packages/gatsby/content/advanced/plugin-tutorial.mdx @@ -133,7 +133,7 @@ module.exports = { name: `plugin-hello-world`, factory: require => ({ hooks: { - setupScriptEnvironment(project, scriptEnv) { + setupScriptEnvironment({project, env: scriptEnv}) { scriptEnv.HELLO_WORLD = `my first plugin!`; }, }, diff --git a/packages/plugin-compat/sources/index.ts b/packages/plugin-compat/sources/index.ts index 3cd3d4461530..92dec252ad0e 100644 --- a/packages/plugin-compat/sources/index.ts +++ b/packages/plugin-compat/sources/index.ts @@ -14,13 +14,13 @@ const PATCHES = new Map([ const plugin: Plugin = { hooks: { - registerPackageExtensions: async (configuration, registerPackageExtension) => { + registerPackageExtensions: async ({registerPackageExtension}) => { for (const [descriptorStr, extensionData] of packageExtensions) { registerPackageExtension(structUtils.parseDescriptor(descriptorStr, true), extensionData); } }, - getBuiltinPatch: async (project, name) => { + getBuiltinPatch: async ({name}) => { const TAG = `compat/`; if (!name.startsWith(TAG)) return undefined; @@ -31,7 +31,7 @@ const plugin: Plugin = { return typeof patch !== `undefined` ? patch : null; }, - reduceDependency: async (dependency, project, locator, initialDescriptor) => { + reduceDependency: async ({dependency}) => { const patch = PATCHES.get(dependency.identHash); if (typeof patch === `undefined`) return dependency; diff --git a/packages/plugin-essentials/sources/commands/add.ts b/packages/plugin-essentials/sources/commands/add.ts index 5ea8ed8d2f27..846826f6cf65 100644 --- a/packages/plugin-essentials/sources/commands/add.ts +++ b/packages/plugin-essentials/sources/commands/add.ts @@ -210,19 +210,19 @@ export default class AddCommand extends BaseCommand { let askedQuestions = false; - const afterWorkspaceDependencyAdditionList: Array<[ - Workspace, - suggestUtils.Target, - Descriptor, - Array, - ]> = []; - - const afterWorkspaceDependencyReplacementList: Array<[ - Workspace, - suggestUtils.Target, - Descriptor, - Descriptor, - ]> = []; + const afterWorkspaceDependencyAdditionList: Array<{ + workspace: Workspace; + target: suggestUtils.Target; + descriptor: Descriptor; + strategies: Array; + }> = []; + + const afterWorkspaceDependencyReplacementList: Array<{ + workspace: Workspace; + target: suggestUtils.Target; + fromDescriptor: Descriptor; + toDescriptor: Descriptor; + }> = []; for (const [/*request*/, {suggestions}, target] of allSuggestions) { let selected: Descriptor; @@ -284,19 +284,19 @@ export default class AddCommand extends BaseCommand { } if (typeof current === `undefined`) { - afterWorkspaceDependencyAdditionList.push([ + afterWorkspaceDependencyAdditionList.push({ workspace, target, - selected, + descriptor: selected, strategies, - ]); + }); } else { - afterWorkspaceDependencyReplacementList.push([ + afterWorkspaceDependencyReplacementList.push({ workspace, target, - current, - selected, - ]); + fromDescriptor: current, + toDescriptor: selected, + }); } } } diff --git a/packages/plugin-essentials/sources/commands/info.ts b/packages/plugin-essentials/sources/commands/info.ts index 94c97fba4a4a..2e0dd6fe6cd2 100644 --- a/packages/plugin-essentials/sources/commands/info.ts +++ b/packages/plugin-essentials/sources/commands/info.ts @@ -341,7 +341,7 @@ export default class InfoCommand extends BaseCommand { await configuration.triggerHook((hooks: Hooks) => { return hooks.fetchPackageInfo; - }, pkg, extraSet, registerData); + }, {pkg, extra: extraSet, registerData}); } if (pkg.bin.size > 0 && !isVirtual) { diff --git a/packages/plugin-essentials/sources/commands/remove.ts b/packages/plugin-essentials/sources/commands/remove.ts index d0b0112fb50f..5927cf893fb4 100644 --- a/packages/plugin-essentials/sources/commands/remove.ts +++ b/packages/plugin-essentials/sources/commands/remove.ts @@ -82,11 +82,11 @@ export default class RemoveCommand extends BaseCommand { const unreferencedPatterns = []; let hasChanged = false; - const afterWorkspaceDependencyRemovalList: Array<[ - Workspace, - suggestUtils.Target, - Descriptor, - ]> = []; + const afterWorkspaceDependencyRemovalList: Array<{ + workspace: Workspace; + target: suggestUtils.Target; + descriptor: Descriptor; + }> = []; for (const pattern of this.patterns) { let isReferenced = false; @@ -119,11 +119,11 @@ export default class RemoveCommand extends BaseCommand { workspace.manifest[target].delete(identHash); - afterWorkspaceDependencyRemovalList.push([ + afterWorkspaceDependencyRemovalList.push({ workspace, target, - removedDescriptor, - ]); + descriptor: removedDescriptor, + }); hasChanged = true; isReferenced = true; diff --git a/packages/plugin-essentials/sources/index.ts b/packages/plugin-essentials/sources/index.ts index 8142e5c54a72..0161c46dc393 100644 --- a/packages/plugin-essentials/sources/index.ts +++ b/packages/plugin-essentials/sources/index.ts @@ -53,12 +53,12 @@ export interface Hooks { * dependencies into the manifest and running `yarn install` won't trigger * it. */ - afterWorkspaceDependencyAddition?: ( - workspace: Workspace, - target: suggestUtils.Target, - descriptor: Descriptor, - strategies: Array - ) => Promise; + afterWorkspaceDependencyAddition?: ({workspace, target, descriptor, strategies}: { + workspace: Workspace; + target: suggestUtils.Target; + descriptor: Descriptor; + strategies: Array; + }) => Promise; /** * Called when a dependency range is replaced inside a workspace. Note that @@ -66,12 +66,12 @@ export interface Hooks { * updating the dependencies from the manifest and running `yarn install` * won't trigger it. */ - afterWorkspaceDependencyReplacement?: ( - workspace: Workspace, - target: suggestUtils.Target, - fromDescriptor: Descriptor, - toDescriptor: Descriptor, - ) => Promise; + afterWorkspaceDependencyReplacement?: ({workspace, target, fromDescriptor, toDescriptor}: { + workspace: Workspace; + target: suggestUtils.Target; + fromDescriptor: Descriptor; + toDescriptor: Descriptor; + }) => Promise; /** * Called when a dependency range is removed from a workspace. Note that @@ -79,11 +79,11 @@ export interface Hooks { * removing the dependencies from the manifest and running `yarn install` * won't trigger it. */ - afterWorkspaceDependencyRemoval?: ( - workspace: Workspace, - target: suggestUtils.Target, - descriptor: Descriptor, - ) => Promise; + afterWorkspaceDependencyRemoval?: ({workspace, target, descriptor}: { + workspace: Workspace; + target: suggestUtils.Target; + descriptor: Descriptor; + }) => Promise; /** * Called by `yarn info`. The `extra` field is the set of parameters passed @@ -94,11 +94,11 @@ export interface Hooks { * requested audit information (via `-X audit`), and call `registerData` * with those information (retrieved dynamically) if they did. */ - fetchPackageInfo?: ( - pkg: Package, - extra: Set, - registerData: (namespace: string, data: Array | {[key: string]: formatUtils.Tuple | undefined}) => void, - ) => Promise; + fetchPackageInfo?: ({pkg, extra, registerData}: { + pkg: Package; + extra: Set; + registerData: (namespace: string, data: Array | {[key: string]: formatUtils.Tuple | undefined}) => void; + }) => Promise; } declare module '@yarnpkg/core' { diff --git a/packages/plugin-git/sources/GitFetcher.ts b/packages/plugin-git/sources/GitFetcher.ts index fe2a4fce86f0..01f22bde28f3 100644 --- a/packages/plugin-git/sources/GitFetcher.ts +++ b/packages/plugin-git/sources/GitFetcher.ts @@ -43,10 +43,10 @@ export class GitFetcher implements Fetcher { }; } - async downloadHosted(locator: Locator, opts: FetchOptions) { - return opts.project.configuration.reduceHook((hooks: Hooks) => { + async downloadHosted(locator: Locator, options: FetchOptions) { + return options.project.configuration.reduceHook((hooks: Hooks) => { return hooks.fetchHostedRepository; - }, null as FetchResult | null, locator, opts); + }, {current: null as FetchResult | null, locator, options}, `current`); } async cloneFromRemote(locator: Locator, opts: FetchOptions) { diff --git a/packages/plugin-git/sources/gitUtils.ts b/packages/plugin-git/sources/gitUtils.ts index 2dddc0b27562..7f2f0baa4468 100644 --- a/packages/plugin-git/sources/gitUtils.ts +++ b/packages/plugin-git/sources/gitUtils.ts @@ -371,10 +371,13 @@ export async function fetchChangedWorkspaces({ref, project}: {ref: string | true ]; await project.configuration.triggerHook((hooks: Hooks) => { return hooks.populateYarnPaths; - }, project, (path: PortablePath | null) => { - if (path != null) { - ignoredPaths.push(path); - } + }, { + project, + definePath: (path: PortablePath | null) => { + if (path != null) { + ignoredPaths.push(path); + } + }, }); const root = await fetchRoot(project.configuration.projectCwd); diff --git a/packages/plugin-git/sources/index.ts b/packages/plugin-git/sources/index.ts index c89f04909f77..51222bf12607 100644 --- a/packages/plugin-git/sources/index.ts +++ b/packages/plugin-git/sources/index.ts @@ -15,11 +15,11 @@ export interface Hooks { * supports downloading repository tarballs, which are more efficient than * cloning the repository (even without its history). */ - fetchHostedRepository?: ( - current: FetchResult | null, - locator: Locator, - opts: FetchOptions, - ) => Promise; + fetchHostedRepository?: ({current, locator, options}: { + current: FetchResult | null; + locator: Locator; + options: FetchOptions; + }) => Promise; } declare module '@yarnpkg/core' { diff --git a/packages/plugin-github/sources/index.ts b/packages/plugin-github/sources/index.ts index 15cf15e09a73..f4f1e6b06245 100644 --- a/packages/plugin-github/sources/index.ts +++ b/packages/plugin-github/sources/index.ts @@ -5,16 +5,16 @@ import {GithubFetcher} from './GithubFetcher'; const plugin: Plugin = { hooks: { - async fetchHostedRepository(previous, locator, opts) { + async fetchHostedRepository({previous, locator, options}) { if (previous !== null) return previous; const fetcher = new GithubFetcher(); - if (!fetcher.supports(locator, opts)) + if (!fetcher.supports(locator, options)) return null; try { - return await fetcher.fetch(locator, opts); + return await fetcher.fetch(locator, options); } catch (error) { return null; } diff --git a/packages/plugin-npm/sources/index.ts b/packages/plugin-npm/sources/index.ts index ec0bee623991..8de60c4432b4 100644 --- a/packages/plugin-npm/sources/index.ts +++ b/packages/plugin-npm/sources/index.ts @@ -19,10 +19,12 @@ export interface Hooks { * You can use this mechanism to dynamically query a CLI for the credentials for a * specific registry. */ - getNpmAuthenticationHeader?: (currentHeader: string | undefined, registry: string, { - configuration, - ident, - }: { configuration: Configuration, ident?: Ident }) => Promise; + getNpmAuthenticationHeader?: ({currentHeader, registry, configuration, ident}: { + currentHeader?: string; + registry: string; + configuration: Configuration; + ident?: Ident; + }) => Promise; } diff --git a/packages/plugin-npm/sources/npmHttpUtils.ts b/packages/plugin-npm/sources/npmHttpUtils.ts index 17bcb6b53ce9..4482aa66d54f 100644 --- a/packages/plugin-npm/sources/npmHttpUtils.ts +++ b/packages/plugin-npm/sources/npmHttpUtils.ts @@ -203,7 +203,7 @@ async function getAuthenticationHeader(registry: string, {authType = AuthType.CO const header = await configuration.reduceHook((hooks: Hooks) => { return hooks.getNpmAuthenticationHeader; - }, undefined, registry, {configuration, ident}); + }, {registry, configuration, ident}, `currentHeader`); if (header) return header; diff --git a/packages/plugin-pack/sources/index.ts b/packages/plugin-pack/sources/index.ts index 0bd76598a8aa..8268404f9b34 100644 --- a/packages/plugin-pack/sources/index.ts +++ b/packages/plugin-pack/sources/index.ts @@ -12,16 +12,16 @@ export interface Hooks { * parameter is allowed to be mutated at will, with the changes being only * applied to the packed manifest (the original one won't be mutated). */ - beforeWorkspacePacking?: ( - workspace: Workspace, - rawManifest: object, - ) => Promise | void; + beforeWorkspacePacking?: ({workspace, rawManifest}: { + workspace: Workspace; + rawManifest: object; + }) => Promise | void; } const DEPENDENCY_TYPES = [`dependencies`, `devDependencies`, `peerDependencies`]; const WORKSPACE_PROTOCOL = `workspace:`; -const beforeWorkspacePacking = (workspace: Workspace, rawManifest: any) => { +const beforeWorkspacePacking = ({workspace, rawManifest}: { workspace: Workspace, rawManifest: any }) => { if (rawManifest.publishConfig) { if (rawManifest.publishConfig.type) rawManifest.type = rawManifest.publishConfig.type; diff --git a/packages/plugin-pack/sources/packUtils.ts b/packages/plugin-pack/sources/packUtils.ts index 0613f7ffda2d..0d2918683495 100644 --- a/packages/plugin-pack/sources/packUtils.ts +++ b/packages/plugin-pack/sources/packUtils.ts @@ -146,8 +146,7 @@ export async function genPackageManifest(workspace: Workspace): Promise await workspace.project.configuration.triggerHook( (hooks: Hooks) => hooks.beforeWorkspacePacking, - workspace, - data, + {workspace, rawManifest: data}, ); return data; @@ -190,8 +189,11 @@ export async function genPackList(workspace: Workspace) { await configuration.triggerHook((hooks: StageHooks) => { return hooks.populateYarnPaths; - }, project, (path: PortablePath | null) => { - maybeRejectPath(path); + }, { + project, + definePath: (path: PortablePath | null) => { + maybeRejectPath(path); + }, }); // All child workspaces are ignored diff --git a/packages/plugin-patch/sources/index.ts b/packages/plugin-patch/sources/index.ts index 816826ba03f8..0b5ab4c9a7a2 100644 --- a/packages/plugin-patch/sources/index.ts +++ b/packages/plugin-patch/sources/index.ts @@ -15,10 +15,10 @@ export interface Hooks { * syntax: `patch:builtin`. This is for instance how the TypeScript * patch is automatically registered. */ - getBuiltinPatch?: ( - project: Project, - name: string, - ) => Promise; + getBuiltinPatch?: ({project, name}: { + project: Project; + name: string; + }) => Promise; } declare module '@yarnpkg/core' { diff --git a/packages/plugin-patch/sources/patchUtils.ts b/packages/plugin-patch/sources/patchUtils.ts index 11945905f266..082c719f0ce4 100644 --- a/packages/plugin-patch/sources/patchUtils.ts +++ b/packages/plugin-patch/sources/patchUtils.ts @@ -198,7 +198,7 @@ export async function loadPatchFiles(parentLocator: Locator | null, patchPaths: onBuiltin: async name => { return await opts.project.configuration.firstHook((hooks: PatchHooks) => { return hooks.getBuiltinPatch; - }, opts.project, name); + }, {project: opts.project, name}); }, }, patchPath); diff --git a/packages/plugin-pnp/sources/index.ts b/packages/plugin-pnp/sources/index.ts index b01ebd2ebcdc..38f2af0f7de5 100644 --- a/packages/plugin-pnp/sources/index.ts +++ b/packages/plugin-pnp/sources/index.ts @@ -24,7 +24,7 @@ export const quotePathIfNeeded = (path: string) => { return /\s/.test(path) ? JSON.stringify(path) : path; }; -async function setupScriptEnvironment(project: Project, env: {[key: string]: string}, makePathWrapper: (name: string, argv0: string, args: Array) => Promise) { +async function setupScriptEnvironment({project, env}: { project: Project, env: {[key: string]: string}, makePathWrapper: (name: string, argv0: string, args: Array) => Promise }) { const pnpPath = getPnpPath(project); let pnpRequire = `--require ${quotePathIfNeeded(npath.fromPortablePath(pnpPath.cjs))}`; @@ -49,7 +49,7 @@ async function setupScriptEnvironment(project: Project, env: {[key: string]: str } } -async function populateYarnPaths(project: Project, definePath: (path: PortablePath | null) => void) { +async function populateYarnPaths({project, definePath}: { project: Project, definePath: (path: PortablePath | null) => void }) { const pnpPath = getPnpPath(project); definePath(pnpPath.cjs); definePath(pnpPath.esmLoader); diff --git a/packages/plugin-stage/sources/commands/stage.ts b/packages/plugin-stage/sources/commands/stage.ts index 2e9f04f20494..d5c5884a294d 100644 --- a/packages/plugin-stage/sources/commands/stage.ts +++ b/packages/plugin-stage/sources/commands/stage.ts @@ -69,8 +69,11 @@ export default class StageCommand extends BaseCommand { await configuration.triggerHook((hooks: Hooks) => { return hooks.populateYarnPaths; - }, project, (path: PortablePath | null) => { - basePaths.push(path); + }, { + project, + definePath: (path: PortablePath | null) => { + basePaths.push(path); + }, }); const yarnPaths = new Set(); diff --git a/packages/plugin-stage/sources/index.ts b/packages/plugin-stage/sources/index.ts index b3424d76db96..4e7ac598e71c 100644 --- a/packages/plugin-stage/sources/index.ts +++ b/packages/plugin-stage/sources/index.ts @@ -4,10 +4,10 @@ import {PortablePath} from '@yarnpkg/fslib'; import stage from './commands/stage'; export interface Hooks { - populateYarnPaths?: ( - project: Project, - definePath: (path: PortablePath | null) => void, - ) => Promise; + populateYarnPaths?: ({project, definePath}: { + project: Project; + definePath: (path: PortablePath | null) => void; + }) => Promise; } const plugin: Plugin = { diff --git a/packages/plugin-typescript/sources/index.ts b/packages/plugin-typescript/sources/index.ts index 5bc58afa389a..6669f8f4e091 100644 --- a/packages/plugin-typescript/sources/index.ts +++ b/packages/plugin-typescript/sources/index.ts @@ -14,12 +14,10 @@ const getTypesName = (descriptor: Descriptor) => { : `${descriptor.name}`; }; -const afterWorkspaceDependencyAddition = async ( - workspace: Workspace, - dependencyTarget: suggestUtils.Target, - descriptor: Descriptor, - strategies: Array, -) => { +const afterWorkspaceDependencyAddition = async ({workspace, descriptor}: { + workspace: Workspace; + descriptor: Descriptor; +}) => { if (descriptor.scope === `types`) return; @@ -107,11 +105,10 @@ const afterWorkspaceDependencyAddition = async ( } }; -const afterWorkspaceDependencyRemoval = async ( - workspace: Workspace, - dependencyTarget: suggestUtils.Target, - descriptor: Descriptor, -) => { +const afterWorkspaceDependencyRemoval = async ({workspace, descriptor}: { + workspace: Workspace; + descriptor: Descriptor; +}) => { if (descriptor.scope === `types`) return; @@ -138,7 +135,7 @@ const afterWorkspaceDependencyRemoval = async ( } }; -const beforeWorkspacePacking = (workspace: Workspace, rawManifest: any) => { +const beforeWorkspacePacking = ({rawManifest}: { rawManifest: any }) => { if (rawManifest.publishConfig && rawManifest.publishConfig.typings) rawManifest.typings = rawManifest.publishConfig.typings; diff --git a/packages/yarnpkg-core/sources/Configuration.ts b/packages/yarnpkg-core/sources/Configuration.ts index 554158cf9256..25112f79ab62 100644 --- a/packages/yarnpkg-core/sources/Configuration.ts +++ b/packages/yarnpkg-core/sources/Configuration.ts @@ -1553,7 +1553,7 @@ export class Configuration { await this.triggerHook(hooks => { return hooks.registerPackageExtensions; - }, this, registerPackageExtension); + }, {configuration: this, registerPackageExtension}); for (const [descriptorString, extensionData] of this.get(`packageExtensions`)) { registerPackageExtension(structUtils.parseDescriptor(descriptorString, true), miscUtils.convertMapsToIndexableObjects(extensionData), {userProvided: true}); @@ -1710,8 +1710,8 @@ export class Configuration { } } - async reduceHook, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((reduced: V, ...args: U) => Promise) | undefined, initialValue: V, ...args: U): Promise { - let value = initialValue; + async reduceHook, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((reduced: V, ...args: U) => Promise) | undefined, value: V, attribute: string, ...args: U): Promise { + let accumulator = value?.[attribute]; for (const plugin of this.plugins.values()) { const hooks = plugin.hooks as HooksDefinition; @@ -1722,10 +1722,10 @@ export class Configuration { if (!hook) continue; - value = await hook(value, ...args); + accumulator = await hook({...value, [attribute]: accumulator}, ...args); } - return value; + return accumulator; } async firstHook, V, HooksDefinition = Hooks>(get: (hooks: HooksDefinition) => ((...args: U) => Promise) | undefined, ...args: U): Promise | null> { diff --git a/packages/yarnpkg-core/sources/CorePlugin.ts b/packages/yarnpkg-core/sources/CorePlugin.ts index 5e025a7544f8..138c3c6d2990 100644 --- a/packages/yarnpkg-core/sources/CorePlugin.ts +++ b/packages/yarnpkg-core/sources/CorePlugin.ts @@ -8,7 +8,7 @@ import {Descriptor, Locator} from './types'; export const CorePlugin: Plugin = { hooks: { - reduceDependency: (dependency: Descriptor, project: Project, locator: Locator, initialDependency: Descriptor, {resolver, resolveOptions}: {resolver: Resolver, resolveOptions: ResolveOptions}) => { + reduceDependency: ({dependency, project, locator, initialDependency, resolver, resolveOptions}: { dependency: Descriptor, project: Project, locator: Locator, initialDependency: Descriptor, resolver: Resolver, resolveOptions: ResolveOptions }) => { for (const {pattern, reference} of project.topLevelWorkspace.manifest.resolutions) { if (pattern.from && pattern.from.fullName !== structUtils.stringifyIdent(locator)) continue; @@ -32,26 +32,29 @@ export const CorePlugin: Plugin = { return dependency; }, - validateProject: async (project: Project, report: { + validateProject: async ({project, report}: { project: Project; report: { reportWarning: (name: MessageName, text: string) => void; reportError: (name: MessageName, text: string) => void; - }) => { + };}) => { for (const workspace of project.workspaces) { const workspaceName = structUtils.prettyWorkspace(project.configuration, workspace); await project.configuration.triggerHook(hooks => { return hooks.validateWorkspace; - }, workspace, { - reportWarning: (name: MessageName, text: string) => report.reportWarning(name, `${workspaceName}: ${text}`), - reportError: (name: MessageName, text: string) => report.reportError(name, `${workspaceName}: ${text}`), + }, { + workspace, + report: { + reportWarning: (name: MessageName, text: string) => report.reportWarning(name, `${workspaceName}: ${text}`), + reportError: (name: MessageName, text: string) => report.reportError(name, `${workspaceName}: ${text}`), + }, }); } }, - validateWorkspace: async (workspace: Workspace, report: { + validateWorkspace: async ({workspace, report}: { workspace: Workspace; report: { reportWarning: (name: MessageName, text: string) => void; reportError: (name: MessageName, text: string) => void; - }) => { + };}) => { // Validate manifest const {manifest} = workspace; diff --git a/packages/yarnpkg-core/sources/Plugin.ts b/packages/yarnpkg-core/sources/Plugin.ts index 81b92997a12c..8845de16b808 100644 --- a/packages/yarnpkg-core/sources/Plugin.ts +++ b/packages/yarnpkg-core/sources/Plugin.ts @@ -49,10 +49,10 @@ export interface Hooks { * ones. That's for example what the compat plugin uses to automatically fix * packages with known flaws. */ - registerPackageExtensions?: ( - configuration: Configuration, - registerPackageExtension: (descriptor: Descriptor, extensionData: PackageExtensionData) => void, - ) => Promise; + registerPackageExtensions?: ({configuration, registerPackageExtension}: { + configuration: Configuration; + registerPackageExtension: (descriptor: Descriptor, extensionData: PackageExtensionData) => void; + }) => Promise; /** * Called before a script is executed. The hooks are allowed to modify the @@ -64,11 +64,10 @@ export interface Hooks { * suggest you adopt this convention for any new key added to the env (we * might enforce it later on). */ - setupScriptEnvironment?: ( - project: Project, - env: ProcessEnvironment, - makePathWrapper: (name: string, argv0: string, args: Array) => Promise, - ) => Promise; + setupScriptEnvironment?: ({project, env}: { + project: Project; + env: ProcessEnvironment; + }) => Promise; /** * Called as a script is getting executed. The `executor` function parameter, @@ -76,13 +75,13 @@ export interface Hooks { * script executions, for example to run some validation or add some * performance monitoring. */ - wrapScriptExecution?: ( - executor: () => Promise, - project: Project, - locator: Locator, - scriptName: string, - extra: {script: string, args: Array, cwd: PortablePath, env: ProcessEnvironment, stdin: Readable | null, stdout: Writable, stderr: Writable}, - ) => Promise<() => Promise>; + wrapScriptExecution?: ({project, locator, scriptName, extra}: { + executor: () => Promise; + project: Project; + locator: Locator; + scriptName: string; + extra: {script: string, args: Array, cwd: PortablePath, env: ProcessEnvironment, stdin: Readable | null, stdout: Writable, stderr: Writable}; + }) => Promise<() => Promise>; /** * Called when a network request is being made. The `executor` function @@ -90,20 +89,20 @@ export interface Hooks { * mechanism to wrap network requests, for example to run some validation or * add some logging. */ - wrapNetworkRequest?: ( - executor: () => Promise, - extra: WrapNetworkRequestInfo - ) => Promise<() => Promise>; + wrapNetworkRequest?: ({executor, extra}: { + executor: () => Promise; + extra: WrapNetworkRequestInfo; + }) => Promise<() => Promise>; /** * Called before the build, to compute a global hash key that we will use * to detect whether packages must be rebuilt (typically when the Node * version changes). */ - globalHashGeneration?: ( - project: Project, - contributeHash: (data: string | Buffer) => void, - ) => Promise; + globalHashGeneration?: ({project, contributeHash}: { + project: Project; + contributeHash: (data: string | Buffer) => void; + }) => Promise; /** * Called during the resolution, once for each resolved package and each of @@ -116,63 +115,63 @@ export interface Hooks { * `initialDependency` will be the descriptor before any plugin attempted to * change it. */ - reduceDependency?: ( - dependency: Descriptor, - project: Project, - locator: Locator, - initialDependency: Descriptor, - extra: {resolver: Resolver, resolveOptions: ResolveOptions}, - ) => Promise; + reduceDependency?: ({dependency, project, locator, initialDependency, extra}: { + dependency: Descriptor; + project: Project; + locator: Locator; + initialDependency: Descriptor; + extra: {resolver: Resolver, resolveOptions: ResolveOptions}; + }) => Promise; /** * Called after the `install` method from the `Project` class successfully * completed. */ - afterAllInstalled?: ( - project: Project, - options: InstallOptions - ) => void; + afterAllInstalled?: ({project, options}: { + project: Project; + options: InstallOptions; + }) => void; /** * Called during the `Validation step` of the `install` method from the * `Project` class. */ - validateProject?: ( - project: Project, + validateProject?: ({project, report}: { + project: Project; report: { reportWarning: (name: MessageName, text: string) => void; reportError: (name: MessageName, text: string) => void; - } - ) => void; + }; + }) => void; /** * Called during the `Validation step` of the `install` method from the * `Project` class by the `validateProject` hook. */ - validateWorkspace?: ( - workspace: Workspace, + validateWorkspace?: ({workspace, report}: { + workspace: Workspace; report: { reportWarning: (name: MessageName, text: string) => void; reportError: (name: MessageName, text: string) => void; - } - ) => void; + }; + }) => void; /** * Used to notify the core of all the potential artifacts of the available * linkers. */ - populateYarnPaths?: ( - project: Project, - definePath: (path: PortablePath | null) => void, - ) => Promise; + populateYarnPaths?: ({project, definePath}: { + project: Project; + definePath: (path: PortablePath | null) => void; + }) => Promise; /** * Called when the user requests to clean the global cache. Plugins should * use this hook to remove their own global artifacts. */ - cleanGlobalArtifacts?: ( - configuration: Configuration, - ) => Promise; + cleanGlobalArtifacts?: ({configuration}: { + configuration: Configuration; + }) => Promise; } export type Plugin = { diff --git a/packages/yarnpkg-core/sources/Project.ts b/packages/yarnpkg-core/sources/Project.ts index 29d8f7e2c0c5..e1ea06fd40c7 100644 --- a/packages/yarnpkg-core/sources/Project.ts +++ b/packages/yarnpkg-core/sources/Project.ts @@ -672,10 +672,14 @@ export class Project { for (const [identHash, descriptor] of pkg.dependencies) { const dependency = await this.configuration.reduceHook(hooks => { return hooks.reduceDependency; - }, descriptor, this, pkg, descriptor, { + }, { + dependency: descriptor, + project: this, + locator: pkg, + initialDependency: descriptor, resolver, resolveOptions, - }); + }, `dependency`); if (!structUtils.areIdentsEqual(descriptor, dependency)) throw new Error(`Assertion failed: The descriptor ident cannot be changed through aliases`); @@ -1510,9 +1514,12 @@ export class Project { }, async () => { await this.configuration.triggerHook(hooks => { return hooks.validateProject; - }, this, { - reportWarning: opts.report.reportWarning.bind(opts.report), - reportError: opts.report.reportError.bind(opts.report), + }, { + project: this, + report: { + reportWarning: opts.report.reportWarning.bind(opts.report), + reportError: opts.report.reportError.bind(opts.report), + }, }); }); diff --git a/packages/yarnpkg-core/sources/httpUtils.ts b/packages/yarnpkg-core/sources/httpUtils.ts index 13bd2e6c94d0..92deb5cf04fe 100644 --- a/packages/yarnpkg-core/sources/httpUtils.ts +++ b/packages/yarnpkg-core/sources/httpUtils.ts @@ -181,7 +181,10 @@ export async function request(target: string | URL, body: Body, {configuration, const executor = await configuration.reduceHook(hooks => { return hooks.wrapNetworkRequest; - }, realRequest, {target, body, configuration, headers, jsonRequest, jsonResponse, method}); + }, { + executor: realRequest, + extra: {target, body, configuration, headers, jsonRequest, jsonResponse, method}, + }, `executor`); return await executor(); } diff --git a/packages/yarnpkg-core/sources/scriptUtils.ts b/packages/yarnpkg-core/sources/scriptUtils.ts index 6aa4fdd3ea8a..436b02d685a4 100644 --- a/packages/yarnpkg-core/sources/scriptUtils.ts +++ b/packages/yarnpkg-core/sources/scriptUtils.ts @@ -172,11 +172,12 @@ export async function makeScriptEnv({project, locator, binFolder, lifecycleScrip if (project) { await project.configuration.triggerHook( - hook => hook.setupScriptEnvironment, - project, - scriptEnv, - async (name: string, argv0: string, args: Array) => { - return await makePathWrapper(binFolder, toFilename(name), argv0, args); + hook => hook.setupScriptEnvironment, { + project, + env: scriptEnv, + makePathWrapper: async (name: string, argv0: string, args: Array) => { + return await makePathWrapper(binFolder, toFilename(name), argv0, args); + }, }, ); } @@ -430,9 +431,15 @@ export async function executePackageScript(locator: Locator, scriptName: string, const executor = await project.configuration.reduceHook(hooks => { return hooks.wrapScriptExecution; - }, realExecutor, project, locator, scriptName, { - script, args, cwd: realCwd, env, stdin, stdout, stderr, - }); + }, { + executor: realExecutor, + project, + locator, + scriptName, + extra: { + script, args, cwd: realCwd, env, stdin, stdout, stderr, + }, + }, `executor`); return await executor(); });