1- import * as execa from 'execa' ;
2- import { readFileSync , readdirSync , writeFileSync , statSync } from 'fs' ;
1+ import { readdirSync , readFileSync , statSync , writeFileSync } from 'fs' ;
32import * as path from 'path' ;
43import { inc } from 'semver' ;
4+ import { changeScopeInFile , getLernaModules } from './changePackageScope' ;
55
66let lernaModules : string [ ] = [ ] ;
77let lernaModuleLocations : string [ ] = [ ] ;
88let TARGET_SCOPE = '@bitgo-beta' ;
99let filesChanged = 0 ;
1010
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- ) ;
30- lernaModules = modules . map ( ( { name } ) => name ) ;
31- lernaModuleLocations = modules . map ( ( { location } ) => location ) ;
11+ const setLernaModules = async ( ) : Promise < void > => {
12+ ( { lernaModules, lernaModuleLocations } = await getLernaModules ( ) ) ;
3213} ;
3314
3415const walk = ( dir : string ) : string [ ] => {
@@ -50,23 +31,12 @@ const walk = (dir: string): string[] => {
5031 return results ;
5132} ;
5233
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-
6634const replacePackageScopes = ( ) => {
6735 // replace all @bitgo packages & source code with alternate SCOPE
6836 const filePaths = [ ...walk ( path . join ( __dirname , '../' , 'modules' ) ) , ...walk ( path . join ( __dirname , '../' , 'webpack' ) ) ] ;
69- filePaths . forEach ( ( file ) => changeScopeInFile ( file ) ) ;
37+ filePaths . forEach ( ( file ) => {
38+ filesChanged += changeScopeInFile ( file , lernaModules , TARGET_SCOPE ) ;
39+ } ) ;
7040} ;
7141
7242/**
@@ -213,7 +183,7 @@ const getArgs = () => {
213183
214184const main = async ( preid ?: string ) => {
215185 getArgs ( ) ;
216- await getLernaModules ( ) ;
186+ await setLernaModules ( ) ;
217187 replacePackageScopes ( ) ;
218188 replaceBitGoPackageScope ( ) ;
219189 await incrementVersions ( preid ) ;
0 commit comments