Skip to content

Commit 81d239f

Browse files
committed
fix: improve compatibility with pnpm by using Set for node_modules tracking
1 parent e567283 commit 81d239f

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

packages/agent-cli/src/utils/index.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,32 @@ import path from 'node:path';
33

44
function findAllNodeModulesDirectories(
55
directory: string,
6-
found: string[] = [],
7-
visited: Set<string> = new Set()
8-
): string[] {
6+
found: Set<string> = new Set()
7+
): Set<string> {
98
if (!fs.existsSync(directory)) {
109
return found;
1110
}
1211

13-
const realPath = fs.realpathSync(directory);
14-
if (visited.has(realPath)) {
15-
return found;
16-
}
17-
visited.add(realPath);
18-
1912
const entries = fs
2013
.readdirSync(directory, { withFileTypes: true })
2114
.sort((a, b) => a.name.localeCompare(b.name, 'en'));
2215
for (const entry of entries) {
23-
const fullPath = path.join(directory, entry.name);
16+
const fullPath = path.resolve(directory, entry.name);
2417
// Use statSync to follow symlinks (important for pnpm compatibility)
2518
let isDirectory = false;
2619
try {
27-
isDirectory = fs.statSync(fullPath).isDirectory();
20+
const stats = fs.statSync(fullPath);
21+
isDirectory = stats.isDirectory();
2822
} catch {
2923
// Skip entries that can't be accessed
3024
continue;
3125
}
3226

3327
if (isDirectory) {
3428
if (entry.name === 'node_modules') {
35-
const path = fs.realpathSync(fullPath);
36-
found.push(path);
37-
visited.add(path);
38-
} else {
39-
findAllNodeModulesDirectories(fullPath, found, visited);
29+
found.add(fs.realpathSync(fullPath));
30+
} else if (!entry.name.startsWith('.')){
31+
findAllNodeModulesDirectories(fullPath, found);
4032
}
4133
}
4234
}
@@ -46,7 +38,7 @@ function findAllNodeModulesDirectories(
4638

4739
export const getInstructions = (rootPath: string): string => {
4840
const nodeModulesDirectories = findAllNodeModulesDirectories(rootPath);
49-
if (nodeModulesDirectories.length === 0) {
41+
if (nodeModulesDirectories.size === 0) {
5042
return 'No node_modules folders found.';
5143
}
5244

@@ -67,11 +59,22 @@ export const getInstructions = (rootPath: string): string => {
6759
withFileTypes: true
6860
});
6961
for (const package_ of packages) {
70-
const packagePath = path.join(databaseUxPath, package_.name);
62+
let packagePath = path.resolve(databaseUxPath, package_.name);
7163
// Use statSync to follow symlinks (important for pnpm compatibility)
7264
let isDirectory = false;
7365
try {
74-
isDirectory = fs.statSync(packagePath).isDirectory();
66+
const stats = fs.statSync(packagePath);
67+
isDirectory = stats.isDirectory();
68+
if (!isDirectory && stats.isFile()) {
69+
const content = fs.readFileSync(packagePath, 'utf8').trim();
70+
if (!content.includes('\n')) {
71+
const targetPath = path.resolve(path.dirname(packagePath), content);
72+
if (fs.existsSync(targetPath) && fs.statSync(targetPath).isDirectory()) {
73+
isDirectory = true;
74+
packagePath = targetPath;
75+
}
76+
}
77+
}
7578
} catch {
7679
// Skip entries that can't be accessed
7780
continue;

0 commit comments

Comments
 (0)