Skip to content

Commit a934bc9

Browse files
committed
feat: enhance pnpm workspace support
1 parent 5473877 commit a934bc9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ export function sortPaths(paths: string[]): string[] {
147147
}
148148

149149
export function readYamlFile<T>(filePath: string): T | null {
150+
if (!fs.existsSync(filePath)) {
151+
return null;
152+
}
153+
150154
let doc: T | null;
151155
try {
152156
doc = yaml.load(fs.readFileSync(filePath, "utf8")) as T;
@@ -175,24 +179,22 @@ export function normalizePackageGlobOptions(
175179
: PNPM_WORKSPACE_FILENAME,
176180
);
177181

178-
if (fs.existsSync(pnpmWorkspacePath)) {
179-
const pnpmWorkspace =
180-
readYamlFile<Record<"packages", string[]>>(pnpmWorkspacePath);
182+
const pnpmWorkspaceRes =
183+
readYamlFile<Record<"packages", string[]>>(pnpmWorkspacePath);
181184

182-
if (pnpmWorkspace?.packages?.length) {
183-
packagePatterns = pnpmWorkspace.packages;
184-
}
185+
if (pnpmWorkspaceRes?.packages?.length) {
186+
packagePatterns = pnpmWorkspaceRes.packages;
185187
}
186188
}
187189

188-
if (patterns) {
190+
if (patterns?.length) {
189191
packagePatterns = packagePatterns
190-
? [...packagePatterns, ...patterns]
192+
? unique([...packagePatterns, ...patterns])
191193
: patterns;
192194
}
193195

194196
return {
195-
patterns: packagePatterns ? unique(packagePatterns) : undefined,
197+
patterns: packagePatterns?.length ? packagePatterns : undefined,
196198
...restOptions,
197199
};
198200
}

tests/resolve/monorepo.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ describe("Test monorepo", () => {
1818
});
1919
});
2020

21+
it("pnpm monorepo", () => {
22+
expect(
23+
resolve("@lib/foo", getMonoRepoPath("main/index.js"), {
24+
roots,
25+
alias: { "@lib": "./lib" },
26+
packages: {
27+
pnpmWorkspace: true,
28+
},
29+
}),
30+
).deep.equal({
31+
found: true,
32+
path: getMonoRepoPath("main/lib/foo.js"),
33+
});
34+
});
35+
2136
it("tsconfig", () => {
2237
expect(
2338
resolve("@/a", getMonoRepoPath("packages/ts/index.ts"), {

0 commit comments

Comments
 (0)