Skip to content

Commit

Permalink
feat(codemod): add missing package names (#8186)
Browse files Browse the repository at this point in the history
### Description

Codemod to add missing name (or fix duplicates)
  • Loading branch information
tknickman authored and chris-olszewski committed May 28, 2024
1 parent f81c2ee commit f85a4de
Show file tree
Hide file tree
Showing 24 changed files with 385 additions and 27 deletions.
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": "npm@1.2.3"
}
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": "npm@1.2.3"
}
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": "npm@1.2.3"
}
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 () => {
// 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

0 comments on commit f85a4de

Please sign in to comment.