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(codemod): migrate turbo.json dependsOn #2022

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
import merge from "deepmerge";
import {
hasLegacyEnvVarDependencies,
migratePipeline,
migrateConfig,
} from "../src/transforms/migrate-env-var-dependencies";
import type { Schema } from "turbo-types";

const getTestTurboConfig = (override: Schema = { pipeline: {} }): Schema => {
const config = {
$schema: "./docs/public/schema.json",
globalDependencies: ["$GLOBAL_ENV_KEY"],
pipeline: {
test: {
outputs: ["coverage/**/*"],
dependsOn: ["^build"],
},
lint: {
outputs: [],
},
dev: {
cache: false,
},
build: {
outputs: ["dist/**/*", ".next/**/*"],
dependsOn: ["^build", "$TASK_ENV_KEY", "$ANOTHER_ENV_KEY"],
},
},
};

return merge(config, override, {
arrayMerge: (_, sourceArray) => sourceArray,
});
};

describe("migrate-env-var-dependencies", () => {
describe("hasLegacyEnvVarDependencies", () => {
it("finds env keys in legacy turbo.json - has keys", async () => {
const config = getTestTurboConfig();
const { hasKeys, envVars } = hasLegacyEnvVarDependencies(config);
expect(hasKeys).toEqual(true);
expect(envVars).toMatchInlineSnapshot(`
Array [
"$GLOBAL_ENV_KEY",
"$TASK_ENV_KEY",
"$ANOTHER_ENV_KEY",
]
`);
});

it("finds env keys in legacy turbo.json - multiple pipeline keys", async () => {
const config = getTestTurboConfig({
pipeline: { test: { dependsOn: ["$MY_ENV"] } },
});
const { hasKeys, envVars } = hasLegacyEnvVarDependencies(config);
expect(hasKeys).toEqual(true);
expect(envVars).toMatchInlineSnapshot(`
Array [
"$GLOBAL_ENV_KEY",
"$MY_ENV",
"$TASK_ENV_KEY",
"$ANOTHER_ENV_KEY",
]
`);
});

it("finds env keys in legacy turbo.json - no keys", async () => {
// override to exclude keys
const config = getTestTurboConfig({
globalDependencies: [],
pipeline: { build: { dependsOn: [] } },
});
const { hasKeys, envVars } = hasLegacyEnvVarDependencies(config);
expect(hasKeys).toEqual(false);
expect(envVars).toMatchInlineSnapshot(`Array []`);
});
});

describe("migratePipeline", () => {
it("migrates pipeline with env var dependencies", async () => {
const config = getTestTurboConfig();
const { build } = config.pipeline;
const pipeline = migratePipeline(build);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Array [
"TASK_ENV_KEY",
"ANOTHER_ENV_KEY",
]
`);
expect(pipeline?.dependsOn).toMatchInlineSnapshot(`
Array [
"^build",
]
`);
});

it("migrates pipeline with no env var dependencies", async () => {
const config = getTestTurboConfig();
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline.env).toBeUndefined();
expect(pipeline?.dependsOn).toMatchInlineSnapshot(`
Array [
"^build",
]
`);
});

it("migrates pipeline with existing env key", async () => {
const config = getTestTurboConfig({
pipeline: { test: { env: ["$MY_ENV"], dependsOn: ["^build"] } },
tknickman marked this conversation as resolved.
Show resolved Hide resolved
});
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Array [
"$MY_ENV",
]
`);
expect(pipeline?.dependsOn).toMatchInlineSnapshot(`
Array [
"^build",
]
`);
});

it("migrates pipeline with incomplete env key", async () => {
const config = getTestTurboConfig({
pipeline: {
test: { env: ["$MY_ENV"], dependsOn: ["^build", "$SUPER_COOL"] },
},
});
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Array [
tknickman marked this conversation as resolved.
Show resolved Hide resolved
"$MY_ENV",
"SUPER_COOL",
]
`);
expect(pipeline?.dependsOn).toMatchInlineSnapshot(`
Array [
"^build",
]
`);
});

it("migrates pipeline with duplicate env keys", async () => {
const config = getTestTurboConfig({
pipeline: {
test: { env: ["$MY_ENV"], dependsOn: ["^build", "$MY_ENV"] },
tknickman marked this conversation as resolved.
Show resolved Hide resolved
},
});
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Array [
"$MY_ENV",
"MY_ENV",
]
`);
expect(pipeline?.dependsOn).toMatchInlineSnapshot(`
Array [
"^build",
]
`);
});
});

describe("migrateConfig", () => {
it("migrates config with env var dependencies", async () => {
const config = getTestTurboConfig();
const pipeline = migrateConfig(config);
expect(pipeline).toMatchInlineSnapshot(`
Object {
"$schema": "./docs/public/schema.json",
"globalDependencies": Array [],
"globalEnv": Array [
"GLOBAL_ENV_KEY",
],
"pipeline": Object {
"build": Object {
"dependsOn": Array [
"^build",
],
"env": Array [
"TASK_ENV_KEY",
"ANOTHER_ENV_KEY",
],
"outputs": Array [
"dist/**/*",
".next/**/*",
],
},
"dev": Object {
"cache": false,
},
"lint": Object {
"outputs": Array [],
},
"test": Object {
"dependsOn": Array [
"^build",
],
"outputs": Array [
"coverage/**/*",
],
},
},
}
`);
});

it("migrates config with no env var dependencies", async () => {
const config = getTestTurboConfig({
globalDependencies: [],
pipeline: {
build: { dependsOn: ["^build"] },
},
});
const pipeline = migrateConfig(config);
expect(pipeline).toMatchInlineSnapshot(`
Object {
"$schema": "./docs/public/schema.json",
"globalDependencies": Array [],
"pipeline": Object {
"build": Object {
"dependsOn": Array [
"^build",
],
"outputs": Array [
"dist/**/*",
".next/**/*",
],
},
"dev": Object {
"cache": false,
},
"lint": Object {
"outputs": Array [],
},
"test": Object {
"dependsOn": Array [
"^build",
],
"outputs": Array [
"coverage/**/*",
],
},
},
}
`);
});

it("migrates config with inconsistent config", async () => {
const config = getTestTurboConfig({
pipeline: {
test: { env: ["$MY_ENV"], dependsOn: ["^build", "$SUPER_COOL"] },
},
});
const pipeline = migrateConfig(config);
expect(pipeline).toMatchInlineSnapshot(`
Object {
"$schema": "./docs/public/schema.json",
"globalDependencies": Array [],
"globalEnv": Array [
"GLOBAL_ENV_KEY",
],
"pipeline": Object {
"build": Object {
"dependsOn": Array [
"^build",
],
"env": Array [
"TASK_ENV_KEY",
"ANOTHER_ENV_KEY",
],
"outputs": Array [
"dist/**/*",
".next/**/*",
],
},
"dev": Object {
"cache": false,
},
"lint": Object {
"outputs": Array [],
},
"test": Object {
"dependsOn": Array [
"^build",
],
"env": Array [
"$MY_ENV",
"SUPER_COOL",
],
"outputs": Array [
"coverage/**/*",
],
},
},
}
`);
});

it("migrates config with duplicate env keys", async () => {
const config = getTestTurboConfig({
pipeline: {
test: { env: ["$MY_ENV"], dependsOn: ["^build", "$MY_ENV"] },
},
});
const pipeline = migrateConfig(config);
expect(pipeline).toMatchInlineSnapshot(`
Object {
"$schema": "./docs/public/schema.json",
"globalDependencies": Array [],
"globalEnv": Array [
"GLOBAL_ENV_KEY",
],
"pipeline": Object {
"build": Object {
"dependsOn": Array [
"^build",
],
"env": Array [
"TASK_ENV_KEY",
"ANOTHER_ENV_KEY",
],
"outputs": Array [
"dist/**/*",
".next/**/*",
],
},
"dev": Object {
"cache": false,
},
"lint": Object {
"outputs": Array [],
},
"test": Object {
"dependsOn": Array [
"^build",
],
"env": Array [
"$MY_ENV",
"MY_ENV",
],
"outputs": Array [
"coverage/**/*",
],
},
},
}
`);
});
});
});
5 changes: 5 additions & 0 deletions packages/turbo-codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bin": "dist/index.js",
"scripts": {
"build": "tsup",
"test": "jest",
"lint": "eslint src/**/*.ts",
"check-types": "tsc --noEmit"
},
Expand All @@ -38,12 +39,16 @@
"@types/fs-extra": "^9.0.13",
"@types/gradient-string": "^1.1.2",
"@types/inquirer": "^7.3.1",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.12",
"@types/semver": "^7.3.9",
"deepmerge": "^4.2.2",
"eslint": "^7.23.0",
"jest": "^27.4.3",
"semver": "^7.3.5",
"strip-ansi": "^6.0.1",
"tsconfig": "workspace:*",
"turbo-types": "workspace:*",
"tsup": "^5.10.3",
"typescript": "^4.5.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-codemod/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function checkGitStatus(force: boolean) {
clean = isGitClean.sync(process.cwd());
errorMessage = "Git directory is not clean";
} catch (err: any) {
if (err && err.stderr && err.stderr.indexOf("Not a git repository") >= 0) {
if (err && err.stderr && err.stderr.indexOf("not a git repository") >= 0) {
clean = true;
}
}
Expand Down