"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeDiagnosticDirPlugin = void 0;
const node_path_1 = __importDefault(require("node:path"));
const node_core_library_1 = require("@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.
 */
class NodeDiagnosticDirPlugin {
    constructor(options) {
        this._diagnosticsDir = options.diagnosticDir;
    }
    apply(hooks) {
        const getDiagnosticDir = (operation) => {
            const { associatedProject } = operation;
            const diagnosticDir = node_path_1.default.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:
            node_core_library_1.FileSystem.ensureFolder(diagnosticDir);
            const { NODE_OPTIONS } = env;
            const diagnosticDirEnv = `--diagnostic-dir="${diagnosticDir}"`;
            env.NODE_OPTIONS = NODE_OPTIONS ? `${NODE_OPTIONS} ${diagnosticDirEnv}` : diagnosticDirEnv;
            return env;
        });
    }
}
exports.NodeDiagnosticDirPlugin = NodeDiagnosticDirPlugin;
//# sourceMappingURL=NodeDiagnosticDirPlugin.js.map