10
10
*/
11
11
12
12
const { REPO_ROOT } = require ( '../consts' ) ;
13
- const fs = require ( 'fs' ) ;
13
+ const { promises : fs } = require ( 'fs' ) ;
14
14
const glob = require ( 'glob' ) ;
15
15
const path = require ( 'path' ) ;
16
16
@@ -91,7 +91,7 @@ async function parsePackageInfo(
91
91
) /*: Promise<[string, PackageInfo]> */ {
92
92
const packagePath = path . dirname ( packageJsonPath ) ;
93
93
const packageJson /*: PackageJson */ = JSON . parse (
94
- await fs . promises . readFile ( packageJsonPath , 'utf-8' ) ,
94
+ await fs . readFile ( packageJsonPath , 'utf-8' ) ,
95
95
) ;
96
96
97
97
return [
@@ -104,7 +104,52 @@ async function parsePackageInfo(
104
104
] ;
105
105
}
106
106
107
+ /**
108
+ * Update a given package with the package versions.
109
+ */
110
+ async function updatePackageJson (
111
+ packagePath /*: string */ ,
112
+ packageJson /*: PackageJson */ ,
113
+ newPackageVersions /*: $ReadOnly<{[string]: string}> */ ,
114
+ ) /*: Promise<void> */ {
115
+ const packageName = packageJson . name ;
116
+
117
+ if ( packageName in newPackageVersions ) {
118
+ packageJson . version = newPackageVersions [ packageName ] ;
119
+ }
120
+
121
+ for ( const dependencyField of [ 'dependencies' , 'devDependencies' ] ) {
122
+ const deps = packageJson [ dependencyField ] ;
123
+
124
+ if ( deps == null ) {
125
+ continue ;
126
+ }
127
+
128
+ for ( const dependency in newPackageVersions ) {
129
+ if ( dependency in deps ) {
130
+ deps [ dependency ] = newPackageVersions [ dependency ] ;
131
+ }
132
+ }
133
+ }
134
+
135
+ return writePackageJson ( path . join ( packagePath , 'package.json' ) , packageJson ) ;
136
+ }
137
+
138
+ /**
139
+ * Write a `package.json` file to disk.
140
+ */
141
+ async function writePackageJson (
142
+ packageJsonPath /*: string */ ,
143
+ packageJson /*: PackageJson */ ,
144
+ ) /*: Promise<void> */ {
145
+ return fs . writeFile (
146
+ packageJsonPath ,
147
+ JSON . stringify ( packageJson , null , 2 ) + '\n' ,
148
+ ) ;
149
+ }
150
+
107
151
module . exports = {
108
152
getPackages,
109
153
getWorkspaceRoot,
154
+ updatePackageJson,
110
155
} ;
0 commit comments