Skip to content

Commit f174292

Browse files
feat(scripts): refactor package scope utilities to separate module
Move package scope change logic to separate module for better organization and reuse. Issue: BTC-1826
1 parent 1011819 commit f174292

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as execa from 'execa';
2+
import { readFileSync, writeFileSync } from 'fs';
3+
4+
/**
5+
* Create a function which can run lerna commands
6+
* @param {String} lernaPath - path to lerna binary
7+
* @returns {function(string, string[], Object.<string, string>): Promise<string>}
8+
*/
9+
function getLernaRunner(lernaPath: string) {
10+
return async (command: string, args: string[] = [], options = {}) => {
11+
const { stdout } = await execa(lernaPath, [command, ...args], options);
12+
return stdout;
13+
};
14+
}
15+
16+
export async function getLernaModules(): Promise<{
17+
lernaModules: string[];
18+
lernaModuleLocations: string[];
19+
}> {
20+
const { stdout: lernaBinary } = await execa('yarn', ['bin', 'lerna'], { cwd: process.cwd() });
21+
22+
const lerna = getLernaRunner(lernaBinary);
23+
const modules: Array<{ name: string; location: string }> = JSON.parse(
24+
await lerna('list', ['--loglevel', 'silent', '--json', '--all'])
25+
);
26+
const lernaModules = modules.map(({ name }) => name);
27+
const lernaModuleLocations = modules.map(({ location }) => location);
28+
return { lernaModules, lernaModuleLocations };
29+
}
30+
31+
export function changeScopeInFile(filePath: string, lernaModules: string[], targetScope: string): number {
32+
const oldContent = readFileSync(filePath, { encoding: 'utf8' });
33+
let newContent = oldContent;
34+
lernaModules.forEach((moduleName) => {
35+
const newName = `${moduleName.replace('@bitgo/', `${targetScope}/`)}`;
36+
newContent = newContent.replace(new RegExp(moduleName, 'g'), newName);
37+
});
38+
if (newContent !== oldContent) {
39+
writeFileSync(filePath, newContent, { encoding: 'utf-8' });
40+
return 1;
41+
}
42+
return 0;
43+
}

scripts/prepare-release.ts

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
import * as execa from 'execa';
2-
import { readFileSync, readdirSync, writeFileSync, statSync } from 'fs';
1+
import { readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
32
import * as path from 'path';
43
import { inc } from 'semver';
4+
import { changeScopeInFile, getLernaModules } from './changePackageScope';
55

66
let lernaModules: string[] = [];
77
let lernaModuleLocations: string[] = [];
88
let TARGET_SCOPE = '@bitgo-beta';
99
let 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

3415
const 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-
6634
const 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

214184
const main = async (preid?: string) => {
215185
getArgs();
216-
await getLernaModules();
186+
await setLernaModules();
217187
replacePackageScopes();
218188
replaceBitGoPackageScope();
219189
await incrementVersions(preid);

0 commit comments

Comments
 (0)