Skip to content

Commit

Permalink
Merge branch 'main' into lockfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
EladBezalel committed Dec 11, 2023
2 parents e4812be + a25470c commit 435d82a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 31 deletions.
14 changes: 7 additions & 7 deletions libs/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ const affected = await trueAffected({

### **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` |
| `includeFiles` | `string[]` | Glob patterns to include (relative to projects' source root) | |
| 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.
Expand Down
2 changes: 1 addition & 1 deletion libs/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@traf/core",
"version": "0.0.16",
"version": "0.0.17",
"type": "module",
"license": "MIT",
"description": "Find truly affected pacakges in monorepos",
Expand Down
14 changes: 5 additions & 9 deletions libs/core/src/true-affected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('trueAffected', () => {
it('should ignore files that are not in projects files', async () => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj1/index.spec.ts',
filePath: 'proj1/README.md',
changedLines: [6],
},
]);
Expand Down Expand Up @@ -340,16 +340,12 @@ describe('trueAffected', () => {

it.each([
['regular path', ['package.json'], ['proj3']],
['glob path', ['**/package.json'], ['proj3']],
[
'multiple paths',
['package.json', '**/jest.config.js'],
['proj2', 'proj3'],
],
['regexp path', [/\.spec\.ts$/], ['proj1']],
['multiple paths', ['package.json', /\.spec\.ts$/], ['proj1', 'proj3']],
])('should include files with %s', async (title, filePatterns, expected) => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj2/jest.config.js',
filePath: 'proj1/test.spec.ts',
changedLines: [1],
},
{
Expand Down Expand Up @@ -379,7 +375,7 @@ describe('trueAffected', () => {
tsConfig: 'proj3/tsconfig.json',
},
],
includeFiles: filePatterns,
include: filePatterns,
});

expect(affected).toEqual(expected);
Expand Down
25 changes: 17 additions & 8 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ const ignoredRootNodeTypes = [
SyntaxKind.IfStatement,
];

export const DEFAULT_INCLUDE_TEST_FILES = /\.(spec|test)\.(ts|js)x?/;

export const trueAffected = async ({
cwd,
rootTsConfig,
base = 'origin/main',
projects,
includeFiles = [],
include = [DEFAULT_INCLUDE_TEST_FILES],
}: TrueAffected) => {
const project = new Project({
compilerOptions: {
Expand Down Expand Up @@ -59,10 +61,6 @@ export const trueAffected = async ({
join(resolve(cwd, sourceRoot), '**/*.{ts,js}')
);
}

project.addSourceFilesAtPaths(
includeFiles.map((path) => `${resolve(cwd, sourceRoot)}/${path}`)
);
}
);

Expand All @@ -71,13 +69,13 @@ export const trueAffected = async ({
cwd,
});

const sourceChangedFiles: ChangedFiles[] = changedFiles.filter(
const sourceChangedFiles = changedFiles.filter(
({ filePath }) => project.getSourceFile(resolve(cwd, filePath)) != null
);

const ignoredPaths = ['./node_modules', './dist', './.git'];

const nonSourceChangedFiles: ChangedFiles[] = changedFiles
const nonSourceChangedFiles = changedFiles
.filter(
({ filePath }) =>
!filePath.match(/.*\.(ts|js)x?$/g) &&
Expand Down Expand Up @@ -105,7 +103,18 @@ export const trueAffected = async ({
...changedFilesByLockfile,
];

const affectedPackages = new Set<string>();
const changedIncludedFilesPackages = changedFiles
.filter(({ filePath }) =>
include.some((file) =>
typeof file === 'string'
? filePath.endsWith(file)
: filePath.match(file)
)
)
.map(({ filePath }) => getPackageNameByPath(filePath, projects))
.filter((v): v is string => v != null);

const affectedPackages = new Set<string>(changedIncludedFilesPackages);
const visitedIdentifiers = new Map<string, string[]>();

const findReferencesLibs = (node: Node<ts.Node>) => {
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export interface TrueAffected {
rootTsConfig?: string;
base?: string;
projects: TrueAffectedProject[];
includeFiles?: string[];
include?: (string | RegExp)[];
}
2 changes: 1 addition & 1 deletion libs/nx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@traf/nx",
"version": "0.0.16",
"version": "0.0.17",
"main": "index.js",
"types": "src/index.d.ts",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion libs/nx/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('cli', () => {
rootTsConfig: 'tsconfig.base.json',
base: 'origin/main',
projects: [],
includeFiles: [],
include: [],
});
});

Expand Down
4 changes: 2 additions & 2 deletions libs/nx/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'node:child_process';
import { CommandModule } from 'yargs';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
import { trueAffected } from '@traf/core';
import { DEFAULT_INCLUDE_TEST_FILES, trueAffected } from '@traf/core';
import { getNxTrueAffectedProjects } from './nx';

const color = '#ff0083';
Expand Down Expand Up @@ -43,7 +43,7 @@ export const affectedAction = async ({
rootTsConfig: tsConfigFilePath,
base,
projects,
includeFiles,
include: [...includeFiles, DEFAULT_INCLUDE_TEST_FILES],
});

if (json) {
Expand Down
2 changes: 1 addition & 1 deletion libs/turbo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@traf/turbo",
"version": "0.0.16",
"version": "0.0.17",
"type": "module",
"main": "index.js",
"types": "src/index.d.ts",
Expand Down

0 comments on commit 435d82a

Please sign in to comment.