cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From TimBarham <...@git.apache.org>
Subject [GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...
Date Fri, 27 Mar 2015 07:49:51 GMT
GitHub user TimBarham opened a pull request:

    https://github.com/apache/cordova-lib/pull/194

    CB-8754 Auto-restoring a plugin fails when adding a platform.

    Start with a blank Cordova app, then enter the following:
    
        cordova plugin add org.apache.cordova.camera --save
        cordova plugin remove org.apache.cordova.camera
        cordova platform add browser
    
    The final step should restore the camera plugin, but it fails with the following exception:
    
        TypeError: Cannot read property 'latest' of undefined
            at next (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:694:35)
            at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:682:5
            at RegClient.get_ (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:105:14)
            at RegClient.<anonymous> (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:41:12)
            at fs.js:336:14
            at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:103:5
            at FSReqWrap.oncomplete (fs.js:99:15)
    
    The core problem here is an issue with how we work with npm. Every time we are going to
use npm (when dealing with plugins), we call `npm.load()` passing it our settings. But `npm.load()`
can only be called once per session - subsequent calls are ignored. The correct approach is
to call `npm.load()` without any settings (to make sure npm is loaded), then call `npm.config.set()`
for each setting. This change had been made for platforms, but not plugins.
    
    I also wanted to make sure each time we worked with npm we had a clean config (for example,
if we get an npm package from the Cordova Plugin Respository, then later from the npm repository,
it would try to get the second package from the CPR because that setting would still be around
and not get overridden). So now any code that wants to load and init npm I pass through a
central "load and restore" method. You pass this method your npm settings and the chain of
promises you want to execute with those settings applied. It loads and initializes npm, executes
the promises, then restores npm's configuration to what it was before we started.
    
    Finally, once that problem was fixed there was an additional problem - we were adding
plugins too early and the platform wasn't fully initialized, so we'd get an error about not
being able to find the platform's `config.xml` file.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/MSOpenTech/cordova-lib CB-8754

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/cordova-lib/pull/194.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #194
    
----
commit 0df6b800c6013bcf53045040ade81345fd9b47ec
Author: Tim Barham <tim.barham@microsoft.com>
Date:   2015-03-26T11:54:37Z

    CB-8754 Auto-restoring a plugin fails when adding a platform.
    
    Start with a blank Cordova app, then enter the following:
    
    cordova plugin add org.apache.cordova.camera --save
    cordova plugin remove org.apache.cordova.camera
    cordova platform add browser
    
    The final step should restore the camera plugin, but it fails with the following exception:
    
    TypeError: Cannot read property 'latest' of undefined
        at next (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:694:35)
        at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:682:5
        at RegClient.get_ (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:105:14)
        at RegClient.<anonymous> (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:41:12)
        at fs.js:336:14
        at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:103:5
        at FSReqWrap.oncomplete (fs.js:99:15)
    
    The core problem here is an issue with how we work with npm. Every time we are going to
use npm (when dealing with plugins), we call npm.load() passing it our settings. But npm.load()
can only be called once per session - subsequent calls are ignored. The correct approach is
to call npm.load() without any settings (to make sure npm is loaded), then call npm.config.set()
for each setting.
    
    This change had been made for platforms, but not plugins. I also wanted to make sure each
time we worked with npm we had a clean config (for example, if we get an npm package from
the Cordova plugin respository, then later from the npm repository, it would try to get the
second package from the CPR because that setting would still be around and not get overridden).
So now any code that wants to load and init npm I pass through a central "load and restore"
method. You pass this method your npm settings and the chain of promises you want to execute
with those settings applied. It loads and initializes npm, executes the promises, then restores
npm's configuration to what it was before we started.
    
    Finally, once that problem was fixed there was an additional problem - we were adding
plugins too early and the platform wasn't fully initialized, so we'd get an error about not
being able to find the platform's config.xml file.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message