1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.BasePackageManager = void 0;
7const spawn_async_1 = __importDefault(require("@expo/spawn-async"));
8const assert_1 = __importDefault(require("assert"));
9const fs_1 = __importDefault(require("fs"));
10const path_1 = __importDefault(require("path"));
11class BasePackageManager {
12    constructor({ silent, log, env = process.env, ...options } = {}) {
13        this.silent = !!silent;
14        this.log = log ?? (!silent ? console.log : undefined);
15        this.options = {
16            stdio: silent ? undefined : 'inherit',
17            ...options,
18            env: { ...this.getDefaultEnvironment(), ...env },
19        };
20    }
21    /** Get the default environment variables used when running the package manager. */
22    getDefaultEnvironment() {
23        return {
24            ADBLOCK: '1',
25            DISABLE_OPENCOLLECTIVE: '1',
26        };
27    }
28    /** Ensure the CWD is set to a non-empty string */
29    ensureCwdDefined(method) {
30        const cwd = this.options.cwd?.toString();
31        const className = this.constructor.name;
32        const methodName = method ? `.${method}` : '';
33        (0, assert_1.default)(cwd, `cwd is required for ${className}${methodName}`);
34        return cwd;
35    }
36    runAsync(command) {
37        this.log?.(`> ${this.name} ${command.join(' ')}`);
38        return (0, spawn_async_1.default)(this.bin, command, this.options);
39    }
40    async versionAsync() {
41        return await this.runAsync(['--version']).then(({ stdout }) => stdout.trim());
42    }
43    async getConfigAsync(key) {
44        return await this.runAsync(['config', 'get', key]).then(({ stdout }) => stdout.trim());
45    }
46    async removeLockfileAsync() {
47        const cwd = this.ensureCwdDefined('removeLockFile');
48        const filePath = path_1.default.join(cwd, this.lockFile);
49        await fs_1.default.promises.rm(filePath, { force: true });
50    }
51    installAsync(flags = []) {
52        return this.runAsync(['install', ...flags]);
53    }
54    async uninstallAsync() {
55        const cwd = this.ensureCwdDefined('uninstallAsync');
56        const modulesPath = path_1.default.join(cwd, 'node_modules');
57        await fs_1.default.promises.rm(modulesPath, { force: true, recursive: true });
58    }
59}
60exports.BasePackageManager = BasePackageManager;
61//# sourceMappingURL=BasePackageManager.js.map