You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2020/01/08 23:45:31 UTC

[GitHub] [cordova-ios] erisu commented on a change in pull request #763: refactor: replace superspawn & child_process with execa

erisu commented on a change in pull request #763: refactor: replace superspawn & child_process with execa
URL: https://github.com/apache/cordova-ios/pull/763#discussion_r364499259
 
 

 ##########
 File path: bin/templates/scripts/cordova/lib/versions.js
 ##########
 @@ -19,73 +19,55 @@
     under the License.
 */
 
-const child_process = require('child_process');
 const Q = require('q');
+const execa = require('execa');
 const semver = require('semver');
 
-exports.get_apple_ios_version = () => {
-    const d = Q.defer();
-    child_process.exec('xcodebuild -showsdks', (error, stdout, stderr) => {
-        if (error) {
-            d.reject(stderr);
-        } else {
-            d.resolve(stdout);
-        }
-    });
-
-    return d.promise.then(output => {
-        const regex = /[0-9]*\.[0-9]*/;
-        const versions = [];
-        const regexIOS = /^iOS \d+/;
-        output = output.split('\n');
-        for (let i = 0; i < output.length; i++) {
-            if (output[i].trim().match(regexIOS)) {
-                versions[versions.length] = parseFloat(output[i].match(regex)[0]);
+function fetchSdkVersionByType (sdkType) {
+    return execa('xcodebuild', ['-showsdks'])
+        .then(
+            ({ stdout }) => stdout,
+            ({ stderr }) => stderr
+        )
+        .then(output => {
+            output = output.split('\n');
+
+            const versions = [];
+            const regexSdk = new RegExp(`^${sdkType} \\d`);
+
+            for (const line of output) {
+                const matched = line.trim();
+
+                if (matched.match(regexSdk)) {
+                    versions.push(parseFloat(matched.match(/[0-9]*\.[0-9]*/)[0]));
+                }
             }
-        }
-        versions.sort();
-        console.log(versions[0]);
-        return Q();
-    }, stderr => Q.reject(stderr));
+
+            versions.sort();
+            console.log(versions[0]);
+        });
+}
+
+exports.get_apple_ios_version = () => {
+    return fetchSdkVersionByType('iOS');
 };
 
 exports.get_apple_osx_version = () => {
-    const d = Q.defer();
-    child_process.exec('xcodebuild -showsdks', (error, stdout, stderr) => {
-        if (error) {
-            d.reject(stderr);
-        } else {
-            d.resolve(stdout);
-        }
-    });
-
-    return d.promise.then(output => {
-        const regex = /[0-9]*\.[0-9]*/;
-        const versions = [];
-        const regexOSX = /^macOS \d+/;
-        output = output.split('\n');
-        for (let i = 0; i < output.length; i++) {
-            if (output[i].trim().match(regexOSX)) {
-                versions[versions.length] = parseFloat(output[i].match(regex)[0]);
-            }
-        }
-        versions.sort();
-        console.log(versions[0]);
-        return Q();
-    }, stderr => Q.reject(stderr));
+    return fetchSdkVersionByType('macOS');
 };
 
 exports.get_apple_xcode_version = () => {
-    const d = Q.defer();
-    child_process.exec('xcodebuild -version', (error, stdout, stderr) => {
-        const versionMatch = /Xcode (.*)/.exec(stdout);
-        if (error || !versionMatch) {
-            d.reject(stderr);
-        } else {
-            d.resolve(versionMatch[1]);
-        }
-    });
-    return d.promise;
+    return execa('xcodebuild', ['-version'])
+        .then(
+            ({ stdout }) => {
 
 Review comment:
   ```suggestion
               ({ stdout, stderr }) => {
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org