-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add cache for downloaded binary files #1566
Changes from 3 commits
7d77916
36ac14e
1000858
e244410
1ce1bc1
a07af5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*.log | ||
.idea | ||
.DS_Store | ||
.sass-cache | ||
build | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ function download(url, dest, cb) { | |
return response.statusCode >= 200 && response.statusCode < 300; | ||
}; | ||
|
||
var options = { | ||
var options = { | ||
rejectUnauthorized: false, | ||
proxy: getProxy(), | ||
headers: { | ||
|
@@ -109,17 +109,67 @@ function checkAndDownloadBinary() { | |
return; | ||
} | ||
|
||
download(sass.getBinaryUrl(), sass.getBinaryPath(), function(err) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
var tmpPath = getTempPath(sass.getBinaryName()); | ||
if (!(sass.hasBinary(tmpPath))) { // download and install | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xzyfer this is the part I'm saying will always fall back to the first installed binary file in the temp folder There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said I've haven't look over the implementation yet. It's entirely likely there are issues that will need to be addressed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of the binary file in the tmp folder based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're both correct. The binaries won't clash on different node/arch/os You'll need to create a folder for the node-sass version for this to work |
||
download(sass.getBinaryUrl(), tmpPath, function(err) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
console.log('Binary downloaded at', tmpPath); | ||
copyBinary(tmpPath, sass.getBinaryPath()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copyBinary(tmpPath, sass.getBinaryPath()); |
||
}); | ||
|
||
console.log('Binary downloaded and installed at', sass.getBinaryPath()); | ||
}); | ||
} else { // install only | ||
copyBinary(tmpPath, sass.getBinaryPath()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copyBinary(tmpPath, sass.getBinaryPath()); |
||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Find a temp folder for file | ||
* | ||
* @param {String} binaryName | ||
* @returns {string} | ||
* @api private | ||
*/ | ||
|
||
function getTempPath(binaryName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function doesn't need to care about the binary name. |
||
var candidateTmpDirs = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var tmpdir = require('os-tmpdir');
// ...
var candidateTmpDirs = [
tmpdir(),
process.env.npm_config_tmp,
sass.getBinaryPath(),
]; |
||
process.env.TMPDIR || process.env.TEMP || process.env.npm_config_tmp, | ||
'/tmp', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. process.env.TMPDIR is the same as os-tmpdir. Line 140 is a fallback, which should never be reached. These lines are copied from the install script of phantomjs. They use it long ago without any problems. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's ok, I'd still prefer you use a tested package that abstracts away this concern. |
||
path.join(process.cwd(), 'tmp') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last resort fallback should the current behaviour of installing in the local |
||
]; | ||
|
||
for (var i = 0; i < candidateTmpDirs.length; i++) { | ||
var candidatePath = path.join(candidateTmpDirs[i], 'node-sass'); | ||
|
||
try { | ||
mkdir.sync(candidatePath); | ||
return path.join(candidatePath, binaryName); | ||
} catch (err) { | ||
console.error(candidatePath, 'is not writable:', err.message); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Copy file | ||
* | ||
* @param tmp | ||
* @param dest | ||
* @api private | ||
*/ | ||
function copyBinary(tmp, dest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is named specifically for copying the binaries so you can make the binary name it's responsibility. var from = path.join(tmp, sass.getBinaryName()); |
||
try { | ||
fs.createReadStream(tmp).pipe(fs.createWriteStream(dest)); | ||
|
||
console.log('Binary installed at', sass.getBinaryPath()); | ||
} catch (err) { | ||
console.err('Cannot install binary', err.message); | ||
} | ||
} | ||
|
||
/** | ||
* Skip if CI | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to handle a failure to create the temp directory.