@@ -179,4 +179,27 @@ export default class PackageJson {
179
179
packageJson . dependencies = Object . assign ( { } , packageJson . dependencies , convertComponentsToValidPackageNames ( registryPrefix , components ) ) ;
180
180
await saveRawObject ( packageJson ) ;
181
181
}
182
+ /*
183
+ * For an existing package.json file of the root project, we don't want to do any change, other than what needed.
184
+ * That's why this method doesn't use the 'load' and 'write' methods of this class. Otherwise, it'd write only the
185
+ * PackageJsonPropsNames attributes.
186
+ * Also, in case there is no package.json file in this project, it generates a new one with only the 'dependencies'
187
+ * adds workspaces with private flag if dosent exist.
188
+ */
189
+ static async addWorkspacesToPackageJson ( rootDir : string , componentsDefaultDirectory : string , dependenciesDirectory : string , customImportPath : ?string ) {
190
+ const getRawObject = ( ) => fs . readJson ( composePath ( rootDir ) ) ;
191
+ const saveRawObject = obj => fs . outputJSON ( composePath ( rootDir ) , obj , { spaces : 2 } ) ;
192
+ const getPackageJson = async ( ) => {
193
+ const exist = PackageJson . hasExisting ( rootDir ) ;
194
+ return exist ? getRawObject ( ) : { workspaces : { } , private : true } ;
195
+ } ;
196
+ const pkg = await getPackageJson ( ) ;
197
+ pkg . private = pkg . private || true ;
198
+ const workSpaces = pkg . workspaces || [ ] ;
199
+ workSpaces . push ( dependenciesDirectory ) ;
200
+ workSpaces . push ( componentsDefaultDirectory ) ;
201
+ if ( customImportPath ) workSpaces . push ( customImportPath ) ;
202
+ pkg . workspaces = R . uniq ( workSpaces ) ;
203
+ await saveRawObject ( pkg ) ;
204
+ }
182
205
}
0 commit comments