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

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: #712.
PR URL: #743.
  • Loading branch information
am11 committed Mar 8, 2015
1 parent beffe92 commit b75f9e0
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 126 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
103 changes: 92 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,101 @@ 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;
var binaryPathSlug = sass.binaryName.replace(/_/, '/');

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

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;
12 changes: 8 additions & 4 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var fs = require('fs'),
chalk = require('chalk'),
sass = require('./'),
/*!
* node-sass: lib/render.js
*/

var chalk = require('chalk'),
fs = require('fs'),
mkdirp = require('mkdirp'),
path = require('path'),
mkdirp = require('mkdirp');
sass = require('./');

/**
* Render
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.binaryPath, '` exists.', 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
10 changes: 7 additions & 3 deletions scripts/coverage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var path = require('path'),
spawn = require('child_process').spawn,
bin = path.join.bind(null, __dirname, '..', 'node_modules', '.bin');
/*!
* node-sass: scripts/coverage.js
*/

var bin = path.join.bind(null, __dirname, '..', 'node_modules', '.bin'),
path = require('path'),
spawn = require('child_process').spawn;

/**
* Run test suite
Expand Down
Loading

0 comments on commit b75f9e0

Please sign in to comment.