// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import path from 'node:path';
import { FileSystem } from '@rushstack/node-core-library';
const PLUGIN_NAME = 'NodeDiagnosticDirPlugin';
/**
 * Phased command plugin that configures the NodeJS --diagnostic-dir option to contain the project and phase name.
 */
export class NodeDiagnosticDirPlugin {
    constructor(options) {
        this._diagnosticsDir = options.diagnosticDir;
    }
    apply(hooks) {
        const getDiagnosticDir = (operation) => {
            const { associatedProject } = operation;
            const diagnosticDir = path.resolve(this._diagnosticsDir, associatedProject.packageName, operation.logFilenameIdentifier);
            return diagnosticDir;
        };
        hooks.createEnvironmentForOperation.tap(PLUGIN_NAME, (env, record) => {
            const diagnosticDir = getDiagnosticDir(record.operation);
            if (!diagnosticDir) {
                return env;
            }
            // Not all versions of NodeJS create the directory, so ensure it exists:
            FileSystem.ensureFolder(diagnosticDir);
            const { NODE_OPTIONS } = env;
            const diagnosticDirEnv = `--diagnostic-dir="${diagnosticDir}"`;
            env.NODE_OPTIONS = NODE_OPTIONS ? `${NODE_OPTIONS} ${diagnosticDirEnv}` : diagnosticDirEnv;
            return env;
        });
    }
}
//# sourceMappingURL=NodeDiagnosticDirPlugin.js.map