@@ -62,34 +62,51 @@ export const getChangedPackages = async (
6262 // Map changed files to packages
6363 for ( const file of changedFiles ) {
6464 // Check if file is in packages directory
65- const packagesMatch = file . match (
66- / ^ p a c k a g e s \/ ( [ @ a - z 0 - 9 ] [ @ a - z 0 - 9 / _ - ] * ) \/ / i,
67- ) ;
68- if ( packagesMatch ?. [ 1 ] ) {
69- const packageDir = packagesMatch [ 1 ] ;
65+ if ( file . startsWith ( "packages/" ) ) {
66+ // Find the nearest package.json
67+ const parts = file . split ( "/" ) ;
68+ // Start from the directory containing the file, go up until we hit "packages"
69+ // parts[0] is "packages"
70+ let packageFound = false ;
71+
72+ // Iterate from deep to shallow, but stop before "packages" (index 0)
73+ for ( let i = parts . length - 1 ; i > 0 ; i -- ) {
74+ const potentialPackageDir = parts . slice ( 1 , i ) . join ( "/" ) ;
75+ if ( ! potentialPackageDir ) continue ;
76+
77+ const packageJsonPath = join (
78+ process . cwd ( ) ,
79+ "packages" ,
80+ potentialPackageDir ,
81+ "package.json" ,
82+ ) ;
7083
71- // Get actual package name from package.json
72- const packageJsonPath = join (
73- process . cwd ( ) ,
74- "packages" ,
75- packageDir ,
76- "package.json" ,
77- ) ;
78- if ( existsSync ( packageJsonPath ) ) {
79- try {
80- const packageJson = JSON . parse (
81- readFileSync ( packageJsonPath , "utf-8" ) ,
82- ) ;
83- if ( packageJson . name ) {
84- changedPackages . add ( packageJson . name ) ;
85- } else {
86- changedPackages . add ( packageDir ) ;
84+ if ( existsSync ( packageJsonPath ) ) {
85+ try {
86+ const packageJson = JSON . parse (
87+ readFileSync ( packageJsonPath , "utf-8" ) ,
88+ ) ;
89+ if ( packageJson . name ) {
90+ changedPackages . add ( packageJson . name ) ;
91+ packageFound = true ;
92+ break ;
93+ }
94+ } catch {
95+ // Ignore invalid package.json
8796 }
88- } catch {
89- changedPackages . add ( packageDir ) ;
9097 }
91- } else {
92- changedPackages . add ( packageDir ) ;
98+ }
99+
100+ // Fallback: if we couldn't find a package.json, try to guess the package directory
101+ // This handles cases where package.json might be deleted or we can't read it
102+ if ( ! packageFound ) {
103+ // Regex that handles scoped and unscoped packages
104+ const packagesMatch = file . match (
105+ / ^ p a c k a g e s \/ ( (?: @ [ ^ / ] + \/ ) ? [ ^ / ] + ) \/ / i,
106+ ) ;
107+ if ( packagesMatch ?. [ 1 ] ) {
108+ changedPackages . add ( packagesMatch [ 1 ] ) ;
109+ }
93110 }
94111 }
95112 }
0 commit comments