// 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 { BaseRushAction } from './BaseRushAction';
import { Stopwatch } from '../../utilities/Stopwatch';
import { PurgeManager } from '../../logic/PurgeManager';
import { UnlinkManager } from '../../logic/UnlinkManager';
import { PURGE_ACTION_NAME } from '../../utilities/actionNameConstants';
export class PurgeAction extends BaseRushAction {
    constructor(parser) {
        super({
            actionName: PURGE_ACTION_NAME,
            summary: 'For diagnostic purposes, use this command to delete caches and other temporary files used by Rush',
            documentation: 'The "rush purge" command is used to delete temporary files created by Rush.  This is' +
                ' useful if you are having problems and suspect that cache files may be corrupt.',
            parser
        });
        this._unsafeParameter = this.defineFlagParameter({
            parameterLongName: '--unsafe',
            description: '(UNSAFE!) Also delete shared files such as the package manager instances stored in' +
                ' the ".rush" folder in the user\'s home directory.  This is a more aggressive fix that is' +
                ' NOT SAFE to run in a live environment because it will cause other concurrent Rush processes to fail.'
        });
    }
    async runAsync() {
        const stopwatch = Stopwatch.start();
        const unlinkManager = new UnlinkManager(this.rushConfiguration);
        const purgeManager = new PurgeManager(this.rushConfiguration, this.rushGlobalFolder);
        await unlinkManager.unlinkAsync(/*force:*/ true);
        if (this._unsafeParameter.value) {
            purgeManager.purgeUnsafe();
        }
        else {
            purgeManager.purgeNormal();
        }
        await purgeManager.startDeleteAllAsync();
        // eslint-disable-next-line no-console
        console.log('\n' +
            Colorize.green(`Rush purge started successfully and will complete asynchronously. (${stopwatch.toString()})`));
    }
}
//# sourceMappingURL=PurgeAction.js.map