Skip to content

Commit eb618d8

Browse files
committed
Build: Centralises binary naming convensions.
* 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.
1 parent ff7f9f9 commit eb618d8

File tree

11 files changed

+180
-126
lines changed

11 files changed

+180
-126
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: 92 additions & 11 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,101 @@ 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() {
33-
var v8SemVersion = process.versions.v8.split('.').slice(0, 3).join('.');
42+
function getBinaryName() {
43+
var binaryName;
44+
45+
if (flags.binaryName) {
46+
binaryName = flags.binaryName;
47+
} else if (process.env.SASS_BINARY_NAME) {
48+
binaryName = process.env.SASS_BINARY_NAME;
49+
} else {
50+
var v8SemVersion = process.versions.v8.split('.').slice(0, 3).join('.');
51+
52+
binaryName = [process.platform, '-',
53+
process.arch, '-',
54+
v8SemVersion].join('');
55+
}
56+
57+
return [binaryName, 'binding.node'].join('_');
58+
}
59+
60+
/**
61+
* Retrieve the URL to fetch binary.
62+
* If environment variable SASS_BINARY_URL
63+
* is set, return that path. Otherwise make
64+
* path using current release version and
65+
* binary name.
66+
*
67+
* @api private
68+
*/
3469

35-
return [process.platform, '-',
36-
process.arch, '-',
37-
v8SemVersion].join('');
70+
function getBinaryUrl() {
71+
return flags.binaryUrl ||
72+
process.env.SASS_BINARY_URL ||
73+
['https://github.com/sass/node-sass/releases/download//v',
74+
package.version, '/', sass.binaryName].join('');
3875
}
3976

40-
function getSassInfo() {
77+
/**
78+
* Get Sass version information
79+
*
80+
* @api private
81+
*/
82+
83+
function getVersionInfo() {
4184
return [
4285
['node-sass', package.version, '(Wrapper)', '[JavaScript]'].join('\t'),
4386
['libsass ', package.libsass, '(Sass Compiler)', '[C/C++]'].join('\t'),
4487
].join(eol);
4588
}
4689

47-
process.runtime = getRuntimeInfo();
48-
process.sassInfo = getSassInfo();
49-
process.sassBinaryName = getBinaryIdentifiableName();
90+
var sass = process.sass = {};
91+
92+
sass.binaryName = getBinaryName();
93+
sass.binaryUrl = getBinaryUrl();
94+
sass.runtime = getRuntimeInfo();
95+
sass.versionInfo = getVersionInfo();
96+
97+
/**
98+
* Get binary path.
99+
* If environment variable SASS_BINARY_PATH or
100+
* process aurgument --binary-path is provide,
101+
* select it by appending binary name, otherwise
102+
* make default binary path using binary name.
103+
* Once the primary selection is made, check if
104+
* callers wants to throw if file not exists before
105+
* returning.
106+
*
107+
* @param {Boolean} throwIfNotExists
108+
* @api private
109+
*/
110+
111+
sass.getBinaryPath = function(throwIfNotExists) {
112+
var binaryPath;
113+
var binaryPathSlug = sass.binaryName.replace(/_/, '/');
114+
115+
if (flags.binaryPath) {
116+
binaryPath = path.join(flags.binaryPath, binaryPathSlug);
117+
} else if (process.env.SASS_BINARY_PATH) {
118+
binaryPath = path.join(process.env.SASS_BINARY_PATH, binaryPathSlug);
119+
} else {
120+
binaryPath = path.join(__dirname, '..', 'vendor', binaryPathSlug);
121+
}
122+
123+
if (!fs.existsSync(binaryPath) && throwIfNotExists) {
124+
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
125+
}
126+
127+
return binaryPath;
128+
};
129+
130+
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
var fs = require('fs'),
2-
chalk = require('chalk'),
3-
sass = require('./'),
1+
/*!
2+
* node-sass: lib/render.js
3+
*/
4+
5+
var chalk = require('chalk'),
6+
fs = require('fs'),
7+
mkdirp = require('mkdirp'),
48
path = require('path'),
5-
mkdirp = require('mkdirp');
9+
sass = require('./');
610

711
/**
812
* Render

scripts/build.js

Lines changed: 25 additions & 22 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,11 +18,10 @@ 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', options.debug ? 'Debug' : 'Release', 'binding.node');
2023

21-
mkdir(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function(err) {
24+
mkdir(path.dirname(install), function(err) {
2225
if (err && err.code !== 'EEXIST') {
2326
console.error(err.message);
2427
return;
@@ -36,7 +39,7 @@ function afterBuild(options) {
3639
return;
3740
}
3841

39-
console.log('Installed in `' + install + '`');
42+
console.log('Installed in `', install, '`');
4043
});
4144
});
4245
});
@@ -52,9 +55,9 @@ function afterBuild(options) {
5255
function build(options) {
5356
var arguments = [path.join('node_modules', 'pangyp', 'bin', 'node-gyp'), 'rebuild'].concat(options.args);
5457

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

57-
var proc = spawn(process.runtime.execPath, arguments, {
60+
var proc = spawn(process.sass.runtime.execPath, arguments, {
5861
stdio: [0, 1, 2]
5962
});
6063

@@ -110,25 +113,25 @@ function testBinary(options) {
110113
return build(options);
111114
}
112115

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

118-
console.log('`' + process.sassBinaryName + '` exists; testing.');
122+
console.log('`', process.sass.binaryPath, '` exists.', eol, 'testing binary.');
119123

120-
try {
121-
require('../').renderSync({
122-
data: 's: { a: ss }'
123-
});
124+
try {
125+
require('../').renderSync({
126+
data: 's: { a: ss }'
127+
});
124128

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

129-
return build(options);
130-
}
131-
});
133+
return build(options);
134+
}
132135
}
133136

134137
/**

scripts/coverage.js

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

58
/**
69
* Run test suite
@@ -14,7 +17,7 @@ function suite() {
1417
var coveralls = spawn(bin('coveralls'));
1518

1619
var args = [bin('_mocha')].concat(['--reporter', 'mocha-lcov-reporter']);
17-
var mocha = spawn(process.execPath, args, {
20+
var mocha = spawn(process.sass.runtime.execPath, args, {
1821
env: process.env
1922
});
2023

0 commit comments

Comments
 (0)