"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.TempProjectHelper = void 0;
const path = __importStar(require("node:path"));
const tar = __importStar(require("tar"));
const node_core_library_1 = require("@rushstack/node-core-library");
const RushConstants_1 = require("./RushConstants");
// The PosixModeBits are intended to be used with bitwise operations.
/* eslint-disable no-bitwise */
class TempProjectHelper {
    constructor(rushConfiguration, subspace) {
        this._rushConfiguration = rushConfiguration;
        this._subspace = subspace;
    }
    /**
     * Deletes the existing tarball and creates a tarball for the given rush project
     */
    createTempProjectTarball(rushProject) {
        node_core_library_1.FileSystem.ensureFolder(path.resolve(this._subspace.getSubspaceTempFolderPath(), 'projects'));
        const tarballFile = this.getTarballFilePath(rushProject);
        const tempProjectFolder = this.getTempProjectFolder(rushProject);
        node_core_library_1.FileSystem.deleteFile(tarballFile);
        // NPM expects the root of the tarball to have a directory called 'package'
        const npmPackageFolder = 'package';
        const tarOptions = {
            gzip: true,
            file: tarballFile,
            cwd: tempProjectFolder,
            portable: true,
            noMtime: true,
            noPax: true,
            sync: true,
            prefix: npmPackageFolder,
            filter: (tarPath, stat) => {
                if (!this._rushConfiguration.experimentsConfiguration.configuration.noChmodFieldInTarHeaderNormalization) {
                    stat.mode =
                        (stat.mode & ~0x1ff) | node_core_library_1.PosixModeBits.AllRead | node_core_library_1.PosixModeBits.UserWrite | node_core_library_1.PosixModeBits.AllExecute;
                }
                return true;
            }
        };
        // create the new tarball
        tar.create(tarOptions, [node_core_library_1.FileConstants.PackageJson]);
    }
    /**
     * Gets the path to the tarball
     * Example: "C:\MyRepo\common\temp\projects\my-project-2.tgz"
     */
    getTarballFilePath(project) {
        return path.join(this._subspace.getSubspaceTempFolderPath(), RushConstants_1.RushConstants.rushTempProjectsFolderName, `${project.unscopedTempProjectName}.tgz`);
    }
    getTempProjectFolder(rushProject) {
        const unscopedTempProjectName = rushProject.unscopedTempProjectName;
        return path.join(this._subspace.getSubspaceTempFolderPath(), RushConstants_1.RushConstants.rushTempProjectsFolderName, unscopedTempProjectName);
    }
}
exports.TempProjectHelper = TempProjectHelper;
//# sourceMappingURL=TempProjectHelper.js.map