Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Allow setting of SASS_BINARY_DIR without setting explicit binary name #2119

Merged
merged 2 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ Variable name | .npmrc parameter | Process argument | Value
SASS_BINARY_NAME | sass_binary_name | --sass-binary-name | path
SASS_BINARY_SITE | sass_binary_site | --sass-binary-site | URL
SASS_BINARY_PATH | sass_binary_path | --sass-binary-path | path
SASS_BINARY_DIR | sass_binary_dir | --sass-binary-dir | path

These parameters can be used as environment variable:

Expand Down
39 changes: 36 additions & 3 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ function getHumanEnvironment(env) {
* @api public
*/
function getInstalledBinaries() {
return fs.readdirSync(defaultBinaryPath);
return fs.readdirSync(getBinaryDir());
}

/**
Expand Down Expand Up @@ -245,6 +245,38 @@ function getBinaryUrl() {
return [site, 'v' + pkg.version, getBinaryName()].join('/');
}

/**
* 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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions test/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down