Filter Pipeline Tasks Using Turborepo

Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

You can set up all of your specific pipelines in the turbo.json file, or you can filter through your pipelines in the terminal. If we wanted to run the typecheck command in just the utils package, our command would look like this: npx turbo run typecheck --filter=@monorepo/utils. You can remove @monorepo as long as the utils package is unique.

Turborepo also gives you the option to filter your commands based on what files have changed on your branch using `--filter="[HEAD^1]".

Now that we know a bit more about Turborepo, we can edit all of the scripts that we build in the previous course to use turbo instead of npm.

Instructor: [0:00] You can use the filter flag to tell Turborepo to run a pipeline only in one package. You just have to specify the package name. In this case, it's going to be @monorepo/utils. [0:14] You can get rid of the monorepo namespace if you want, as long as utils is unique across the monorepo. You can also run the typecheck pipeline in all the dependents of the utils package, if you want to, by adding three dots before the package name, and it will run typecheck in dashboard, utils, and blog.

[0:42] You can also tell Turborepo to run the typecheck pipeline in the dependents of utils, but not in utils, by adding the caret right after the three dots.

[0:54] Hit run type check in dashboard and in block. You can do the same, but instead of building the dependence of a package, you can build the dependencies of a package.

[1:07] In this case, we are using the three dots right after the name of the package, and because we are not using the caret, it will build this package and its dependencies. In this case, utils doesn't have dependencies. That's why we only run type check in the utils package.

[1:26] If we would like to run type check across all the dependencies of a package but not in that package itself, we would have to add the caret right before the three dots. In this case, because utils doesn't have any dependency, we didn't run anything.

[1:45] Turborepo is also capable of running pipelines only on the files that have changed in your branch. Right now, we are in the recordings branch. Let's change a file to see this in action. I am in the index TSX file inside the Next.js application. Let's go in to modify it.

[2:09] Now let's run git status to see my changes. Effectively we have only changed the Next.js application. Now let's run the filter again, and we're going to double quotes here, square brackets, and we're going to say hit caret and one.

[2:26] This means that we are only going to run type check against the files or projects that have changed since the last time we made a commit.

[2:38] Because blog was the only package affected by my changes, we only run typecheck in the blog project. We can also tell Turborepo to run typecheck against the files that have changed compared to the origin main branch. This will run all the three projects because the main branch that is in the remote is completely different from this branch.

[3:02] With all that said, we can go to the package.json file in the root of the project. We can remove a bunch of these scripts. Let's do that. Let's replace npm with turbo. Then, we have the command. Let's remove this -w with --filter=. That should do it.

[3:28] Let's try this out with the dev utils package, so npm run dev utils. Here, as you can see, we are just running the dev script in the utils package.

[3:41] To recap, we can filter out certain packages when Turborepo executes a pipeline. For instance, we can filter by package name. We can filter by dependence, by the dependencies of a package. We can also run Turborepo against what have changed in our branch compared to the origin main or any branch that is in origin.

[4:08] We can also tell Turborepo to run a pipeline against the packages that have changed since the last time we committed our work. We can also tell Turborepo to run the pipelines against whatever have changed between two branches. In this case, base branch could be main and the current branch could be recordings, which is the branch that I'm in now.

[4:32] The last thing that we did here was that we replaced all the npm Workspace-specific commands with Turborepo commands.