diff --git a/packages/plugin-typescript/README.md b/packages/plugin-typescript/README.md index dedbf7832f1c..7ea17c70e1ab 100644 --- a/packages/plugin-typescript/README.md +++ b/packages/plugin-typescript/README.md @@ -2,14 +2,12 @@ This plugin automatically adds `@types/` packages into your dependencies when you add a package that's covered by one. -This is a crude implementation so far (an ideal implementation should use `preferInteractive` in order to ask the user for confirmation), but it gives an idea of how hooks work. Help appreciated! - ## Usage Since Yarn doesn't support dynamic linking yet, you must clone this repository and generate a bundle manually: ``` -$ yarn build:cli --plugin @berry/typescript +$ yarn build:cli --plugin @berry/plugin-typescript ``` Then put the generated file (`packages/berry-cli/bin/berry.js`) into your project. You can easily try it out by running `yarn add lodash` somewhere: diff --git a/packages/plugin-typescript/sources/index.ts b/packages/plugin-typescript/sources/index.ts index 87a034373dc8..2917551539cc 100644 --- a/packages/plugin-typescript/sources/index.ts +++ b/packages/plugin-typescript/sources/index.ts @@ -1,8 +1,20 @@ import {Cache, Descriptor, Plugin, Workspace} from '@berry/core'; -import {httpUtils, structUtils} from '@berry/core'; +import {structUtils} from '@berry/core'; import {Hooks as EssentialsHooks} from '@berry/plugin-essentials'; import {suggestUtils} from '@berry/plugin-essentials'; +const getDependencyName = (descriptor: Descriptor) => { + return descriptor.scope + ? `@${descriptor.scope}/${descriptor.name}` + : descriptor.name; +}; + +const getTypesName = (descriptor: Descriptor) => { + return descriptor.scope + ? `${descriptor.scope}__${descriptor.name}` + : `${descriptor.name}`; +}; + const afterWorkspaceDependencyAddition = async ( workspace: Workspace, dependencyTarget: suggestUtils.Target, @@ -14,12 +26,9 @@ const afterWorkspaceDependencyAddition = async ( const project = workspace.project; const configuration = project.configuration; const cache = await Cache.find(configuration); + const typesName = getTypesName(descriptor); - const typesName = descriptor.scope - ? `${descriptor.scope}__${descriptor.name}` - : `${descriptor.name}`; - - const target = suggestUtils.Target.REGULAR; + const target = suggestUtils.Target.DEVELOPMENT; const modifier = suggestUtils.Modifier.EXACT; const strategies = [suggestUtils.Strategy.LATEST]; @@ -40,9 +49,30 @@ const afterWorkspaceDependencyAddition = async ( ); }; +const afterWorkspaceDependencyRemoval = async ( + workspace: Workspace, + dependencyTarget: suggestUtils.Target, + descriptor: Descriptor, +) => { + if (descriptor.scope === `types`) + return; + + const target = suggestUtils.Target.DEVELOPMENT; + const typesName = getTypesName(descriptor); + + const ident = structUtils.makeIdent(`types`, typesName); + const current = workspace.manifest[target].get(ident.identHash); + + if (typeof current === `undefined`) + return; + + workspace.manifest[target].delete(ident.identHash); +}; + const plugin: Plugin = { hooks: { afterWorkspaceDependencyAddition, + afterWorkspaceDependencyRemoval, } as ( EssentialsHooks ),