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

fix(turbo-utils): support old workspace format #4682

Merged
merged 1 commit into from
Apr 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function docs() {
if (!process.env.ENV_1) {
return "bar";
}
return "foo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "docs",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function web() {
if (!process.env.ENV_2) {
return "bar";
}
return "foo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "web",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"private": true,
"workspaces": {
"packages": [
"apps/*",
"packages/*"
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like you need 3 workspaces for this test and this use case. I'd try to keep fixtures as small as possible so it's easier to understand what part of them matters

},
"scripts": {
"build": "turbo run build"
},
"devDependencies": {
"turbo": "latest"
},
"packageManager": "yarn@1.22.19"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function foo() {
if (!process.env.IS_SERVER) {
return "bar";
}
return "foo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ui",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function foo() {
if (!process.env.IS_SERVER) {
return "bar";
}
return "foo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "utils",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["**/.env.*local"],
"pipeline": {
"build": {
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
}
33 changes: 31 additions & 2 deletions packages/turbo-utils/__tests__/getTurboConfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("getTurboConfigs", () => {
test: "common",
});

it("single-package", async () => {
it("supports single-package repos", async () => {
const { root } = useFixture({ fixture: `single-package` });
const configs = getTurboConfigs(root);
expect(configs).toHaveLength(1);
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("getTurboConfigs", () => {
`);
});

it("workspace-configs", async () => {
it("supports repos using workspace configs", async () => {
const { root } = useFixture({ fixture: `workspace-configs` });
const configs = getTurboConfigs(root);

Expand Down Expand Up @@ -109,4 +109,33 @@ describe("getTurboConfigs", () => {
}
`);
});

it("supports repos with old workspace configuration format", async () => {
const { root } = useFixture({ fixture: `old-workspace-config` });
const configs = getTurboConfigs(root);

expect(configs).toHaveLength(1);
expect(configs[0].isRootConfig).toBe(true);
expect(configs[0].config).toMatchInlineSnapshot(`
Object {
"$schema": "https://turbo.build/schema.json",
"globalDependencies": Array [
"**/.env.*local",
],
"pipeline": Object {
"build": Object {
"outputs": Array [
".next/**",
"!.next/cache/**",
],
},
"dev": Object {
"cache": false,
"persistent": true,
},
"lint": Object {},
},
}
`);
});
});
16 changes: 14 additions & 2 deletions packages/turbo-utils/src/getTurboConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export type TurboConfigs = Array<{
isRootConfig: boolean;
}>;

interface PackageJson {
turbo?: Schema;
workspaces?: { packages: Array<string> } | Array<string>;
}

interface Options {
cache?: boolean;
}
Expand All @@ -34,8 +39,15 @@ function getWorkspaceGlobs(root: string): Array<string> {
} else {
const packageJson = JSON.parse(
fs.readFileSync(path.join(root, "package.json"), "utf8")
);
return packageJson?.workspaces || [];
) as PackageJson;
if (packageJson?.workspaces) {
// support nested packages workspace format
if ("packages" in packageJson?.workspaces) {
return packageJson.workspaces.packages || [];
}
return packageJson?.workspaces || [];
}
return [];
}
} catch (e) {
return [];
Expand Down