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

Add codemod to transform literals to wildcards #5054

Merged
merged 2 commits into from
May 24, 2023
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
55 changes: 55 additions & 0 deletions docs/pages/repo/docs/reference/codemods.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ npx @turbo/codemod <transform> <path>
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-env-literals-to-wildcards](#transform-env-literals-to-wildcards)

### `add-package-manager`

Expand Down Expand Up @@ -327,3 +328,57 @@ Run the codemod:
```sh
npx @turbo/codemod stabilize-env-mode
```

### `transform-env-literals-to-wildcards`

<Callout type="info">
Introduced in v1.10.0
</Callout>

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-env-literals-to-wildcards
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": [],
"globalPassThroughEnv": [],
"pipeline": {
"build": {
"env": [],
"passThroughEnv": []
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["NO!", "!!!", "!!!"],
"globalPassThroughEnv": ["DOES", "**BOLD**", "WORK"],
"pipeline": {
"build": {
"env": ["PLAIN", "SMALL_PRINT*"],
"passThroughEnv": ["PASSWORD", "*****"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "transform-env-literals-to-wildcards-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,8 @@
{
"name": "transform-env-literals-to-wildcards-old-config",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "npm@1.2.3",
"turbo": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "transform-env-literals-to-wildcards-docs"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["//"],
"pipeline": {
"build": {
"env": ["NO_DOCS_ENV", "!*!*DOCS"],
"passThroughEnv": ["NO_DOCS_PASSTHROUGH_ENV", "!*!*DOCS"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "transform-env-literals-to-wildcards-website"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["//"],
"pipeline": {
"build": {
"env": ["NO_WEBSITE_ENV", "!*!*WEBSITE"],
"passThroughEnv": ["NO_WEBSITE_PASSTHROUGH_ENV", "!*!*WEBSITE"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"workspaces": [
"apps/*"
],
"packageManager": "yarn@1.22.19"
}
Original file line number Diff line number Diff line change
@@ -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"]
}
}
}
Original file line number Diff line number Diff line change
@@ -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-env-literals-to-wildcards",
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import { transformer } from "../src/transforms/transform-env-literals-to-wildcards";
import { setupTestFixtures } from "@turbo/test-utils";

describe.only("transform-env-literals-to-wildcards", () => {
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "transform-env-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'
);
});
});
Loading