feat(turbo): add support for turbo 2 configuration (#11680)

With the release of Turborepo 2 we're renaming `pipeline` to `tasks`. 

This PR updates the default settings logic to look in `tasks` for a
`build` task definition in addition to looking at `pipeline`. It also
updates the message to no longer mention Turbo configuration in
`package.json` as this is fully ignored in Turborepo 2.

Added a quick unit test to verify `build` task definitions are found in
the `tasks` section of `turbo.json`.

Please let me know if there are other tests/places I should update.
This commit is contained in:
Chris Olszewski
2024-06-04 10:36:35 -07:00
committed by GitHub
parent 4337ea0654
commit 5dedc7b2ce
6 changed files with 39 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
'@vercel/fs-detectors': patch
---
Add support for detecting Turborepo 2

View File

@@ -7,10 +7,11 @@ import JSON5 from 'json5';
import semver from 'semver';
export class MissingBuildPipeline extends Error {
constructor() {
super(
'Missing required `build` pipeline in turbo.json or package.json Turbo configuration.'
);
constructor(usesTasks: boolean) {
const message = usesTasks
? 'Missing required `build` task in turbo.json.'
: 'Missing required `build` pipeline in turbo.json or package.json Turbo configuration.';
super(message);
}
}
@@ -65,12 +66,15 @@ export async function getMonorepoDefaultSettings(
]);
let hasBuildPipeline = false;
let hasTurboTasks = false;
let turboSemVer = null;
if (turboJSONBuf !== null) {
const turboJSON = JSON5.parse(turboJSONBuf.toString('utf-8'));
if (turboJSON?.pipeline?.build) {
hasTurboTasks = 'tasks' in (turboJSON || {});
if (turboJSON?.pipeline?.build || turboJSON?.tasks?.build) {
hasBuildPipeline = true;
}
}
@@ -89,7 +93,7 @@ export async function getMonorepoDefaultSettings(
}
if (!hasBuildPipeline) {
throw new MissingBuildPipeline();
throw new MissingBuildPipeline(hasTurboTasks);
}
if (projectPath === '/') {

View File

@@ -0,0 +1,9 @@
{
"private": true,
"workspaces": [
"packages/*"
],
"devDependencies": {
"turbo": "latest"
}
}

View File

@@ -0,0 +1,5 @@
{
"name": "app-14",
"version": "0.0.1",
"main": "index.js"
}

View File

@@ -0,0 +1,2 @@
// TEST COMMENT TO VERIFY JSON5 SUPPORT
{ "tasks": { "build": { "dependsOn": ["^build"], "outputs": ["dist/**"] } } }

View File

@@ -17,11 +17,17 @@ describe('getMonorepoDefaultSettings', () => {
);
});
test('MissingBuildPipeline is an error', () => {
const missingBuildPipeline = new MissingBuildPipeline();
const missingBuildPipeline = new MissingBuildPipeline(false);
expect(missingBuildPipeline).toBeInstanceOf(Error);
expect(missingBuildPipeline.message).toBe(
'Missing required `build` pipeline in turbo.json or package.json Turbo configuration.'
);
const missingBuildTask = new MissingBuildPipeline(true);
expect(missingBuildTask).toBeInstanceOf(Error);
expect(missingBuildTask.message).toBe(
'Missing required `build` task in turbo.json.'
);
});
test.each([
@@ -31,6 +37,7 @@ describe('getMonorepoDefaultSettings', () => {
['turbo-npm', 'turbo', true, 'app-15', false, false],
['turbo-npm-root-proj', 'turbo', true, 'app-root-proj', true, false],
['turbo-latest', 'turbo', false, 'app-14', false, false],
['turbo-2', 'turbo', false, 'app-14', false, false],
['nx', 'nx', false, 'app-12', false, false],
['nx-package-config', 'nx', false, 'app-11', false, false],
['nx-project-and-package-config-1', 'nx', false, 'app-10', false, false],