Skip to content

Commit 805f3de

Browse files
feat(utxo-staking): allow custom dist tags file for pack-scoped script
Add option to load versions from a package.json file instead of npm registry. This is useful when working with some downstream project that is not on the most recent package beta versions. Issue: BTC-1933
1 parent 1048583 commit 805f3de

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

scripts/pack-scoped.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ async function changeModuleVersions(
4747
const packageJsonPath = mpath.join(dir, 'package.json');
4848
const packageJson = JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf-8'));
4949
newModuleNames.forEach((m) => {
50+
if (!distTagsByModuleName.has(m)) {
51+
console.warn(`No dist tags found for ${m}`);
52+
return;
53+
}
5054
const newVersion = distTagsByModuleName.get(m)?.beta;
5155
if (newVersion) {
5256
setDependencyVersion(packageJson, m, newVersion);
@@ -55,14 +59,37 @@ async function changeModuleVersions(
5559
await fs.promises.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
5660
}
5761

62+
async function getDistTagsFromPackageJson(
63+
newModuleNames: string[],
64+
packageJsonPath: string
65+
): Promise<Map<string, DistTags>> {
66+
const packageJson = JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf-8'));
67+
return new Map<string, DistTags>(
68+
newModuleNames.map((m) => {
69+
const distTags = packageJson.dependencies[m];
70+
if (distTags) {
71+
return [m, { beta: distTags }];
72+
}
73+
return [m, { beta: '0.0.0' }];
74+
})
75+
);
76+
}
77+
5878
async function getDistTagsForModuleNamesCached(
5979
dir: string,
60-
moduleNames: string[],
80+
newModuleNames: string[],
6181
params: {
6282
scope: string;
83+
sourceFile?: string;
6384
cache?: string;
6485
}
6586
): Promise<Map<string, DistTags>> {
87+
if (params.sourceFile) {
88+
if (params.sourceFile.endsWith('package.json')) {
89+
return getDistTagsFromPackageJson(newModuleNames, params.sourceFile);
90+
}
91+
}
92+
6693
if (params.cache) {
6794
try {
6895
console.log(`Loading cached dist tags from ${params.cache}`);
@@ -77,7 +104,6 @@ async function getDistTagsForModuleNamesCached(
77104
}
78105
}
79106

80-
const newModuleNames = moduleNames.map((m) => updateModuleNames(m, moduleNames, params.scope));
81107
const distTagsByModuleName = await getDistTagsForModuleNames(newModuleNames);
82108
if (params.cache) {
83109
console.log(`Caching dist tags to ${params.cache}`);
@@ -89,16 +115,23 @@ async function getDistTagsForModuleNamesCached(
89115
/** Change the scope of a module and update its dependencies */
90116
async function runChangeScope(
91117
dir: string,
92-
params: { lernaModules?: LernaModule[]; scope: string; cacheDistTags?: string }
118+
params: {
119+
lernaModules?: LernaModule[];
120+
scope: string;
121+
distTagsFrom?: string;
122+
cacheDistTags?: string;
123+
}
93124
) {
94125
const { lernaModules = await getLernaModules() } = params;
95126
const moduleNames = lernaModules.map((m) => m.name);
127+
const newModuleNames = moduleNames.map((m) => updateModuleNames(m, moduleNames, params.scope));
96128
await changeModuleScope(dir, { ...params, lernaModules });
97129
await changeModuleVersions(dir, {
98130
...params,
99131
moduleNames,
100-
distTagsByModuleName: await getDistTagsForModuleNamesCached(dir, moduleNames, {
132+
distTagsByModuleName: await getDistTagsForModuleNamesCached(dir, newModuleNames, {
101133
scope: params.scope,
134+
sourceFile: params.distTagsFrom,
102135
cache: params.cacheDistTags,
103136
}),
104137
});
@@ -184,16 +217,21 @@ yargs
184217
})
185218
.options({
186219
scope: optScope,
220+
distTagsFrom: {
221+
describe: 'Path to a file to read dist tags from',
222+
type: 'string',
223+
},
187224
});
188225
},
189-
async handler({ dir, scope }) {
226+
async handler({ dir, scope, distTagsFrom }) {
190227
const lernaModules = await getLernaModules();
191228
const module = getModuleByDir(lernaModules, dir);
192229
const archiveName = getArchiveName(module);
193230
await packExtract(dir, archiveName, scopedPackageDir);
194231
await runChangeScope(mpath.join(dir, scopedPackageDir, 'package'), {
195232
scope,
196233
lernaModules,
234+
distTagsFrom,
197235
cacheDistTags: mpath.join(dir, scopedPackageDir, '.distTags.cache.json'),
198236
});
199237
await packArchive(dir, archiveName, scopedPackageDir);

0 commit comments

Comments
 (0)