From fd188f996f4f4000a69f064615d0cc5d79ca6709 Mon Sep 17 00:00:00 2001 From: Kristaps Austers Date: Sat, 21 Oct 2017 04:55:21 +0300 Subject: [PATCH 1/2] Allow setting of SASS_BINARY_DIR without setting explicit binary name --- lib/extensions.js | 39 ++++++++++++++++++++++++++++++++++++--- test/runtime.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 48e2c8ec5..4c410f5d5 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -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()); } /** @@ -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, @@ -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; diff --git a/test/runtime.js b/test/runtime.js index ab4b0c206..c4ae9d557 100644 --- a/test/runtime.js +++ b/test/runtime.js @@ -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'); From d9f9f51efb85844ee0543e657dcd52af57b081e6 Mon Sep 17 00:00:00 2001 From: Kristaps Austers Date: Sun, 22 Oct 2017 03:57:18 +0300 Subject: [PATCH 2/2] Add SASS_BINARY_DIR to Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 67b14120e..e0d09f59d 100644 --- a/README.md +++ b/README.md @@ -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: