"use strict";
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.packageManagers = exports.EXPO = exports.NPM = exports.PNPM = exports.YARN_V2 = exports.YARN_V1 = exports.BUN = void 0;
exports.detectAllPackageManagers = detectAllPackageManagers;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const telemetry_1 = require("../telemetry");
const setup_utils_1 = require("./setup-utils");
const analytics_1 = require("./analytics");
exports.BUN = {
    name: 'bun',
    label: 'Bun',
    installCommand: 'bun add',
    buildCommand: 'bun run build',
    runScriptCommand: 'bun run',
    flags: '',
    forceInstallFlag: '--force',
    detect: ({ installDir }) => ['bun.lockb', 'bun.lock'].some((lockFile) => fs.existsSync(path.join(installDir, lockFile))),
    addOverride: async (pkgName, pkgVersion, { installDir }) => {
        const packageDotJson = await (0, setup_utils_1.getPackageDotJson)({ installDir });
        const overrides = packageDotJson.overrides || {};
        await (0, setup_utils_1.updatePackageDotJson)({
            ...packageDotJson,
            overrides: {
                ...overrides,
                [pkgName]: pkgVersion,
            },
        }, { installDir: installDir });
    },
};
exports.YARN_V1 = {
    name: 'yarn',
    label: 'Yarn V1',
    installCommand: 'yarn add',
    buildCommand: 'yarn build',
    runScriptCommand: 'yarn',
    flags: '--ignore-workspace-root-check',
    forceInstallFlag: '--force',
    detect: ({ installDir }) => {
        try {
            return fs
                .readFileSync(path.join(installDir, 'yarn.lock'), 'utf-8')
                .slice(0, 500)
                .includes('yarn lockfile v1');
        }
        catch {
            return false;
        }
    },
    addOverride: async (pkgName, pkgVersion, { installDir }) => {
        const packageDotJson = await (0, setup_utils_1.getPackageDotJson)({ installDir });
        const resolutions = packageDotJson.resolutions || {};
        await (0, setup_utils_1.updatePackageDotJson)({
            ...packageDotJson,
            resolutions: {
                ...resolutions,
                [pkgName]: pkgVersion,
            },
        }, { installDir });
    },
};
/** YARN V2/3/4 */
exports.YARN_V2 = {
    name: 'yarn',
    label: 'Yarn V2/3/4',
    installCommand: 'yarn add',
    buildCommand: 'yarn build',
    runScriptCommand: 'yarn',
    flags: '',
    forceInstallFlag: '--force',
    detect: ({ installDir }) => {
        try {
            return fs
                .readFileSync(path.join(installDir, 'yarn.lock'), 'utf-8')
                .slice(0, 500)
                .includes('__metadata');
        }
        catch {
            return false;
        }
    },
    addOverride: async (pkgName, pkgVersion, { installDir }) => {
        const packageDotJson = await (0, setup_utils_1.getPackageDotJson)({ installDir });
        const resolutions = packageDotJson.resolutions || {};
        await (0, setup_utils_1.updatePackageDotJson)({
            ...packageDotJson,
            resolutions: {
                ...resolutions,
                [pkgName]: pkgVersion,
            },
        }, { installDir });
    },
};
exports.PNPM = {
    name: 'pnpm',
    label: 'pnpm',
    installCommand: 'pnpm add',
    buildCommand: 'pnpm build',
    runScriptCommand: 'pnpm',
    flags: '--ignore-workspace-root-check',
    forceInstallFlag: '--force',
    detect: ({ installDir }) => fs.existsSync(path.join(installDir, 'pnpm-lock.yaml')),
    addOverride: async (pkgName, pkgVersion, { installDir }) => {
        const packageDotJson = await (0, setup_utils_1.getPackageDotJson)({ installDir });
        const pnpm = packageDotJson.pnpm || {};
        const overrides = pnpm.overrides || {};
        await (0, setup_utils_1.updatePackageDotJson)({
            ...packageDotJson,
            pnpm: {
                ...pnpm,
                overrides: {
                    ...overrides,
                    [pkgName]: pkgVersion,
                },
            },
        }, { installDir });
    },
};
exports.NPM = {
    name: 'npm',
    label: 'npm',
    installCommand: 'npm add',
    buildCommand: 'npm run build',
    runScriptCommand: 'npm run',
    flags: '',
    forceInstallFlag: '--force',
    detect: ({ installDir }) => fs.existsSync(path.join(installDir, 'package-lock.json')),
    addOverride: async (pkgName, pkgVersion, { installDir }) => {
        const packageDotJson = await (0, setup_utils_1.getPackageDotJson)({ installDir });
        const overrides = packageDotJson.overrides || {};
        await (0, setup_utils_1.updatePackageDotJson)({
            ...packageDotJson,
            overrides: {
                ...overrides,
                [pkgName]: pkgVersion,
            },
        }, { installDir });
    },
};
exports.EXPO = {
    name: 'expo',
    label: 'Expo',
    installCommand: 'npx expo install',
    buildCommand: 'npx expo build',
    runScriptCommand: 'npx expo run',
    flags: '',
    forceInstallFlag: '--force',
    detect: () => false,
    addOverride: async (pkgName, pkgVersion, { installDir }) => {
        const packageDotJson = await (0, setup_utils_1.getPackageDotJson)({ installDir });
        const overrides = packageDotJson.overrides || {};
        await (0, setup_utils_1.updatePackageDotJson)({
            ...packageDotJson,
            overrides: {
                ...overrides,
                [pkgName]: pkgVersion,
            },
        }, { installDir });
    },
};
exports.packageManagers = [exports.BUN, exports.YARN_V1, exports.YARN_V2, exports.PNPM, exports.NPM, exports.EXPO];
function detectAllPackageManagers({ installDir, }) {
    return (0, telemetry_1.traceStep)('detect-package-manager', () => {
        const detectedManagers = [];
        for (const packageManager of exports.packageManagers) {
            if (packageManager.detect({ installDir })) {
                detectedManagers.push(packageManager);
            }
        }
        if (detectedManagers.length === 0) {
            analytics_1.analytics.setSessionProperty('package manager', 'not-detected');
        }
        return detectedManagers;
    });
}
