Skip to content

Commit

Permalink
fixes mjeanroy#10 by using the bower cache instead of tmp directories
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriend committed Aug 16, 2016
1 parent 92aacb3 commit cb32f6e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"homepage": "https://github.com/mjeanroy/bower-npm-resolver",
"dependencies": {
"escape-string-regexp": "^1.0.5",
"mkdirp": "^0.5.1",
"npm": ">=1.0.0",
"q": "1.4.1",
"request": "2.72.0",
"tar-fs": "1.12.0",
"tmp": "0.0.28",
"underscore": "1.8.3"
},
"devDependencies": {
Expand All @@ -47,6 +47,7 @@
"gulp-jasmine": "2.3.0",
"gulp-util": "3.0.7",
"jasmine-core": "2.4.1",
"run-sequence": "1.1.5"
"run-sequence": "1.1.5",
"tmp": "0.0.28"
}
}
5 changes: 0 additions & 5 deletions src/npm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/

var Q = require('q');
var fs = require('fs');
var npm = require('npm');
var path = require('path');

Expand Down Expand Up @@ -131,14 +130,10 @@ module.exports = {
* @return {Promise} The promise object.
*/
downloadTarball: function(pkg, version, dir) {
var oldCWD = process.cwd();
process.chdir(dir);
return execPackCommand([pkg + '@' + version])
.then(function(filename) {
return path.resolve(dir || process.cwd(), filename[0]);
})
.finally(function() {
process.chdir(oldCWD);
});
}
};
36 changes: 12 additions & 24 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
* SOFTWARE.
*/

/**
* Factory function for the resolver.
* Will be called only one time by Bower, to instantiate resolver.
*/
/**
* Factory function for the resolver.
* Will be called only one time by Bower, to instantiate resolver.
*/

var tmp = require('tmp');
var mkdirp = require('mkdirp');
var path = require('path');
var npmUtils = require('./npm-utils');
var extract = require('./extract');
Expand Down Expand Up @@ -129,22 +129,20 @@ module.exports = function resolver(bower) {
var pkg = extractPackageName(endpoint.source);

// Directory where the tgz will be stored.
var tmpTar = tmp.dirSync({
unsafeCleanup: true
});
var compressedDir = path.join(bower.config.storage.packages, 'npm-resolver/compressed');
mkdirp.sync(compressedDir);

// Directory where the tgz file will be extracted.
var tmpPackage = tmp.dirSync({
unsafeCleanup: true
});
var uncompressedDir = path.join(bower.config.storage.packages, 'npm-resolver/uncompressed', pkg, endpoint.target);
mkdirp.sync(uncompressedDir);

logger.debug('npm-resolver', 'downloading tarball for: ' + pkg + '#' + endpoint.target);
return npmUtils.downloadTarball(pkg, endpoint.target, tmpTar.name)
return npmUtils.downloadTarball(pkg, endpoint.target, compressedDir)

// Download ok, extract tarball.
.then(function(tarballPath) {
logger.debug('npm-resolver', 'extracting tarball from: ' + tarballPath);
return extract.tgz(tarballPath, tmpPackage.name);
logger.debug('npm-resolver', 'extracting tarball from: "' + tarballPath + '" to "' + uncompressedDir);
return extract.tgz(tarballPath, uncompressedDir);
})

// Patch configuration with `package.json` file if `bower.json` does not exist.
Expand All @@ -157,16 +155,6 @@ module.exports = function resolver(bower) {
removeIgnores: true
};
});
})

// When an error occured, remove temporary directory.
.catch(function() {
tmpPackage.removeCallback();
})

// Always remove the temporary directory for the tgz file.
.finally(function() {
tmpTar.removeCallback();
});
}
};
Expand Down
16 changes: 15 additions & 1 deletion test/resolver-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,32 @@ var factory = require('../src/resolver');
var npmUtils = require('../src/npm-utils');
var extract = require('../src/extract');
var bowerUtils = require('../src/bower-utils');
var tmp = require('tmp');

describe('resolver', function() {
var resolver;
var tmpDir;

beforeEach(function() {
tmpDir = tmp.dirSync({
unsafeCleanup: true
});

resolver = factory({
config: {},
config: {
storage: {
packages: tmpDir.name
}
},
version: '1.7.7',
logger: jasmine.createSpyObj('logger', ['debug'])
});
});

afterEach(function() {
tmpDir.removeCallback();
});

it('should get list of releases', function(done) {
var source = 'npm:bower';
var defer = Q.defer();
Expand Down

0 comments on commit cb32f6e

Please sign in to comment.