"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnlinkManager = void 0;
const path = __importStar(require("node:path"));
const node_core_library_1 = require("@rushstack/node-core-library");
const terminal_1 = require("@rushstack/terminal");
const Utilities_1 = require("../utilities/Utilities");
const BaseProjectShrinkwrapFile_1 = require("./base/BaseProjectShrinkwrapFile");
const FlagFile_1 = require("../api/FlagFile");
const RushConstants_1 = require("./RushConstants");
/**
 * This class implements the logic for "rush unlink"
 */
class UnlinkManager {
    constructor(rushConfiguration) {
        this._rushConfiguration = rushConfiguration;
    }
    /**
     * Delete flag file and all the existing node_modules symlinks and all
     * project/.rush/temp/shrinkwrap-deps.json files
     *
     * Returns true if anything was deleted.
     */
    async unlinkAsync(force = false) {
        const useWorkspaces = this._rushConfiguration.pnpmOptions && this._rushConfiguration.pnpmOptions.useWorkspaces;
        if (!force && useWorkspaces) {
            // eslint-disable-next-line no-console
            console.log(terminal_1.Colorize.red('Unlinking is not supported when using workspaces. Run "rush purge" to remove ' +
                'project node_modules folders.'));
            throw new node_core_library_1.AlreadyReportedError();
        }
        await new FlagFile_1.FlagFile(this._rushConfiguration.defaultSubspace.getSubspaceTempFolderPath(), RushConstants_1.RushConstants.lastLinkFlagFilename, {}).clearAsync();
        return this._deleteProjectFiles();
    }
    /**
     * Delete:
     *  - all the node_modules symlinks of configured Rush projects
     *  - all of the project/.rush/temp/shrinkwrap-deps.json files of configured Rush projects
     *
     * Returns true if anything was deleted
     * */
    _deleteProjectFiles() {
        let didDeleteAnything = false;
        for (const rushProject of this._rushConfiguration.projects) {
            const localModuleFolder = path.join(rushProject.projectFolder, 'node_modules');
            if (node_core_library_1.FileSystem.exists(localModuleFolder)) {
                // eslint-disable-next-line no-console
                console.log(`Purging ${localModuleFolder}`);
                Utilities_1.Utilities.dangerouslyDeletePath(localModuleFolder);
                didDeleteAnything = true;
            }
            const projectShrinkwrapFilePath = BaseProjectShrinkwrapFile_1.BaseProjectShrinkwrapFile.getFilePathForProject(rushProject);
            if (node_core_library_1.FileSystem.exists(projectShrinkwrapFilePath)) {
                // eslint-disable-next-line no-console
                console.log(`Deleting ${projectShrinkwrapFilePath}`);
                node_core_library_1.FileSystem.deleteFile(projectShrinkwrapFilePath);
                didDeleteAnything = true;
            }
        }
        return didDeleteAnything;
    }
}
exports.UnlinkManager = UnlinkManager;
//# sourceMappingURL=UnlinkManager.js.map