Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rush-lib] Use pnpm-sync-lib logging APIs to customize the log message for pnpm-sync operations. #4594

Merged
merged 12 commits into from
Mar 26, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Use pnpm-sync-lib logging APIs to customize the log message for pnpm-sync operations",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
10 changes: 5 additions & 5 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/rush/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "1da0835844b2dddb8f340063ceb13ccf02a65dea",
"pnpmShrinkwrapHash": "ad10d70df4b7a0566f8b4275f15a636958303afb",
"preferredVersionsHash": "c3ce06bc821dea3e1fe5dbc73da35b305908795a"
}
2 changes: 1 addition & 1 deletion libraries/rush-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"tar": "~6.1.11",
"true-case-path": "~2.2.1",
"uuid": "~8.3.2",
"pnpm-sync-lib": "0.1.4"
"pnpm-sync-lib": "0.1.6"
},
"devDependencies": {
"@pnpm/logger": "4.0.0",
Expand Down
27 changes: 25 additions & 2 deletions libraries/rush-lib/src/cli/RushXCommandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as path from 'path';
import { PackageJsonLookup, type IPackageJson, Text, FileSystem, Async } from '@rushstack/node-core-library';
import { Colorize, DEFAULT_CONSOLE_WIDTH, PrintUtilities } from '@rushstack/terminal';
import { pnpmSyncCopyAsync } from 'pnpm-sync-lib';
import { type ILogMessageCallbackOptions, pnpmSyncCopyAsync } from 'pnpm-sync-lib';

import { Utilities } from '../utilities/Utilities';
import { ProjectCommandSet } from '../logic/ProjectCommandSet';
Expand Down Expand Up @@ -223,7 +223,30 @@ export class RushXCommandLine {
pnpmSyncJsonPath,
ensureFolder: FileSystem.ensureFolderAsync,
forEachAsyncWithConcurrency: Async.forEachAsync,
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync,
logMessageCallback: (logMessageOptions: ILogMessageCallbackOptions) => {
const { message, messageKind } = logMessageOptions;
switch (messageKind) {
case 'error':
// eslint-disable-next-line no-console
console.error(Colorize.red(message));
break;
case 'warning':
// eslint-disable-next-line no-console
console.error(Colorize.yellow(message));
break;
case 'verbose':
if (rushxArguments.isDebug) {
// eslint-disable-next-line no-console
console.error(message);
}
break;
default:
// eslint-disable-next-line no-console
console.log(message);
break;
g-chao marked this conversation as resolved.
Show resolved Hide resolved
}
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,11 @@ export class PhasedScriptAction extends BaseScriptAction<IPhasedCommandConfig> {
this.rushConfiguration?.packageManager === 'pnpm' &&
experiments?.usePnpmSyncForInjectedDependencies
) {
const isVerbose: boolean = !this._verboseParameter.value || this.parser.isDebug;
g-chao marked this conversation as resolved.
Show resolved Hide resolved
const { PnpmSyncCopyOperationPlugin } = await import(
'../../logic/operations/PnpmSyncCopyOperationPlugin'
);
new PnpmSyncCopyOperationPlugin().apply(this.hooks);
new PnpmSyncCopyOperationPlugin(terminal, isVerbose).apply(this.hooks);
}

const projectConfigurations: ReadonlyMap<RushConfigurationProject, RushProjectConfiguration> = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { BaseProjectShrinkwrapFile } from '../base/BaseProjectShrinkwrapFile';
import { type CustomTipId, type ICustomTipInfo, PNPM_CUSTOM_TIPS } from '../../api/CustomTipsConfiguration';
import { PnpmShrinkwrapFile } from '../pnpm/PnpmShrinkwrapFile';
import { objectsAreDeepEqual } from '../../utilities/objectUtilities';
import { type ILockfile, pnpmSyncPrepareAsync } from 'pnpm-sync-lib';
import { type ILockfile, pnpmSyncPrepareAsync, type ILogMessageCallbackOptions } from 'pnpm-sync-lib';
import type { Subspace } from '../../api/Subspace';
import { Colorize, ConsoleTerminalProvider } from '@rushstack/terminal';
import { BaseLinkManager, SymlinkKind } from '../base/BaseLinkManager';
Expand Down Expand Up @@ -513,7 +513,7 @@ export class WorkspaceInstallManager extends BaseInstallManager {
// the pnpm-sync will generate the pnpm-sync.json based on lockfile
if (this.rushConfiguration.packageManager === 'pnpm' && experiments?.usePnpmSyncForInjectedDependencies) {
const pnpmLockfilePath: string = subspace.getTempShrinkwrapFilename();
const pnpmStorePath: string = `${this.rushConfiguration.commonTempFolder}/node_modules/.pnpm`;
const pnpmStorePath: string = `${subspace.getSubspaceTempFolder()}/node_modules/.pnpm`;
await pnpmSyncPrepareAsync({
lockfilePath: pnpmLockfilePath,
storePath: pnpmStorePath,
Expand All @@ -527,10 +527,31 @@ export class WorkspaceInstallManager extends BaseInstallManager {
return undefined;
} else {
const result: ILockfile = {
lockfileVersion: wantedPnpmLockfile.shrinkwrapFileMajorVersion,
importers: Object.fromEntries(wantedPnpmLockfile.importers.entries())
};
return result;
}
},
logMessageCallback: (logMessageOptions: ILogMessageCallbackOptions) => {
g-chao marked this conversation as resolved.
Show resolved Hide resolved
const { message, messageKind } = logMessageOptions;
switch (messageKind) {
case 'error':
this._terminal.writeErrorLine(message);
break;
case 'warning':
this._terminal.writeWarningLine(message);
break;
case 'verbose':
if (this.options.debug) {
this._terminal.writeLine(message);
}
g-chao marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
this._terminal.writeLine();
g-chao marked this conversation as resolved.
Show resolved Hide resolved
this._terminal.writeLine(message);
break;
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
// See LICENSE in the project root for license information.

import { Async, FileSystem } from '@rushstack/node-core-library';
import { pnpmSyncCopyAsync } from 'pnpm-sync-lib';
import { type ILogMessageCallbackOptions, pnpmSyncCopyAsync } from 'pnpm-sync-lib';

import { OperationStatus } from './OperationStatus';
import type { IOperationRunnerContext } from './IOperationRunner';
import type { IPhasedCommandPlugin, PhasedCommandHooks } from '../../pluginFramework/PhasedCommandHooks';
import type { OperationExecutionRecord } from './OperationExecutionRecord';
import type { ITerminal } from '@rushstack/terminal';
g-chao marked this conversation as resolved.
Show resolved Hide resolved

const PLUGIN_NAME: 'PnpmSyncCopyOperationPlugin' = 'PnpmSyncCopyOperationPlugin';

export class PnpmSyncCopyOperationPlugin implements IPhasedCommandPlugin {
private readonly _terminal: ITerminal;
private readonly _isVerbose: boolean;

public constructor(terminal: ITerminal, isVerbose: boolean) {
this._terminal = terminal;
this._isVerbose = isVerbose;
}
g-chao marked this conversation as resolved.
Show resolved Hide resolved
public apply(hooks: PhasedCommandHooks): void {
hooks.afterExecuteOperation.tapPromise(
PLUGIN_NAME,
Expand Down Expand Up @@ -42,7 +50,26 @@ export class PnpmSyncCopyOperationPlugin implements IPhasedCommandPlugin {
pnpmSyncJsonPath,
ensureFolder: FileSystem.ensureFolderAsync,
forEachAsyncWithConcurrency: Async.forEachAsync,
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync,
logMessageCallback: (logMessageOptions: ILogMessageCallbackOptions) => {
const { message, messageKind } = logMessageOptions;
switch (messageKind) {
case 'error':
this._terminal.writeErrorLine(message);
break;
case 'warning':
this._terminal.writeWarningLine(message);
break;
case 'verbose':
if (this._isVerbose) {
this._terminal.writeVerboseLine(message);
}
g-chao marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
this._terminal.writeLine(message);
break;
}
}
});
}
}
Expand Down