1import spawnAsync, { SpawnOptions } from '@expo/spawn-async';
2import { realpathSync } from 'fs';
3import sudo from 'sudo-prompt';
4
5export type Logger = (...args: any[]) => void;
6
7export interface PackageManager {
8  installAsync(): Promise<void>;
9  addWithParametersAsync(names: string[], parameters: string[]): Promise<void>;
10  addAsync(...names: string[]): Promise<void>;
11  addDevAsync(...names: string[]): Promise<void>;
12  versionAsync(): Promise<string>;
13  getConfigAsync(key: string): Promise<string>;
14  removeLockfileAsync(): Promise<void>;
15  cleanAsync(): Promise<void>;
16}
17
18export function getPossibleProjectRoot(): string {
19  return realpathSync(process.cwd());
20}
21
22export async function spawnSudoAsync(command: string[], spawnOptions: SpawnOptions): Promise<void> {
23  // sudo prompt only seems to work on win32 machines.
24  if (process.platform === 'win32') {
25    return new Promise((resolve, reject) => {
26      sudo.exec(command.join(' '), { name: 'pod install' }, (error) => {
27        if (error) {
28          reject(error);
29        }
30        resolve();
31      });
32    });
33  } else {
34    // Attempt to use sudo to run the command on Mac and Linux.
35    // TODO(Bacon): Make a v of sudo-prompt that's win32 only for better bundle size.
36    console.log(
37      'Your password might be needed to install CocoaPods CLI: https://guides.cocoapods.org/using/getting-started.html#installation'
38    );
39    await spawnAsync('sudo', command, spawnOptions);
40  }
41}
42