installPods.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.promptCocoaPodsInstallationQuestion = promptCocoaPodsInstallationQuestion;
  6. exports.runSudo = runSudo;
  7. exports.installCocoaPods = installCocoaPods;
  8. exports.default = void 0;
  9. function _fs() {
  10. const data = _interopRequireDefault(require("fs"));
  11. _fs = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _execa() {
  17. const data = _interopRequireDefault(require("execa"));
  18. _execa = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _chalk() {
  24. const data = _interopRequireDefault(require("chalk"));
  25. _chalk = function () {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _inquirer() {
  31. const data = _interopRequireDefault(require("inquirer"));
  32. _inquirer = function () {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function _cliTools() {
  38. const data = require("@react-native-community/cli-tools");
  39. _cliTools = function () {
  40. return data;
  41. };
  42. return data;
  43. }
  44. var _loader = require("./loader");
  45. function _sudoPrompt() {
  46. const data = _interopRequireDefault(require("sudo-prompt"));
  47. _sudoPrompt = function () {
  48. return data;
  49. };
  50. return data;
  51. }
  52. var _brewInstall = require("./brewInstall");
  53. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  54. // @ts-ignore untyped
  55. // @ts-ignore untyped
  56. async function runPodInstall(loader, projectName, shouldHandleRepoUpdate = true) {
  57. try {
  58. loader.start(`Installing CocoaPods dependencies ${_chalk().default.dim('(this may take a few minutes)')}`);
  59. await (0, _execa().default)('pod', ['install']);
  60. } catch (error) {
  61. // "pod" command outputs errors to stdout (at least some of them)
  62. const stderr = error.stderr || error.stdout;
  63. /**
  64. * If CocoaPods failed due to repo being out of date, it will
  65. * include the update command in the error message.
  66. *
  67. * `shouldHandleRepoUpdate` will be set to `false` to
  68. * prevent infinite loop (unlikely scenario)
  69. */
  70. if (stderr.includes('pod repo update') && shouldHandleRepoUpdate) {
  71. await runPodUpdate(loader);
  72. await runPodInstall(loader, projectName, false);
  73. } else {
  74. loader.fail();
  75. 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/')}`);
  76. }
  77. }
  78. }
  79. async function runPodUpdate(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. loader.fail();
  87. 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/')}`);
  88. }
  89. }
  90. function runSudo(command) {
  91. return new Promise((resolve, reject) => {
  92. _sudoPrompt().default.exec(command, {
  93. name: 'React Native CLI'
  94. }, error => {
  95. if (error) {
  96. reject(error);
  97. }
  98. resolve();
  99. });
  100. });
  101. }
  102. async function promptCocoaPodsInstallationQuestion() {
  103. 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?')}`;
  104. const installWithGem = 'Yes, with gem (may require sudo)';
  105. const installWithHomebrew = 'Yes, with Homebrew';
  106. const {
  107. shouldInstallCocoaPods
  108. } = await _inquirer().default.prompt([{
  109. type: 'list',
  110. name: 'shouldInstallCocoaPods',
  111. message: promptQuestion,
  112. choices: [installWithGem, installWithHomebrew]
  113. }]);
  114. const shouldInstallWithGem = shouldInstallCocoaPods === installWithGem;
  115. return {
  116. installMethod: shouldInstallWithGem ? 'gem' : 'homebrew',
  117. // This is used for removing the message in `doctor` after it's answered
  118. promptQuestion: `? ${promptQuestion} ${shouldInstallWithGem ? installWithGem : installWithHomebrew}`
  119. };
  120. }
  121. async function installCocoaPodsWithGem() {
  122. const options = ['install', 'cocoapods', '--no-document'];
  123. try {
  124. // First attempt to install `cocoapods`
  125. await (0, _execa().default)('gem', options);
  126. } catch (_error) {
  127. // If that doesn't work then try with sudo
  128. await runSudo(`gem ${options.join(' ')}`);
  129. }
  130. }
  131. async function installCocoaPods(loader) {
  132. loader.stop();
  133. const {
  134. installMethod
  135. } = await promptCocoaPodsInstallationQuestion();
  136. if (installMethod === 'gem') {
  137. loader.start('Installing CocoaPods');
  138. try {
  139. await installCocoaPodsWithGem();
  140. return loader.succeed();
  141. } catch (error) {
  142. loader.fail();
  143. _cliTools().logger.error(error.stderr);
  144. 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/')}`);
  145. }
  146. }
  147. if (installMethod === 'homebrew') {
  148. return await (0, _brewInstall.brewInstall)({
  149. pkg: 'cocoapods',
  150. label: 'Installing CocoaPods',
  151. loader
  152. });
  153. }
  154. }
  155. async function installPods({
  156. projectName,
  157. loader
  158. }) {
  159. loader = loader || new _loader.NoopLoader();
  160. try {
  161. if (!_fs().default.existsSync('ios')) {
  162. return;
  163. }
  164. process.chdir('ios');
  165. const hasPods = _fs().default.existsSync('Podfile');
  166. if (!hasPods) {
  167. return;
  168. }
  169. try {
  170. // Check if "pod" is available and usable. It happens that there are
  171. // multiple versions of "pod" command and even though it's there, it exits
  172. // with a failure
  173. await (0, _execa().default)('pod', ['--version']);
  174. } catch (e) {
  175. loader.info();
  176. await installCocoaPods(loader);
  177. }
  178. await runPodInstall(loader, projectName);
  179. } catch (error) {
  180. throw error;
  181. } finally {
  182. process.chdir('..');
  183. }
  184. }
  185. var _default = installPods;
  186. exports.default = _default;
  187. //# sourceMappingURL=installPods.js.map