Commit 4c72885a by Adeel

Build: Use node-gyp from dependencies.

Code: Minor refactoring.
parent fe1d59f5
...@@ -10,19 +10,22 @@ var semver = require('semver'), ...@@ -10,19 +10,22 @@ var semver = require('semver'),
function getRuntimeName() { function getRuntimeName() {
return process.execPath return process.execPath
.split(/[\\/]+/).pop() .split(/[\\/]+/).pop()
.split('.')[0]; .split('.').shift();
} }
/** /**
* Get unique name of binary for current platform * Get unique name of binary for current platform
* *
* @api public * @api private
*/ */
module.exports.getBinaryIdentifiableName = function() { function getBinaryIdentifiableName() {
return process.platform + '-' + return [process.platform, '-',
process.arch + '-' + process.arch, '-',
getRuntimeName() + '-' + getRuntimeName(), '-',
runtimeVersion.major + '.' + runtimeVersion.major, '.',
runtimeVersion.minor; runtimeVersion.minor].join('');
}; }
process.runtime = getRuntimeName();
process.sassBinaryName = getBinaryIdentifiableName();
var fs = require('fs'), var fs = require('fs'),
path = require('path'), path = require('path');
utils = require('./utils');
require('./extensions');
/** /**
* Get binding * Get binding
...@@ -12,10 +13,10 @@ function getBinding() { ...@@ -12,10 +13,10 @@ function getBinding() {
var candidates = [ var candidates = [
path.join(__dirname, '..', 'build', 'Debug', 'binding.node'), path.join(__dirname, '..', 'build', 'Debug', 'binding.node'),
path.join(__dirname, '..', 'build', 'Release', 'binding.node'), path.join(__dirname, '..', 'build', 'Release', 'binding.node'),
path.join(__dirname, '..', 'vendor', utils.getBinaryIdentifiableName(), 'binding.node') path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node')
]; ];
var candidate = candidates.filter(fs.existsSync)[0]; var candidate = candidates.filter(fs.existsSync).shift();
if (!candidate) { if (!candidate) {
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?'); throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
...@@ -146,12 +147,7 @@ function getOptions(options) { ...@@ -146,12 +147,7 @@ function getOptions(options) {
var success = options.success; var success = options.success;
options.error = function(err, code) { options.error = function(err, code) {
try {
err = JSON.parse(err); err = JSON.parse(err);
} catch (e) {
err = { message: err };
}
err.code = code; err.code = code;
if (error) { if (error) {
......
...@@ -2,8 +2,9 @@ var fs = require('fs'), ...@@ -2,8 +2,9 @@ var fs = require('fs'),
path = require('path'), path = require('path'),
spawn = require('child_process').spawn, spawn = require('child_process').spawn,
mkdir = require('mkdirp'), mkdir = require('mkdirp'),
Mocha = require('mocha'), Mocha = require('mocha');
utils = require('../lib/utils');
require('../lib/extensions');
/** /**
* After build * After build
...@@ -15,9 +16,9 @@ var fs = require('fs'), ...@@ -15,9 +16,9 @@ var fs = require('fs'),
function afterBuild(options) { function afterBuild(options) {
var folder = options.debug ? 'Debug' : 'Release'; var folder = options.debug ? 'Debug' : 'Release';
var target = path.join(__dirname, '..', 'build', folder, 'binding.node'); var target = path.join(__dirname, '..', 'build', folder, 'binding.node');
var install = path.join(__dirname, '..', 'vendor', options.bin, 'binding.node'); var install = path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node');
mkdir(path.join(__dirname, '..', 'vendor', options.bin), function (err) { mkdir(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function (err) {
if (err && err.code !== 'EEXIST') { if (err && err.code !== 'EEXIST') {
console.error(err.message); console.error(err.message);
return; return;
...@@ -49,9 +50,8 @@ function afterBuild(options) { ...@@ -49,9 +50,8 @@ function afterBuild(options) {
*/ */
function build(options) { function build(options) {
var bin = options.platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp'; var proc = spawn(process.execPath, ['node_modules/pangyp/bin/node-gyp', 'rebuild'].concat(options.args), {
var proc = spawn(bin, ['rebuild'].concat(options.args), { stdio: [0, 1, 2]
customFds: [0, 1, 2]
}); });
proc.on('exit', function(code) { proc.on('exit', function(code) {
...@@ -92,7 +92,7 @@ function parseArgs(args) { ...@@ -92,7 +92,7 @@ function parseArgs(args) {
return false; return false;
} else if (arg.substring(0, 13) === '--target_arch') { } else if (arg.substring(0, 13) === '--target_arch') {
options.arch = arg.substring(14); options.arch = arg.substring(14);
} else if (arg === '--debug') { } else if (arg === '-d' || arg === '--debug') {
options.debug = true; options.debug = true;
} }
...@@ -110,18 +110,16 @@ function parseArgs(args) { ...@@ -110,18 +110,16 @@ function parseArgs(args) {
*/ */
function testBinary(options) { function testBinary(options) {
options.bin = utils.getBinaryIdentifiableName();
if (options.force || process.env.SASS_FORCE_BUILD) { if (options.force || process.env.SASS_FORCE_BUILD) {
return build(options); return build(options);
} }
fs.stat(path.join(__dirname, '..', 'vendor', options.bin, 'binding.node'), function (err) { fs.stat(path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node'), function (err) {
if (err) { if (err) {
return build(options); return build(options);
} }
console.log('`' + options.bin + '` exists; testing'); console.log('`' + process.sassBinaryName + '` exists; testing');
var mocha = new Mocha({ var mocha = new Mocha({
ui: 'bdd', ui: 'bdd',
......
...@@ -2,8 +2,9 @@ var fs = require('fs'), ...@@ -2,8 +2,9 @@ var fs = require('fs'),
path = require('path'), path = require('path'),
request = require('request'), request = require('request'),
mkdirp = require('mkdirp'), mkdirp = require('mkdirp'),
exec = require('shelljs').exec, exec = require('shelljs').exec;
utils = require('../lib/utils');
require('../lib/extensions');
/** /**
* Download file, if succeeds save, if not delete * Download file, if succeeds save, if not delete
...@@ -77,31 +78,28 @@ function applyProxy(options, cb) { ...@@ -77,31 +78,28 @@ function applyProxy(options, cb) {
*/ */
function exists() { function exists() {
var name = utils.getBinaryIdentifiableName(); fs.exists(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function (exists) {
fs.exists(path.join(__dirname, '..', 'vendor', name), function (exists) {
if (exists) { if (exists) {
return; return;
} }
fetch(name); fetch();
}); });
} }
/** /**
* Fetch binaries * Fetch binaries
* *
* @param {String} name
* @api private * @api private
*/ */
function fetch(name) { function fetch() {
var url = [ var url = [
'https://raw.githubusercontent.com/sass/node-sass-binaries/v', 'https://raw.githubusercontent.com/sass/node-sass-binaries/v',
require('../package.json').version, '/', name, require('../package.json').version, '/', process.sassBinaryName,
'/binding.node' '/binding.node'
].join(''); ].join('');
var dir = path.join(__dirname, '..', 'vendor', name); var dir = path.join(__dirname, '..', 'vendor', process.sassBinaryName);
var dest = path.join(dir, 'binding.node'); var dest = path.join(dir, 'binding.node');
mkdirp(dir, function(err) { mkdirp(dir, function(err) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment