Skip to content

Commit

Permalink
Merge pull request #4594 from g-chao/chao/refactor-pnpm-sync-log-message
Browse files Browse the repository at this point in the history
[rush-lib] Use pnpm-sync-lib logging APIs to customize the log message for pnpm-sync operations.
  • Loading branch information
octogonz committed Mar 26, 2024
2 parents e0de053 + 6067a1e commit 5846cb6
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 41 deletions.
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": "e7958f5fd48cecb1d8b0ff62ca2795884ec9e477",
"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.2.0"
},
"devDependencies": {
"@pnpm/logger": "4.0.0",
Expand Down
12 changes: 11 additions & 1 deletion libraries/rush-lib/src/cli/RushCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
CommandLineHelper
} from '@rushstack/ts-command-line';
import { InternalError, AlreadyReportedError } from '@rushstack/node-core-library';
import { ConsoleTerminalProvider, Terminal, PrintUtilities, Colorize } from '@rushstack/terminal';
import {
ConsoleTerminalProvider,
Terminal,
PrintUtilities,
Colorize,
type ITerminal
} from '@rushstack/terminal';

import { RushConfiguration } from '../api/RushConfiguration';
import { RushConstants } from '../logic/RushConstants';
Expand Down Expand Up @@ -165,6 +171,10 @@ export class RushCommandLineParser extends CommandLineParser {
return this._quietParameter.value;
}

public get terminal(): ITerminal {
return this._terminal;
}

/**
* Utility to determine if the app should restrict writing to the console.
*/
Expand Down
3 changes: 2 additions & 1 deletion libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ export class RushPnpmCommandLineParser {
pnpmFilterArguments: [],
checkOnly: false,
// TODO: Support subspaces
subspace: this._rushConfiguration.defaultSubspace
subspace: this._rushConfiguration.defaultSubspace,
terminal: this._terminal
};

const installManagerFactoryModule: typeof import('../logic/InstallManagerFactory') = await import(
Expand Down
23 changes: 20 additions & 3 deletions libraries/rush-lib/src/cli/RushXCommandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@

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 {
Colorize,
ConsoleTerminalProvider,
DEFAULT_CONSOLE_WIDTH,
type ITerminalProvider,
PrintUtilities,
Terminal,
type ITerminal
} from '@rushstack/terminal';
import { type ILogMessageCallbackOptions, pnpmSyncCopyAsync } from 'pnpm-sync-lib';

import { Utilities } from '../utilities/Utilities';
import { ProjectCommandSet } from '../logic/ProjectCommandSet';
Expand All @@ -16,6 +24,7 @@ import { EventHooksManager } from '../logic/EventHooksManager';
import { Event } from '../api/EventHooks';
import { EnvironmentVariableNames } from '../api/EnvironmentConfiguration';
import { RushConstants } from '../logic/RushConstants';
import { PnpmSyncUtilities } from '../utilities/PnpmSyncUtilities';

interface IRushXCommandLineArguments {
/**
Expand Down Expand Up @@ -209,6 +218,12 @@ export class RushXCommandLine {
}
});

const terminalProvider: ITerminalProvider = new ConsoleTerminalProvider({
debugEnabled: rushxArguments.isDebug,
verboseEnabled: rushxArguments.isDebug
});
const terminal: ITerminal = new Terminal(terminalProvider);

if (rushConfiguration?.packageManager === 'pnpm' && rushConfiguration?.experimentsConfiguration) {
const { configuration: experiments } = rushConfiguration?.experimentsConfiguration;

Expand All @@ -223,7 +238,9 @@ export class RushXCommandLine {
pnpmSyncJsonPath,
ensureFolder: FileSystem.ensureFolderAsync,
forEachAsyncWithConcurrency: Async.forEachAsync,
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync,
logMessageCallback: (logMessageOptions: ILogMessageCallbackOptions) =>
PnpmSyncUtilities.processLogMessage(logMessageOptions, terminal)
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/rush-lib/src/cli/actions/BaseInstallAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
IRequiredCommandLineIntegerParameter
} from '@rushstack/ts-command-line';
import { AlreadyReportedError } from '@rushstack/node-core-library';
import { ConsoleTerminalProvider, type ITerminal, Terminal, Colorize } from '@rushstack/terminal';
import { type ITerminal, Colorize } from '@rushstack/terminal';

import { BaseRushAction, type IBaseRushActionOptions } from './BaseRushAction';
import { Event } from '../../api/EventHooks';
Expand Down Expand Up @@ -47,7 +47,7 @@ export abstract class BaseInstallAction extends BaseRushAction {
public constructor(options: IBaseRushActionOptions) {
super(options);

this._terminal = new Terminal(new ConsoleTerminalProvider({ verboseEnabled: options.parser.isDebug }));
this._terminal = options.parser.terminal;

this._purgeParameter = this.defineFlagParameter({
parameterLongName: '--purge',
Expand Down
3 changes: 2 additions & 1 deletion libraries/rush-lib/src/cli/actions/InstallAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export class InstallAction extends BaseInstallAction {
pnpmFilterArguments: await this._selectionParameters!.getPnpmFilterArgumentsAsync(this._terminal),
checkOnly: this._checkOnlyParameter.value,
subspace: this.getTargetSubspace(),
beforeInstallAsync: () => this.rushSession.hooks.beforeInstall.promise(this)
beforeInstallAsync: () => this.rushSession.hooks.beforeInstall.promise(this),
terminal: this._terminal
};
}
}
3 changes: 2 additions & 1 deletion libraries/rush-lib/src/cli/actions/UpdateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class UpdateAction extends BaseInstallAction {
checkOnly: false,
subspace: this.getTargetSubspace(),

beforeInstallAsync: () => this.rushSession.hooks.beforeInstall.promise(this)
beforeInstallAsync: () => this.rushSession.hooks.beforeInstall.promise(this),
terminal: this._terminal
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class PhasedScriptAction extends BaseScriptAction<IPhasedCommandConfig> {
const { PnpmSyncCopyOperationPlugin } = await import(
'../../logic/operations/PnpmSyncCopyOperationPlugin'
);
new PnpmSyncCopyOperationPlugin().apply(this.hooks);
new PnpmSyncCopyOperationPlugin(terminal).apply(this.hooks);
}

const projectConfigurations: ReadonlyMap<RushConfigurationProject, RushProjectConfiguration> = this
Expand Down
3 changes: 2 additions & 1 deletion libraries/rush-lib/src/logic/PackageJsonUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ export class PackageJsonUpdater {
maxInstallAttempts: RushConstants.defaultMaxInstallAttempts,
pnpmFilterArguments: [],
checkOnly: false,
subspace: subspace
subspace: subspace,
terminal: this._terminal
};

const installManager: BaseInstallManager = await InstallManagerFactory.getInstallManagerAsync(
Expand Down
15 changes: 3 additions & 12 deletions libraries/rush-lib/src/logic/base/BaseInstallManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ import {
type FolderItem,
Async
} from '@rushstack/node-core-library';
import {
PrintUtilities,
ConsoleTerminalProvider,
Terminal,
type ITerminalProvider,
Colorize
} from '@rushstack/terminal';
import { PrintUtilities, Colorize, type ITerminal } from '@rushstack/terminal';

import { ApprovedPackagesChecker } from '../ApprovedPackagesChecker';
import type { AsyncRecycler } from '../../utilities/AsyncRecycler';
Expand Down Expand Up @@ -69,8 +63,7 @@ export abstract class BaseInstallManager {
private _npmSetupValidated: boolean = false;
private _syncNpmrcAlreadyCalled: boolean = false;

private readonly _terminalProvider: ITerminalProvider;
protected readonly _terminal: Terminal;
protected readonly _terminal: ITerminal;

protected readonly rushConfiguration: RushConfiguration;
protected readonly rushGlobalFolder: RushGlobalFolder;
Expand All @@ -85,6 +78,7 @@ export abstract class BaseInstallManager {
purgeManager: PurgeManager,
options: IInstallManagerOptions
) {
this._terminal = options.terminal;
this.rushConfiguration = rushConfiguration;
this.rushGlobalFolder = rushGlobalFolder;
this.installRecycler = purgeManager.commonTempFolderRecycler;
Expand All @@ -101,9 +95,6 @@ export abstract class BaseInstallManager {
);
}
}

this._terminalProvider = new ConsoleTerminalProvider();
this._terminal = new Terminal(this._terminalProvider);
}

public async doInstallAsync(): Promise<void> {
Expand Down
6 changes: 6 additions & 0 deletions libraries/rush-lib/src/logic/base/BaseInstallManagerTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import type { ITerminal } from '@rushstack/terminal';
import type { Subspace } from '../../api/Subspace';

export interface IInstallManagerOptions {
Expand Down Expand Up @@ -94,4 +95,9 @@ export interface IInstallManagerOptions {
* The specific subspace to install.
*/
subspace: Subspace;

/**
* The terminal where output should be printed.
*/
terminal: ITerminal;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ 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,
type ILockfilePackage
} from 'pnpm-sync-lib';
import type { Subspace } from '../../api/Subspace';
import { Colorize, ConsoleTerminalProvider } from '@rushstack/terminal';
import { BaseLinkManager, SymlinkKind } from '../base/BaseLinkManager';
import { PnpmSyncUtilities } from '../../utilities/PnpmSyncUtilities';

export interface IPnpmModules {
hoistedDependencies: { [dep in string]: { [depPath in string]: string } };
Expand Down Expand Up @@ -513,10 +519,11 @@ 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 = `${subspace.getSubspaceTempFolder()}/node_modules/.pnpm`;
const dotPnpmFolder: string = `${subspace.getSubspaceTempFolder()}/node_modules/.pnpm`;
await pnpmSyncPrepareAsync({
lockfilePath: pnpmLockfilePath,
storePath: pnpmStorePath,
dotPnpmFolder,
ensureFolder: FileSystem.ensureFolderAsync,
readPnpmLockfile: async (lockfilePath: string) => {
const wantedPnpmLockfile: PnpmShrinkwrapFile | undefined = await PnpmShrinkwrapFile.loadFromFile(
lockfilePath,
Expand All @@ -526,12 +533,29 @@ export class WorkspaceInstallManager extends BaseInstallManager {
if (!wantedPnpmLockfile) {
return undefined;
} else {
const lockfilePackages: Record<string, ILockfilePackage> = Object.create(null);
for (const versionPath of wantedPnpmLockfile.packages.keys()) {
lockfilePackages[versionPath] = {
dependencies: wantedPnpmLockfile.packages.get(versionPath)?.dependencies as Record<
string,
string
>,
optionalDependencies: wantedPnpmLockfile.packages.get(versionPath)
?.optionalDependencies as Record<string, string>
};
}

const result: ILockfile = {
importers: Object.fromEntries(wantedPnpmLockfile.importers.entries())
lockfileVersion: wantedPnpmLockfile.shrinkwrapFileMajorVersion,
importers: Object.fromEntries(wantedPnpmLockfile.importers.entries()),
packages: lockfilePackages
};

return result;
}
}
},
logMessageCallback: (logMessageOptions: ILogMessageCallbackOptions) =>
PnpmSyncUtilities.processLogMessage(logMessageOptions, this._terminal)
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export async function doBasicInstallAsync(options: IRunInstallOptions): Promise<
pnpmFilterArguments: [],
maxInstallAttempts: 1,
networkConcurrency: undefined,
subspace: rushConfiguration.defaultSubspace
subspace: rushConfiguration.defaultSubspace,
terminal: options.terminal
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
// 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 { ITerminal } from '@rushstack/terminal';
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 { PnpmSyncUtilities } from '../../utilities/PnpmSyncUtilities';

const PLUGIN_NAME: 'PnpmSyncCopyOperationPlugin' = 'PnpmSyncCopyOperationPlugin';

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

public constructor(terminal: ITerminal) {
this._terminal = terminal;
}
public apply(hooks: PhasedCommandHooks): void {
hooks.afterExecuteOperation.tapPromise(
PLUGIN_NAME,
Expand Down Expand Up @@ -42,7 +49,9 @@ export class PnpmSyncCopyOperationPlugin implements IPhasedCommandPlugin {
pnpmSyncJsonPath,
ensureFolder: FileSystem.ensureFolderAsync,
forEachAsyncWithConcurrency: Async.forEachAsync,
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync
getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync,
logMessageCallback: (logMessageOptions: ILogMessageCallbackOptions) =>
PnpmSyncUtilities.processLogMessage(logMessageOptions, this._terminal)
});
}
}
Expand Down
9 changes: 6 additions & 3 deletions libraries/rush-lib/src/logic/test/BaseInstallManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE in the project root for license information.

import * as path from 'path';
import { ConsoleTerminalProvider } from '@rushstack/terminal';
import { ConsoleTerminalProvider, type ITerminal, Terminal } from '@rushstack/terminal';

import { PurgeManager } from '../PurgeManager';
import { BaseInstallManager, pnpmIgnoreCompatibilityDbParameter } from '../base/BaseInstallManager';
Expand Down Expand Up @@ -51,11 +51,14 @@ describe('BaseInstallManager Test', () => {
RushConfiguration.loadFromConfigurationFile(rushJsonFilePnpmV6);
const rushConfigurationV7: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(rushJsonFilePnpmV7);
const terminal: ITerminal = new Terminal(new ConsoleTerminalProvider());
const options6: IInstallManagerOptions = {
subspace: rushConfigurationV6.defaultSubspace
subspace: rushConfigurationV6.defaultSubspace,
terminal
} as IInstallManagerOptions;
const options7: IInstallManagerOptions = {
subspace: rushConfigurationV7.defaultSubspace
subspace: rushConfigurationV7.defaultSubspace,
terminal
} as IInstallManagerOptions;
const purgeManager6: typeof PurgeManager.prototype = new PurgeManager(
rushConfigurationV6,
Expand Down

0 comments on commit 5846cb6

Please sign in to comment.