-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod): add missing package names (#8186)
### Description Codemod to add missing name (or fix duplicates)
- Loading branch information
1 parent
29bf2a9
commit 88be732
Showing
24 changed files
with
385 additions
and
27 deletions.
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.