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): add missing package names #8186

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
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]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ui",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "utils",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
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]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@acme/docs",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@acme/docs",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "some-pkg",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "some-pkg",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
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]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
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 packages/turbo-codemod/__tests__/add-package-names.test.ts
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 () => {
Copy link
Member

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

Copy link
Member Author

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.

// 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 packages/turbo-codemod/__tests__/generate-package-name.test.ts
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);
}
);
});
12 changes: 10 additions & 2 deletions packages/turbo-codemod/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ codemodCli
"Bypass Git safety checks and forcibly run codemods",
false
)
.option("--dry", "Dry run (no changes are made to files)", false)
.option(
"--dry, --dry-run, -d",
"Dry run (no changes are made to files)",
false
)
.option("--print", "Print transformed files to your terminal", false)
.action(migrate);

Expand All @@ -52,7 +56,11 @@ codemodCli
false
)
.option("--list", "List all available transforms", false)
.option("--dry", "Dry run (no changes are made to files)", false)
.option(
"--dry, --dry-run, -d",
"Dry run (no changes are made to files)",
false
)
.option("--print", "Print transformed files to your terminal", false)
.action(transform);

Expand Down
4 changes: 2 additions & 2 deletions packages/turbo-codemod/src/transforms/add-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getWorkspaceDetails, type Project } from "@turbo/workspaces";
import { type PackageJson, getAvailablePackageManagers } from "@turbo/utils";
import { getTransformerHelpers } from "../utils/getTransformerHelpers";
import type { TransformerResults } from "../runner";
import type { TransformerArgs } from "../types";
import type { Transformer, TransformerArgs } from "../types";

// transformer details
const TRANSFORMER = "add-package-manager";
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function transformer({
return runner.finish();
}

const transformerMeta = {
const transformerMeta: Transformer = {
name: TRANSFORMER,
description: DESCRIPTION,
introducedIn: INTRODUCED_IN,
Expand Down
Loading
Loading