@@ -7,7 +7,7 @@ import { hideBin } from 'yargs/helpers';
77
88import {
99 walk ,
10- getDistTagsForModuleLocations ,
10+ getDistTagsForModules ,
1111 getLernaModules ,
1212 changeScopeInFile ,
1313 setDependencyVersion ,
@@ -36,6 +36,24 @@ function replaceBitGoPackageScope(rootDir: string, targetScope: string): void {
3636 writeFileSync ( path . join ( cwd , 'package.json' ) , JSON . stringify ( json , null , 2 ) + '\n' ) ;
3737}
3838
39+ /**
40+ * Read package.json for a module
41+ * @param module The module to read package.json from
42+ * @returns The parsed package.json content
43+ */
44+ function readModulePackageJson ( module : LernaModule ) : any {
45+ return JSON . parse ( readFileSync ( path . join ( module . location , 'package.json' ) , { encoding : 'utf-8' } ) ) ;
46+ }
47+
48+ /**
49+ * Write package.json for a module
50+ * @param module The module to write package.json to
51+ * @param json The content to write
52+ */
53+ function writeModulePackageJson ( module : LernaModule , json : any ) : void {
54+ writeFileSync ( path . join ( module . location , 'package.json' ) , JSON . stringify ( json , null , 2 ) + '\n' ) ;
55+ }
56+
3957/**
4058 * Increment the version for a single module based on the preid.
4159 *
@@ -51,8 +69,7 @@ function incrementVersionsForModuleLocation(
5169 tags : DistTags | undefined ,
5270 allModules : LernaModule [ ]
5371) : string | undefined {
54- const modulePath = module . location ;
55- const json = JSON . parse ( readFileSync ( path . join ( modulePath , 'package.json' ) , { encoding : 'utf-8' } ) ) ;
72+ const json = readModulePackageJson ( module ) ;
5673
5774 let prevTag : string | undefined = undefined ;
5875
@@ -71,20 +88,23 @@ function incrementVersionsForModuleLocation(
7188 assert ( typeof next === 'string' , `Failed to increment version for ${ json . name } prevTag=${ prevTag } ` ) ;
7289 console . log ( `Setting next version for ${ json . name } to ${ next } ` ) ;
7390 json . version = next ;
74- writeFileSync ( path . join ( modulePath , 'package. json' ) , JSON . stringify ( json , null , 2 ) + '\n' ) ;
91+ writeModulePackageJson ( module , json ) ;
7592
7693 // since we're manually setting new versions, we must also reconcile all other lerna packages to use the 'next' version for this module
7794 allModules . forEach ( ( otherModule ) => {
7895 // skip it for the current version
79- if ( otherModule . location === modulePath ) {
96+ if ( otherModule . location === module . location ) {
8097 return ;
8198 }
8299
83- const otherJsonContent = readFileSync ( path . join ( otherModule . location , 'package.json' ) , { encoding : 'utf-8' } ) ;
84- if ( otherJsonContent . includes ( json . name ) ) {
85- const otherJson = JSON . parse ( otherJsonContent ) ;
100+ // Use readModulePackageJson here instead of direct readFileSync
101+ const otherJson = readModulePackageJson ( otherModule ) ;
102+
103+ // Check if this module depends on the one we're updating
104+ const otherJsonString = JSON . stringify ( otherJson ) ;
105+ if ( otherJsonString . includes ( json . name ) ) {
86106 setDependencyVersion ( otherJson , json . name , next ) ;
87- writeFileSync ( path . join ( otherModule . location , 'package.json' ) , JSON . stringify ( otherJson , null , 2 ) + '\n' ) ;
107+ writeModulePackageJson ( otherModule , otherJson ) ;
88108 }
89109 } ) ;
90110
@@ -100,15 +120,14 @@ function incrementVersionsForModuleLocation(
100120 * @param {LernaModule[] } lernaModules - The modules to update
101121 */
102122async function incrementVersions ( preid : string , lernaModules : LernaModule [ ] ) : Promise < void > {
103- const moduleLocations = lernaModules . map ( ( { location } ) => location ) ;
104- const distTags = await getDistTagsForModuleLocations ( moduleLocations ) ;
123+ const distTags = await getDistTagsForModules ( lernaModules ) ;
105124
106- for ( let i = 0 ; i < lernaModules . length ; i ++ ) {
125+ for ( const m of lernaModules ) {
107126 try {
108- incrementVersionsForModuleLocation ( preid , lernaModules [ i ] , distTags [ i ] , lernaModules ) ;
127+ incrementVersionsForModuleLocation ( preid , m , distTags . get ( m ) , lernaModules ) ;
109128 } catch ( e ) {
110129 // it's not necessarily a blocking error. Let lerna try and publish anyways
111- console . warn ( `Couldn't set next version for ${ lernaModules [ i ] . name } at ${ lernaModules [ i ] . location } ` , e ) ;
130+ console . warn ( `Couldn't set next version for ${ m . name } at ${ m . location } ` , e ) ;
112131 }
113132 }
114133}
0 commit comments