[fs-detectors] Fix turbo@1.8 all filters are inferred (#9680)

We no longer need to specify the active folder for `turbo` to identify the context correctly.
This commit is contained in:
Nathan Hammond
2023-03-31 21:02:30 +00:00
committed by GitHub
parent c29f2b2fbd
commit 10d14488db
5 changed files with 60 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import { packageManagers } from '../package-managers/package-managers';
import { DetectorFilesystem } from '../detectors/filesystem';
import { detectFramework } from '../detect-framework';
import JSON5 from 'json5';
import semver from 'semver';
export class MissingBuildPipeline extends Error {
constructor() {
@@ -52,6 +53,7 @@ export async function getMonorepoDefaultSettings(
]);
let hasBuildPipeline = false;
let turboSemVer = null;
if (turboJSONBuf !== null) {
const turboJSON = JSON5.parse(turboJSONBuf.toString('utf-8'));
@@ -59,12 +61,19 @@ export async function getMonorepoDefaultSettings(
if (turboJSON?.pipeline?.build) {
hasBuildPipeline = true;
}
} else if (packageJSONBuf !== null) {
}
if (packageJSONBuf !== null) {
const packageJSON = JSON.parse(packageJSONBuf.toString('utf-8'));
if (packageJSON?.turbo?.pipeline?.build) {
hasBuildPipeline = true;
}
turboSemVer =
packageJSON?.dependencies?.turbo ||
packageJSON?.devDependencies?.turbo ||
null;
}
if (!hasBuildPipeline) {
@@ -74,17 +83,25 @@ export async function getMonorepoDefaultSettings(
if (projectPath === '/') {
return {
monorepoManager: 'turbo',
buildCommand: 'npx turbo run build',
buildCommand: 'turbo run build',
installCommand: packageManager ? `${packageManager} install` : null,
commandForIgnoringBuildStep: 'npx turbo-ignore',
};
}
let buildCommand = null;
if (projectPath) {
if (turboSemVer && !semver.intersects(turboSemVer, '<1.8.0')) {
buildCommand = `turbo run build`;
} else {
// We don't know for sure if the local `turbo` supports inference.
buildCommand = `cd ${relativeToRoot} && turbo run build --filter={${projectPath}}...`;
}
}
return {
monorepoManager: 'turbo',
buildCommand: projectPath
? `cd ${relativeToRoot} && npx turbo run build --filter={${projectPath}}...`
: null,
buildCommand,
installCommand:
packageManager === 'npm'
? `${packageManager} install --prefix=${relativeToRoot}`

View File

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

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
{ "pipeline": { "build": { "dependsOn": ["^build"], "outputs": ["dist/**"] } } }

View File

@@ -26,24 +26,33 @@ describe('getMonorepoDefaultSettings', () => {
});
test.each([
['turbo', 'turbo', false, 'app-14', false],
['turbo-package-config', 'turbo', false, 'app-13', false],
['turbo-npm', 'turbo', true, 'app-15', false],
['turbo-npm-root-proj', 'turbo', true, 'app-root-proj', true],
['nx', 'nx', false, 'app-12', false],
['nx-package-config', 'nx', false, 'app-11', false],
['nx-project-and-package-config-1', 'nx', false, 'app-10', false],
['nx-project-and-package-config-2', 'nx', false, 'app-9', false],
['nx-project-config', 'nx', false, 'app-8', false],
['turbo', 'turbo', false, 'app-14', false, false],
['turbo-has-filter', 'turbo', false, 'app-14', false, true],
['turbo-package-config', 'turbo', false, 'app-13', false, false],
['turbo-npm', 'turbo', true, 'app-15', false, false],
['turbo-npm-root-proj', 'turbo', true, 'app-root-proj', true, 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],
['nx-project-and-package-config-2', 'nx', false, 'app-9', false, false],
['nx-project-config', 'nx', false, 'app-8', false, false],
])(
'fixture %s',
async (fixture, expectedResultKey, isNpm, packageName, isRoot) => {
async (
fixture,
expectedResultKey,
isNpm,
packageName,
isRoot,
supportsInference
) => {
const expectedResultMap: Record<string, Record<string, string>> = {
turbo: {
monorepoManager: 'turbo',
buildCommand: isRoot
? 'npx turbo run build'
: 'cd ../.. && npx turbo run build --filter={packages/app-1}...',
buildCommand:
isRoot || supportsInference
? 'turbo run build'
: 'cd ../.. && turbo run build --filter={packages/app-1}...',
installCommand:
isNpm && isRoot
? 'npm install'