# @traf/core

> Find truly affected pacakges in monorepos

Latest version **0.0.30** (published 2025-04-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @traf/core
pnpm add @traf/core
yarn add @traf/core
bun add @traf/core
```

## Health

**Score 40/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.0.30 |
| Published | 2025-04-22 |
| First published | 2023-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 157 |
| Maintainers | eladbezalel |
| Keywords | monorepo, monorepos, nx, nx-workspace, true-affected, affected |

## Links

- npm: https://www.npmjs.com/package/@traf/core
- Repository: https://github.com/lemonade-hq/traf
- Homepage: https://github.com/lemonade-hq/traf#readme
- Issues: https://github.com/lemonade-hq/traf/issues
- npm.io page: https://npm.io/package/@traf/core

## Dependencies (5)

- [nx](https://npm.io/package/nx.md) 17.1.3
- [chalk](https://npm.io/package/chalk.md) ^5.2.0
- [ts-morph](https://npm.io/package/ts-morph.md) ^18.0.0
- [microdiff](https://npm.io/package/microdiff.md) ^1.3.2
- [fast-find-in-files](https://npm.io/package/fast-find-in-files.md) ^1.0.5

## Recent versions

- 0.0.30 (latest) — 2025-04-22
- 0.0.29 — 2025-02-05
- 0.0.28 — 2024-09-30
- 0.0.27 — 2024-09-30
- 0.0.26 — 2024-09-29
- 0.0.25 — 2024-09-12
- 0.0.24 — 2024-08-20
- 0.0.23 — 2024-06-11
- 0.0.22 — 2024-01-02
- 0.0.21 — 2023-12-21
- 0.0.20 — 2023-12-21
- 0.0.19 — 2023-12-21
- 0.0.18 — 2023-12-21
- 0.0.17 — 2023-12-11
- 0.0.16 — 2023-11-27
- … 15 more at https://npm.io/package/@traf/core/versions

## README

# traf 🚀

Avoid unnecessary builds, tests, lint and more in your monorepo CI. Optimize your development process by intelligently finding affected dependencies and selectively triggering builds and tests only when needed.

## Monorepo support

This lib is designed to work with any monorepo, supported by the following packages:

- nx - [`@traf/nx`](https://github.com/lemonade-hq/traf#trafnx)

If you want to add support for another monorepo tool, please open an issue.

## `@traf/core`

A library that finds affected projects in a monorepo, based on the changed **lines** in the current branch.

### Installation

```bash
npm install @traf/core
```

### **Usage**

```ts
import { trueAffected } = from '@traf/core';

const affected = await trueAffected({
  rootTsConfig: 'tsconfig.base.json',
  projects: [
    {
      name: 'proj1',
      sourceRoot: '<project source>',
      tsConfig: '<project source>/tsconfig.json',
    },
    // ...
  ],
})
```

### **Options**

| Option         | Type                    | Description                                                  | Default           |
| -------------- | ----------------------- | ------------------------------------------------------------ | ----------------- |
| `rootTsConfig` | `string`                | The path to the root tsconfig file                           |                   |
| `projects`     | `Project[]`             | An array of projects to check                                |                   |
| `cwd`          | `string`                | The current working directory                                |                   |
| `base`         | `string`                | The base branch to compare against                           | `origin/main`     |
| `include`      | `(string \| Regexp)[]`  | Glob patterns to include (relative to projects' source root) | spec & test files |

> `rootTsConfig` - The path to the root tsconfig file, should include the `paths` prop with all projects mapping so `ts-morph` can find the references.

### **Project**

| Option                 | Type       | Description                                                       |
| ---------------------- | ---------- | ----------------------------------------------------------------- |
| `name`                 | `string`   | The project name                                                  |
| `sourceRoot`           | `string`   | The project source root                                           |
| `tsConfig`             | `string`   | The project tsconfig file (should only include the project files) |
| `implicitDependencies` | `string[]` | An array of implicit dependencies                                 |

### How it works?

The algorithm is based on the following steps:

1. Using git to find all changed lines in the current branch.
2. Using [ts-morph](https://ts-morph.com/) to find the changed element (function, class, const etc..) per line.
3. Using ts-morph [findReferences](https://ts-morph.com/navigation/finding-references#finding-referencing-nodes) to find all references to the changed element recursively.
4. For each reference, find the project that contains the reference and add it to the affected projects list.

---
_Source: https://npm.io/package/@traf/core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
