1
- import * as execa from 'execa ' ;
2
- import { readFileSync , readdirSync , writeFileSync , statSync } from 'fs' ;
1
+ import * as assert from 'node:assert ' ;
2
+ import { readFileSync , writeFileSync } from 'fs' ;
3
3
import * as path from 'path' ;
4
4
import { inc } from 'semver' ;
5
+ import {
6
+ walk ,
7
+ getDistTagsForModuleLocations ,
8
+ getLernaModules ,
9
+ changeScopeInFile ,
10
+ setDependencyVersion ,
11
+ } from './prepareRelease' ;
5
12
6
13
let lernaModules : string [ ] = [ ] ;
7
14
let lernaModuleLocations : string [ ] = [ ] ;
8
15
let TARGET_SCOPE = '@bitgo-beta' ;
9
16
let filesChanged = 0 ;
10
17
11
- /**
12
- * Create a function which can run lerna commands
13
- * @param {String } lernaPath - path to lerna binary
14
- * @returns {function(string, string[], Object.<string, string>): Promise<string> }
15
- */
16
- function getLernaRunner ( lernaPath : string ) {
17
- return async ( command : string , args : string [ ] = [ ] , options = { } ) => {
18
- const { stdout } = await execa ( lernaPath , [ command , ...args ] , options ) ;
19
- return stdout ;
20
- } ;
21
- }
22
-
23
- const getLernaModules = async ( ) : Promise < void > => {
24
- const { stdout : lernaBinary } = await execa ( 'yarn' , [ 'bin' , 'lerna' ] , { cwd : process . cwd ( ) } ) ;
25
-
26
- const lerna = getLernaRunner ( lernaBinary ) ;
27
- const modules : Array < { name : string ; location : string } > = JSON . parse (
28
- await lerna ( 'list' , [ '--loglevel' , 'silent' , '--json' , '--all' ] )
29
- ) ;
18
+ const setLernaModules = async ( ) : Promise < void > => {
19
+ const modules = await getLernaModules ( ) ;
30
20
lernaModules = modules . map ( ( { name } ) => name ) ;
31
21
lernaModuleLocations = modules . map ( ( { location } ) => location ) ;
32
22
} ;
33
23
34
- const walk = ( dir : string ) : string [ ] => {
35
- let results : string [ ] = [ ] ;
36
- const ignoredFolders = [ / n o d e _ m o d u l e s / ] ;
37
- const list = readdirSync ( dir ) ;
38
- list . forEach ( ( file ) => {
39
- file = path . join ( dir , file ) ;
40
- const stat = statSync ( file ) ;
41
- if ( stat && stat . isDirectory ( ) ) {
42
- if ( ! ignoredFolders . some ( ( folder ) => folder . test ( file ) ) ) {
43
- results = [ ...results , ...walk ( file ) ] ;
44
- }
45
- } else if ( [ '.ts' , '.tsx' , '.js' , '.json' ] . includes ( path . extname ( file ) ) ) {
46
- // Is a file
47
- results . push ( file ) ;
48
- }
49
- } ) ;
50
- return results ;
51
- } ;
52
-
53
- const changeScopeInFile = ( filePath : string ) : void => {
54
- const oldContent = readFileSync ( filePath , { encoding : 'utf8' } ) ;
55
- let newContent = oldContent ;
56
- lernaModules . forEach ( ( moduleName ) => {
57
- const newName = `${ moduleName . replace ( '@bitgo/' , `${ TARGET_SCOPE } /` ) } ` ;
58
- newContent = newContent . replace ( new RegExp ( moduleName , 'g' ) , newName ) ;
59
- } ) ;
60
- if ( newContent !== oldContent ) {
61
- writeFileSync ( filePath , newContent , { encoding : 'utf-8' } ) ;
62
- ++ filesChanged ;
63
- }
64
- } ;
65
-
66
24
const replacePackageScopes = ( ) => {
67
25
// replace all @bitgo packages & source code with alternate SCOPE
68
26
const filePaths = [ ...walk ( path . join ( __dirname , '../' , 'modules' ) ) , ...walk ( path . join ( __dirname , '../' , 'webpack' ) ) ] ;
69
- filePaths . forEach ( ( file ) => changeScopeInFile ( file ) ) ;
70
- } ;
71
-
72
- /**
73
- * Makes an HTTP request to fetch all the dist tags for a given package.
74
- */
75
- type DistTags = Record < string , string > ;
76
- const getDistTags = async ( packageName : string ) : Promise < DistTags > => {
77
- console . log ( `Fetching dist tags for ${ packageName } ` ) ;
78
- const url = `https://registry.npmjs.org/-/package/${ packageName } /dist-tags` ;
79
- const response = await fetch ( url ) ;
80
- if ( ! response . ok ) {
81
- throw new Error ( `Failed ${ url } : ${ response . status } ${ response . statusText } ${ await response . text ( ) } ` ) ;
82
- }
83
- return response . json ( ) ;
27
+ filePaths . forEach ( ( file ) => {
28
+ filesChanged += changeScopeInFile ( file , lernaModules , TARGET_SCOPE ) ;
29
+ } ) ;
84
30
} ;
85
31
86
32
// modules/bitgo is the only package we publish without an `@bitgo` prefix, so
@@ -122,36 +68,13 @@ function compareversion(version1, version2) {
122
68
return result ;
123
69
}
124
70
125
- async function getDistTagsForModules ( moduleLocations : string [ ] ) : Promise < ( DistTags | undefined ) [ ] > {
126
- return await Promise . all (
127
- moduleLocations . map ( async ( modulePath ) => {
128
- const moduleName : string = JSON . parse (
129
- readFileSync ( path . join ( modulePath , 'package.json' ) , { encoding : 'utf-8' } )
130
- ) . name ;
131
- switch ( moduleName ) {
132
- case '@bitgo-beta/express' :
133
- case '@bitgo-beta/web-demo' :
134
- case '@bitgo-beta/sdk-test' :
135
- console . warn ( `Skipping ${ moduleName } as it's not published to npm` ) ;
136
- return undefined ;
137
- }
138
- try {
139
- return await getDistTags ( moduleName ) ;
140
- } catch ( e ) {
141
- console . warn ( `Failed to fetch dist tags for ${ moduleName } ` , e ) ;
142
- return undefined ;
143
- }
144
- } )
145
- ) ;
146
- }
147
-
148
71
/**
149
72
* increment the version based on the preid. default to `beta`
150
73
*
151
74
* @param {String | undefined } preid
152
75
*/
153
76
const incrementVersions = async ( preid = 'beta' ) => {
154
- const distTags = await getDistTagsForModules ( lernaModuleLocations ) ;
77
+ const distTags = await getDistTagsForModuleLocations ( lernaModuleLocations ) ;
155
78
for ( let i = 0 ; i < lernaModuleLocations . length ; i ++ ) {
156
79
try {
157
80
const modulePath = lernaModuleLocations [ i ] ;
@@ -172,6 +95,7 @@ const incrementVersions = async (preid = 'beta') => {
172
95
173
96
if ( prevTag ) {
174
97
const next = inc ( prevTag , 'prerelease' , undefined , preid ) ;
98
+ assert ( typeof next === 'string' , `Failed to increment version for ${ json . name } ` ) ;
175
99
console . log ( `Setting next version for ${ json . name } to ${ next } ` ) ;
176
100
json . version = next ;
177
101
writeFileSync ( path . join ( modulePath , 'package.json' ) , JSON . stringify ( json , null , 2 ) + '\n' ) ;
@@ -184,12 +108,7 @@ const incrementVersions = async (preid = 'beta') => {
184
108
const otherJsonContent = readFileSync ( path . join ( otherModulePath , 'package.json' ) , { encoding : 'utf-8' } ) ;
185
109
if ( otherJsonContent . includes ( json . name ) ) {
186
110
const otherJson = JSON . parse ( otherJsonContent ) ;
187
- if ( otherJson . dependencies && otherJson . dependencies [ json . name ] ) {
188
- otherJson . dependencies [ json . name ] = next ;
189
- }
190
- if ( otherJson . devDependencies && otherJson . devDependencies [ json . name ] ) {
191
- otherJson . devDependencies [ json . name ] = next ;
192
- }
111
+ setDependencyVersion ( otherJson , json . name , next as string ) ;
193
112
writeFileSync ( path . join ( otherModulePath , 'package.json' ) , JSON . stringify ( otherJson , null , 2 ) + '\n' ) ;
194
113
}
195
114
} ) ;
@@ -213,7 +132,7 @@ const getArgs = () => {
213
132
214
133
const main = async ( preid ?: string ) => {
215
134
getArgs ( ) ;
216
- await getLernaModules ( ) ;
135
+ await setLernaModules ( ) ;
217
136
replacePackageScopes ( ) ;
218
137
replaceBitGoPackageScope ( ) ;
219
138
await incrementVersions ( preid ) ;
0 commit comments