Skip to content

Commit

Permalink
Fetch templates to a temporary directory
Browse files Browse the repository at this point in the history
This reduces pollution of user's home directory and definitely makes
the previously performed makeshift cache-busting obsolete since we fetch
into a fresh temp directory every time.

Addresses §6 of apache/cordova-discuss#89

Co-authored-by: Christopher J. Brody <[email protected]>
  • Loading branch information
raphinesse and Christopher J. Brody committed Jul 5, 2018
1 parent 3b8e5a2 commit 111b1a4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
22 changes: 10 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

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

var os = require('os');
var path = require('path');

var Q = require('q');
var tmp = require('tmp');
var isUrl = require('is-url');
var isObject = require('isobject');
var pathIsInside = require('path-is-inside');
Expand All @@ -37,9 +37,6 @@ var CordovaLogger = require('cordova-common').CordovaLogger.get();

const DEFAULT_VERSION = '1.0.0';

// Global configuration paths
var global_config_path = process.env.CORDOVA_HOME || path.join(os.homedir(), '.cordova');

/**
* Sets up to forward events to another instance, or log console.
* This will make the create internal events visible outside
Expand Down Expand Up @@ -140,16 +137,9 @@ function cordovaCreate (dest, opts = {}) {
var isNPM = opts.template && (opts.url.indexOf('@') > -1 || !fs.existsSync(path.resolve(opts.url))) && !isGit;
// Always use cordova fetch to obtain the npm or git template
if (isGit || isNPM) {
// Saved to .Cordova folder (ToDo: Delete installed template after using)
var tempDest = global_config_path;
var target = opts.url;
// add latest to npm module if no version is specified
// this prevents create using an older cached version of the template
if (isNPM && target.indexOf('@') === -1) {
target = opts.url + '@latest';
}
events.emit('verbose', 'Using cordova-fetch for ' + target);
return fetch(target, tempDest, {})
return fetch(target, getSelfDestructingTempDir(), {})
.catch(function (err) {
events.emit('error', '\x1B[1m \x1B[31m Error from Cordova Fetch: ' + err.message);
events.emit('error', 'The template you are trying to use is invalid.' +
Expand Down Expand Up @@ -365,3 +355,11 @@ function linkFromTemplate (templateDir, projectDir) {
function stockAssetPath (p) {
return path.join(require('cordova-app-hello-world').dirname, p);
}

// Creates temp dir that is deleted on process exit
function getSelfDestructingTempDir () {
return tmp.dirSync({
prefix: 'cordova-create-',
unsafeCleanup: true
}).name;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"isobject": "^3.0.1",
"path-is-inside": "^1.0.2",
"q": "^1.5.1",
"tmp": "0.0.33",
"valid-identifier": "0.0.1"
},
"devDependencies": {
Expand Down
7 changes: 3 additions & 4 deletions spec/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('create end-to-end', function () {
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should successfully run with NPM package', function () {
it('should successfully run with NPM package (specific version)', function () {
// Create a real project with npm module as template
var config = {
lib: {
Expand All @@ -171,9 +171,8 @@ describe('create end-to-end', function () {
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should successfully run with NPM package and explicitly fetch latest if no version is given', function () {
it('should successfully run with NPM package (no specific version)', function () {
// Create a real project with npm module as template
// TODO fetch should be responsible for the cache busting part of this test
var config = {
lib: {
www: {
Expand All @@ -185,7 +184,7 @@ describe('create end-to-end', function () {
return createWithMockFetch(project, appId, appName, config, events)
.then(fetchSpy => {
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url + '@latest');
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url);
})
.then(checkProjectCreatedWithDefaultTemplate);
});
Expand Down

0 comments on commit 111b1a4

Please sign in to comment.