You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2017/08/16 18:40:31 UTC
cordova-fetch git commit: CB-13010: Improve logic for searching
packages which being installed from git url
Repository: cordova-fetch
Updated Branches:
refs/heads/master f0b030478 -> 07a3b30c6
CB-13010: Improve logic for searching packages which being installed from git url
close #1
Project: http://git-wip-us.apache.org/repos/asf/cordova-fetch/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-fetch/commit/07a3b30c
Tree: http://git-wip-us.apache.org/repos/asf/cordova-fetch/tree/07a3b30c
Diff: http://git-wip-us.apache.org/repos/asf/cordova-fetch/diff/07a3b30c
Branch: refs/heads/master
Commit: 07a3b30c653ed4a34fa4bb62a678d318d2574554
Parents: f0b0304
Author: Nikita Matrosov <v-...@microsoft.com>
Authored: Mon Aug 14 17:32:02 2017 +0300
Committer: Steve Gill <st...@gmail.com>
Committed: Wed Aug 16 11:40:33 2017 -0700
----------------------------------------------------------------------
index.js | 48 ++++++++++++++++++++++++++++++-------
spec/fetch.spec.js | 63 ++++++++++++++++++++++++++++++++-----------------
2 files changed, 82 insertions(+), 29 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-fetch/blob/07a3b30c/index.js
----------------------------------------------------------------------
diff --git a/index.js b/index.js
index b1d37ec..25b60fe 100644
--- a/index.js
+++ b/index.js
@@ -22,6 +22,7 @@ var shell = require('shelljs');
var superspawn = require('cordova-common').superspawn;
var events = require('cordova-common').events;
var depls = require('dependency-ls');
+var url = require('url');
var path = require('path');
var fs = require('fs');
var CordovaError = require('cordova-common').CordovaError;
@@ -89,7 +90,7 @@ module.exports = function(target, dest, opts) {
//This could happen on a platform update.
var id = getJsonDiff(tree1, tree2) || trimID(target);
- return getPath(id, dest);
+ return getPath(id, dest, target);
})
.fail(function(err){
return Q.reject(new CordovaError(err));
@@ -184,13 +185,44 @@ function trimID(target) {
*
*/
-function getPath(id, dest) {
- var finalDest = path.resolve(path.join(dest, id));
-
- //Sanity check it exists
- if(fs.existsSync(finalDest)){
- return finalDest;
- } else return Q.reject(new CordovaError('Failed to get absolute path to installed module'));
+function getPath(id, dest, target) {
+ var destination = path.resolve(path.join(dest, id));
+ var finalDest = fs.existsSync(destination) ? destination : searchDirForTarget(dest, target);
+
+ if (!finalDest) {
+ throw new CordovaError('Failed to get absolute path to installed module');
+ }
+
+ return finalDest;
+}
+
+/*
+ * Make an additional search in destination folder using repository.url property from package.json
+ *
+ * @param {String} dest destination of where to fetch the modules
+ * @param {String} target target that was passed into cordova-fetch. can be moduleID, moduleID@version or gitURL
+ *
+ * @return {String} Returns the absolute url for the module or null
+ *
+ */
+
+function searchDirForTarget(dest, target) {
+ if (!isUrl(target)) {
+ return;
+ }
+
+ var targetPathname = url.parse(target).pathname;
+
+ var pkgJsonPath = fs.readdirSync(dest).map(function(dir) {
+ return path.join(dest, dir, 'package.json');
+ })
+ .filter(fs.existsSync)
+ .find(function(pkgJsonPath) {
+ var repo = JSON.parse(fs.readFileSync(pkgJsonPath)).repository;
+ return repo && url.parse(repo.url).pathname === targetPathname;
+ });
+
+ return pkgJsonPath && path.dirname(pkgJsonPath);
}
/*
http://git-wip-us.apache.org/repos/asf/cordova-fetch/blob/07a3b30c/spec/fetch.spec.js
----------------------------------------------------------------------
diff --git a/spec/fetch.spec.js b/spec/fetch.spec.js
index 4059bbf..8cdc6e2 100644
--- a/spec/fetch.spec.js
+++ b/spec/fetch.spec.js
@@ -32,34 +32,34 @@ describe('platform fetch/uninstall tests via npm & git', function () {
beforeEach(function() {
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
it('should fetch and uninstall a cordova platform via npm & git', function(done) {
-
+
fetch('cordova-android', tmpDir, opts)
.then(function(result) {
var pkgJSON = require(path.join(result,'package.json'));
expect(result).toBeDefined();
expect(fs.existsSync(result)).toBe(true);
expect(pkgJSON.name).toBe('cordova-android');
-
+
return uninstall('cordova-android', tmpDir, opts);
})
.then(function() {
expect(fs.existsSync(path.join(tmpDir,'node_modules', 'cordova-android'))).toBe(false);
-
- return fetch('https://github.com/apache/cordova-ios.git', tmpDir, opts);
+
+ return fetch('https://github.com/apache/cordova-ios.git', tmpDir, opts);
})
.then(function(result) {
var pkgJSON = require(path.join(result,'package.json'));
expect(result).toBeDefined();
expect(fs.existsSync(result)).toBe(true);
expect(pkgJSON.name).toBe('cordova-ios');
-
+
return uninstall('cordova-ios', tmpDir, opts);
})
.then(function() {
@@ -87,13 +87,13 @@ describe('platform fetch/uninstall test via npm & git tags with --save', functio
var tmpDir = helpers.tmpDir('plat_fetch_save');
var opts = {'save':true};
-
+
beforeEach(function() {
//copy package.json from spec directory to tmpDir
shell.cp('spec/testpkg.json', path.join(tmpDir,'package.json'));
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
@@ -118,7 +118,7 @@ describe('platform fetch/uninstall test via npm & git tags with --save', functio
expect(Object.keys(rootPJ.dependencies).length).toBe(0);
expect(fs.existsSync(path.join(tmpDir,'node_modules', 'cordova-android'))).toBe(false);
- return fetch('https://github.com/apache/cordova-ios.git#rel/4.1.1', tmpDir, opts);
+ return fetch('https://github.com/apache/cordova-ios.git#rel/4.1.1', tmpDir, opts);
})
.then(function(result) {
var pkgJSON = require(path.join(result,'package.json'));
@@ -163,13 +163,13 @@ describe('plugin fetch/uninstall test with --save', function () {
var tmpDir = helpers.tmpDir('plug_fetch_save');
var opts = {'save':true};
-
+
beforeEach(function() {
//copy package.json from spec directory to tmpDir
shell.cp('spec/testpkg.json', path.join(tmpDir,'package.json'));
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
@@ -204,13 +204,14 @@ describe('plugin fetch/uninstall test with --save', function () {
describe('test trimID method for npm and git', function () {
- var tmpDir = helpers.tmpDir('plug_trimID');
+ var tmpDir;
var opts = {};
-
+
beforeEach(function() {
+ tmpDir = helpers.tmpDir('plug_trimID');
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
@@ -223,7 +224,7 @@ describe('test trimID method for npm and git', function () {
expect(result).toBeDefined();
expect(fs.existsSync(result)).toBe(true);
expect(pkgJSON.name).toBe('cordova-plugin-device');
-
+
return fetch('https://github.com/apache/cordova-plugin-media.git', tmpDir, opts);
})
.then(function(result) {
@@ -234,7 +235,7 @@ describe('test trimID method for npm and git', function () {
//refetch to trigger trimID
return fetch('cordova-plugin-device', tmpDir, opts);
-
+
})
.then(function(result) {
expect(result).toBeDefined();
@@ -263,17 +264,37 @@ describe('test trimID method for npm and git', function () {
})
.fin(done);
}, 40000);
+
+ it('should fetch same plugin twice in a row if git repo name differ from plugin id', function(done) {
+ fetch('https://github.com/AzureAD/azure-activedirectory-library-for-cordova.git', tmpDir, opts)
+ .then(function(result) {
+ expect(result).toBeDefined();
+ expect(fs.existsSync(result)).toBe(true);
+ expect(result).toMatch('cordova-plugin-ms-adal');
+ return fetch('https://github.com/AzureAD/azure-activedirectory-library-for-cordova.git', tmpDir, opts);
+ })
+ .then(function(result) {
+ expect(result).toBeDefined();
+ expect(fs.existsSync(result)).toBe(true);
+ expect(result).toMatch('cordova-plugin-ms-adal');
+ })
+ .fail(function(err) {
+ console.error(err);
+ expect(err).toBeUndefined();
+ })
+ .fin(done);
+ }, 30000);
});
describe('fetch failure with unknown module', function () {
var tmpDir = helpers.tmpDir('fetch_fails_npm');
var opts = {};
-
+
beforeEach(function() {
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
@@ -300,7 +321,7 @@ describe('fetch failure with git subdirectory', function () {
beforeEach(function() {
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
@@ -327,13 +348,13 @@ describe('scoped plugin fetch/uninstall tests via npm', function () {
beforeEach(function() {
process.chdir(tmpDir);
});
-
+
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
- it('should fetch a scoped plugin from npm', function(done) {
+ it('should fetch a scoped plugin from npm', function(done) {
fetch('@stevegill/cordova-plugin-device', tmpDir, opts)
.then(function(result) {
var pkgJSON = require(path.join(result,'package.json'));
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org