Commit 1ed5ce94 by Kristaps Austers Committed by Michael Mifsud

Allow setting of SASS_BINARY_DIR without setting explicit binary name

parent b926705b
......@@ -7,7 +7,7 @@ var eol = require('os').EOL,
pkg = require('../package.json'),
mkdir = require('mkdirp'),
path = require('path'),
defaultBinaryPath = path.join(__dirname, '..', 'vendor'),
defaultBinaryDir = path.join(__dirname, '..', 'vendor'),
trueCasePathSync = require('true-case-path');
/**
......@@ -126,7 +126,7 @@ function getHumanEnvironment(env) {
* @api public
*/
function getInstalledBinaries() {
return fs.readdirSync(defaultBinaryPath);
return fs.readdirSync(getBinaryDir());
}
/**
......@@ -246,6 +246,38 @@ function getBinaryUrl() {
}
/**
* Get binary dir.
* If environment variable SASS_BINARY_DIR,
* .npmrc variable sass_binary_dir or
* process argument --sass-binary-dir is provided,
* select it by appending binary name, otherwise
* use default binary dir.
* Once the primary selection is made, check if
* callers wants to throw if file not exists before
* returning.
*
* @api public
*/
function getBinaryDir() {
var binaryDir;
if (getArgument('--sass-binary-dir')) {
binaryDir = getArgument('--sass-binary-dir');
} else if (process.env.SASS_BINARY_DIR) {
binaryDir = process.env.SASS_BINARY_DIR;
} else if (process.env.npm_config_sass_binary_dir) {
binaryDir = process.env.npm_config_sass_binary_dir;
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryDir) {
binaryDir = pkg.nodeSassConfig.binaryDir;
} else {
binaryDir = defaultBinaryDir;
}
return binaryDir;
}
/**
* Get binary path.
* If environment variable SASS_BINARY_PATH,
* .npmrc variable sass_binary_path or
......@@ -271,7 +303,7 @@ function getBinaryPath() {
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) {
binaryPath = pkg.nodeSassConfig.binaryPath;
} else {
binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_(?=binding\.node)/, '/'));
binaryPath = path.join(getBinaryDir(), getBinaryName().replace(/_(?=binding\.node)/, '/'));
}
if (process.versions.modules < 46) {
......@@ -415,6 +447,7 @@ function getPlatformVariant() {
module.exports.hasBinary = hasBinary;
module.exports.getBinaryUrl = getBinaryUrl;
module.exports.getBinaryName = getBinaryName;
module.exports.getBinaryDir = getBinaryDir;
module.exports.getBinaryPath = getBinaryPath;
module.exports.getBinaryCachePath = getBinaryCachePath;
module.exports.getCachedBinary = getCachedBinary;
......
......@@ -83,6 +83,37 @@ describe('runtime parameters', function() {
});
});
describe('SASS_BINARY_DIR', function() {
beforeEach(function() {
process.argv.push('--sass-binary-dir', 'aaa');
process.env.SASS_BINARY_DIR = 'bbb';
process.env.npm_config_sass_binary_dir = 'ccc';
pkg.nodeSassConfig = { binaryDir: 'ddd' };
});
it('command line argument', function() {
assert.equal(sass.getBinaryDir(), 'aaa');
});
it('environment variable', function() {
process.argv = [];
assert.equal(sass.getBinaryDir(), 'bbb');
});
it('npm config variable', function() {
process.argv = [];
process.env.SASS_BINARY_DIR = null;
assert.equal(sass.getBinaryDir(), 'ccc');
});
it('package.json', function() {
process.argv = [];
process.env.SASS_BINARY_DIR = null;
process.env.npm_config_sass_binary_dir = null;
assert.equal(sass.getBinaryDir(), 'ddd');
});
});
describe('SASS_BINARY_PATH', function() {
beforeEach(function() {
process.argv.push('--sass-binary-path', 'aaa_binding.node');
......
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