Skip to content

Commit 3b427fe

Browse files
Copiloticlanton
andcommitted
Add approve-builds command support to rush-pnpm
Co-authored-by: iclanton <5010588+iclanton@users.noreply.github.com>
1 parent 6eccefe commit 3b427fe

3 files changed

Lines changed: 95 additions & 3 deletions

File tree

libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,36 @@ export class RushPnpmCommandLineParser {
359359
}
360360
break;
361361
}
362+
case 'approve-builds': {
363+
const semver: typeof import('semver') = await import('semver');
364+
/**
365+
* The "approve-builds" command was introduced in pnpm version 10.1.0
366+
* to approve packages for running build scripts when onlyBuiltDependencies is used
367+
*/
368+
if (semver.lt(this._rushConfiguration.packageManagerToolVersion, '10.1.0')) {
369+
this._terminal.writeErrorLine(
370+
PrintUtilities.wrapWords(
371+
`Error: The "pnpm approve-builds" command is added after pnpm@10.1.0.` +
372+
` Please update "pnpmVersion" >= 10.1.0 in ${RushConstants.rushJsonFilename} file and run "rush update" to use this command.`
373+
) + '\n'
374+
);
375+
throw new AlreadyReportedError();
376+
}
377+
const pnpmOptionsJsonFilename: string = path.join(
378+
this._rushConfiguration.commonRushConfigFolder,
379+
RushConstants.pnpmConfigFilename
380+
);
381+
if (this._rushConfiguration.rushConfigurationJson.pnpmOptions) {
382+
this._terminal.writeErrorLine(
383+
PrintUtilities.wrapWords(
384+
`Error: The "pnpm approve-builds" command is incompatible with specifying "pnpmOptions" in ${RushConstants.rushJsonFilename} file.` +
385+
` Please move the content of "pnpmOptions" in ${RushConstants.rushJsonFilename} file to ${pnpmOptionsJsonFilename}`
386+
) + '\n'
387+
);
388+
throw new AlreadyReportedError();
389+
}
390+
break;
391+
}
362392

363393
// Known safe
364394
case 'audit':
@@ -532,6 +562,39 @@ export class RushPnpmCommandLineParser {
532562
}
533563
break;
534564
}
565+
case 'approve-builds': {
566+
if (this._subspace.getPnpmOptions() === undefined) {
567+
const subspaceConfigFolder: string = this._subspace.getSubspaceConfigFolderPath();
568+
this._terminal.writeErrorLine(
569+
`The "rush-pnpm approve-builds" command cannot proceed without a pnpm-config.json file.` +
570+
` Create one in this folder: ${subspaceConfigFolder}`
571+
);
572+
break;
573+
}
574+
575+
// Example: "C:\MyRepo\common\temp\package.json"
576+
const commonPackageJsonFilename: string = `${subspaceTempFolder}/${FileConstants.PackageJson}`;
577+
const commonPackageJson: JsonObject = JsonFile.load(commonPackageJsonFilename);
578+
const newGlobalOnlyBuiltDependencies: string[] | undefined =
579+
commonPackageJson?.pnpm?.onlyBuiltDependencies;
580+
const pnpmOptions: PnpmOptionsConfiguration | undefined = this._subspace.getPnpmOptions();
581+
const currentGlobalOnlyBuiltDependencies: string[] | undefined =
582+
pnpmOptions?.globalOnlyBuiltDependencies;
583+
584+
if (!Objects.areDeepEqual(currentGlobalOnlyBuiltDependencies, newGlobalOnlyBuiltDependencies)) {
585+
// Update onlyBuiltDependencies to pnpm configuration file
586+
pnpmOptions?.updateGlobalOnlyBuiltDependencies(newGlobalOnlyBuiltDependencies);
587+
588+
// Rerun installation to update
589+
await this._doRushUpdateAsync();
590+
591+
this._terminal.writeWarningLine(
592+
`Rush refreshed the ${RushConstants.pnpmConfigFilename} and shrinkwrap file.\n` +
593+
' Please commit this change to Git.'
594+
);
595+
}
596+
break;
597+
}
535598
}
536599
}
537600

libraries/rush-lib/src/logic/installManager/WorkspaceInstallManager.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,17 +600,36 @@ export class WorkspaceInstallManager extends BaseInstallManager {
600600
}
601601
}
602602

603-
const onPnpmStdoutChunk: ((chunk: string) => void) | undefined =
603+
const onPnpmStdoutChunk: ((chunk: string) => string | void) | undefined =
604604
pnpmTips.length > 0
605-
? (chunk: string): void => {
605+
? (chunk: string): string | void => {
606606
// Iterate over the supported custom tip metadata and try to match the chunk.
607607
for (const { isMatch, tipId } of pnpmTips) {
608608
if (isMatch?.(chunk)) {
609609
tipIDsToBePrinted.add(tipId);
610610
}
611611
}
612+
613+
// Replace `pnpm approve-builds` with `rush-pnpm approve-builds` when running
614+
// `rush install` or `rush update` to instruct users to use the correct command
615+
const modifiedChunk = chunk.replace(
616+
/pnpm approve-builds/g,
617+
`rush-pnpm --subspace ${subspace.subspaceName} approve-builds`
618+
);
619+
620+
// Return modified chunk if it was changed, otherwise return void to keep original
621+
return modifiedChunk !== chunk ? modifiedChunk : undefined;
612622
}
613-
: undefined;
623+
: (chunk: string): string | void => {
624+
// Even when no tips are registered, we still need to rewrite the approve-builds command
625+
const modifiedChunk = chunk.replace(
626+
/pnpm approve-builds/g,
627+
`rush-pnpm --subspace ${subspace.subspaceName} approve-builds`
628+
);
629+
630+
// Return modified chunk if it was changed, otherwise return void to keep original
631+
return modifiedChunk !== chunk ? modifiedChunk : undefined;
632+
};
614633
try {
615634
await Utilities.executeCommandWithRetryAsync(
616635
{

libraries/rush-lib/src/logic/pnpm/PnpmOptionsConfiguration.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,14 @@ export class PnpmOptionsConfiguration extends PackageManagerOptionsConfiguration
539539
JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
540540
}
541541
}
542+
543+
/**
544+
* Updates globalOnlyBuiltDependencies field of the PNPM options in the common/config/rush/pnpm-config.json file.
545+
*/
546+
public updateGlobalOnlyBuiltDependencies(onlyBuiltDependencies: string[] | undefined): void {
547+
this._json.globalOnlyBuiltDependencies = onlyBuiltDependencies;
548+
if (this.jsonFilename) {
549+
JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
550+
}
551+
}
542552
}

0 commit comments

Comments
 (0)