1- import { readdirSync , readFileSync , statSync , writeFileSync } from 'fs' ;
1+ import * as assert from 'node:assert' ;
2+ import { readFileSync , writeFileSync } from 'fs' ;
23import * as path from 'path' ;
34import { inc } from 'semver' ;
4- import { changeScopeInFile , getLernaModules } from './changePackageScope' ;
5+ import {
6+ walk ,
7+ getDistTagsForModuleLocations ,
8+ getLernaModules ,
9+ changeScopeInFile ,
10+ setDependencyVersion ,
11+ } from './prepareRelease' ;
512
613let lernaModules : string [ ] = [ ] ;
714let lernaModuleLocations : string [ ] = [ ] ;
815let TARGET_SCOPE = '@bitgo-beta' ;
916let filesChanged = 0 ;
1017
1118const setLernaModules = async ( ) : Promise < void > => {
12- ( { lernaModules, lernaModuleLocations } = await getLernaModules ( ) ) ;
13- } ;
14-
15- const walk = ( dir : string ) : string [ ] => {
16- let results : string [ ] = [ ] ;
17- const ignoredFolders = [ / n o d e _ m o d u l e s / ] ;
18- const list = readdirSync ( dir ) ;
19- list . forEach ( ( file ) => {
20- file = path . join ( dir , file ) ;
21- const stat = statSync ( file ) ;
22- if ( stat && stat . isDirectory ( ) ) {
23- if ( ! ignoredFolders . some ( ( folder ) => folder . test ( file ) ) ) {
24- results = [ ...results , ...walk ( file ) ] ;
25- }
26- } else if ( [ '.ts' , '.tsx' , '.js' , '.json' ] . includes ( path . extname ( file ) ) ) {
27- // Is a file
28- results . push ( file ) ;
29- }
30- } ) ;
31- return results ;
19+ const modules = await getLernaModules ( ) ;
20+ lernaModules = modules . map ( ( { name } ) => name ) ;
21+ lernaModuleLocations = modules . map ( ( { location } ) => location ) ;
3222} ;
3323
3424const replacePackageScopes = ( ) => {
@@ -39,20 +29,6 @@ const replacePackageScopes = () => {
3929 } ) ;
4030} ;
4131
42- /**
43- * Makes an HTTP request to fetch all the dist tags for a given package.
44- */
45- type DistTags = Record < string , string > ;
46- const getDistTags = async ( packageName : string ) : Promise < DistTags > => {
47- console . log ( `Fetching dist tags for ${ packageName } ` ) ;
48- const url = `https://registry.npmjs.org/-/package/${ packageName } /dist-tags` ;
49- const response = await fetch ( url ) ;
50- if ( ! response . ok ) {
51- throw new Error ( `Failed ${ url } : ${ response . status } ${ response . statusText } ${ await response . text ( ) } ` ) ;
52- }
53- return response . json ( ) ;
54- } ;
55-
5632// modules/bitgo is the only package we publish without an `@bitgo` prefix, so
5733// we must manually set one
5834const replaceBitGoPackageScope = ( ) => {
@@ -92,36 +68,13 @@ function compareversion(version1, version2) {
9268 return result ;
9369}
9470
95- async function getDistTagsForModules ( moduleLocations : string [ ] ) : Promise < ( DistTags | undefined ) [ ] > {
96- return await Promise . all (
97- moduleLocations . map ( async ( modulePath ) => {
98- const moduleName : string = JSON . parse (
99- readFileSync ( path . join ( modulePath , 'package.json' ) , { encoding : 'utf-8' } )
100- ) . name ;
101- switch ( moduleName ) {
102- case '@bitgo-beta/express' :
103- case '@bitgo-beta/web-demo' :
104- case '@bitgo-beta/sdk-test' :
105- console . warn ( `Skipping ${ moduleName } as it's not published to npm` ) ;
106- return undefined ;
107- }
108- try {
109- return await getDistTags ( moduleName ) ;
110- } catch ( e ) {
111- console . warn ( `Failed to fetch dist tags for ${ moduleName } ` , e ) ;
112- return undefined ;
113- }
114- } )
115- ) ;
116- }
117-
11871/**
11972 * increment the version based on the preid. default to `beta`
12073 *
12174 * @param {String | undefined } preid
12275 */
12376const incrementVersions = async ( preid = 'beta' ) => {
124- const distTags = await getDistTagsForModules ( lernaModuleLocations ) ;
77+ const distTags = await getDistTagsForModuleLocations ( lernaModuleLocations ) ;
12578 for ( let i = 0 ; i < lernaModuleLocations . length ; i ++ ) {
12679 try {
12780 const modulePath = lernaModuleLocations [ i ] ;
@@ -142,6 +95,7 @@ const incrementVersions = async (preid = 'beta') => {
14295
14396 if ( prevTag ) {
14497 const next = inc ( prevTag , 'prerelease' , undefined , preid ) ;
98+ assert ( typeof next === 'string' , `Failed to increment version for ${ json . name } ` ) ;
14599 console . log ( `Setting next version for ${ json . name } to ${ next } ` ) ;
146100 json . version = next ;
147101 writeFileSync ( path . join ( modulePath , 'package.json' ) , JSON . stringify ( json , null , 2 ) + '\n' ) ;
@@ -154,12 +108,7 @@ const incrementVersions = async (preid = 'beta') => {
154108 const otherJsonContent = readFileSync ( path . join ( otherModulePath , 'package.json' ) , { encoding : 'utf-8' } ) ;
155109 if ( otherJsonContent . includes ( json . name ) ) {
156110 const otherJson = JSON . parse ( otherJsonContent ) ;
157- if ( otherJson . dependencies && otherJson . dependencies [ json . name ] ) {
158- otherJson . dependencies [ json . name ] = next ;
159- }
160- if ( otherJson . devDependencies && otherJson . devDependencies [ json . name ] ) {
161- otherJson . devDependencies [ json . name ] = next ;
162- }
111+ setDependencyVersion ( otherJson , json . name , next as string ) ;
163112 writeFileSync ( path . join ( otherModulePath , 'package.json' ) , JSON . stringify ( otherJson , null , 2 ) + '\n' ) ;
164113 }
165114 } ) ;
0 commit comments