From 818b798e6d202bad930781d5c8cfe6325047347a Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Mon, 22 May 2023 20:27:56 +0800 Subject: [PATCH 1/2] Add codemod to transform literals to wildcards --- docs/pages/repo/docs/reference/codemods.mdx | 55 +++++ .../has-empty/turbo.json | 11 + .../has-nothing/turbo.json | 6 + .../needs-rewriting/turbo.json | 11 + .../no-turbo-json/package.json | 7 + .../old-config/package.json | 8 + .../workspace-configs/apps/docs/package.json | 3 + .../workspace-configs/apps/docs/turbo.json | 9 + .../apps/website/package.json | 3 + .../workspace-configs/apps/website/turbo.json | 9 + .../workspace-configs/package.json | 7 + .../workspace-configs/turbo.json | 11 + .../get-transforms-for-migration.test.ts | 19 ++ .../transform-literals-to-wildcards.test.ts | 217 ++++++++++++++++++ .../steps/getTransformsForMigration.ts | 17 +- .../transform-literals-to-wildcards.ts | 124 ++++++++++ packages/turbo-types/src/index.ts | 7 +- packages/turbo-types/src/types/config.ts | 9 +- 18 files changed, 525 insertions(+), 8 deletions(-) create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-empty/turbo.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-nothing/turbo.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/needs-rewriting/turbo.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/turbo.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/turbo.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/package.json create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/turbo.json create mode 100644 packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts create mode 100644 packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts create mode 100644 packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts diff --git a/docs/pages/repo/docs/reference/codemods.mdx b/docs/pages/repo/docs/reference/codemods.mdx index 8be3c4d2df45b..d91ce10c169d4 100644 --- a/docs/pages/repo/docs/reference/codemods.mdx +++ b/docs/pages/repo/docs/reference/codemods.mdx @@ -29,6 +29,7 @@ npx @turbo/codemod 3. [migrate-env-var-dependencies](#migrate-env-var-dependencies) 4. [set-default-outputs](#set-default-outputs) 5. [stabilize-env-mode](#stabilize-env-mode) +6. [transform-literals-to-wildcards](#transform-literals-to-wildcards) ### `add-package-manager` @@ -327,3 +328,57 @@ Run the codemod: ```sh npx @turbo/codemod stabilize-env-mode ``` + +### `transform-literals-to-wildcards` + + + Introduced in v1.10.0 + + +Updates any existing env var fields whose contents would be ambiguous to the new wildcard syntax. + +For example: + +```json +// Before, turbo.json +{ + "$schema": "https://turbo.build/schema.json", + "globalEnv": ["THIS_*_IS_LITERAL"], + "globalPassThroughEnv": ["!LITERAL_LEADING_EXCLAMATION"], + "pipeline": { + "build": { + "env": ["50_PERCENT_OFF*_HAS_SMALL_PRINT"], + "passThroughEnv": ["**BOLDED**"], + } + } +} +``` + +```diff +// After, turbo.json +{ + "$schema": "https://turbo.build/schema.json", + "globalEnv": ["THIS_\\*_IS_LITERAL"], + "globalPassThroughEnv": ["\\!LITERAL_LEADING_EXCLAMATION"], + "pipeline": { + "build": { + "env": ["50_PERCENT_OFF\\*_HAS_SMALL_PRINT"], + "passThroughEnv": ["\\*\\*BOLDED\\*\\*"], + } + } +} +``` + +#### Usage + +Go to your project: + +```sh +cd path-to-your-turborepo/ +``` + +Run the codemod: + +```sh +npx @turbo/codemod transform-literals-to-wildcards +``` diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-empty/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-empty/turbo.json new file mode 100644 index 0000000000000..69b8f1e4742e2 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-empty/turbo.json @@ -0,0 +1,11 @@ +{ + "$schema": "https://turbo.build/schema.json", + "globalEnv": [], + "globalPassThroughEnv": [], + "pipeline": { + "build": { + "env": [], + "passThroughEnv": [] + } + } +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-nothing/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-nothing/turbo.json new file mode 100644 index 0000000000000..d4487028869da --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-nothing/turbo.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://turbo.build/schema.json", + "pipeline": { + "build": {} + } +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/needs-rewriting/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/needs-rewriting/turbo.json new file mode 100644 index 0000000000000..29fb6cba4f9af --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/needs-rewriting/turbo.json @@ -0,0 +1,11 @@ +{ + "$schema": "https://turbo.build/schema.json", + "globalEnv": ["NO!", "!!!", "!!!"], + "globalPassThroughEnv": ["DOES", "**BOLD**", "WORK"], + "pipeline": { + "build": { + "env": ["PLAIN", "SMALL_PRINT*"], + "passThroughEnv": ["PASSWORD", "*****"] + } + } +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json new file mode 100644 index 0000000000000..6a8740c4b6aa1 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json @@ -0,0 +1,7 @@ +{ + "name": "transform-literals-to-wildcards-no-turbo-json", + "version": "1.0.0", + "dependencies": {}, + "devDependencies": {}, + "packageManager": "npm@1.2.3" +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json new file mode 100644 index 0000000000000..ad26a872f3166 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json @@ -0,0 +1,8 @@ +{ + "name": "transform-literals-to-wildcards-old-config", + "version": "1.0.0", + "dependencies": {}, + "devDependencies": {}, + "packageManager": "npm@1.2.3", + "turbo": {} +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json new file mode 100644 index 0000000000000..6ad3faf399e18 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json @@ -0,0 +1,3 @@ +{ + "name": "transform-literals-to-wildcards-docs" +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/turbo.json new file mode 100644 index 0000000000000..0df4958c90ae6 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/turbo.json @@ -0,0 +1,9 @@ +{ + "extends": ["//"], + "pipeline": { + "build": { + "env": ["NO_DOCS_ENV", "!*!*DOCS"], + "passThroughEnv": ["NO_DOCS_PASSTHROUGH_ENV", "!*!*DOCS"] + } + } +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json new file mode 100644 index 0000000000000..35d6442fde363 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json @@ -0,0 +1,3 @@ +{ + "name": "transform-literals-to-wildcards-website" +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/turbo.json new file mode 100644 index 0000000000000..c89a552697396 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/turbo.json @@ -0,0 +1,9 @@ +{ + "extends": ["//"], + "pipeline": { + "build": { + "env": ["NO_WEBSITE_ENV", "!*!*WEBSITE"], + "passThroughEnv": ["NO_WEBSITE_PASSTHROUGH_ENV", "!*!*WEBSITE"] + } + } +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/package.json new file mode 100644 index 0000000000000..60fcf9becdd65 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "workspaces": [ + "apps/*" + ], + "packageManager": "yarn@1.22.19" +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/turbo.json new file mode 100644 index 0000000000000..69dfeba1c2bd1 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/turbo.json @@ -0,0 +1,11 @@ +{ + "$schema": "https://turbo.build/schema.json", + "globalEnv": ["!*!*"], + "globalPassThroughEnv": ["!*!*"], + "pipeline": { + "build": { + "env": ["NO_ROOT_ENV", "!*!*ROOT"], + "passThroughEnv": ["NO_ROOT_PASSTHROUGH_ENV", "!*!*ROOT"] + } + } +} diff --git a/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts b/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts new file mode 100644 index 0000000000000..6e7ac1236cd04 --- /dev/null +++ b/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts @@ -0,0 +1,19 @@ +import getCodemodsForMigration from "../src/commands/migrate/steps/getTransformsForMigration"; + +describe("get-transforms-for-migration", () => { + test("ordering", () => { + let results = getCodemodsForMigration({ + fromVersion: "1.0.0", + toVersion: "1.10.0", + }); + + expect(results.map((transform) => transform.value)).toEqual([ + "add-package-manager", + "create-turbo-config", + "migrate-env-var-dependencies", + "set-default-outputs", + "stabilize-env-mode", + "transform-literals-to-wildcards", + ]); + }); +}); diff --git a/packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts b/packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts new file mode 100644 index 0000000000000..9bd0915fe1122 --- /dev/null +++ b/packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts @@ -0,0 +1,217 @@ +import { transformer } from "../src/transforms/transform-literals-to-wildcards"; +import { setupTestFixtures } from "@turbo/test-utils"; + +describe.only("transform-literals-to-wildcards", () => { + const { useFixture } = setupTestFixtures({ + directory: __dirname, + test: "transform-literals-to-wildcards", + }); + + it("migrates wildcards has-empty", async () => { + // load the fixture for the test + const { root, read } = useFixture({ + fixture: "has-empty", + }); + + // 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", + globalEnv: [], + globalPassThroughEnv: [], + pipeline: { + build: { + env: [], + passThroughEnv: [], + }, + }, + }); + + expect(result.fatalError).toBeUndefined(); + expect(result.changes).toMatchInlineSnapshot(` + Object { + "turbo.json": Object { + "action": "unchanged", + "additions": 0, + "deletions": 0, + }, + } + `); + }); + + it("migrates env-mode has-nothing", async () => { + // load the fixture for the test + const { root, read } = useFixture({ + fixture: "has-nothing", + }); + + // 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", + pipeline: { + build: {}, + }, + }); + + expect(result.fatalError).toBeUndefined(); + expect(result.changes).toMatchInlineSnapshot(` + Object { + "turbo.json": Object { + "action": "unchanged", + "additions": 0, + "deletions": 0, + }, + } + `); + }); + + it("migrates env-mode needs-rewriting", async () => { + // load the fixture for the test + const { root, read } = useFixture({ + fixture: "needs-rewriting", + }); + + // 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", + globalEnv: ["NO!", "\\!!!", "\\!!!"], + globalPassThroughEnv: ["DOES", "\\*\\*BOLD\\*\\*", "WORK"], + pipeline: { + build: { + env: ["PLAIN", "SMALL_PRINT\\*"], + passThroughEnv: ["PASSWORD", "\\*\\*\\*\\*\\*"], + }, + }, + }); + + expect(result.fatalError).toBeUndefined(); + expect(result.changes).toMatchInlineSnapshot(` + Object { + "turbo.json": Object { + "action": "modified", + "additions": 4, + "deletions": 4, + }, + } + `); + }); + + it("migrates env-mode workspace-configs", async () => { + // 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", + globalEnv: ["\\!\\*!\\*"], + globalPassThroughEnv: ["\\!\\*!\\*"], + pipeline: { + build: { + env: ["NO_ROOT_ENV", "\\!\\*!\\*ROOT"], + passThroughEnv: ["NO_ROOT_PASSTHROUGH_ENV", "\\!\\*!\\*ROOT"], + }, + }, + }); + + expect(JSON.parse(read("apps/docs/turbo.json") || "{}")).toStrictEqual({ + extends: ["//"], + pipeline: { + build: { + env: ["NO_DOCS_ENV", "\\!\\*!\\*DOCS"], + passThroughEnv: ["NO_DOCS_PASSTHROUGH_ENV", "\\!\\*!\\*DOCS"], + }, + }, + }); + + expect(JSON.parse(read("apps/website/turbo.json") || "{}")).toStrictEqual({ + extends: ["//"], + pipeline: { + build: { + env: ["NO_WEBSITE_ENV", "\\!\\*!\\*WEBSITE"], + passThroughEnv: ["NO_WEBSITE_PASSTHROUGH_ENV", "\\!\\*!\\*WEBSITE"], + }, + }, + }); + + expect(result.fatalError).toBeUndefined(); + expect(result.changes).toMatchInlineSnapshot(` + Object { + "apps/docs/turbo.json": Object { + "action": "modified", + "additions": 2, + "deletions": 2, + }, + "apps/website/turbo.json": Object { + "action": "modified", + "additions": 2, + "deletions": 2, + }, + "turbo.json": Object { + "action": "modified", + "additions": 4, + "deletions": 4, + }, + } + `); + }); + + it("errors if no turbo.json can be found", async () => { + // 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\?/ + ); + }); + + it("errors if package.json config exists and has not been migrated", async () => { + // load the fixture for the test + const { root } = useFixture({ + fixture: "old-config", + }); + + // run the transformer + const result = transformer({ + root, + options: { force: false, dry: false, print: false }, + }); + + expect(result.fatalError).toBeDefined(); + expect(result.fatalError?.message).toMatch( + 'turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first' + ); + }); +}); diff --git a/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts b/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts index 2224c06d7d25b..9313c701aa1ec 100644 --- a/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts +++ b/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts @@ -1,4 +1,4 @@ -import { gt, lte } from "semver"; +import { gt, lte, eq } from "semver"; import loadTransformers from "../../../utils/loadTransformers"; import type { Transformer } from "../../../types"; @@ -13,13 +13,24 @@ function getTransformsForMigration({ fromVersion: string; toVersion: string; }): Array { - const transforms = loadTransformers(); - return transforms.filter((transformer) => { + const transforms = loadTransformers().filter((transformer) => { return ( gt(transformer.introducedIn, fromVersion) && lte(transformer.introducedIn, toVersion) ); }); + + transforms.sort((a, b) => { + if (gt(a.introducedIn, b.introducedIn)) { + return 1; + } else if (eq(a.introducedIn, b.introducedIn)) { + return 0; + } else { + return -1; + } + }); + + return transforms; } export default getTransformsForMigration; diff --git a/packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts b/packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts new file mode 100644 index 0000000000000..f8f2235f31361 --- /dev/null +++ b/packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts @@ -0,0 +1,124 @@ +import path from "path"; +import fs from "fs-extra"; +import { getTurboConfigs } from "@turbo/utils"; +import type { EnvWildcard, Schema as TurboJsonSchema } from "@turbo/types"; + +import type { TransformerArgs } from "../types"; +import getTransformerHelpers from "../utils/getTransformerHelpers"; +import { TransformerResults } from "../runner"; +import { RootSchema } from "@turbo/types/src/types/config"; + +// transformer details +const TRANSFORMER = "transform-literals-to-wildcards"; +const DESCRIPTION = "Rewrite env fields to distinguish wildcards from literals"; +const INTRODUCED_IN = "1.10.0"; + +// Rewriting of environment variable names. +const asteriskLiteral = new RegExp("\\*", "g"); +function transformEnvVarName(envVarName: string): EnvWildcard { + let output = envVarName; + + // Transform leading ! + if (envVarName[0] === "!") { + output = `\\${output}`; + } + + // Transform literal asterisks + output = output.replace(asteriskLiteral, "\\*"); + + return output; +} + +function migrateRootConfig(config: RootSchema) { + let { globalEnv, globalPassThroughEnv } = config; + + if (Array.isArray(globalEnv)) { + config.globalEnv = globalEnv.map(transformEnvVarName); + } + if (Array.isArray(globalPassThroughEnv)) { + config.globalPassThroughEnv = globalPassThroughEnv.map(transformEnvVarName); + } + + return migrateTaskConfigs(config); +} + +function migrateTaskConfigs(config: TurboJsonSchema) { + for (const [_, taskDef] of Object.entries(config.pipeline)) { + let { env, passThroughEnv } = taskDef; + + if (Array.isArray(env)) { + taskDef.env = env.map(transformEnvVarName); + } + if (Array.isArray(passThroughEnv)) { + taskDef.passThroughEnv = passThroughEnv.map(transformEnvVarName); + } + } + + return config; +} + +export function transformer({ + root, + options, +}: TransformerArgs): TransformerResults { + const { log, runner } = getTransformerHelpers({ + transformer: TRANSFORMER, + rootPath: root, + options, + }); + + // If `turbo` key is detected in package.json, require user to run the other codemod first. + const packageJsonPath = path.join(root, "package.json"); + // package.json should always exist, but if it doesn't, it would be a silly place to blow up this codemod + let packageJSON = {}; + + try { + packageJSON = fs.readJSONSync(packageJsonPath); + } catch (e) { + // readJSONSync probably failed because the file doesn't exist + } + + if ("turbo" in packageJSON) { + return runner.abortTransform({ + reason: + '"turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first', + }); + } + + log.info("Rewriting env vars to support wildcards"); + const turboConfigPath = path.join(root, "turbo.json"); + if (!fs.existsSync(turboConfigPath)) { + return runner.abortTransform({ + reason: `No turbo.json found at ${root}. Is the path correct?`, + }); + } + + const turboJson: RootSchema = fs.readJsonSync(turboConfigPath); + runner.modifyFile({ + filePath: turboConfigPath, + after: migrateRootConfig(turboJson), + }); + + // find and migrate any workspace configs + const allTurboJsons = getTurboConfigs(root); + allTurboJsons.forEach((workspaceConfig) => { + const { config, turboConfigPath, isRootConfig } = workspaceConfig; + if (!isRootConfig) { + runner.modifyFile({ + filePath: turboConfigPath, + after: migrateTaskConfigs(config), + }); + } + }); + + return runner.finish(); +} + +const transformerMeta = { + name: `${TRANSFORMER}: ${DESCRIPTION}`, + value: TRANSFORMER, + introducedIn: INTRODUCED_IN, + transformer, +}; + +export default transformerMeta; diff --git a/packages/turbo-types/src/index.ts b/packages/turbo-types/src/index.ts index 1ade0298ebc99..14ed07d50ad95 100644 --- a/packages/turbo-types/src/index.ts +++ b/packages/turbo-types/src/index.ts @@ -1 +1,6 @@ -export type { Schema, Pipeline, RemoteCache } from "./types/config"; +export type { + EnvWildcard, + Schema, + Pipeline, + RemoteCache, +} from "./types/config"; diff --git a/packages/turbo-types/src/types/config.ts b/packages/turbo-types/src/types/config.ts index 315694a8ff793..d91a59fdb19a9 100644 --- a/packages/turbo-types/src/types/config.ts +++ b/packages/turbo-types/src/types/config.ts @@ -69,7 +69,7 @@ export interface RootSchema extends BaseSchema { * * @default [] */ - globalEnv?: string[]; + globalEnv?: EnvWildcard[]; /** * An allowlist of environment variables that should be made to all tasks, but @@ -90,7 +90,7 @@ export interface RootSchema extends BaseSchema { * * @default null */ - globalPassThroughEnv?: null | string[]; + globalPassThroughEnv?: null | EnvWildcard[]; /** * A priority-ordered (most-significant to least-significant) array of project-anchored @@ -143,7 +143,7 @@ export interface Pipeline { * * @default [] */ - env?: string[]; + env?: EnvWildcard[]; /** * An allowlist of environment variables that should be made available in this @@ -166,7 +166,7 @@ export interface Pipeline { * * @default null */ - passThroughEnv?: null | string[]; + passThroughEnv?: null | EnvWildcard[]; /** * A priority-ordered (most-significant to least-significant) array of workspace-anchored @@ -270,3 +270,4 @@ export type OutputMode = | "none"; export type AnchoredUnixPath = string; +export type EnvWildcard = string; From 0731771aed0f976b225b67981e99c156cde7c69e Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Wed, 24 May 2023 18:06:38 +0800 Subject: [PATCH 2/2] Update for feedback --- docs/pages/repo/docs/reference/codemods.mdx | 6 +++--- .../has-empty/turbo.json | 0 .../has-nothing/turbo.json | 0 .../needs-rewriting/turbo.json | 0 .../no-turbo-json/package.json | 2 +- .../old-config/package.json | 2 +- .../workspace-configs/apps/docs/package.json | 3 +++ .../workspace-configs/apps/docs/turbo.json | 0 .../workspace-configs/apps/website/package.json | 3 +++ .../workspace-configs/apps/website/turbo.json | 0 .../workspace-configs/package.json | 0 .../workspace-configs/turbo.json | 0 .../workspace-configs/apps/docs/package.json | 3 --- .../workspace-configs/apps/website/package.json | 3 --- .../__tests__/get-transforms-for-migration.test.ts | 2 +- ....test.ts => transform-env-literals-to-wildcards.test.ts} | 6 +++--- .../src/commands/migrate/steps/getTransformsForMigration.ts | 1 + ...-wildcards.ts => transform-env-literals-to-wildcards.ts} | 2 +- 18 files changed, 17 insertions(+), 16 deletions(-) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/has-empty/turbo.json (100%) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/has-nothing/turbo.json (100%) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/needs-rewriting/turbo.json (100%) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/no-turbo-json/package.json (62%) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/old-config/package.json (66%) create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/docs/package.json rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/workspace-configs/apps/docs/turbo.json (100%) create mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/website/package.json rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/workspace-configs/apps/website/turbo.json (100%) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/workspace-configs/package.json (100%) rename packages/turbo-codemod/__tests__/__fixtures__/{transform-literals-to-wildcards => transform-env-literals-to-wildcards}/workspace-configs/turbo.json (100%) delete mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json delete mode 100644 packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json rename packages/turbo-codemod/__tests__/{transform-literals-to-wildcards.test.ts => transform-env-literals-to-wildcards.test.ts} (96%) rename packages/turbo-codemod/src/transforms/{transform-literals-to-wildcards.ts => transform-env-literals-to-wildcards.ts} (98%) diff --git a/docs/pages/repo/docs/reference/codemods.mdx b/docs/pages/repo/docs/reference/codemods.mdx index d91ce10c169d4..aa8aae4061384 100644 --- a/docs/pages/repo/docs/reference/codemods.mdx +++ b/docs/pages/repo/docs/reference/codemods.mdx @@ -29,7 +29,7 @@ npx @turbo/codemod 3. [migrate-env-var-dependencies](#migrate-env-var-dependencies) 4. [set-default-outputs](#set-default-outputs) 5. [stabilize-env-mode](#stabilize-env-mode) -6. [transform-literals-to-wildcards](#transform-literals-to-wildcards) +6. [transform-env-literals-to-wildcards](#transform-env-literals-to-wildcards) ### `add-package-manager` @@ -329,7 +329,7 @@ Run the codemod: npx @turbo/codemod stabilize-env-mode ``` -### `transform-literals-to-wildcards` +### `transform-env-literals-to-wildcards` Introduced in v1.10.0 @@ -380,5 +380,5 @@ cd path-to-your-turborepo/ Run the codemod: ```sh -npx @turbo/codemod transform-literals-to-wildcards +npx @turbo/codemod transform-env-literals-to-wildcards ``` diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-empty/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/has-empty/turbo.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-empty/turbo.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/has-empty/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-nothing/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/has-nothing/turbo.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/has-nothing/turbo.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/has-nothing/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/needs-rewriting/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/needs-rewriting/turbo.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/needs-rewriting/turbo.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/needs-rewriting/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/no-turbo-json/package.json similarity index 62% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/no-turbo-json/package.json index 6a8740c4b6aa1..59f9fd0a90528 100644 --- a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/no-turbo-json/package.json +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/no-turbo-json/package.json @@ -1,5 +1,5 @@ { - "name": "transform-literals-to-wildcards-no-turbo-json", + "name": "transform-env-literals-to-wildcards-no-turbo-json", "version": "1.0.0", "dependencies": {}, "devDependencies": {}, diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/old-config/package.json similarity index 66% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/old-config/package.json index ad26a872f3166..0fb3b2d5f94b7 100644 --- a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/old-config/package.json +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/old-config/package.json @@ -1,5 +1,5 @@ { - "name": "transform-literals-to-wildcards-old-config", + "name": "transform-env-literals-to-wildcards-old-config", "version": "1.0.0", "dependencies": {}, "devDependencies": {}, diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/docs/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/docs/package.json new file mode 100644 index 0000000000000..e90b6ceca5a21 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/docs/package.json @@ -0,0 +1,3 @@ +{ + "name": "transform-env-literals-to-wildcards-docs" +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/docs/turbo.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/turbo.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/docs/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/website/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/website/package.json new file mode 100644 index 0000000000000..e0aad834c7347 --- /dev/null +++ b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/website/package.json @@ -0,0 +1,3 @@ +{ + "name": "transform-env-literals-to-wildcards-website" +} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/website/turbo.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/turbo.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/apps/website/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/package.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/package.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/package.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/turbo.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/turbo.json similarity index 100% rename from packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/turbo.json rename to packages/turbo-codemod/__tests__/__fixtures__/transform-env-literals-to-wildcards/workspace-configs/turbo.json diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json deleted file mode 100644 index 6ad3faf399e18..0000000000000 --- a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/docs/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "transform-literals-to-wildcards-docs" -} diff --git a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json b/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json deleted file mode 100644 index 35d6442fde363..0000000000000 --- a/packages/turbo-codemod/__tests__/__fixtures__/transform-literals-to-wildcards/workspace-configs/apps/website/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "transform-literals-to-wildcards-website" -} diff --git a/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts b/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts index 6e7ac1236cd04..b19dcbd3ee77d 100644 --- a/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts +++ b/packages/turbo-codemod/__tests__/get-transforms-for-migration.test.ts @@ -13,7 +13,7 @@ describe("get-transforms-for-migration", () => { "migrate-env-var-dependencies", "set-default-outputs", "stabilize-env-mode", - "transform-literals-to-wildcards", + "transform-env-literals-to-wildcards", ]); }); }); diff --git a/packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts b/packages/turbo-codemod/__tests__/transform-env-literals-to-wildcards.test.ts similarity index 96% rename from packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts rename to packages/turbo-codemod/__tests__/transform-env-literals-to-wildcards.test.ts index 9bd0915fe1122..e0bc7735d0f50 100644 --- a/packages/turbo-codemod/__tests__/transform-literals-to-wildcards.test.ts +++ b/packages/turbo-codemod/__tests__/transform-env-literals-to-wildcards.test.ts @@ -1,10 +1,10 @@ -import { transformer } from "../src/transforms/transform-literals-to-wildcards"; +import { transformer } from "../src/transforms/transform-env-literals-to-wildcards"; import { setupTestFixtures } from "@turbo/test-utils"; -describe.only("transform-literals-to-wildcards", () => { +describe.only("transform-env-literals-to-wildcards", () => { const { useFixture } = setupTestFixtures({ directory: __dirname, - test: "transform-literals-to-wildcards", + test: "transform-env-literals-to-wildcards", }); it("migrates wildcards has-empty", async () => { diff --git a/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts b/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts index 9313c701aa1ec..99e83f9f94bb9 100644 --- a/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts +++ b/packages/turbo-codemod/src/commands/migrate/steps/getTransformsForMigration.ts @@ -20,6 +20,7 @@ function getTransformsForMigration({ ); }); + // Sort the transforms from oldest (1.0) to newest (1.10). transforms.sort((a, b) => { if (gt(a.introducedIn, b.introducedIn)) { return 1; diff --git a/packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts b/packages/turbo-codemod/src/transforms/transform-env-literals-to-wildcards.ts similarity index 98% rename from packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts rename to packages/turbo-codemod/src/transforms/transform-env-literals-to-wildcards.ts index f8f2235f31361..e6f69c44b3c46 100644 --- a/packages/turbo-codemod/src/transforms/transform-literals-to-wildcards.ts +++ b/packages/turbo-codemod/src/transforms/transform-env-literals-to-wildcards.ts @@ -9,7 +9,7 @@ import { TransformerResults } from "../runner"; import { RootSchema } from "@turbo/types/src/types/config"; // transformer details -const TRANSFORMER = "transform-literals-to-wildcards"; +const TRANSFORMER = "transform-env-literals-to-wildcards"; const DESCRIPTION = "Rewrite env fields to distinguish wildcards from literals"; const INTRODUCED_IN = "1.10.0";