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

versions.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. const {
  18. CordovaError,
  19. superspawn: { spawn }
  20. } = require('cordova-common');
  21. const semver = require('semver');
  22. function fetchSdkVersionByType (sdkType) {
  23. return spawn('xcodebuild', ['-showsdks'])
  24. .then(output => {
  25. const regexSdk = new RegExp(`^${sdkType} \\d`);
  26. const versions = output.split('\n')
  27. .filter(line => line.trim().match(regexSdk))
  28. .map(line => line.match(/\d+\.\d+/)[0])
  29. .sort(exports.compareVersions);
  30. console.log(versions[0]);
  31. });
  32. }
  33. exports.get_apple_ios_version = () => {
  34. return fetchSdkVersionByType('iOS');
  35. };
  36. exports.get_apple_osx_version = () => {
  37. return fetchSdkVersionByType('macOS');
  38. };
  39. exports.get_apple_xcode_version = () => {
  40. return spawn('xcodebuild', ['-version'])
  41. .then(output => {
  42. const versionMatch = /Xcode (.*)/.exec(output);
  43. if (!versionMatch) return Promise.reject(output);
  44. return versionMatch[1];
  45. });
  46. };
  47. /**
  48. * Gets ios-deploy util version
  49. * @return {Promise} Promise that either resolved with ios-deploy version
  50. * or rejected in case of error
  51. */
  52. exports.get_ios_deploy_version = () => {
  53. return spawn('ios-deploy', ['--version']);
  54. };
  55. /**
  56. * Gets pod (CocoaPods) util version
  57. * @return {Promise} Promise that either resolved with pod version
  58. * or rejected in case of error
  59. */
  60. exports.get_cocoapods_version = () => {
  61. return spawn('pod', ['--version']);
  62. };
  63. /**
  64. * Gets ios-sim util version
  65. * @return {Promise} Promise that either resolved with ios-sim version
  66. * or rejected in case of error
  67. */
  68. exports.get_ios_sim_version = () => {
  69. return spawn('ios-sim', ['--version']);
  70. };
  71. /**
  72. * Gets specific tool version
  73. * @param {String} toolName Tool name to check. Known tools are 'xcodebuild', 'ios-sim' and 'ios-deploy'
  74. * @return {Promise} Promise that either resolved with tool version
  75. * or rejected in case of error
  76. */
  77. exports.get_tool_version = toolName => {
  78. switch (toolName) {
  79. case 'xcodebuild': return exports.get_apple_xcode_version();
  80. case 'ios-sim': return exports.get_ios_sim_version();
  81. case 'ios-deploy': return exports.get_ios_deploy_version();
  82. case 'pod': return exports.get_cocoapods_version();
  83. default: return Promise.reject(new CordovaError(`${toolName} is not valid tool name. Valid names are: 'xcodebuild', 'ios-sim', 'ios-deploy', and 'pod'`));
  84. }
  85. };
  86. /**
  87. * Compares two version strings that can be coerced to semver.
  88. *
  89. * @param {String} version1 Version to compare
  90. * @param {String} version2 Another version to compare
  91. * @return {Number} Negative number if first version is lower than the second,
  92. * positive otherwise and 0 if versions are equal.
  93. */
  94. exports.compareVersions = (...args) => {
  95. const coerceToSemverIfInvalid = v => {
  96. const semverVersion = semver.parse(v) || semver.coerce(v);
  97. if (!semverVersion) throw new TypeError(`Invalid Version: ${v}`);
  98. return semverVersion;
  99. };
  100. const semverVersions = args.map(coerceToSemverIfInvalid);
  101. return semver.compare(...semverVersions);
  102. };