"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.InitSubspaceAction = void 0;
const node_core_library_1 = require("@rushstack/node-core-library");
const terminal_1 = require("@rushstack/terminal");
const PathConstants_1 = require("../../utilities/PathConstants");
const BaseRushAction_1 = require("./BaseRushAction");
const SubspacesConfiguration_1 = require("../../api/SubspacesConfiguration");
const templateUtilities_1 = require("../../utilities/templateUtilities");
class InitSubspaceAction extends BaseRushAction_1.BaseRushAction {
    constructor(parser) {
        super({
            actionName: 'init-subspace',
            summary: 'Create a new subspace.',
            documentation: 'Use this command to create a new subspace with the default subspace configuration files.',
            parser
        });
        this._subspaceNameParameter = this.defineStringParameter({
            parameterLongName: '--name',
            parameterShortName: '-n',
            argumentName: 'SUBSPACE_NAME',
            description: 'The name of the subspace that is being initialized.',
            required: true
        });
    }
    async runAsync() {
        var _a;
        const terminal = new terminal_1.Terminal(new terminal_1.ConsoleTerminalProvider());
        if (!this.rushConfiguration.subspacesFeatureEnabled) {
            throw new Error('Unable to create a subspace because the subspaces feature is not enabled.');
        }
        const subspacesConfiguration = this.rushConfiguration
            .subspacesConfiguration;
        // Verify this subspace name does not already exist
        const existingSubspaceNames = subspacesConfiguration.subspaceNames;
        const newSubspaceName = this._subspaceNameParameter.value;
        if (existingSubspaceNames.has(newSubspaceName)) {
            throw new Error(`The subspace name: ${this._subspaceNameParameter.value} already exists in the subspace.json file.`);
        }
        if (SubspacesConfiguration_1.SubspacesConfiguration.explainIfInvalidSubspaceName(newSubspaceName, (_a = this.rushConfiguration.subspacesConfiguration) === null || _a === void 0 ? void 0 : _a.splitWorkspaceCompatibility)) {
            return;
        }
        const subspaceConfigPath = `${this.rushConfiguration.commonFolder}/config/subspaces/${newSubspaceName}`;
        const assetsSubfolder = `${PathConstants_1.assetsFolderPath}/rush-init`;
        const templateFilePaths = [
            '[dot]npmrc',
            '.pnpmfile.cjs',
            'common-versions.json',
            'pnpm-config.json'
        ];
        await node_core_library_1.FileSystem.ensureEmptyFolderAsync(subspaceConfigPath);
        await node_core_library_1.Async.forEachAsync(templateFilePaths, async (templateFilePath) => {
            const sourcePath = `${assetsSubfolder}/common/config/rush/${templateFilePath}`;
            const destinationPath = `${subspaceConfigPath}/${templateFilePath.replace('[dot]', '.')}`;
            await (0, templateUtilities_1.copyTemplateFileAsync)(sourcePath, destinationPath, true);
        }, { concurrency: 10 });
        // Add the subspace name to subspaces.json
        const subspaceJson = await node_core_library_1.JsonFile.loadAsync(subspacesConfiguration.subspaceJsonFilePath);
        subspaceJson.subspaceNames.push(newSubspaceName);
        await node_core_library_1.JsonFile.saveAsync(subspaceJson, subspacesConfiguration.subspaceJsonFilePath, {
            updateExistingFile: true
        });
        terminal.writeLine('\nSubspace successfully created. Please review the subspace configuration files before committing.');
    }
}
exports.InitSubspaceAction = InitSubspaceAction;
//# sourceMappingURL=InitSubspaceAction.js.map