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

Commit

Permalink
* Delivers binary name, paths and download URL
Browse files Browse the repository at this point in the history
  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: #712.

//cc @nschonni, @xzyfer
  • Loading branch information
am11 committed Mar 7, 2015
1 parent beffe92 commit f785eb9
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 127 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
94 changes: 86 additions & 8 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,98 @@ 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() {
function getBinaryName() {
if (flags.binaryName) {
return flags.binaryName;
}

if (process.env.SASS_BINARY_NAME) {
return process.env.SASS_BINARY_NAME;
}

var v8SemVersion = process.versions.v8.split('.').slice(0, 3).join('.');

return [process.platform, '-',
process.arch, '-',
v8SemVersion].join('');
v8SemVersion, '.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
*/

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
64 changes: 31 additions & 33 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,30 +18,24 @@ 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) {
if (err && err.code !== 'EEXIST') {
console.error(err.message);
fs.stat(target, function(err) {
if (err) {
console.error('Build succeeded but target not found');
return;
}

fs.stat(target, function(err) {
fs.rename(target, install, function(err) {
if (err) {
console.error('Build succeeded but target not found');
console.error(err.message);
return;
}

fs.rename(target, install, function(err) {
if (err) {
console.error(err.message);
return;
}

console.log('Installed in `' + install + '`');
});
console.log('Installed in `', install, '`');
});
});
}
Expand All @@ -52,9 +50,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 +108,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
Loading

0 comments on commit f785eb9

Please sign in to comment.