Commit b6558523 by Nick Schonning

Install: Use a cache folder for binary download

New order of operations
1. Look for existing binary in vendor folder
2. Create target vendor folder
3. Look to see if we’ve cached a copy in the configured or NPM cache
4. Download to current version’s /CACHE/node-sass/version/binding_file
5. Copy the cached download to the regular vendor directory
Closes #1566
parent 7e27148d
...@@ -259,6 +259,17 @@ function getBinaryPath() { ...@@ -259,6 +259,17 @@ function getBinaryPath() {
} }
/** /**
* Looks for the configured cache path. If none is found, fall back to the NPM
* cache folder
*
* @api public
*/
function getCachePath() {
return process.env.npm_config_sass_binary_cache ||
process.env.npm_config_cache;
}
/**
* Does the supplied binary path exist * Does the supplied binary path exist
* *
* @param {String} binaryPath * @param {String} binaryPath
...@@ -286,6 +297,7 @@ module.exports.hasBinary = hasBinary; ...@@ -286,6 +297,7 @@ module.exports.hasBinary = hasBinary;
module.exports.getBinaryUrl = getBinaryUrl; module.exports.getBinaryUrl = getBinaryUrl;
module.exports.getBinaryName = getBinaryName; module.exports.getBinaryName = getBinaryName;
module.exports.getBinaryPath = getBinaryPath; module.exports.getBinaryPath = getBinaryPath;
module.exports.getCachePath = getCachePath;
module.exports.getVersionInfo = getVersionInfo; module.exports.getVersionInfo = getVersionInfo;
module.exports.getHumanEnvironment = getHumanEnvironment; module.exports.getHumanEnvironment = getHumanEnvironment;
module.exports.getInstalledBinaries = getInstalledBinaries; module.exports.getInstalledBinaries = getInstalledBinaries;
......
...@@ -131,37 +131,50 @@ function getProxy() { ...@@ -131,37 +131,50 @@ function getProxy() {
*/ */
function checkAndDownloadBinary() { function checkAndDownloadBinary() {
if (sass.hasBinary(sass.getBinaryPath())) { if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
console.log('Skipping downloading binaries on CI builds');
return; return;
} }
var binaryPath = sass.getBinaryPath();
mkdir(path.dirname(sass.getBinaryPath()), function(err) { if (sass.hasBinary(binaryPath)) {
return;
}
mkdir(path.dirname(binaryPath), function(err) {
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
download(sass.getBinaryUrl(), sass.getBinaryPath(), function(err) { var cachePath = path.join(sass.getCachePath(), pkg.name, pkg.version);
if (err) { var cacheBinary = path.join(cachePath, sass.getBinaryName());
console.error(err); if (fs.existsSync(cacheBinary)) {
return; console.log('Found existing binary in ' + cacheBinary);
} fs.createReadStream(cacheBinary).pipe(fs.createWriteStream(binaryPath));
} else {
console.log('Binary downloaded and installed at', sass.getBinaryPath()); // In case the cache path doesn't exist
}); mkdir(cachePath, function(err) {
if (err) {
console.error(err);
return;
}
download(sass.getBinaryUrl(), cacheBinary, function(err) {
if (err) {
console.error(err);
return;
}
console.log('Binary downloaded to ' + cacheBinary);
fs.createReadStream(cacheBinary).pipe(fs.createWriteStream(binaryPath));
});
});
}
}); });
} }
/** /**
* Skip if CI
*/
if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
console.log('Skipping downloading binaries on CI builds');
return;
}
/**
* If binary does not exist, download it * If binary does not exist, download it
*/ */
......
...@@ -134,6 +134,29 @@ describe('runtime parameters', function() { ...@@ -134,6 +134,29 @@ describe('runtime parameters', function() {
}); });
}); });
describe('Sass Binary Cache', function() {
var npmCacheDir;
before(function() {
npmCacheDir = process.env.npm_config_cache;
});
beforeEach(function() {
delete process.env.npm_config_sass_binary_cache;
});
it('npm config variable', function() {
var overridenCachePath = '/foo/bar/';
process.env.npm_config_sass_binary_cache = overridenCachePath;
var sass = require(extensionsPath);
assert.equal(sass.getCachePath(), overridenCachePath);
});
it('With no value, falls back to NPM cache', function() {
var sass = require(extensionsPath);
assert.equal(sass.getCachePath(), npmCacheDir);
});
});
}); });
// describe('library detection', function() { // describe('library detection', function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment