Skip to content
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

Adds auto-remove types to plugin-typescript #99

Merged
merged 4 commits into from
May 1, 2019
Merged
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
4 changes: 1 addition & 3 deletions packages/plugin-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 36 additions & 6 deletions packages/plugin-typescript/sources/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this probably should be structUtils.stringifyIdent

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,
Expand All @@ -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];

Expand All @@ -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);
Copy link
Member Author

@deini deini Apr 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I should use makeIdent or something like structUtils.parseIdent(typesName).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use makeIdent for names you generate; parseIdent is mostly meant to be used with user input (like CLI parameters, for example).

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
),
Expand Down