-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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): add missing package names #8186
Merged
tknickman
merged 2 commits into
turborepo_2
from
tomknickman/turbo-3148-codemod-to-add-name-field-where-missing
May 23, 2024
+385
−27
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/turbo-codemod/__tests__/__fixtures__/add-package-names/correct-names/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "root", | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
6 changes: 6 additions & 0 deletions
6
...o-codemod/__tests__/__fixtures__/add-package-names/correct-names/packages/ui/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "ui", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
...odemod/__tests__/__fixtures__/add-package-names/correct-names/packages/utils/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "utils", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/turbo-codemod/__tests__/__fixtures__/add-package-names/duplicate-names/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "root", | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
6 changes: 6 additions & 0 deletions
6
.../__tests__/__fixtures__/add-package-names/duplicate-names/packages/apps/docs/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@acme/docs", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
...d/__tests__/__fixtures__/add-package-names/duplicate-names/packages/apps/web/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@acme/docs", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
...codemod/__tests__/__fixtures__/add-package-names/duplicate-names/packages/ui/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "some-pkg", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
...emod/__tests__/__fixtures__/add-package-names/duplicate-names/packages/utils/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "some-pkg", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/turbo-codemod/__tests__/__fixtures__/add-package-names/missing-names/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "root", | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
5 changes: 5 additions & 0 deletions
5
...o-codemod/__tests__/__fixtures__/add-package-names/missing-names/packages/ui/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
5 changes: 5 additions & 0 deletions
5
...odemod/__tests__/__fixtures__/add-package-names/missing-names/packages/utils/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
117 changes: 117 additions & 0 deletions
117
packages/turbo-codemod/__tests__/add-package-names.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { setupTestFixtures } from "@turbo/test-utils"; | ||
import { transformer } from "../src/transforms/add-package-names"; | ||
|
||
describe("add-package-names", () => { | ||
const { useFixture } = setupTestFixtures({ | ||
directory: __dirname, | ||
test: "add-package-names", | ||
}); | ||
|
||
test("missing names", async () => { | ||
// load the fixture for the test | ||
const { root, readJson } = useFixture({ | ||
fixture: "missing-names", | ||
}); | ||
|
||
// run the transformer | ||
const result = await transformer({ | ||
root, | ||
options: { force: false, dry: false, print: false }, | ||
}); | ||
|
||
// result should be correct | ||
expect(result.fatalError).toBeUndefined(); | ||
expect(result.changes).toMatchInlineSnapshot(` | ||
Object { | ||
"packages/ui/package.json": Object { | ||
"action": "modified", | ||
"additions": 1, | ||
"deletions": 0, | ||
}, | ||
"packages/utils/package.json": Object { | ||
"action": "modified", | ||
"additions": 1, | ||
"deletions": 0, | ||
}, | ||
} | ||
`); | ||
|
||
// validate unique names | ||
const names = new Set(); | ||
|
||
for (const pkg of ["ui", "utils"]) { | ||
const pkgJson = readJson<{ name: string }>( | ||
`packages/${pkg}/package.json` | ||
); | ||
expect(pkgJson?.name).toBeDefined(); | ||
expect(names.has(pkgJson?.name)).toBe(false); | ||
names.add(pkgJson?.name); | ||
} | ||
}); | ||
|
||
test("duplicate names", async () => { | ||
// load the fixture for the test | ||
const { root, readJson } = useFixture({ | ||
fixture: "duplicate-names", | ||
}); | ||
|
||
// run the transformer | ||
const result = await transformer({ | ||
root, | ||
options: { force: false, dry: false, print: false }, | ||
}); | ||
|
||
// result should be correct | ||
expect(result.fatalError).toBeUndefined(); | ||
expect(result.changes).toMatchInlineSnapshot(` | ||
Object { | ||
"packages/utils/package.json": Object { | ||
"action": "modified", | ||
"additions": 1, | ||
"deletions": 1, | ||
}, | ||
} | ||
`); | ||
|
||
// validate unique names | ||
const names = new Set(); | ||
|
||
for (const pkg of ["ui", "utils"]) { | ||
const pkgJson = readJson<{ name: string }>( | ||
`packages/${pkg}/package.json` | ||
); | ||
expect(pkgJson?.name).toBeDefined(); | ||
expect(names.has(pkgJson?.name)).toBe(false); | ||
names.add(pkgJson?.name); | ||
} | ||
}); | ||
|
||
test("correct names", async () => { | ||
// load the fixture for the test | ||
const { root, readJson } = useFixture({ | ||
fixture: "correct-names", | ||
}); | ||
|
||
// run the transformer | ||
const result = await transformer({ | ||
root, | ||
options: { force: false, dry: false, print: false }, | ||
}); | ||
|
||
// result should be correct | ||
expect(result.fatalError).toBeUndefined(); | ||
expect(result.changes).toMatchInlineSnapshot(`Object {}`); | ||
|
||
// validate unique names | ||
const names = new Set(); | ||
|
||
for (const pkg of ["ui", "utils"]) { | ||
const pkgJson = readJson<{ name: string }>( | ||
`packages/${pkg}/package.json` | ||
); | ||
expect(pkgJson?.name).toBeDefined(); | ||
expect(names.has(pkgJson?.name)).toBe(false); | ||
names.add(pkgJson?.name); | ||
} | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/turbo-codemod/__tests__/generate-package-name.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { getNewPkgName } from "../src/transforms/add-package-names"; | ||
|
||
describe("getNewPkgName", () => { | ||
it.each([ | ||
{ | ||
pkgPath: "/packages/ui/package.json", | ||
pkgName: "old-name", | ||
expected: "ui-old-name", | ||
}, | ||
// scoped | ||
{ | ||
pkgPath: "/packages/ui/package.json", | ||
pkgName: "@acme/name", | ||
expected: "@acme/ui-name", | ||
}, | ||
// no name | ||
{ | ||
pkgPath: "/packages/ui/package.json", | ||
pkgName: undefined, | ||
expected: "ui", | ||
}, | ||
])( | ||
"should return a new package name for pkgPath: $pkgPath and pkgName: $pkgName", | ||
({ pkgPath, pkgName, expected }) => { | ||
const newName = getNewPkgName({ pkgPath, pkgName }); | ||
expect(newName).toBe(expected); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW we've never supported duplicate package names in the workspace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea - this was an easy add since if we're generating new names we need to ensure they're unique, and if we're ensuring new ones are unique we should ensure they're all unique.
There still might be an edge case or two here, but this should be good for most cases.