Skip to content

Commit

Permalink
Build: Centralises binary naming convensions.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
am11 committed Mar 8, 2015
1 parent beffe92 commit faa4420
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 118 deletions.
3 changes: 2 additions & 1 deletion bin/node-sass
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

var Emitter = require('events').EventEmitter,
Gaze = require('gaze'),
grapher = require('sass-graph'),
Expand All @@ -13,7 +14,7 @@ var Emitter = require('events').EventEmitter,

var cli = meow({
pkg: '../package.json',
version: process.sassInfo,
version: process.sass.versionInfo,
help: [
'Usage',
' node-sass [options] <input.scss> [output.css]',
Expand Down
102 changes: 91 additions & 11 deletions lib/extensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*!
* node-sass: lib/extensions.js
*/

var eol = require('os').EOL,
flags = require('meow')({ pkg: '../package.json' }).flags,
fs = require('fs'),
package = require('../package.json');
package = require('../package.json'),
path = require('path');

/**
* Get Runtime Info
Expand All @@ -24,26 +30,100 @@ function getRuntimeInfo() {
}

/**
* Get unique name of binary for current platform
* Get binary name.
* If environment variable SASS_BINARY_NAME or
* process aurgument --binary-name is provide,
* return it as is, otherwise make default binary
* name: {platform}-{arch}-{v8 version}.node
*
* @api private
*/

function getBinaryIdentifiableName() {
var v8SemVersion = process.versions.v8.split('.').slice(0, 3).join('.');
function getBinaryName() {
var binaryName;

if (flags.binaryName) {
binaryName = flags.binaryName;
} else if (process.env.SASS_BINARY_NAME) {
binaryName = process.env.SASS_BINARY_NAME;
} else {
var v8SemVersion = process.versions.v8.split('.').slice(0, 3).join('.');

binaryName = [process.platform, '-',
process.arch, '-',
v8SemVersion].join('');
}

return [binaryName, 'binding.node'].join('/');
}

/**
* Retrieve the URL to fetch binary.
* If environment variable SASS_BINARY_URL
* is set, return that path. Otherwise make
* path using current release version and
* binary name.
*
* @api private
*/

return [process.platform, '-',
process.arch, '-',
v8SemVersion].join('');
function getBinaryUrl() {
return flags.binaryUrl ||
process.env.SASS_BINARY_URL ||
['https://github.com/sass/node-sass/releases/download//v',
package.version, '/', sass.binaryName].join('');
}

function getSassInfo() {
/**
* Get Sass version information
*
* @api private
*/

function getVersionInfo() {
return [
['node-sass', package.version, '(Wrapper)', '[JavaScript]'].join('\t'),
['libsass ', package.libsass, '(Sass Compiler)', '[C/C++]'].join('\t'),
].join(eol);
}

process.runtime = getRuntimeInfo();
process.sassInfo = getSassInfo();
process.sassBinaryName = getBinaryIdentifiableName();
var sass = process.sass = {};

sass.binaryName = getBinaryName();
sass.binaryUrl = getBinaryUrl();
sass.runtime = getRuntimeInfo();
sass.versionInfo = getVersionInfo();

/**
* Get binary path.
* If environment variable SASS_BINARY_PATH or
* process aurgument --binary-path is provide,
* select it by appending binary name, otherwise
* make default binary path using binary name.
* Once the primary selection is made, check if
* callers wants to throw if file not exists before
* returning.
*
* @param {Boolean} throwIfNotExists
* @api private
*/

sass.getBinaryPath = function(throwIfNotExists) {
var binaryPath;

if (flags.binaryPath) {
binaryPath = path.join(flags.binaryPath, sass.binaryName);
} else if (process.env.SASS_BINARY_PATH) {
binaryPath = path.join(process.env.SASS_BINARY_PATH, sass.binaryName);
} else {
binaryPath = path.join(__dirname, '..', 'vendor', sass.binaryName);
}

if (!fs.existsSync(binaryPath) && throwIfNotExists) {
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
}

return binaryPath;
};

sass.binaryPath = sass.getBinaryPath();
35 changes: 8 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
var fs = require('fs'),
path = require('path'),
/*!
* node-sass: lib/index.js
*/

var path = require('path'),
util = require('util');

require('./extensions');

/**
* Get binding
*
* @api private
* Require binding
*/

function getBinding() {
var candidates = [
path.join(__dirname, '..', 'build', 'Debug', 'binding.node'),
path.join(__dirname, '..', 'build', 'Release', 'binding.node'),
path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node')
];

var candidate = candidates.filter(fs.existsSync).shift();

if (!candidate) {
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
}

return candidate;
}
var binding = require(process.sass.getBinaryPath(true));

/**
* Get input file
Expand Down Expand Up @@ -149,12 +136,6 @@ function getOptions(options, cb) {
return options;
}

/**
* Require binding
*/

var binding = require(getBinding());

/**
* Render
*
Expand Down Expand Up @@ -246,4 +227,4 @@ module.exports.renderSync = function(options) {
* @api public
*/

module.exports.info = process.sassInfo;
module.exports.info = process.sass.versionInfo;
4 changes: 4 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*!
* node-sass: lib/render.js
*/

var fs = require('fs'),
chalk = require('chalk'),
sass = require('./'),
Expand Down
47 changes: 25 additions & 22 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*!
* node-sass: scripts/build.js
*/

var eol = require('os').EOL,
fs = require('fs'),
mkdir = require('mkdirp'),
Expand All @@ -14,11 +18,10 @@ require('../lib/extensions');
*/

function afterBuild(options) {
var folder = options.debug ? 'Debug' : 'Release';
var target = path.join(__dirname, '..', 'build', folder, 'binding.node');
var install = path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node');
var install = process.sass.binaryPath;
var target = path.join(__dirname, '..', 'build', options.debug ? 'Debug' : 'Release', 'binding.node');

mkdir(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function(err) {
mkdir(path.dirname(install), function(err) {
if (err && err.code !== 'EEXIST') {
console.error(err.message);
return;
Expand All @@ -36,7 +39,7 @@ function afterBuild(options) {
return;
}

console.log('Installed in `' + install + '`');
console.log('Installed in `', install, '`');
});
});
});
Expand All @@ -52,9 +55,9 @@ function afterBuild(options) {
function build(options) {
var arguments = [path.join('node_modules', 'pangyp', 'bin', 'node-gyp'), 'rebuild'].concat(options.args);

console.log(['Building:', process.runtime.execPath].concat(arguments).join(' '));
console.log(['Building:', process.sass.runtime.execPath].concat(arguments).join(' '));

var proc = spawn(process.runtime.execPath, arguments, {
var proc = spawn(process.sass.runtime.execPath, arguments, {
stdio: [0, 1, 2]
});

Expand Down Expand Up @@ -110,25 +113,25 @@ function testBinary(options) {
return build(options);
}

fs.stat(path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node'), function(err) {
if (err) {
return build(options);
}
try {
process.sass.getBinaryPath(true);
} catch (e) {
return build(options);
}

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

try {
require('../').renderSync({
data: 's: { a: ss }'
});
try {
require('../').renderSync({
data: 's: { a: ss }'
});

console.log('Binary is fine; exiting.');
} catch (e) {
console.log(['Problem with the binary.', 'Manual build incoming.'].join(eol));
console.log('Binary is fine; exiting.');
} catch (e) {
console.log(['Problem with the binary.', 'Manual build incoming.'].join(eol));

return build(options);
}
});
return build(options);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions scripts/coverage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*!
* node-sass: scripts/coverage.js
*/

var path = require('path'),
spawn = require('child_process').spawn,
bin = path.join.bind(null, __dirname, '..', 'node_modules', '.bin');
Expand Down
58 changes: 22 additions & 36 deletions scripts/install.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*!
* node-sass: scripts/install.js
*/

var fs = require('fs'),
path = require('path'),
request = require('request'),
mkdirp = require('mkdirp'),
npmconf = require('npmconf'),
packageInfo = require('../package.json');
mkdir = require('mkdirp');
path = require('path'),
request = require('request'),
npmconf = require('npmconf'),
packageInfo = require('../package.json');

require('../lib/extensions');

Expand All @@ -24,7 +28,7 @@ function download(url, dest, cb) {

request.get(url, options).on('response', function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
returnError('Can not download file from ' + url);
returnError(['Can not download file from:', url].join());
return;
}

Expand Down Expand Up @@ -64,49 +68,31 @@ function applyProxy(options, cb) {
}

/**
* Check if binaries exists
*
* @api private
*/

function checkAndFetchBinaries() {
fs.exists(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function (exists) {
if (exists) {
return;
}

fetch();
});
}

/**
* Fetch binaries
* Check and download binary
*
* @api private
*/

function fetch() {
var url = [
'https://raw.githubusercontent.com/sass/node-sass-binaries/v',
packageInfo.version, '/', process.sassBinaryName,
'/binding.node'
].join('');
var dir = path.join(__dirname, '..', 'vendor', process.sassBinaryName);
var dest = path.join(dir, 'binding.node');
function checkAndDownloadBinary() {
try {
process.sass.getBinaryPath(true);
} catch (e) {
return;
}

mkdirp(dir, function(err) {
mkdirp(path.dirname(process.sass.binaryPath), function(err) {
if (err) {
console.error(err);
return;
}

download(url, dest, function(err) {
download(process.sass.binaryUrl, process.sass.binaryPath, function(err) {
if (err) {
console.error(err);
return;
}

console.log('Binary downloaded and installed at ' + dest);
console.log('Binary downloaded and installed at', process.sass.binaryPath);
});
});
}
Expand All @@ -121,7 +107,7 @@ if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
}

/**
* Run
* If binary does not exsits, download it
*/

checkAndFetchBinaries();
checkAndDownloadBinary();
Loading

0 comments on commit faa4420

Please sign in to comment.