1
- import { readFileSync , writeFileSync } from 'fs' ;
1
+ import { readFileSync , writeFileSync , existsSync } from 'fs' ;
2
+ import { join } from 'path' ;
3
+ import glob from 'tiny-glob'
2
4
3
5
console . log ( '[Prepublish] Cleansing package.json' ) ;
4
6
@@ -12,5 +14,40 @@ delete packageJson.devDependencies;
12
14
packageJson . peerDependencies = { ...packageJson . dependencies } ;
13
15
delete packageJson . dependencies ;
14
16
17
+ // Iterate all workspaces and hoist the dependencies to the root package.json
18
+ const workspaces = packageJson . workspaces || [ ] ;
19
+ const workspacePromises = workspaces . map ( async workspaceGlob => {
20
+ // Use glob to find the workspace path
21
+ const localWorkspace = workspaceGlob . replace ( / \. \/ s r c / , './dist-cms' ) ;
22
+ const workspacePaths = await glob ( localWorkspace , { cwd : './' , absolute : true } ) ;
23
+
24
+ workspacePaths . forEach ( workspace => {
25
+ const workspacePackageFile = join ( workspace , 'package.json' ) ;
26
+
27
+ // Ensure the workspace package.json exists
28
+ if ( ! existsSync ( workspacePackageFile ) ) {
29
+ // If the package.json does not exist, log a warning and continue
30
+ console . warn ( `No package.json found in workspace: ${ workspace } ` ) ;
31
+ return ;
32
+ }
33
+
34
+ const workspacePackageJson = JSON . parse ( readFileSync ( workspacePackageFile , 'utf8' ) ) ;
35
+
36
+ // Move dependencies from the workspace to the root package.json
37
+ if ( workspacePackageJson . dependencies ) {
38
+ Object . entries ( workspacePackageJson . dependencies ) . forEach ( ( [ key , value ] ) => {
39
+ console . log ( 'Hoisting dependency:' , key , 'from workspace:' , workspace , 'with version:' , value ) ;
40
+ packageJson . peerDependencies [ key ] = value ;
41
+ } ) ;
42
+ }
43
+ } )
44
+ } ) ;
45
+
46
+ // Wait for all workspace processing to complete
47
+ await Promise . all ( workspacePromises ) ;
48
+
49
+ // Remove the workspaces field from the root package.json
50
+ delete packageJson . workspaces ;
51
+
15
52
// Write the package.json back to disk
16
53
writeFileSync ( packageFile , JSON . stringify ( packageJson , null , 2 ) , 'utf8' ) ;
0 commit comments