Skip to content

Commit 1380de4

Browse files
committed
* Delivers binary name, paths and download URL
from lib/extensions.js. * Allows user to set binary name as environment variable with `SASS_BINARY_NAME`. * This name will be used to construct the: * Binary path. * Binary download URL. * Upload URL. * Note: this will supersede default name. * Allows user to set binary name as parameter to invoke any node-sass script with `--binary-name` flag. * This name will be used to construct the: * Binary path. * Binary download URL. * Upload URL. * Note: this will supersede both default name as well as the `SASS_BINARY_NAME` environment variable. * Allows user to set binary path as environment variable with `SASS_BINARY_PATH`. * This name will be used when: * Requiring node-sass package. * Downloading binary. * Uploading binary. * Note: this will supersede default path. * Allows user to set binary path as parameter to invoke any node-sass script with `--binary-path` flag. * This name will be used when: * Requiring node-sass package. * Downloading binary. * Uploading binary. * Note: this will supersede both default path as well as the `SASS_BINARY_PATH` environment variable. * Wraps all extensions in `process.sass` namespace. Issue URL: sass#712. PR URL: sass#743. //cc @nschonni, @xzyfer
1 parent beffe92 commit 1380de4

File tree

11 files changed

+166
-127
lines changed

11 files changed

+166
-127
lines changed

bin/node-sass

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
var Emitter = require('events').EventEmitter,
34
Gaze = require('gaze'),
45
grapher = require('sass-graph'),
@@ -13,7 +14,7 @@ var Emitter = require('events').EventEmitter,
1314

1415
var cli = meow({
1516
pkg: '../package.json',
16-
version: process.sassInfo,
17+
version: process.sass.versionInfo,
1718
help: [
1819
'Usage',
1920
' node-sass [options] <input.scss> [output.css]',

lib/extensions.js

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/*!
2+
* node-sass: lib/extensions.js
3+
*/
4+
15
var eol = require('os').EOL,
6+
flags = require('meow')({ pkg: '../package.json' }).flags,
27
fs = require('fs'),
3-
package = require('../package.json');
8+
package = require('../package.json'),
9+
path = require('path');
410

511
/**
612
* Get Runtime Info
@@ -24,26 +30,98 @@ function getRuntimeInfo() {
2430
}
2531

2632
/**
27-
* Get unique name of binary for current platform
33+
* Get binary name.
34+
* If environment variable SASS_BINARY_NAME or
35+
* process aurgument --binary-name is provide,
36+
* return it as is, otherwise make default binary
37+
* name: {platform}-{arch}-{v8 version}.node
2838
*
2939
* @api private
3040
*/
3141

32-
function getBinaryIdentifiableName() {
42+
function getBinaryName() {
43+
if (flags.binaryName) {
44+
return flags.binaryName;
45+
}
46+
47+
if (process.env.SASS_BINARY_NAME) {
48+
return process.env.SASS_BINARY_NAME;
49+
}
50+
3351
var v8SemVersion = process.versions.v8.split('.').slice(0, 3).join('.');
3452

3553
return [process.platform, '-',
3654
process.arch, '-',
37-
v8SemVersion].join('');
55+
v8SemVersion, '.node'].join('');
56+
}
57+
58+
/**
59+
* Retrieve the URL to fetch binary.
60+
* If environment variable SASS_BINARY_URL
61+
* is set, return that path. Otherwise make
62+
* path using current release version and
63+
* binary name.
64+
*
65+
* @api private
66+
*/
67+
68+
function getBinaryUrl() {
69+
return flags.binaryUrl ||
70+
process.env.SASS_BINARY_URL ||
71+
['https://github.com/sass/node-sass/releases/download//v',
72+
package.version, '/', sass.binaryName].join('');
3873
}
3974

40-
function getSassInfo() {
75+
/**
76+
* Get Sass version information
77+
*
78+
* @api private
79+
*/
80+
81+
function getVersionInfo() {
4182
return [
4283
['node-sass', package.version, '(Wrapper)', '[JavaScript]'].join('\t'),
4384
['libsass ', package.libsass, '(Sass Compiler)', '[C/C++]'].join('\t'),
4485
].join(eol);
4586
}
4687

47-
process.runtime = getRuntimeInfo();
48-
process.sassInfo = getSassInfo();
49-
process.sassBinaryName = getBinaryIdentifiableName();
88+
var sass = process.sass = {};
89+
90+
sass.binaryName = getBinaryName();
91+
sass.binaryUrl = getBinaryUrl();
92+
sass.runtime = getRuntimeInfo();
93+
sass.versionInfo = getVersionInfo();
94+
95+
/**
96+
* Get binary path.
97+
* If environment variable SASS_BINARY_PATH or
98+
* process aurgument --binary-path is provide,
99+
* select it by appending binary name, otherwise
100+
* make default binary path using binary name.
101+
* Once the primary selection is made, check if
102+
* callers wants to throw if file not exists before
103+
* returning.
104+
*
105+
* @param {Boolean} throwIfNotExists
106+
* @api private
107+
*/
108+
109+
sass.getBinaryPath = function(throwIfNotExists) {
110+
var binaryPath;
111+
112+
if (flags.binaryPath) {
113+
binaryPath = path.join(flags.binaryPath, sass.binaryName);
114+
} else if (process.env.SASS_BINARY_PATH) {
115+
binaryPath = path.join(process.env.SASS_BINARY_PATH, sass.binaryName);
116+
} else {
117+
binaryPath = path.join(__dirname, '..', 'vendor', sass.binaryName);
118+
}
119+
120+
if (!fs.existsSync(binaryPath) && throwIfNotExists) {
121+
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
122+
}
123+
124+
return binaryPath;
125+
};
126+
127+
sass.binaryPath = sass.getBinaryPath();

lib/index.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
var fs = require('fs'),
2-
path = require('path'),
1+
/*!
2+
* node-sass: lib/index.js
3+
*/
4+
5+
var path = require('path'),
36
util = require('util');
47

58
require('./extensions');
69

710
/**
8-
* Get binding
9-
*
10-
* @api private
11+
* Require binding
1112
*/
1213

13-
function getBinding() {
14-
var candidates = [
15-
path.join(__dirname, '..', 'build', 'Debug', 'binding.node'),
16-
path.join(__dirname, '..', 'build', 'Release', 'binding.node'),
17-
path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node')
18-
];
19-
20-
var candidate = candidates.filter(fs.existsSync).shift();
21-
22-
if (!candidate) {
23-
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
24-
}
25-
26-
return candidate;
27-
}
14+
var binding = require(process.sass.getBinaryPath(true));
2815

2916
/**
3017
* Get input file
@@ -149,12 +136,6 @@ function getOptions(options, cb) {
149136
return options;
150137
}
151138

152-
/**
153-
* Require binding
154-
*/
155-
156-
var binding = require(getBinding());
157-
158139
/**
159140
* Render
160141
*
@@ -246,4 +227,4 @@ module.exports.renderSync = function(options) {
246227
* @api public
247228
*/
248229

249-
module.exports.info = process.sassInfo;
230+
module.exports.info = process.sass.versionInfo;

lib/render.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*!
2+
* node-sass: lib/render.js
3+
*/
4+
15
var fs = require('fs'),
26
chalk = require('chalk'),
37
sass = require('./'),

scripts/build.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*!
2+
* node-sass: scripts/build.js
3+
*/
4+
15
var eol = require('os').EOL,
26
fs = require('fs'),
37
mkdir = require('mkdirp'),
@@ -14,30 +18,24 @@ require('../lib/extensions');
1418
*/
1519

1620
function afterBuild(options) {
17-
var folder = options.debug ? 'Debug' : 'Release';
18-
var target = path.join(__dirname, '..', 'build', folder, 'binding.node');
19-
var install = path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node');
21+
var install = process.sass.binaryPath;
22+
var target = path.join(__dirname, '..', 'build',
23+
options.debug ? 'Debug' : 'Release',
24+
'binding.node');
2025

21-
mkdir(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function(err) {
22-
if (err && err.code !== 'EEXIST') {
23-
console.error(err.message);
26+
fs.stat(target, function(err) {
27+
if (err) {
28+
console.error('Build succeeded but target not found');
2429
return;
2530
}
2631

27-
fs.stat(target, function(err) {
32+
fs.rename(target, install, function(err) {
2833
if (err) {
29-
console.error('Build succeeded but target not found');
34+
console.error(err.message);
3035
return;
3136
}
3237

33-
fs.rename(target, install, function(err) {
34-
if (err) {
35-
console.error(err.message);
36-
return;
37-
}
38-
39-
console.log('Installed in `' + install + '`');
40-
});
38+
console.log('Installed in `', install, '`');
4139
});
4240
});
4341
}
@@ -52,9 +50,9 @@ function afterBuild(options) {
5250
function build(options) {
5351
var arguments = [path.join('node_modules', 'pangyp', 'bin', 'node-gyp'), 'rebuild'].concat(options.args);
5452

55-
console.log(['Building:', process.runtime.execPath].concat(arguments).join(' '));
53+
console.log(['Building:', process.sass.runtime.execPath].concat(arguments).join(' '));
5654

57-
var proc = spawn(process.runtime.execPath, arguments, {
55+
var proc = spawn(process.sass.runtime.execPath, arguments, {
5856
stdio: [0, 1, 2]
5957
});
6058

@@ -110,25 +108,25 @@ function testBinary(options) {
110108
return build(options);
111109
}
112110

113-
fs.stat(path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node'), function(err) {
114-
if (err) {
115-
return build(options);
116-
}
111+
try {
112+
process.sass.getBinaryPath(true);
113+
} catch (e) {
114+
return build(options);
115+
}
117116

118-
console.log('`' + process.sassBinaryName + '` exists; testing.');
117+
console.log('`', process.sass.binaryName, '`exists at', eol, process.sass.binaryPath, eol, 'testing binary.');
119118

120-
try {
121-
require('../').renderSync({
122-
data: 's: { a: ss }'
123-
});
119+
try {
120+
require('../').renderSync({
121+
data: 's: { a: ss }'
122+
});
124123

125-
console.log('Binary is fine; exiting.');
126-
} catch (e) {
127-
console.log(['Problem with the binary.', 'Manual build incoming.'].join(eol));
124+
console.log('Binary is fine; exiting.');
125+
} catch (e) {
126+
console.log(['Problem with the binary.', 'Manual build incoming.'].join(eol));
128127

129-
return build(options);
130-
}
131-
});
128+
return build(options);
129+
}
132130
}
133131

134132
/**

scripts/coverage.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*!
2+
* node-sass: scripts/coverage.js
3+
*/
4+
15
var path = require('path'),
26
spawn = require('child_process').spawn,
37
bin = path.join.bind(null, __dirname, '..', 'node_modules', '.bin');

0 commit comments

Comments
 (0)