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

Commit 1ed5ce9

Browse files
kaustersxzyfer
authored andcommitted
Allow setting of SASS_BINARY_DIR without setting explicit binary name
1 parent b926705 commit 1ed5ce9

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

lib/extensions.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var eol = require('os').EOL,
77
pkg = require('../package.json'),
88
mkdir = require('mkdirp'),
99
path = require('path'),
10-
defaultBinaryPath = path.join(__dirname, '..', 'vendor'),
10+
defaultBinaryDir = path.join(__dirname, '..', 'vendor'),
1111
trueCasePathSync = require('true-case-path');
1212

1313
/**
@@ -126,7 +126,7 @@ function getHumanEnvironment(env) {
126126
* @api public
127127
*/
128128
function getInstalledBinaries() {
129-
return fs.readdirSync(defaultBinaryPath);
129+
return fs.readdirSync(getBinaryDir());
130130
}
131131

132132
/**
@@ -245,6 +245,38 @@ function getBinaryUrl() {
245245
return [site, 'v' + pkg.version, getBinaryName()].join('/');
246246
}
247247

248+
/**
249+
* Get binary dir.
250+
* If environment variable SASS_BINARY_DIR,
251+
* .npmrc variable sass_binary_dir or
252+
* process argument --sass-binary-dir is provided,
253+
* select it by appending binary name, otherwise
254+
* use default binary dir.
255+
* Once the primary selection is made, check if
256+
* callers wants to throw if file not exists before
257+
* returning.
258+
*
259+
* @api public
260+
*/
261+
262+
function getBinaryDir() {
263+
var binaryDir;
264+
265+
if (getArgument('--sass-binary-dir')) {
266+
binaryDir = getArgument('--sass-binary-dir');
267+
} else if (process.env.SASS_BINARY_DIR) {
268+
binaryDir = process.env.SASS_BINARY_DIR;
269+
} else if (process.env.npm_config_sass_binary_dir) {
270+
binaryDir = process.env.npm_config_sass_binary_dir;
271+
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryDir) {
272+
binaryDir = pkg.nodeSassConfig.binaryDir;
273+
} else {
274+
binaryDir = defaultBinaryDir;
275+
}
276+
277+
return binaryDir;
278+
}
279+
248280
/**
249281
* Get binary path.
250282
* If environment variable SASS_BINARY_PATH,
@@ -271,7 +303,7 @@ function getBinaryPath() {
271303
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) {
272304
binaryPath = pkg.nodeSassConfig.binaryPath;
273305
} else {
274-
binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_(?=binding\.node)/, '/'));
306+
binaryPath = path.join(getBinaryDir(), getBinaryName().replace(/_(?=binding\.node)/, '/'));
275307
}
276308

277309
if (process.versions.modules < 46) {
@@ -415,6 +447,7 @@ function getPlatformVariant() {
415447
module.exports.hasBinary = hasBinary;
416448
module.exports.getBinaryUrl = getBinaryUrl;
417449
module.exports.getBinaryName = getBinaryName;
450+
module.exports.getBinaryDir = getBinaryDir;
418451
module.exports.getBinaryPath = getBinaryPath;
419452
module.exports.getBinaryCachePath = getBinaryCachePath;
420453
module.exports.getCachedBinary = getCachedBinary;

test/runtime.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,37 @@ describe('runtime parameters', function() {
8383
});
8484
});
8585

86+
describe('SASS_BINARY_DIR', function() {
87+
beforeEach(function() {
88+
process.argv.push('--sass-binary-dir', 'aaa');
89+
process.env.SASS_BINARY_DIR = 'bbb';
90+
process.env.npm_config_sass_binary_dir = 'ccc';
91+
pkg.nodeSassConfig = { binaryDir: 'ddd' };
92+
});
93+
94+
it('command line argument', function() {
95+
assert.equal(sass.getBinaryDir(), 'aaa');
96+
});
97+
98+
it('environment variable', function() {
99+
process.argv = [];
100+
assert.equal(sass.getBinaryDir(), 'bbb');
101+
});
102+
103+
it('npm config variable', function() {
104+
process.argv = [];
105+
process.env.SASS_BINARY_DIR = null;
106+
assert.equal(sass.getBinaryDir(), 'ccc');
107+
});
108+
109+
it('package.json', function() {
110+
process.argv = [];
111+
process.env.SASS_BINARY_DIR = null;
112+
process.env.npm_config_sass_binary_dir = null;
113+
assert.equal(sass.getBinaryDir(), 'ddd');
114+
});
115+
});
116+
86117
describe('SASS_BINARY_PATH', function() {
87118
beforeEach(function() {
88119
process.argv.push('--sass-binary-path', 'aaa_binding.node');

0 commit comments

Comments
 (0)