UNPKG

4.56 kBJavaScriptView Raw
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
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20const execa = require('execa');
21const semver = require('semver');
22const { CordovaError } = require('cordova-common');
23
24function fetchSdkVersionByType (sdkType) {
25 return execa('xcodebuild', ['-showsdks'])
26 .then(({ stdout }) => {
27 const regexSdk = new RegExp(`^${sdkType} \\d`);
28
29 const versions = stdout.split('\n')
30 .filter(line => line.trim().match(regexSdk))
31 .map(line => line.match(/\d+\.\d+/)[0])
32 .sort(exports.compareVersions);
33
34 return versions[0];
35 });
36}
37
38exports.get_apple_ios_version = () => {
39 return fetchSdkVersionByType('iOS');
40};
41
42exports.get_apple_osx_version = () => {
43 return fetchSdkVersionByType('macOS');
44};
45
46exports.get_apple_xcode_version = () => {
47 return execa('xcodebuild', ['-version'])
48 .then(({ stdout }) => {
49 const versionMatch = /Xcode (.*)/.exec(stdout);
50 if (!versionMatch) {
51 throw new CordovaError('Could not determine Xcode version from output:\n' + stdout);
52 }
53 return versionMatch[1];
54 });
55};
56
57/**
58 * Gets ios-deploy util version
59 * @return {Promise} Promise that either resolved with ios-deploy version
60 * or rejected in case of error
61 */
62exports.get_ios_deploy_version = () => {
63 return execa('ios-deploy', ['--version'])
64 .then(({ stdout }) => stdout);
65};
66
67/**
68 * Gets pod (CocoaPods) util version
69 * @return {Promise} Promise that either resolved with pod version
70 * or rejected in case of error
71 */
72exports.get_cocoapods_version = () => {
73 return execa('pod', ['--version'])
74 .then(({ stdout }) => stdout);
75};
76
77/**
78 * Gets ios-sim util version
79 * @return {Promise} Promise that either resolved with ios-sim version
80 * or rejected in case of error
81 */
82exports.get_ios_sim_version = () => {
83 return execa('ios-sim', ['--version'])
84 .then(({ stdout }) => stdout);
85};
86
87/**
88 * Gets specific tool version
89 * @param {String} toolName Tool name to check. Known tools are 'xcodebuild', 'ios-sim' and 'ios-deploy'
90 * @return {Promise} Promise that either resolved with tool version
91 * or rejected in case of error
92 */
93exports.get_tool_version = toolName => {
94 switch (toolName) {
95 case 'xcodebuild': return exports.get_apple_xcode_version();
96 case 'ios-sim': return exports.get_ios_sim_version();
97 case 'ios-deploy': return exports.get_ios_deploy_version();
98 case 'pod': return exports.get_cocoapods_version();
99 default: return Promise.reject(new CordovaError(`${toolName} is not valid tool name. Valid names are: 'xcodebuild', 'ios-sim', 'ios-deploy', and 'pod'`));
100 }
101};
102
103/**
104 * Compares two version strings that can be coerced to semver.
105 *
106 * @param {String} version1 Version to compare
107 * @param {String} version2 Another version to compare
108 * @return {Number} Negative number if first version is lower than the second,
109 * positive otherwise and 0 if versions are equal.
110 */
111exports.compareVersions = (...args) => {
112 const coerceToSemverIfInvalid = v => {
113 const semverVersion = semver.parse(v) || semver.coerce(v);
114 if (!semverVersion) throw new TypeError(`Invalid Version: ${v}`);
115 return semverVersion;
116 };
117
118 const semverVersions = args.map(coerceToSemverIfInvalid);
119 return semver.compare(...semverVersions);
120};
121
122exports.printOrDie = versionName =>
123 exports[`get_${versionName}_version`]().then(
124 version => {
125 console.log(version);
126 },
127 err => {
128 console.error(err.message);
129 process.exit(2);
130 }
131 );