1010 *
1111 * Usage: npx tsx scripts/prepare-release.ts [preid] --scope [scope] --root-dir [dir]
1212 */
13- import assert from 'node:assert' ;
1413import { readFileSync , writeFileSync } from 'fs' ;
1514import path from 'path' ;
16- import { inc , lt } from 'semver' ;
1715import yargs from 'yargs' ;
1816import { hideBin } from 'yargs/helpers' ;
1917
@@ -22,8 +20,7 @@ import {
2220 getDistTagsForModules ,
2321 getLernaModules ,
2422 changeScopeInFile ,
25- setDependencyVersion ,
26- DistTags ,
23+ incrementVersionsForModuleLocation ,
2724 LernaModule ,
2825} from './prepareRelease' ;
2926
@@ -60,95 +57,6 @@ function replaceBitGoPackageScope(rootDir: string, targetScope: string): void {
6057 ) ;
6158}
6259
63- /**
64- * Read package.json for a module
65- * @param module The module to read package.json from
66- * @returns The parsed package.json content
67- */
68- function readModulePackageJson ( module : LernaModule ) : any {
69- return JSON . parse (
70- readFileSync ( path . join ( module . location , 'package.json' ) , {
71- encoding : 'utf-8' ,
72- } ) ,
73- ) ;
74- }
75-
76- /**
77- * Write package.json for a module
78- * @param module The module to write package.json to
79- * @param json The content to write
80- */
81- function writeModulePackageJson ( module : LernaModule , json : any ) : void {
82- writeFileSync (
83- path . join ( module . location , 'package.json' ) ,
84- JSON . stringify ( json , null , 2 ) + '\n' ,
85- ) ;
86- }
87-
88- /**
89- * Increment the version for a single module based on the preid.
90- *
91- * @param {String } preid - The prerelease identifier
92- * @param {LernaModule } module - The module to update
93- * @param {DistTags|undefined } tags - The dist tags for the module
94- * @param {LernaModule[] } allModules - All modules for dependency updates
95- * @returns {String|undefined } - The new version if set, undefined otherwise
96- */
97- function incrementVersionsForModuleLocation (
98- preid : string ,
99- module : LernaModule ,
100- tags : DistTags | undefined ,
101- allModules : LernaModule [ ] ,
102- ) : string | undefined {
103- const json = readModulePackageJson ( module ) ;
104-
105- let prevTag : string | undefined = undefined ;
106-
107- if ( tags ) {
108- if ( tags [ preid ] ) {
109- const version = tags [ preid ] . split ( '-' ) ;
110- const latest = tags ?. latest ?. split ( '-' ) ?? [ '0.0.0' ] ;
111- prevTag = lt ( version [ 0 ] , latest [ 0 ] )
112- ? `${ tags . latest } -${ preid } `
113- : tags [ preid ] ;
114- } else {
115- prevTag = `${ tags . latest } -${ preid } ` ;
116- }
117- }
118-
119- if ( prevTag ) {
120- const next = inc ( prevTag , 'prerelease' , undefined , preid ) ;
121- assert (
122- typeof next === 'string' ,
123- `Failed to increment version for ${ json . name } prevTag=${ prevTag } ` ,
124- ) ;
125- console . log ( `Setting next version for ${ json . name } to ${ next } ` ) ;
126- json . version = next ;
127- writeModulePackageJson ( module , json ) ;
128-
129- // since we're manually setting new versions, we must also reconcile all other lerna packages to use the 'next' version for this module
130- allModules . forEach ( ( otherModule ) => {
131- // skip it for the current version
132- if ( otherModule . location === module . location ) {
133- return ;
134- }
135-
136- // Use readModulePackageJson here instead of direct readFileSync
137- const otherJson = readModulePackageJson ( otherModule ) ;
138-
139- // Check if this module depends on the one we're updating
140- const otherJsonString = JSON . stringify ( otherJson ) ;
141- if ( otherJsonString . includes ( json . name ) ) {
142- setDependencyVersion ( otherJson , json . name , next ) ;
143- writeModulePackageJson ( otherModule , otherJson ) ;
144- }
145- } ) ;
146-
147- return next ;
148- }
149- return undefined ;
150- }
151-
15260/**
15361 * increment the version based on the preid.
15462 *
@@ -162,20 +70,12 @@ async function incrementVersions(
16270 const distTags = await getDistTagsForModules ( lernaModules ) ;
16371
16472 for ( const m of lernaModules ) {
165- try {
166- incrementVersionsForModuleLocation (
167- preid ,
168- m ,
169- distTags . get ( m ) ,
170- lernaModules ,
171- ) ;
172- } catch ( e ) {
173- // it's not necessarily a blocking error. Let lerna try and publish anyways
174- console . warn (
175- `Couldn't set next version for ${ m . name } at ${ m . location } ` ,
176- e ,
177- ) ;
178- }
73+ await incrementVersionsForModuleLocation (
74+ preid ,
75+ m ,
76+ distTags . get ( m ) ,
77+ lernaModules ,
78+ ) ;
17979 }
18080}
18181
0 commit comments