Skip to content

Commit 3537128

Browse files
Merge pull request #5844 from BitGo/DX-1201.add-prepareRelease-tests
feat(scripts): add tests for prepare-release script
2 parents 58e3a21 + d4f7df8 commit 3537128

File tree

6 files changed

+2619
-3
lines changed

6 files changed

+2619
-3
lines changed

scripts/prepare-release.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ let lernaModules: string[] = [];
1414
let lernaModuleLocations: string[] = [];
1515
let TARGET_SCOPE = '@bitgo-beta';
1616
let filesChanged = 0;
17+
// Default to __dirname/.. but allow override via environment variable
18+
const ROOT_DIR = process.env.BITGO_PREPARE_RELEASE_ROOT_DIR || path.join(__dirname, '..');
1719

1820
async function setLernaModules(): Promise<void> {
1921
const modules = await getLernaModules();
@@ -23,7 +25,7 @@ async function setLernaModules(): Promise<void> {
2325

2426
function replacePackageScopes() {
2527
// replace all @bitgo packages & source code with alternate SCOPE
26-
const filePaths = [...walk(path.join(__dirname, '../', 'modules')), ...walk(path.join(__dirname, '../', 'webpack'))];
28+
const filePaths = [...walk(path.join(ROOT_DIR, 'modules')), ...walk(path.join(ROOT_DIR, 'webpack'))];
2729
filePaths.forEach((file) => {
2830
filesChanged += changeScopeInFile(file, lernaModules, TARGET_SCOPE);
2931
});
@@ -128,6 +130,7 @@ function getArgs() {
128130
TARGET_SCOPE = split[1] || TARGET_SCOPE;
129131
}
130132
console.log(`Preparing to re-target to ${TARGET_SCOPE}`);
133+
console.log(`Using root directory: ${ROOT_DIR}`);
131134
}
132135

133136
async function main(preid?: string) {

scripts/prepareRelease/distTags.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync } from 'fs';
1+
import { readFileSync, writeFileSync, existsSync } from 'fs';
22
import * as path from 'path';
33

44
export type DistTags = Record<string, string>;
@@ -16,8 +16,24 @@ export async function getDistTags(packageName: string): Promise<DistTags> {
1616
return response.json();
1717
}
1818

19+
// Add this function to read from cache
20+
export function getDistTagsCache(): string | undefined {
21+
return process.env.BITGO_PREPARE_RELEASE_CACHE_DIST_TAGS;
22+
}
23+
1924
export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<Map<string, DistTags>> {
20-
return new Map(
25+
const cachePath = getDistTagsCache();
26+
27+
// If cache path is set and file exists, read from cache
28+
if (cachePath && existsSync(cachePath)) {
29+
console.log(`Reading dist tags from cache: ${cachePath}`);
30+
const cacheContent = readFileSync(cachePath, { encoding: 'utf-8' });
31+
const cachedTags = JSON.parse(cacheContent);
32+
return new Map(Object.entries(cachedTags));
33+
}
34+
35+
// Otherwise fetch from npm
36+
const tagsMap = new Map(
2137
(
2238
await Promise.all(
2339
moduleNames.map(async (moduleName): Promise<[string, DistTags][]> => {
@@ -38,6 +54,19 @@ export async function getDistTagsForModuleNames(moduleNames: string[]): Promise<
3854
)
3955
).flat()
4056
);
57+
58+
// If cache path is set but file doesn't exist, write to cache
59+
if (cachePath) {
60+
console.log(`Writing dist tags to cache: ${cachePath}`);
61+
const cacheDir = path.dirname(cachePath);
62+
if (!existsSync(cacheDir)) {
63+
const { mkdirSync } = require('fs');
64+
mkdirSync(cacheDir, { recursive: true });
65+
}
66+
writeFileSync(cachePath, JSON.stringify(Object.fromEntries(tagsMap), null, 2), { encoding: 'utf-8' });
67+
}
68+
69+
return tagsMap;
4170
}
4271

4372
export async function getDistTagsForModuleLocations(moduleLocations: string[]): Promise<(DistTags | undefined)[]> {

0 commit comments

Comments
 (0)