Skip to content

Commit

Permalink
Use NPM cache folder for binary download
Browse files Browse the repository at this point in the history
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 NPM cache for this version
4. Download to current version’s cached NPM download
5. Copy the NPM cached version to the regular vendor directory
Lookup the configured NPM cache folder.
Closes sass#1566
  • Loading branch information
nschonni committed Sep 9, 2016
1 parent ccfbbcb commit f082aa4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
20 changes: 20 additions & 0 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var eol = require('os').EOL,
fs = require('fs'),
pkg = require('../package.json'),
path = require('path'),
spawn = require('cross-spawn'),
defaultBinaryPath = path.join(__dirname, '..', 'vendor');

/**
Expand Down Expand Up @@ -258,6 +259,24 @@ function getBinaryPath() {
return binaryPath;
}

/**
* Looks for the configured NPM cache by calling `npm config get cache`
* and joins the result with the `package_name/version`
*
* @api public
*/
function getNpmCachePath() {
var cache = spawn.sync('npm', ['config', 'get', 'cache']);
if (cache.status !== 0) {
console.warn('Couldn\'t find the NPM cache directory');
} else {
return path.join(
cache.stdout.toString('utf8').replace(eol, '')
, pkg.name
, pkg.version);
}
}

/**
* Does the supplied binary path exist
*
Expand Down Expand Up @@ -286,6 +305,7 @@ module.exports.hasBinary = hasBinary;
module.exports.getBinaryUrl = getBinaryUrl;
module.exports.getBinaryName = getBinaryName;
module.exports.getBinaryPath = getBinaryPath;
module.exports.getNpmCachePath = getNpmCachePath;
module.exports.getVersionInfo = getVersionInfo;
module.exports.getHumanEnvironment = getHumanEnvironment;
module.exports.getInstalledBinaries = getInstalledBinaries;
Expand Down
52 changes: 34 additions & 18 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ var fs = require('fs'),
*/

function download(url, dest, cb) {
if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
console.log('Skipping downloading binaries on CI builds');
return;
}

var reportError = function(err) {
var timeoutMessge;

Expand Down Expand Up @@ -131,36 +136,47 @@ function getProxy() {
*/

function checkAndDownloadBinary() {
if (sass.hasBinary(sass.getBinaryPath())) {
var binaryPath = sass.getBinaryPath();

if (sass.hasBinary(binaryPath)) {
return;
}

mkdir(path.dirname(sass.getBinaryPath()), function(err) {
mkdir(path.dirname(binaryPath), function(err) {
if (err) {
console.error(err);
return;
}

download(sass.getBinaryUrl(), sass.getBinaryPath(), function(err) {
if (err) {
console.error(err);
return;
}
var npmCachDir = sass.getNpmCachePath();
var npmBinary = path.join(npmCachDir, sass.getBinaryName().replace('/binding.node', '_binding.node'));
if (!fs.existsSync(npmBinary)) {
// In case we're building for the first time
mkdir(npmCachDir, function(err) {
if (err) {
console.error(err);
return;
}
download(sass.getBinaryUrl(), npmBinary, function(err) {
if (err) {
console.error(err);
return;
}

fs.createReadStream(npmBinary).pipe(fs.createWriteStream(binaryPath));

console.log('Binary downloaded to', npmCachDir);
});
});
} else {
console.log('Found existing binary in', npmCachDir);
fs.createReadStream(npmBinary).pipe(fs.createWriteStream(binaryPath));
}

console.log('Binary downloaded and installed at', sass.getBinaryPath());
});
console.log('Binary installed at', 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
*/
Expand Down

0 comments on commit f082aa4

Please sign in to comment.