11import fs from 'node:fs/promises' ;
22import path from 'node:path' ;
3+ import { glob } from 'glob' ;
4+ import { parse as parseYaml } from 'yaml' ;
35
46const DEPENDENCY_TYPES = [
57 'dependencies' ,
@@ -8,36 +10,53 @@ const DEPENDENCY_TYPES = [
810 'optionalDependencies'
911] as const ;
1012
13+ interface PnpmWorkspace {
14+ packages ?: string [ ] ;
15+ }
16+
1117export async function runUpdateWorkspace ( _args : string [ ] ) {
12- // Find all package.json files in the workspace
13- const packagesDir = path . join ( process . cwd ( ) , 'packages ' ) ;
18+ const cwd = process . cwd ( ) ;
19+ const workspaceFile = path . join ( cwd , 'pnpm-workspace.yaml ' ) ;
1420
21+ // Read and parse pnpm-workspace.yaml
22+ let workspaceConfig : PnpmWorkspace ;
1523 try {
16- await fs . access ( packagesDir ) ;
24+ const content = await fs . readFile ( workspaceFile , 'utf-8' ) ;
25+ workspaceConfig = parseYaml ( content ) as PnpmWorkspace ;
1726 } catch {
18- throw new Error ( 'No "packages" directory found. Run this command from the monorepo root.' ) ;
27+ throw new Error ( 'No "pnpm-workspace.yaml" found. Run this command from the monorepo root.' ) ;
1928 }
2029
21- const dirs = await fs . readdir ( packagesDir , { withFileTypes : true } ) ;
22- const packageDirs = dirs
23- . filter ( dirent => dirent . isDirectory ( ) )
24- . map ( dirent => dirent . name ) ;
25-
26- const packageFiles : string [ ] = [ ] ;
27- for ( const dir of packageDirs ) {
28- const pkgPath = path . join ( packagesDir , dir , 'package.json' ) ;
29- try {
30- await fs . access ( pkgPath ) ;
31- packageFiles . push ( path . join ( dir , 'package.json' ) ) ;
32- } catch {
33- // Skip if no package.json
34- }
30+ const patterns = workspaceConfig . packages ;
31+ if ( ! patterns || patterns . length === 0 ) {
32+ throw new Error ( 'No package patterns found in pnpm-workspace.yaml' ) ;
33+ }
34+
35+ console . log ( `[makage] Workspace patterns:` , patterns ) ;
36+
37+ // Find all package.json files matching the workspace patterns
38+ const packageJsonPatterns = patterns . map ( p => {
39+ // Convert workspace pattern to package.json glob
40+ // e.g., 'packages/*' -> 'packages/*/package.json'
41+ const normalized = p . replace ( / \/ ? \* \* ? $ / , '' ) ;
42+ return `${ normalized } /*/package.json` ;
43+ } ) ;
44+
45+ const packageFiles = await glob ( packageJsonPatterns , {
46+ cwd,
47+ absolute : false ,
48+ ignore : [ '**/node_modules/**' ]
49+ } ) ;
50+
51+ if ( packageFiles . length === 0 ) {
52+ console . log ( '[makage] No packages found matching workspace patterns' ) ;
53+ return ;
3554 }
3655
3756 // Build a set of internal package names
3857 const internalPackages = new Set < string > ( ) ;
3958 for ( const file of packageFiles ) {
40- const pkgPath = path . join ( packagesDir , file ) ;
59+ const pkgPath = path . join ( cwd , file ) ;
4160 const content = await fs . readFile ( pkgPath , 'utf-8' ) ;
4261 const pkg = JSON . parse ( content ) ;
4362 if ( pkg . name ) {
@@ -50,7 +69,7 @@ export async function runUpdateWorkspace(_args: string[]) {
5069 // Update each package.json
5170 let totalUpdates = 0 ;
5271 for ( const file of packageFiles ) {
53- const pkgPath = path . join ( packagesDir , file ) ;
72+ const pkgPath = path . join ( cwd , file ) ;
5473 const content = await fs . readFile ( pkgPath , 'utf-8' ) ;
5574 const pkg = JSON . parse ( content ) ;
5675
0 commit comments