// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { pnpmSyncCopyAsync } from 'pnpm-sync-lib';
import { Async, FileSystem } from '@rushstack/node-core-library';
import { OperationStatus } from './OperationStatus';
import { PnpmSyncUtilities } from '../../utilities/PnpmSyncUtilities';
import { RushConstants } from '../RushConstants';
const PLUGIN_NAME = 'PnpmSyncCopyOperationPlugin';
export class PnpmSyncCopyOperationPlugin {
    constructor(terminal) {
        this._terminal = terminal;
    }
    apply(hooks) {
        hooks.afterExecuteOperation.tapPromise(PLUGIN_NAME, async (runnerContext) => {
            const record = runnerContext;
            const { status, operation: { associatedProject: project } } = record;
            //skip if the phase is skipped or no operation
            if (status === OperationStatus.Skipped ||
                status === OperationStatus.NoOp ||
                status === OperationStatus.Failure) {
                return;
            }
            const pnpmSyncJsonPath = `${project.projectFolder}/${RushConstants.nodeModulesFolderName}/${RushConstants.pnpmSyncFilename}`;
            if (await FileSystem.exists(pnpmSyncJsonPath)) {
                const { PackageExtractor } = await import(
                /* webpackChunkName: 'PackageExtractor' */
                '@rushstack/package-extractor');
                await pnpmSyncCopyAsync({
                    pnpmSyncJsonPath,
                    ensureFolderAsync: FileSystem.ensureFolderAsync,
                    forEachAsyncWithConcurrency: Async.forEachAsync,
                    getPackageIncludedFiles: PackageExtractor.getPackageIncludedFilesAsync,
                    logMessageCallback: (logMessageOptions) => PnpmSyncUtilities.processLogMessage(logMessageOptions, this._terminal)
                });
            }
        });
    }
}
//# sourceMappingURL=PnpmSyncCopyOperationPlugin.js.map