From 6add4d6177d2112bc3f4687b08b231df08ead086 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Wed, 12 Jun 2024 20:49:19 -0500 Subject: [PATCH] fix(@turbo/codemode): no-op when turbo.json already contains tasks key (#8471) --- .../rename-pipeline/with-tasks/turbo.json | 8 +++++++ .../__tests__/rename-pipeline.ts | 24 +++++++++++++++++++ .../src/transforms/rename-pipeline.ts | 9 ++++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/rename-pipeline/with-tasks/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/rename-pipeline/with-tasks/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/rename-pipeline/with-tasks/turbo.json new file mode 100644 index 0000000000000..3d6dcac06cfac --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/rename-pipeline/with-tasks/turbo.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://turbo.build/schema.json", + "tasks": { + "build": { + "outputs": ["dist"] + } + } +} diff --git a/packages/turbo-codemod/__tests__/rename-pipeline.ts b/packages/turbo-codemod/__tests__/rename-pipeline.ts index c91619588e3ec..18e2cec7e6798 100644 --- a/packages/turbo-codemod/__tests__/rename-pipeline.ts +++ b/packages/turbo-codemod/__tests__/rename-pipeline.ts @@ -139,4 +139,28 @@ describe("rename-pipeline", () => { /No turbo\.json found at .*?\. Is the path correct\?/ ); }); + + it("does not do anything if there is already a top level tasks key", () => { + // load the fixture for the test + const { root, read } = useFixture({ + fixture: "with-tasks", + }); + + // run the transformer + const result = transformer({ + root, + options: { force: false, dryRun: false, print: false }, + }); + + expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({ + $schema: "https://turbo.build/schema.json", + tasks: { + build: { + outputs: ["dist"], + }, + }, + }); + expect(result.fatalError).toBeUndefined(); + expect(result.changes).toStrictEqual({}); + }); }); diff --git a/packages/turbo-codemod/src/transforms/rename-pipeline.ts b/packages/turbo-codemod/src/transforms/rename-pipeline.ts index 6b0045b6c98c4..f5c28f7d42ffe 100644 --- a/packages/turbo-codemod/src/transforms/rename-pipeline.ts +++ b/packages/turbo-codemod/src/transforms/rename-pipeline.ts @@ -36,7 +36,14 @@ export function transformer({ }); } - const turboJson: SchemaV1 = loadTurboJson(turboConfigPath); + const _turboJson: SchemaV1 | Schema = loadTurboJson(turboConfigPath); + if ("tasks" in _turboJson) { + // Don't do anything + log.info("turbo.json already has a tasks key, exiting"); + return runner.finish(); + } + + const turboJson = _turboJson as SchemaV1; runner.modifyFile({ filePath: turboConfigPath, after: migrateConfig(turboJson),