Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

check_reqs.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. 'use strict';
  18. const which = require('which');
  19. const versions = require('./versions');
  20. const { CordovaError } = require('cordova-common');
  21. const SUPPORTED_OS_PLATFORMS = ['darwin'];
  22. const XCODEBUILD_MIN_VERSION = '9.0.0';
  23. const XCODEBUILD_NOT_FOUND_MESSAGE =
  24. `Please install version ${XCODEBUILD_MIN_VERSION} or greater from App Store`;
  25. const IOS_DEPLOY_MIN_VERSION = '1.9.2';
  26. const IOS_DEPLOY_NOT_FOUND_MESSAGE =
  27. `Please download, build and install version ${IOS_DEPLOY_MIN_VERSION} or greater from https://github.com/ios-control/ios-deploy into your path, or do 'npm install -g ios-deploy'`;
  28. const COCOAPODS_MIN_VERSION = '1.8.0';
  29. const COCOAPODS_NOT_FOUND_MESSAGE = `Please install version ${COCOAPODS_MIN_VERSION} or greater from https://cocoapods.org/`;
  30. /**
  31. * Checks if xcode util is available
  32. * @return {Promise} Returns a promise either resolved with xcode version or rejected
  33. */
  34. module.exports.run = module.exports.check_xcodebuild = () => {
  35. return checkTool('xcodebuild', XCODEBUILD_MIN_VERSION, XCODEBUILD_NOT_FOUND_MESSAGE);
  36. };
  37. /**
  38. * Checks if ios-deploy util is available
  39. * @return {Promise} Returns a promise either resolved with ios-deploy version or rejected
  40. */
  41. module.exports.check_ios_deploy = () => {
  42. return checkTool('ios-deploy', IOS_DEPLOY_MIN_VERSION, IOS_DEPLOY_NOT_FOUND_MESSAGE);
  43. };
  44. module.exports.check_os = () => {
  45. // Build iOS apps available for OSX platform only, so we reject on others platforms
  46. return os_platform_is_supported()
  47. ? Promise.resolve(process.platform)
  48. : Promise.reject(new CordovaError('Cordova tooling for iOS requires Apple macOS'));
  49. };
  50. function os_platform_is_supported () {
  51. return (SUPPORTED_OS_PLATFORMS.indexOf(process.platform) !== -1);
  52. }
  53. /**
  54. * Checks if cocoapods is available.
  55. * @return {Promise} Returns a promise either resolved or rejected
  56. */
  57. module.exports.check_cocoapods = toolChecker => {
  58. return checkTool('pod', COCOAPODS_MIN_VERSION, COCOAPODS_NOT_FOUND_MESSAGE, 'CocoaPods');
  59. };
  60. /**
  61. * Checks if specific tool is available.
  62. * @param {String} tool Tool name to check. Known tools are 'xcodebuild' and 'ios-deploy'
  63. * @param {Number} minVersion Min allowed tool version.
  64. * @param {String} message Message that will be used to reject promise.
  65. * @param {String} toolFriendlyName Friendly name of the tool, to report to the user. Optional.
  66. * @return {Promise} Returns a promise either resolved with tool version or rejected
  67. */
  68. function checkTool (tool, minVersion, message, toolFriendlyName) {
  69. toolFriendlyName = toolFriendlyName || tool;
  70. // Check whether tool command is available at all
  71. const tool_command = which.sync(tool, { nothrow: true });
  72. if (!tool_command) {
  73. return Promise.reject(new CordovaError(`${toolFriendlyName} was not found. ${message || ''}`));
  74. }
  75. // check if tool version is greater than specified one
  76. return versions.get_tool_version(tool).then(version => {
  77. version = version.trim();
  78. return versions.compareVersions(version, minVersion) >= 0
  79. ? Promise.resolve({ version })
  80. : Promise.reject(new CordovaError(`Cordova needs ${toolFriendlyName} version ${minVersion} or greater, you have version ${version}. ${message || ''}`));
  81. });
  82. }
  83. /**
  84. * Object that represents one of requirements for current platform.
  85. * @param {String} id The unique identifier for this requirements.
  86. * @param {String} name The name of requirements. Human-readable field.
  87. * @param {Boolean} isFatal Marks the requirement as fatal. If such requirement will fail
  88. * next requirements' checks will be skipped.
  89. */
  90. const Requirement = function (id, name, isFatal) {
  91. this.id = id;
  92. this.name = name;
  93. this.installed = false;
  94. this.metadata = {};
  95. this.isFatal = isFatal || false;
  96. };
  97. /**
  98. * Methods that runs all checks one by one and returns a result of checks
  99. * as an array of Requirement objects. This method intended to be used by cordova-lib check_reqs method
  100. *
  101. * @return Promise<Requirement[]> Array of requirements. Due to implementation, promise is always fulfilled.
  102. */
  103. module.exports.check_all = () => {
  104. const requirements = [
  105. new Requirement('os', 'Apple macOS', true),
  106. new Requirement('xcode', 'Xcode'),
  107. new Requirement('ios-deploy', 'ios-deploy'),
  108. new Requirement('CocoaPods', 'CocoaPods')
  109. ];
  110. const result = [];
  111. let fatalIsHit = false;
  112. const checkFns = [
  113. module.exports.check_os,
  114. module.exports.check_xcodebuild,
  115. module.exports.check_ios_deploy,
  116. module.exports.check_cocoapods
  117. ];
  118. // Then execute requirement checks one-by-one
  119. return checkFns.reduce((promise, checkFn, idx) => {
  120. return promise.then(() => {
  121. // If fatal requirement is failed,
  122. // we don't need to check others
  123. if (fatalIsHit) return Promise.resolve();
  124. const requirement = requirements[idx];
  125. return checkFn()
  126. .then(version => {
  127. requirement.installed = true;
  128. requirement.metadata.version = version;
  129. result.push(requirement);
  130. }, err => {
  131. if (requirement.isFatal) fatalIsHit = true;
  132. requirement.metadata.reason = err;
  133. result.push(requirement);
  134. });
  135. });
  136. }, Promise.resolve())
  137. // When chain is completed, return requirements array to upstream API
  138. .then(() => result);
  139. };