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.spawnSudoAsync = exports.createPendingSpawnAsync = void 0;
7const spawn_async_1 = __importDefault(require("@expo/spawn-async"));
8const sudo_prompt_1 = __importDefault(require("sudo-prompt"));
9function createPendingSpawnAsync(actionAsync, spawnAsync) {
10    // Manually rsolve the child promise whenever the prepending async action is resolved.
11    // Avoid `childReject` to prevent "unhandled promise rejection" for one of the two promises.
12    let childResolve;
13    const child = new Promise((resolve, reject) => {
14        childResolve = resolve;
15    });
16    const pendingPromise = new Promise((spawnResolve, spawnReject) => {
17        actionAsync()
18            .then((result) => {
19            const spawnPromise = spawnAsync(result);
20            childResolve(spawnPromise.child);
21            spawnPromise.then(spawnResolve).catch(spawnReject);
22        })
23            .catch((error) => {
24            childResolve(null);
25            spawnReject(error);
26        });
27    });
28    pendingPromise.child = child;
29    return pendingPromise;
30}
31exports.createPendingSpawnAsync = createPendingSpawnAsync;
32/**
33 * Spawn a command with sudo privileges.
34 * On windows, this uses the `sudo-prompt` package.
35 * on other systems, this uses the `sudo` binary.
36 */
37async function spawnSudoAsync(command, spawnOptions) {
38    // sudo prompt only seems to work on win32 machines.
39    if (process.platform === 'win32') {
40        return new Promise((resolve, reject) => {
41            sudo_prompt_1.default.exec(command.join(' '), { name: 'pod install' }, (error) => {
42                if (error) {
43                    reject(error);
44                }
45                resolve();
46            });
47        });
48    }
49    else {
50        // Attempt to use sudo to run the command on Mac and Linux.
51        // TODO(Bacon): Make a v of sudo-prompt that's win32 only for better bundle size.
52        console.log('Your password might be needed to install CocoaPods CLI: https://guides.cocoapods.org/using/getting-started.html#installation');
53        await (0, spawn_async_1.default)('sudo', command, spawnOptions);
54    }
55}
56exports.spawnSudoAsync = spawnSudoAsync;
57//# sourceMappingURL=spawn.js.map