Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: JS package updates for pipeline -> tasks #8253

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/eslint-plugin-turbo/lib/utils/calculate-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import path from "node:path";
import type { WorkspaceConfig } from "@turbo/utils";
import { getWorkspaceConfigs } from "@turbo/utils";
import type { Pipeline } from "@turbo/types";
import type { RootSchema } from "@turbo/types/src/types/config";
import type { RootSchema, RootSchemaV1 } from "@turbo/types/src/types/config";
import { forEachTaskDef } from "@turbo/utils/src/getTurboConfigs";
import { dotEnv } from "./dotenv-processing";
import { wildcardTests } from "./wildcard-processing";

Expand Down Expand Up @@ -131,7 +132,7 @@ function processDotEnv(

function processGlobal(
workspacePath: string,
rootTurboJson: RootSchema
rootTurboJson: RootSchema | RootSchemaV1
): EnvironmentConfig {
return {
legacyConfig: processLegacyConfig(rootTurboJson.globalDependencies),
Expand Down Expand Up @@ -314,7 +315,8 @@ export class Project {
this.projectRoot.turboConfig
);

Object.entries(this.projectRoot.turboConfig.pipeline).forEach(
forEachTaskDef(
this.projectRoot.turboConfig,
([taskName, taskDefinition]) => {
const { workspaceName, scriptName } = getTaskAddress(taskName);
if (workspaceName) {
Expand All @@ -341,7 +343,8 @@ export class Project {
return;
}

Object.entries(projectWorkspace.turboConfig.pipeline).forEach(
forEachTaskDef(
projectWorkspace.turboConfig,
([taskName, taskDefinition]) => {
const { workspaceName: erroneousWorkspaceName, scriptName } =
getTaskAddress(taskName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "set-default-outputs-no-turbo-json",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "npm@1.2.3"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["important.txt"],
"pipeline": {
"build": {
"outputs": ["dist"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function docs() {
if (process.env.ENV_1 === undefined) {
return "does not exist";
}
return "exists";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "docs",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"pipeline": {
"build": {
"env": ["ENV_3"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function web() {
if (!process.env.ENV_2) {
return "bar";
}
return "foo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "web",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"pipeline": {
"build": {
"dependsOn": []
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"workspaces": [
"apps/*",
"packages/*"
],
"scripts": {
"build": "turbo run build"
},
"devDependencies": {
"turbo": "latest"
},
"packageManager": "yarn@1.22.19"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function foo() {
if (!process.env.IS_SERVER) {
return "bar";
}
return "foo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ui",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"pipeline": {
"test": {
"dependsOn": ["build"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"outputs": [".next/**", "!.next/cache/**"],
"dependsOn": ["^build"]
},
"lint": {
"outputs": []
},
"test": {
"outputs": []
},
"dev": {
"cache": false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import merge from "deepmerge";
import type { Schema } from "@turbo/types";
import type { SchemaV1 } from "@turbo/types/src/types/config";
import { setupTestFixtures } from "@turbo/test-utils";
import {
hasLegacyEnvVarDependencies,
Expand All @@ -8,7 +9,9 @@ import {
transformer,
} from "../src/transforms/migrate-env-var-dependencies";

const getTestTurboConfig = (override: Schema = { pipeline: {} }): Schema => {
const getTestTurboConfig = (
override: SchemaV1 = { pipeline: {} }
): SchemaV1 => {
const config = {
$schema: "./docs/public/schema.json",
globalDependencies: ["$GLOBAL_ENV_KEY"],
Expand Down
142 changes: 142 additions & 0 deletions packages/turbo-codemod/__tests__/rename-pipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { setupTestFixtures } from "@turbo/test-utils";
import { transformer } from "../src/transforms/rename-pipeline";

describe("rename-pipeline", () => {
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "rename-pipeline",
});

it("migrates turbo.json pipeline - root config only", () => {
// load the fixture for the test
const { root, read } = useFixture({
fixture: "root-only",
});

// run the transformer
const result = transformer({
root,
options: { force: false, dry: false, print: false },
});

expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
$schema: "https://turbo.build/schema.json",
globalDependencies: ["important.txt"],
tasks: {
build: {
outputs: ["dist"],
},
},
});

expect(result.fatalError).toBeUndefined();
expect(result.changes).toMatchInlineSnapshot(`
Object {
"turbo.json": Object {
"action": "modified",
"additions": 1,
"deletions": 1,
},
}
`);
});

it("migrates turbo.json pipeline - workspace configs", () => {
// load the fixture for the test
const { root, read } = useFixture({
fixture: "workspace-configs",
});

// run the transformer
const result = transformer({
root,
options: { force: false, dry: false, print: false },
});

expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
$schema: "https://turbo.build/schema.json",
tasks: {
build: {
dependsOn: ["^build"],
outputs: [".next/**", "!.next/cache/**"],
},
dev: {
cache: false,
},
lint: {
outputs: [],
},
test: {
outputs: [],
},
},
});

expect(JSON.parse(read("apps/web/turbo.json") || "{}")).toStrictEqual({
$schema: "https://turbo.build/schema.json",
extends: ["//"],
tasks: {
build: {
dependsOn: [],
},
},
});

expect(JSON.parse(read("packages/ui/turbo.json") || "{}")).toStrictEqual({
$schema: "https://turbo.build/schema.json",
extends: ["//"],
tasks: {
test: {
dependsOn: ["build"],
},
},
});

expect(result.fatalError).toBeUndefined();
expect(result.changes).toMatchInlineSnapshot(`
Object {
"apps/docs/turbo.json": Object {
"action": "modified",
"additions": 1,
"deletions": 1,
},
"apps/web/turbo.json": Object {
"action": "modified",
"additions": 1,
"deletions": 1,
},
"packages/ui/turbo.json": Object {
"action": "modified",
"additions": 1,
"deletions": 1,
},
"turbo.json": Object {
"action": "modified",
"additions": 1,
"deletions": 1,
},
}
`);
});

it("errors if no turbo.json can be found", () => {
// load the fixture for the test
const { root, read } = useFixture({
fixture: "no-turbo-json",
});

expect(read("turbo.json")).toBeUndefined();

// run the transformer
const result = transformer({
root,
options: { force: false, dry: false, print: false },
});

expect(read("turbo.json")).toBeUndefined();
expect(result.fatalError).toBeDefined();
expect(result.fatalError?.message).toMatch(
/No turbo\.json found at .*?\. Is the path correct\?/
);
});
});
16 changes: 9 additions & 7 deletions packages/turbo-codemod/src/transforms/clean-globs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import type { Schema as TurboJsonSchema } from "@turbo/types";
import type { SchemaV1 } from "@turbo/types/src/types/config";
import { readJsonSync } from "fs-extra";
import { getTurboConfigs } from "@turbo/utils";
import type { TransformerArgs, Transformer } from "../types";
Expand All @@ -24,7 +24,7 @@ export function transformer({

const turboConfigPath = path.join(root, "turbo.json");

const turboJson = readJsonSync(turboConfigPath) as TurboJsonSchema;
const turboJson = readJsonSync(turboConfigPath) as SchemaV1;
runner.modifyFile({
filePath: turboConfigPath,
after: migrateConfig(turboJson),
Expand All @@ -34,16 +34,18 @@ export function transformer({
const workspaceConfigs = getTurboConfigs(root);
workspaceConfigs.forEach((workspaceConfig) => {
const { config, turboConfigPath: filePath } = workspaceConfig;
runner.modifyFile({
filePath,
after: migrateConfig(config),
});
if ("pipeline" in config) {
runner.modifyFile({
filePath,
after: migrateConfig(config),
});
}
});

return runner.finish();
}

function migrateConfig(config: TurboJsonSchema) {
function migrateConfig(config: SchemaV1) {
const mapGlob = (glob: string) => fixGlobPattern(glob);
for (const [_, taskDef] of Object.entries(config.pipeline)) {
taskDef.inputs = taskDef.inputs?.map(mapGlob);
Expand Down
Loading
Loading