Skip to content

Commit

Permalink
fix(turbo-utils): support old workspace format
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Apr 24, 2023
1 parent fd52579 commit 4516ba9
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 4 deletions.
@@ -0,0 +1,6 @@
export default function docs() {
if (!process.env.ENV_1) {
return "bar";
}
return "foo";
}
@@ -0,0 +1,4 @@
{
"name": "docs",
"version": "1.0.0"
}
@@ -0,0 +1,6 @@
export default function web() {
if (!process.env.ENV_2) {
return "bar";
}
return "foo";
}
@@ -0,0 +1,4 @@
{
"name": "web",
"version": "1.0.0"
}
@@ -0,0 +1,16 @@
{
"private": true,
"workspaces": {
"packages": [
"apps/*",
"packages/*"
]
},
"scripts": {
"build": "turbo run build"
},
"devDependencies": {
"turbo": "latest"
},
"packageManager": "yarn@1.22.19"
}
@@ -0,0 +1,6 @@
export default function foo() {
if (!process.env.IS_SERVER) {
return "bar";
}
return "foo";
}
@@ -0,0 +1,4 @@
{
"name": "ui",
"version": "1.0.0"
}
@@ -0,0 +1,6 @@
export default function foo() {
if (!process.env.IS_SERVER) {
return "bar";
}
return "foo";
}
@@ -0,0 +1,4 @@
{
"name": "utils",
"version": "1.0.0"
}
@@ -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
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
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

0 comments on commit 4516ba9

Please sign in to comment.