// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Colorize } from '@rushstack/terminal';
import { Utilities } from '../utilities/Utilities';
import { Event } from '../api/EventHooks';
import { Stopwatch } from '../utilities/Stopwatch';
import { EnvironmentVariableNames } from '../api/EnvironmentConfiguration';
export 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 ${Event[event]} since --ignore-hooks was specified`);
                return;
            }
            const stopwatch = Stopwatch.start();
            // eslint-disable-next-line no-console
            console.log('\n' + Colorize.green(`Executing event hooks for ${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[EnvironmentVariableNames.RUSH_INVOKED_ARGS] = JSON.stringify(process.argv);
                    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' +
                        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' + Colorize.green(`Event hooks finished. (${stopwatch.toString()})`));
        }
    }
}
//# sourceMappingURL=EventHooksManager.js.map