UNPKG

5.79 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.promptCocoaPodsInstallationQuestion = promptCocoaPodsInstallationQuestion;
7exports.runSudo = runSudo;
8exports.installCocoaPods = installCocoaPods;
9exports.default = void 0;
10
11function _fs() {
12 const data = _interopRequireDefault(require("fs"));
13
14 _fs = function () {
15 return data;
16 };
17
18 return data;
19}
20
21function _execa() {
22 const data = _interopRequireDefault(require("execa"));
23
24 _execa = function () {
25 return data;
26 };
27
28 return data;
29}
30
31function _chalk() {
32 const data = _interopRequireDefault(require("chalk"));
33
34 _chalk = function () {
35 return data;
36 };
37
38 return data;
39}
40
41function _inquirer() {
42 const data = _interopRequireDefault(require("inquirer"));
43
44 _inquirer = function () {
45 return data;
46 };
47
48 return data;
49}
50
51function _cliTools() {
52 const data = require("@react-native-community/cli-tools");
53
54 _cliTools = function () {
55 return data;
56 };
57
58 return data;
59}
60
61var _loader = require("./loader");
62
63function _sudoPrompt() {
64 const data = _interopRequireDefault(require("sudo-prompt"));
65
66 _sudoPrompt = function () {
67 return data;
68 };
69
70 return data;
71}
72
73var _brewInstall = require("./brewInstall");
74
75function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
76
77// @ts-ignore untyped
78// @ts-ignore untyped
79async function updatePods(loader) {
80 try {
81 loader.start(`Updating CocoaPods repositories ${_chalk().default.dim('(this may take a few minutes)')}`);
82 await (0, _execa().default)('pod', ['repo', 'update']);
83 } catch (error) {
84 // "pod" command outputs errors to stdout (at least some of them)
85 _cliTools().logger.log(error.stderr || error.stdout);
86
87 loader.fail();
88 throw new Error(`Failed to update CocoaPods repositories for iOS project.\nPlease try again manually: "pod repo update".\nCocoaPods documentation: ${_chalk().default.dim.underline('https://cocoapods.org/')}`);
89 }
90}
91
92function runSudo(command) {
93 return new Promise((resolve, reject) => {
94 _sudoPrompt().default.exec(command, error => {
95 if (error) {
96 reject(error);
97 }
98
99 resolve();
100 });
101 });
102}
103
104async function promptCocoaPodsInstallationQuestion() {
105 const promptQuestion = `CocoaPods ${_chalk().default.dim.underline('(https://cocoapods.org/)')} ${_chalk().default.reset.bold('is not installed. CocoaPods is necessary for the iOS project to run correctly. Do you want to install it?')}`;
106 const installWithGem = 'Yes, with gem (may require sudo)';
107 const installWithHomebrew = 'Yes, with Homebrew';
108 const {
109 shouldInstallCocoaPods
110 } = await _inquirer().default.prompt([{
111 type: 'list',
112 name: 'shouldInstallCocoaPods',
113 message: promptQuestion,
114 choices: [installWithGem, installWithHomebrew]
115 }]);
116 const shouldInstallWithGem = shouldInstallCocoaPods === installWithGem;
117 return {
118 installMethod: shouldInstallWithGem ? 'gem' : 'homebrew',
119 // This is used for removing the message in `doctor` after it's answered
120 promptQuestion: `? ${promptQuestion} ${shouldInstallWithGem ? installWithGem : installWithHomebrew}`
121 };
122}
123
124async function installCocoaPodsWithGem() {
125 const options = ['install', 'cocoapods', '--no-document'];
126
127 try {
128 // First attempt to install `cocoapods`
129 await (0, _execa().default)('gem', options);
130 } catch (_error) {
131 // If that doesn't work then try with sudo
132 await runSudo(`gem ${options.join(' ')}`);
133 }
134}
135
136async function installCocoaPods(loader) {
137 loader.stop();
138 const {
139 installMethod
140 } = await promptCocoaPodsInstallationQuestion();
141
142 if (installMethod === 'gem') {
143 loader.start('Installing CocoaPods');
144
145 try {
146 await installCocoaPodsWithGem();
147 return loader.succeed();
148 } catch (error) {
149 loader.fail();
150
151 _cliTools().logger.error(error.stderr);
152
153 throw new Error(`An error occured while trying to install CocoaPods, which is required by this template.\nPlease try again manually: sudo gem install cocoapods.\nCocoaPods documentation: ${_chalk().default.dim.underline('https://cocoapods.org/')}`);
154 }
155 }
156
157 if (installMethod === 'homebrew') {
158 return await (0, _brewInstall.brewInstall)({
159 pkg: 'cocoapods',
160 label: 'Installing CocoaPods',
161 loader
162 });
163 }
164}
165
166async function installPods({
167 projectName,
168 loader,
169 shouldUpdatePods
170}) {
171 loader = loader || new _loader.NoopLoader();
172
173 try {
174 if (!_fs().default.existsSync('ios')) {
175 return;
176 }
177
178 process.chdir('ios');
179
180 const hasPods = _fs().default.existsSync('Podfile');
181
182 if (!hasPods) {
183 return;
184 }
185
186 try {
187 // Check if "pod" is available and usable. It happens that there are
188 // multiple versions of "pod" command and even though it's there, it exits
189 // with a failure
190 await (0, _execa().default)('pod', ['--version']);
191 } catch (e) {
192 loader.info();
193 await installCocoaPods(loader);
194 }
195
196 if (shouldUpdatePods) {
197 await updatePods(loader);
198 }
199
200 try {
201 loader.start(`Installing CocoaPods dependencies ${_chalk().default.dim('(this may take a few minutes)')}`);
202 await (0, _execa().default)('pod', ['install']);
203 } catch (error) {
204 // "pod" command outputs errors to stdout (at least some of them)
205 _cliTools().logger.log(error.stderr || error.stdout);
206
207 throw new Error(`Failed to install CocoaPods dependencies for iOS project, which is required by this template.\nPlease try again manually: "cd ./${projectName}/ios && pod install".\nCocoaPods documentation: ${_chalk().default.dim.underline('https://cocoapods.org/')}`);
208 }
209 } catch (error) {
210 throw error;
211 } finally {
212 process.chdir('..');
213 }
214}
215
216var _default = installPods;
217exports.default = _default;
\No newline at end of file