Skip to content
Permalink
Browse files
Fix isRemoteUri (#52)
* Add unit tests for isRemoteUri

Including one failing test for paths that include @

* Fix isRemoteUri using npm-package-arg

Fixes #34

* Rename isRemoteUri to needsToBeFetched after switch to npa

The reason for the rename is, that there are also local specs where we
need to call fetch. For example tarballs.
  • Loading branch information
raphinesse committed Nov 6, 2019
1 parent ef7f8cb commit d89424341df936462977c3ced24bc05e63155510
Showing 4 changed files with 26 additions and 10 deletions.
@@ -22,7 +22,7 @@ const fs = require('fs-extra');
var path = require('path');

var tmp = require('tmp');
var isUrl = require('is-url');
const npa = require('npm-package-arg');
var isObject = require('isobject');
var pathIsInside = require('path-is-inside');
var requireFresh = require('import-fresh');
@@ -88,7 +88,7 @@ function cordovaCreate (dest, opts = {}) {
emit('log', 'Creating a new cordova project.');

// Use cordova-fetch to obtain npm or git templates
if (isRemoteUri(opts.template)) {
if (needsToBeFetched(opts.template)) {
var target = opts.template;
emit('verbose', 'Using cordova-fetch for ' + target);
return fetch(target, getSelfDestructingTempDir(), {});
@@ -167,6 +167,6 @@ function getSelfDestructingTempDir () {
}).name;
}

function isRemoteUri (uri) {
return isUrl(uri) || uri.includes('@') || !fs.existsSync(uri);
function needsToBeFetched (uri) {
return npa(uri).type !== 'directory';
}

Some generated files are not rendered by default. Learn more.

@@ -29,8 +29,8 @@
"cordova-fetch": "^2.0.0",
"fs-extra": "^8.1.0",
"import-fresh": "^3.1.0",
"is-url": "^1.2.4",
"isobject": "^4.0.0",
"npm-package-arg": "^6.1.1",
"path-is-inside": "^1.0.2",
"tmp": "^0.1.0",
"valid-identifier": "0.0.2"
@@ -18,6 +18,7 @@
*/

const fs = require('fs-extra');
const rewire = require('rewire');

var path = require('path');

@@ -227,3 +228,23 @@ describe('when shit happens', function () {
);
});
});

describe('cordova create needsToBeFetched', () => {
let needsToBeFetched;

beforeEach(() => {
needsToBeFetched = rewire('..').__get__('needsToBeFetched');
});

it('should recognize URLs as remote', () => {
expect(needsToBeFetched('https://example.com/pkg/foo')).toBe(true);
});

it('should recognize package@version as remote', () => {
expect(needsToBeFetched('foo@1')).toBe(true);
});

it('should not detect paths as remote only because they include an @', () => {
expect(needsToBeFetched('../foo@1')).toBe(false);
});
});

0 comments on commit d894243

Please sign in to comment.