You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2019/11/08 09:46:30 UTC

[cordova-lib] branch master updated: chore: replace superspawn with execa (#812)

This is an automated email from the ASF dual-hosted git repository.

erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-lib.git


The following commit(s) were added to refs/heads/master by this push:
     new be96be9  chore: replace superspawn with execa (#812)
be96be9 is described below

commit be96be9b54bb222c843eee15029902fd02baa4e1
Author: エリス <er...@users.noreply.github.com>
AuthorDate: Fri Nov 8 18:46:19 2019 +0900

    chore: replace superspawn with execa (#812)
    
    * chore: replace superspawn with execa
    * chore: remove comments
    * chore: move catch to onRejected
    * chore: move catch to onRejected
    * chore: bump execa dependency
    * chore: remove unused execa require
    
    Co-Authored-By: Raphael von der Grün <ra...@gmail.com>
    * Remove duplicate spy strategy application
    * Show error message instead of stderr in info command
    * Fix failSafeSpawn to always return a string
    This also minimizes the diff
    * Change warning as error.message already includes exit code
    * Remove chmod option that was unique to superspawn
    * Replace superspawn's printCommand w/ manual logging
    * Use execa's error message which is a better version of the current one
    * Minimize diff
---
 package.json                 |  1 +
 spec/plugman/install.spec.js | 20 ++++++++++++--------
 src/cordova/info.js          |  6 +++---
 src/cordova/plugin/util.js   |  6 +++---
 src/cordova/targets.js       |  8 ++++----
 src/hooks/HooksRunner.js     | 12 ++++++------
 src/plugman/install.js       | 12 ++++++------
 7 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/package.json b/package.json
index 864815f..abac78a 100644
--- a/package.json
+++ b/package.json
@@ -24,6 +24,7 @@
     "dep-graph": "^1.1.0",
     "detect-indent": "^6.0.0",
     "elementtree": "^0.1.7",
+    "execa": "^3.2.0",
     "fs-extra": "^8.1.0",
     "globby": "^10.0.1",
     "indent-string": "^4.0.0",
diff --git a/spec/plugman/install.spec.js b/spec/plugman/install.spec.js
index bc7cf04..65cc086 100644
--- a/spec/plugman/install.spec.js
+++ b/spec/plugman/install.spec.js
@@ -22,7 +22,7 @@ const path = require('path');
 const semver = require('semver');
 const rewire = require('rewire');
 
-const { events, PlatformJson, superspawn } = require('cordova-common');
+const { events, PlatformJson } = require('cordova-common');
 const { spy: emitSpyHelper } = require('../common');
 const knownPlatforms = require('../../src/platforms/platforms');
 
@@ -67,6 +67,7 @@ const fake = {
 describe('plugman/install', () => {
     let install = require('../../src/plugman/install');
     let fetchSpy;
+    let execaSpy;
 
     beforeAll(() => {
         let api;
@@ -115,7 +116,10 @@ describe('plugman/install', () => {
         fetchSpy = jasmine.createSpy('plugmanFetch').and.returnValue(Promise.resolve(pluginDir('com.cordova.engine')));
         install.__set__({ plugmanFetch: fetchSpy });
 
-        spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(''));
+        execaSpy = jasmine.createSpy('execa');
+        execaSpy.and.returnValue(Promise.resolve({ stdout: '' }));
+        install.__set__('execa', execaSpy);
+
         spyOn(fs, 'ensureDirSync');
         spyOn(fs, 'writeFileSync');
         spyOn(fs, 'copySync');
@@ -153,21 +157,21 @@ describe('plugman/install', () => {
             });
 
             it('Test 007 : should check version if plugin has engine tag', () => {
-                superspawn.spawn.and.returnValue(Promise.resolve('2.5.0'));
+                execaSpy.and.returnValue(Promise.resolve({ stdout: '2.5.0' }));
                 return install('android', project, pluginDir('com.cordova.engine'))
                     .then(() => {
                         expect(satisfies).toHaveBeenCalledWith('2.5.0', '>=1.0.0', true);
                     });
             }, TIMEOUT);
             it('Test 008 : should check version and munge it a little if it has "rc" in it so it plays nice with semver (introduce a dash in it)', () => {
-                superspawn.spawn.and.returnValue(Promise.resolve('3.0.0rc1'));
+                execaSpy.and.returnValue(Promise.resolve({ stdout: '3.0.0rc1' }));
                 return install('android', project, pluginDir('com.cordova.engine'))
                     .then(() => {
                         expect(satisfies).toHaveBeenCalledWith('3.0.0-rc1', '>=1.0.0', true);
                     });
             }, TIMEOUT);
             it('Test 009 : should check specific platform version over cordova version if specified', () => {
-                superspawn.spawn.and.returnValue(Promise.resolve('3.1.0'));
+                execaSpy.and.returnValue(Promise.resolve({ stdout: '3.1.0' }));
                 return install('android', project, pluginDir('com.cordova.engine-android'))
                     .then(() => {
                         expect(satisfies).toHaveBeenCalledWith('3.1.0', '>=3.1.0', true);
@@ -175,7 +179,7 @@ describe('plugman/install', () => {
             }, TIMEOUT);
             it('Test 010 : should check platform sdk version if specified', () => {
                 const cordovaVersion = require('../../package.json').version.replace(/-dev|-nightly.*$/, '');
-                superspawn.spawn.and.returnValue(Promise.resolve('18'));
+                execaSpy.and.returnValue(Promise.resolve({ stdout: '18' }));
                 return install('android', project, pluginDir('com.cordova.engine-android'))
                     .then(() => {
                         expect(satisfies.calls.count()).toBe(3);
@@ -220,7 +224,7 @@ describe('plugman/install', () => {
                 spyOn(fs, 'existsSync').and.callFake(fake['existsSync']['noPlugins']);
                 fetchSpy.and.callFake(fake['fetch']['dependencies']);
                 emit = spyOn(events, 'emit');
-                superspawn.spawn.and.returnValue(Promise.resolve('9.0.0'));
+                execaSpy.and.returnValue(Promise.resolve({ stdout: '9.0.0' }));
 
                 class PlatformApiMock {
                     static addPlugin () { return Promise.resolve(); }
@@ -339,7 +343,7 @@ describe('plugman/install', () => {
 
         it('Test 025 :should not fail when trying to install plugin less than minimum version. Skip instead  ', () => {
             spyOn(semver, 'satisfies').and.returnValue(false);
-            superspawn.spawn.and.returnValue(Promise.resolve('0.0.1'));
+            execaSpy.and.returnValue(Promise.resolve({ stdout: '0.0.1' }));
 
             return install('android', project, pluginDir('com.cordova.engine'))
                 .then(result => {
diff --git a/src/cordova/info.js b/src/cordova/info.js
index 8592540..1859ef7 100644
--- a/src/cordova/info.js
+++ b/src/cordova/info.js
@@ -17,8 +17,8 @@ specific language governing permissions and limitations
 under the License.
 */
 
+const execa = require('execa');
 var cordova_util = require('./util');
-var superspawn = require('cordova-common').superspawn;
 var pkg = require('../../package');
 var path = require('path');
 var fs = require('fs-extra');
@@ -105,8 +105,8 @@ function getPlatformInfo (platform) {
 }
 
 function failSafeSpawn (command, args) {
-    return superspawn.spawn(command, args)
-        .catch(err => `ERROR: ${err.message}`);
+    return execa(command, args)
+        .then(({ stdout }) => stdout, err => `ERROR: ${err.message}`);
 }
 
 function displayFileContents (filePath) {
diff --git a/src/cordova/plugin/util.js b/src/cordova/plugin/util.js
index 379df63..016c8c4 100644
--- a/src/cordova/plugin/util.js
+++ b/src/cordova/plugin/util.js
@@ -17,13 +17,13 @@
     under the License.
 */
 
+const execa = require('execa');
 var path = require('path');
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
 var fs = require('fs-extra');
 var events = require('cordova-common').events;
 var CordovaError = require('cordova-common').CordovaError;
 var fetch = require('cordova-fetch');
-var superspawn = require('cordova-common').superspawn;
 
 module.exports.getInstalledPlugins = getInstalledPlugins;
 module.exports.mergeVariables = mergeVariables;
@@ -80,8 +80,8 @@ function info (plugin) {
     // check if npm is installed
     return fetch.isNpmInstalled()
         .then(function () {
-            return superspawn.spawn('npm', viewArgs)
-                .then(function (info) {
+            return execa('npm', viewArgs)
+                .then(({ stdout: info }) => {
                     var pluginInfo = JSON.parse(info);
                     return pluginInfo;
                 });
diff --git a/src/cordova/targets.js b/src/cordova/targets.js
index 12ec86c..04b7e4c 100644
--- a/src/cordova/targets.js
+++ b/src/cordova/targets.js
@@ -17,8 +17,8 @@
     under the License.
 */
 
+const execa = require('execa');
 var cordova_util = require('./util');
-var superspawn = require('cordova-common').superspawn;
 var path = require('path');
 var events = require('cordova-common').events;
 
@@ -27,7 +27,7 @@ function handleError (error) {
         events.emit('warn', 'Platform does not support ' + this.script);
     } else {
         events.emit('warn', 'An unexpected error has occured while running ' + this.script +
-            ' with code ' + error.code + ': ' + error);
+            ':\n' + error.message);
     }
 }
 
@@ -35,14 +35,14 @@ function displayDevices (projectRoot, platform, options) {
     var caller = { 'script': 'list-devices' };
     events.emit('log', 'Available ' + platform + ' devices:');
     var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'lib', 'list-devices');
-    return superspawn.spawn(cmd, options.argv, { stdio: 'inherit', chmod: true }).catch(handleError.bind(caller));
+    return execa(cmd, options.argv, { stdio: 'inherit' }).then(data => data.stdout, handleError.bind(caller));
 }
 
 function displayVirtualDevices (projectRoot, platform, options) {
     var caller = { 'script': 'list-emulator-images' };
     events.emit('log', 'Available ' + platform + ' virtual devices:');
     var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'lib', 'list-emulator-images');
-    return superspawn.spawn(cmd, options.argv, { stdio: 'inherit', chmod: true }).catch(handleError.bind(caller));
+    return execa(cmd, options.argv, { stdio: 'inherit' }).then(data => data.stdout, handleError.bind(caller));
 }
 
 module.exports = function targets (options) {
diff --git a/src/hooks/HooksRunner.js b/src/hooks/HooksRunner.js
index 16c518c..c89a941 100644
--- a/src/hooks/HooksRunner.js
+++ b/src/hooks/HooksRunner.js
@@ -15,6 +15,7 @@
  under the License.
  */
 
+const execa = require('execa');
 const fs = require('fs-extra');
 const os = require('os');
 const path = require('path');
@@ -24,7 +25,7 @@ const shebangCommand = require('shebang-command');
 const cordovaUtil = require('../cordova/util');
 const scriptsFinder = require('./scriptsFinder');
 const Context = require('./Context');
-const { CordovaError, events, superspawn } = require('cordova-common');
+const { CordovaError, events } = require('cordova-common');
 
 const isWindows = os.platform().slice(0, 3) === 'win';
 
@@ -184,7 +185,6 @@ function runScriptViaChildProcessSpawn (script, context) {
 
     const execOpts = {
         cwd: opts.projectRoot,
-        printCommand: true,
         stdio: 'inherit',
         env: {
             CORDOVA_VERSION: require('../../package').version,
@@ -195,10 +195,10 @@ function runScriptViaChildProcessSpawn (script, context) {
         }
     };
 
-    return superspawn.spawn(command, args, execOpts)
-        .catch(function (err) {
-            throw new Error('Hook failed with error code ' + err.code + ': ' + script.fullPath);
-        });
+    events.emit('log', `Running hook: ${command} ${args.join(' ')}`);
+
+    return execa(command, args, execOpts)
+        .then(data => data.stdout);
 }
 
 /**
diff --git a/src/plugman/install.js b/src/plugman/install.js
index 89b8575..835ce5a 100644
--- a/src/plugman/install.js
+++ b/src/plugman/install.js
@@ -17,6 +17,7 @@
     under the License.
 */
 
+const execa = require('execa');
 var path = require('path');
 var fs = require('fs-extra');
 var ActionStack = require('cordova-common').ActionStack;
@@ -32,7 +33,6 @@ var isWindows = (os.platform().substr(0, 3) === 'win');
 var pluginSpec = require('../cordova/plugin/plugin_spec_parser');
 var cordovaUtil = require('../cordova/util');
 
-var superspawn = require('cordova-common').superspawn;
 var PluginInfo = require('cordova-common').PluginInfo;
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
 var variableMerge = require('./variable-merge');
@@ -170,8 +170,8 @@ function callEngineScripts (engines, project_dir) {
                 if (!isWindows) { // not required on Windows
                     fs.chmodSync(engine.scriptSrc, '755');
                 }
-                return superspawn.spawn(scriptPath)
-                    .then(stdout => {
+                return execa(scriptPath)
+                    .then(({ stdout }) => {
                         engine.currentVersion = cleanVersionOutput(stdout, engine.name);
                         if (engine.currentVersion === '') {
                             events.emit('warn', engine.name + ' version check returned nothing (' + scriptPath + '), continuing anyways.');
@@ -433,15 +433,15 @@ function tryFetchDependency (dep, install, options) {
 
             dep.url = fetchdata.source.path;
 
-            return superspawn.spawn('git rev-parse --show-toplevel', { cwd: dep.url })
+            return execa.command('git rev-parse --show-toplevel', { cwd: dep.url })
                 .catch(err => {
-                    if (err.code === 128) {
+                    if (err.exitCode === 128) {
                         throw new Error('Plugin ' + dep.id + ' is not in git repository. All plugins must be in a git repository.');
                     } else {
                         throw new Error('Failed to locate git repository for ' + dep.id + ' plugin.');
                     }
                 })
-                .then(function (git_repo) {
+                .then(({ stdout: git_repo }) => {
                     // Clear out the subdir since the url now contains it
                     var url = path.join(git_repo, dep.subdir);
                     dep.subdir = '';


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