diff --git a/scripts/workspace-utils.js b/scripts/workspace-utils.js index 2b3018d6d..9b609ebb9 100644 --- a/scripts/workspace-utils.js +++ b/scripts/workspace-utils.js @@ -98,10 +98,34 @@ function getWorkspaceGlobs(rootPackage) { throw new Error("Unsupported workspace configuration in package.json"); } +function escapeRegExp(value) { + return value.replace(/[.+?^${}()|[\]\\]/g, "\\$&"); +} + +function segmentHasPatternSyntax(segment) { + return segment.includes("*") || /\{[^{}]+,[^{}]*\}/u.test(segment); +} + function segmentPatternToRegExp(segment) { - return new RegExp( - `^${segment.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*")}$`, - ); + let pattern = ""; + for (let index = 0; index < segment.length; index += 1) { + const char = segment[index]; + if (char === "*") { + pattern += "[^/]*"; + continue; + } + if (char === "{") { + const end = segment.indexOf("}", index + 1); + const body = end === -1 ? "" : segment.slice(index + 1, end); + if (body.includes(",")) { + pattern += `(?:${body.split(",").map(escapeRegExp).join("|")})`; + index = end; + continue; + } + } + pattern += escapeRegExp(char); + } + return new RegExp(`^${pattern}$`); } function listDirectories(path) { @@ -120,7 +144,7 @@ function expandWorkspacePattern(rootDir, pattern) { for (const segment of segments) { if (segment === "**") { - const descendants = []; + const descendants = [...directories]; const visit = (dir) => { for (const child of listDirectories(dir)) { descendants.push(child); @@ -130,11 +154,11 @@ function expandWorkspacePattern(rootDir, pattern) { for (const dir of directories) { visit(dir); } - directories = descendants; + directories = Array.from(new Set(descendants)); continue; } - if (segment.includes("*")) { + if (segmentHasPatternSyntax(segment)) { const regexp = segmentPatternToRegExp(segment); directories = directories.flatMap((dir) => listDirectories(dir).filter((child) => diff --git a/test/scripts/workspace-utils.test.ts b/test/scripts/workspace-utils.test.ts new file mode 100644 index 000000000..8f6f16a31 --- /dev/null +++ b/test/scripts/workspace-utils.test.ts @@ -0,0 +1,99 @@ +import { execFileSync } from "node:child_process"; +import { + mkdirSync, + readFileSync, + realpathSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; + +const fixtures: string[] = []; + +function makeFixture() { + const root = join( + tmpdir(), + `maestro-workspace-utils-${process.pid}-${Date.now()}-${fixtures.length}`, + ); + fixtures.push(root); + return root; +} + +function copyScript(root: string, scriptName: string) { + mkdirSync(join(root, "scripts"), { recursive: true }); + writeFileSync( + join(root, "scripts", scriptName), + readFileSync(join(process.cwd(), "scripts", scriptName), "utf8"), + ); +} + +function writePackage(root: string, relativePath: string) { + const packageDir = join(root, relativePath); + mkdirSync(packageDir, { recursive: true }); + writeFileSync( + join(packageDir, "package.json"), + JSON.stringify({ name: relativePath.replaceAll("/", "-") }), + ); +} + +function readWorkspacePaths(root: string) { + const output = execFileSync(process.execPath, ["--input-type=module"], { + cwd: root, + encoding: "utf8", + input: [ + 'import { getWorkspacePackagePaths, loadRootPackage } from "./scripts/workspace-utils.js";', + "console.log(JSON.stringify(await getWorkspacePackagePaths(loadRootPackage())));", + ].join("\n"), + }); + return JSON.parse(output).sort(); +} + +describe("getWorkspacePackagePaths", () => { + afterEach(() => { + for (const fixture of fixtures.splice(0)) { + rmSync(fixture, { recursive: true, force: true }); + } + }); + + it("treats ** as zero or more path segments without glob installed", () => { + const root = makeFixture(); + copyScript(root, "workspace-utils.js"); + writeFileSync( + join(root, "package.json"), + JSON.stringify({ + name: "fixture", + type: "module", + workspaces: ["packages/**/pkg-*"], + }), + ); + writePackage(root, "packages/pkg-a"); + writePackage(root, "packages/nested/pkg-b"); + + expect(readWorkspacePaths(root)).toEqual([ + realpathSync(resolve(root, "packages/nested/pkg-b/package.json")), + realpathSync(resolve(root, "packages/pkg-a/package.json")), + ]); + }); + + it("treats brace segments as alternatives without glob installed", () => { + const root = makeFixture(); + copyScript(root, "workspace-utils.js"); + writeFileSync( + join(root, "package.json"), + JSON.stringify({ + name: "fixture", + type: "module", + workspaces: ["{packages,apps}/*"], + }), + ); + writePackage(root, "apps/app-a"); + writePackage(root, "packages/pkg-a"); + + expect(readWorkspacePaths(root)).toEqual([ + realpathSync(resolve(root, "apps/app-a/package.json")), + realpathSync(resolve(root, "packages/pkg-a/package.json")), + ]); + }); +});