"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventHooksManager = void 0;
const terminal_1 = require("@rushstack/terminal");
const Utilities_1 = require("../utilities/Utilities");
const EventHooks_1 = require("../api/EventHooks");
const Stopwatch_1 = require("../utilities/Stopwatch");
const EnvironmentConfiguration_1 = require("../api/EnvironmentConfiguration");
class EventHooksManager {
    constructor(rushConfiguration) {
        this._rushConfiguration = rushConfiguration;
        this._eventHooks = rushConfiguration.eventHooks;
        this._commonTempFolder = rushConfiguration.commonTempFolder;
    }
    handle(event, isDebug, ignoreHooks) {
        if (!this._eventHooks) {
            return;
        }
        const scripts = this._eventHooks.get(event);
        if (scripts.length > 0) {
            if (ignoreHooks) {
                // eslint-disable-next-line no-console
                console.log(`Skipping event hooks for ${EventHooks_1.Event[event]} since --ignore-hooks was specified`);
                return;
            }
            const stopwatch = Stopwatch_1.Stopwatch.start();
            // eslint-disable-next-line no-console
            console.log('\n' + terminal_1.Colorize.green(`Executing event hooks for ${EventHooks_1.Event[event]}`));
            const printEventHooksOutputToConsole = isDebug ||
                this._rushConfiguration.experimentsConfiguration.configuration.printEventHooksOutputToConsole;
            scripts.forEach((script) => {
                try {
                    const environment = { ...process.env };
                    // NOTE: Do NOT expose this variable to other subprocesses besides telemetry hooks.  We do NOT want
                    // child processes to inspect Rush's raw command line and magically change their behavior in a way
                    // that might be confusing to end users, or rely on CLI parameters that the build cache is unaware of.
                    environment[EnvironmentConfiguration_1.EnvironmentVariableNames.RUSH_INVOKED_ARGS] = JSON.stringify(process.argv);
                    Utilities_1.Utilities.executeLifecycleCommand(script, {
                        rushConfiguration: this._rushConfiguration,
                        workingDirectory: this._rushConfiguration.rushJsonFolder,
                        initCwd: this._commonTempFolder,
                        handleOutput: !printEventHooksOutputToConsole,
                        initialEnvironment: environment,
                        environmentPathOptions: {
                            includeRepoBin: true
                        }
                    });
                }
                catch (error) {
                    // eslint-disable-next-line no-console
                    console.error('\n' +
                        terminal_1.Colorize.yellow(`Event hook "${script}" failed: ${error}\nRun "rush" with --debug` +
                            ` to see detailed error information.`));
                    if (isDebug) {
                        // eslint-disable-next-line no-console
                        console.error('\n' + error.message);
                    }
                }
            });
            stopwatch.stop();
            // eslint-disable-next-line no-console
            console.log('\n' + terminal_1.Colorize.green(`Event hooks finished. (${stopwatch.toString()})`));
        }
    }
}
exports.EventHooksManager = EventHooksManager;
//# sourceMappingURL=EventHooksManager.js.map