Skip to content

Commit 3537617

Browse files
committed
fix: parse pnpmworkspace error
1 parent 5b1b93b commit 3537617

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

ts-parser/src/utils/monorepo.ts

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ export class MonorepoUtils {
7575
const pnpmWorkspacePath = path.join(rootPath, 'pnpm-workspace.yaml');
7676
// const yarnWorkspacePath = path.join(rootPath, 'yarn.lock');
7777
const lernaConfigPath = path.join(rootPath, 'lerna.json');
78-
79-
return fs.existsSync(edenConfigPath) ||
80-
fs.existsSync(pnpmWorkspacePath) ||
81-
// fs.existsSync(yarnWorkspacePath) ||
82-
fs.existsSync(lernaConfigPath);
78+
79+
return fs.existsSync(edenConfigPath) ||
80+
fs.existsSync(pnpmWorkspacePath) ||
81+
// fs.existsSync(yarnWorkspacePath) ||
82+
fs.existsSync(lernaConfigPath);
8383
}
8484

8585
/**
@@ -90,7 +90,7 @@ export class MonorepoUtils {
9090
const pnpmWorkspacePath = path.join(rootPath, 'pnpm-workspace.yaml');
9191
// const yarnWorkspacePath = path.join(rootPath, 'yarn.lock');
9292
const lernaConfigPath = path.join(rootPath, 'lerna.json');
93-
93+
9494
if (fs.existsSync(edenConfigPath)) {
9595
return { type: 'eden', configPath: edenConfigPath };
9696
}
@@ -103,7 +103,7 @@ export class MonorepoUtils {
103103
if (fs.existsSync(lernaConfigPath)) {
104104
return { type: 'lerna', configPath: lernaConfigPath };
105105
}
106-
106+
107107
return null;
108108
}
109109

@@ -115,10 +115,10 @@ export class MonorepoUtils {
115115
if (!fs.existsSync(configPath)) {
116116
return null;
117117
}
118-
118+
119119
const configContent = fs.readFileSync(configPath, 'utf-8');
120120
const config: EdenMonorepoConfig = JSON.parse(configContent);
121-
121+
122122
return config;
123123
} catch (error) {
124124
console.warn(`Failed to parse Eden monorepo config at ${configPath}:`, error);
@@ -133,34 +133,34 @@ export class MonorepoUtils {
133133
*/
134134
static getEdenPackages(rootPath: string, config: EdenMonorepoConfig): MonorepoPackage[] {
135135
const packages: MonorepoPackage[] = [];
136-
136+
137137
if (config.pnpmWorkspace && config.pnpmWorkspace.packages && config.pnpmWorkspace.packages.length > 0) {
138138
for (const workspace of config.pnpmWorkspace.packages) {
139139
const workspacePackages = this.expandWorkspacePattern(rootPath, workspace);
140140
packages.push(...workspacePackages);
141141
}
142142
return packages; // Return early if pnpmWorkspace is configured
143143
}
144-
144+
145145
// Handle new workspaces array format
146146
if (config.workspaces && config.workspaces.length > 0) {
147147
for (const workspace of config.workspaces) {
148148
const workspacePackages = this.expandWorkspacePattern(rootPath, workspace);
149149
packages.push(...workspacePackages);
150150
}
151151
}
152-
152+
153153
// Handle legacy packages array format
154154
if (config.packages && config.packages.length > 0) {
155155
for (const pkg of config.packages) {
156156
const absolutePath = path.resolve(rootPath, pkg.path);
157-
157+
158158
// Check if package directory exists
159159
if (fs.existsSync(absolutePath)) {
160160
// Try to get package name from package.json
161161
let packageName: string | undefined;
162162
const packageJsonPath = path.join(absolutePath, 'package.json');
163-
163+
164164
if (fs.existsSync(packageJsonPath)) {
165165
try {
166166
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
@@ -169,7 +169,7 @@ export class MonorepoUtils {
169169
console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
170170
}
171171
}
172-
172+
173173
packages.push({
174174
path: pkg.path,
175175
absolutePath,
@@ -181,7 +181,7 @@ export class MonorepoUtils {
181181
}
182182
}
183183
}
184-
184+
185185
return packages;
186186
}
187187

@@ -191,33 +191,33 @@ export class MonorepoUtils {
191191
*/
192192
private static expandWorkspacePattern(rootPath: string, pattern: string): MonorepoPackage[] {
193193
const packages: MonorepoPackage[] = [];
194-
194+
195195
try {
196196
// Handle glob patterns
197197
if (pattern.includes('*')) {
198198
const basePath = pattern.replace('/*', '');
199199
const baseDir = path.resolve(rootPath, basePath);
200-
200+
201201
if (fs.existsSync(baseDir)) {
202202
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
203-
203+
204204
for (const entry of entries) {
205205
if (entry.isDirectory()) {
206206
const packagePath = path.join(basePath, entry.name);
207207
const absolutePath = path.resolve(rootPath, packagePath);
208208
const packageJsonPath = path.join(absolutePath, 'package.json');
209-
209+
210210
// Only include directories that have package.json
211211
if (fs.existsSync(packageJsonPath)) {
212212
let packageName: string | undefined;
213-
213+
214214
try {
215215
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
216216
packageName = packageJson.name;
217217
} catch (error) {
218218
console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
219219
}
220-
220+
221221
packages.push({
222222
path: packagePath,
223223
absolutePath,
@@ -232,17 +232,17 @@ export class MonorepoUtils {
232232
// Handle exact path
233233
const absolutePath = path.resolve(rootPath, pattern);
234234
const packageJsonPath = path.join(absolutePath, 'package.json');
235-
235+
236236
if (fs.existsSync(packageJsonPath)) {
237237
let packageName: string | undefined;
238-
238+
239239
try {
240240
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
241241
packageName = packageJson.name;
242242
} catch (error) {
243243
console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
244244
}
245-
245+
246246
packages.push({
247247
path: pattern,
248248
absolutePath,
@@ -254,7 +254,7 @@ export class MonorepoUtils {
254254
} catch (error) {
255255
console.warn(`Failed to expand workspace pattern "${pattern}":`, error);
256256
}
257-
257+
258258
return packages;
259259
}
260260

@@ -263,11 +263,11 @@ export class MonorepoUtils {
263263
*/
264264
static getMonorepoPackages(rootPath: string): MonorepoPackage[] {
265265
const monorepoInfo = this.detectMonorepoType(rootPath);
266-
266+
267267
if (!monorepoInfo) {
268268
return [];
269269
}
270-
270+
271271
switch (monorepoInfo.type) {
272272
case 'eden': {
273273
const config = this.parseEdenMonorepoConfig(monorepoInfo.configPath);
@@ -287,31 +287,33 @@ export class MonorepoUtils {
287287
continue;
288288
}
289289
if (inPackages && line.trim().startsWith('-')) {
290-
const glob = line.trim().substring(1).trim().replace(/'/g, '').replace(/"/g, '');
290+
let glob = line.trim().substring(1).trim().replace(/'/g, '').replace(/"/g, '');
291291
if (glob.endsWith('/*')) {
292-
const packageDir = path.join(rootPath, glob.slice(0, -2));
293-
if (fs.existsSync(packageDir) && fs.statSync(packageDir).isDirectory()) {
294-
const packageNames = fs.readdirSync(packageDir);
295-
for (const pkgName of packageNames) {
296-
const pkgAbsolutePath = path.join(packageDir, pkgName);
297-
if (fs.statSync(pkgAbsolutePath).isDirectory()) {
298-
const pkgRelativePath = path.relative(rootPath, pkgAbsolutePath);
299-
let packageName: string | undefined;
300-
const packageJsonPath = path.join(pkgAbsolutePath, 'package.json');
301-
if (fs.existsSync(packageJsonPath)) {
302-
try {
303-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
304-
packageName = packageJson.name;
305-
} catch (error) {
306-
console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
307-
}
292+
glob = glob.slice(0, -2);
293+
}
294+
let packageDir = path.join(rootPath, glob);
295+
if (fs.existsSync(packageDir) && fs.statSync(packageDir).isDirectory()) {
296+
const packageNames = fs.readdirSync(packageDir);
297+
packageNames.push(".");
298+
for (const pkgName of packageNames) {
299+
const pkgAbsolutePath = path.join(packageDir, pkgName);
300+
if (fs.statSync(pkgAbsolutePath).isDirectory()) {
301+
const pkgRelativePath = path.relative(rootPath, pkgAbsolutePath);
302+
let packageName: string | undefined;
303+
const packageJsonPath = path.join(pkgAbsolutePath, 'package.json');
304+
if (fs.existsSync(packageJsonPath)) {
305+
try {
306+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
307+
packageName = packageJson.name;
308+
packages.push({
309+
path: pkgRelativePath,
310+
absolutePath: pkgAbsolutePath,
311+
shouldPublish: false, // Cannot determine from pnpm-workspace.yaml
312+
name: packageName
313+
});
314+
} catch (error) {
315+
console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
308316
}
309-
packages.push({
310-
path: pkgRelativePath,
311-
absolutePath: pkgAbsolutePath,
312-
shouldPublish: false, // Cannot determine from pnpm-workspace.yaml
313-
name: packageName
314-
});
315317
}
316318
}
317319
}
@@ -321,14 +323,15 @@ export class MonorepoUtils {
321323
break;
322324
}
323325
}
326+
console.log('pnpm packages:', packages);
324327
return packages;
325328
}
326329
// TODO: Add support for other monorepo types (yarn, lerna)
327330
default:
328331
console.warn(`Monorepo type '${monorepoInfo.type}' is not yet supported`);
329332
break;
330333
}
331-
334+
332335
return [];
333336
}
334337

@@ -337,14 +340,14 @@ export class MonorepoUtils {
337340
*/
338341
static findPackageForPath(filePath: string, packages: MonorepoPackage[]): MonorepoPackage | null {
339342
const absoluteFilePath = path.resolve(filePath);
340-
343+
341344
for (const pkg of packages) {
342-
if (absoluteFilePath.startsWith(pkg.absolutePath + path.sep) ||
343-
absoluteFilePath === pkg.absolutePath) {
345+
if (absoluteFilePath.startsWith(pkg.absolutePath + path.sep) ||
346+
absoluteFilePath === pkg.absolutePath) {
344347
return pkg;
345348
}
346349
}
347-
350+
348351
return null;
349352
}
350353

0 commit comments

Comments
 (0)