@@ -57,13 +57,36 @@ export function analyzeChanges(extensionPath, extensionFiles, projectPath) {
5757 templateMerges : [ ]
5858 } ;
5959
60+ // Get existing workspaces from root package.json to avoid duplicates
61+ const existingWorkspaces = new Set ( ) ;
62+ try {
63+ const rootPkgPath = path . join ( projectPath , 'package.json' ) ;
64+ if ( fs . existsSync ( rootPkgPath ) ) {
65+ const rootPkg = JSON . parse ( fs . readFileSync ( rootPkgPath , 'utf8' ) ) ;
66+ if ( Array . isArray ( rootPkg . workspaces ) ) {
67+ rootPkg . workspaces . forEach ( w => existingWorkspaces . add ( w ) ) ;
68+ } else if ( rootPkg . workspaces ?. packages ) {
69+ rootPkg . workspaces . packages . forEach ( w => existingWorkspaces . add ( w ) ) ;
70+ }
71+ }
72+ } catch ( error ) {
73+ // If we can't read root package.json, continue anyway
74+ }
75+
6076 // Track which .template.mjs files have corresponding .args.mjs files
6177 const argsFiles = new Set (
6278 extensionFiles
6379 . filter ( f => f . endsWith ( '.args.mjs' ) )
6480 . map ( f => f . replace ( '.args.mjs' , '' ) )
6581 ) ;
6682
83+ // Track packages with package.json for workspace detection
84+ const extensionPackages = new Set (
85+ extensionFiles
86+ . filter ( f => f . replace ( / \\ / g, '/' ) . match ( / ^ p a c k a g e s \/ ( [ ^ / ] + ) \/ p a c k a g e \. j s o n $ / ) )
87+ . map ( f => f . replace ( / \\ / g, '/' ) . match ( / ^ p a c k a g e s \/ ( [ ^ / ] + ) \/ / ) [ 1 ] )
88+ ) ;
89+
6790 for ( const file of extensionFiles ) {
6891 // Handle .args.mjs files
6992 if ( file . endsWith ( '.args.mjs' ) ) {
@@ -155,21 +178,19 @@ export function analyzeChanges(extensionPath, extensionFiles, projectPath) {
155178 }
156179 }
157180
158- // Detect new workspace packages
159- const newPackageDirs = new Set ( ) ;
160- for ( const file of extensionFiles ) {
161- // Normalize path separators for cross-platform compatibility
162- const normalizedFile = file . replace ( / \\ / g, '/' ) ;
163- const match = normalizedFile . match ( / ^ p a c k a g e s \/ ( [ ^ / ] + ) \/ / ) ;
164- if ( match ) {
165- const pkgName = match [ 1 ] ;
166- const pkgDir = path . join ( projectPath , 'packages' , pkgName ) ;
167- if ( ! fs . existsSync ( pkgDir ) ) {
168- newPackageDirs . add ( `packages/${ pkgName } ` ) ;
169- }
181+ // Detect new workspace packages that need registration
182+ // Add packages that: (1) have a package.json in extension, (2) aren't already registered
183+ for ( const pkgName of extensionPackages ) {
184+ const workspaceKey = `packages/${ pkgName } ` ;
185+
186+ // Skip if already registered in root package.json
187+ if ( existingWorkspaces . has ( workspaceKey ) ) {
188+ continue ;
170189 }
190+
191+ // Register as new workspace (even if directory exists but isn't registered)
192+ changes . newWorkspaces . push ( workspaceKey ) ;
171193 }
172- changes . newWorkspaces = Array . from ( newPackageDirs ) ;
173194
174195 return changes ;
175196}
0 commit comments