Commit 22c560c7 by xzyfer

Remove use of global process.sass

This replaces the global `process.sass` with proper module exports.
Unfortunately `process.sass`, although undocumented, is available
to other modules. This patch keeps the existing public API available
until we bump the major.
parent b0df78d6
...@@ -8,6 +8,7 @@ var Emitter = require('events').EventEmitter, ...@@ -8,6 +8,7 @@ var Emitter = require('events').EventEmitter,
util = require('util'), util = require('util'),
path = require('path'), path = require('path'),
glob = require('glob'), glob = require('glob'),
sass = require('../lib'),
render = require('../lib/render'), render = require('../lib/render'),
stdin = require('get-stdin'), stdin = require('get-stdin'),
fs = require('fs'); fs = require('fs');
...@@ -18,7 +19,7 @@ var Emitter = require('events').EventEmitter, ...@@ -18,7 +19,7 @@ var Emitter = require('events').EventEmitter,
var cli = meow({ var cli = meow({
pkg: '../package.json', pkg: '../package.json',
version: process.sass.versionInfo, version: sass.info,
help: [ help: [
'Usage:', 'Usage:',
' node-sass [options] <input.scss>', ' node-sass [options] <input.scss>',
......
...@@ -2,27 +2,28 @@ ...@@ -2,27 +2,28 @@
* node-sass: lib/extensions.js * node-sass: lib/extensions.js
*/ */
var flags = {}, var eol = require('os').EOL,
fs = require('fs'), fs = require('fs'),
pkg = require('../package.json'), pkg = require('../package.json'),
path = require('path'); path = require('path');
/** /**
* Collect Arguments * Get the value of a CLI argument
* *
* @param {String} name
* @param {Array} args * @param {Array} args
* @api private * @api private
*/ */
function collectArguments(args) { function getArgument(name, args) {
for (var i = 0; i < args.length; i += 2) { var flags = args || process.argv.slice(2),
if (args[i].lastIndexOf('--', 0) !== 0) { index = flags.lastIndexOf(name);
--i;
continue;
}
flags[args[i]] = args[i + 1]; if (index === -1 || index + 1 >= flags.length) {
return null;
} }
return flags[index + 1];
} }
/** /**
...@@ -33,14 +34,14 @@ function collectArguments(args) { ...@@ -33,14 +34,14 @@ function collectArguments(args) {
* return it as is, otherwise make default binary * return it as is, otherwise make default binary
* name: {platform}-{arch}-{v8 version}.node * name: {platform}-{arch}-{v8 version}.node
* *
* @api private * @api public
*/ */
function getBinaryName() { function getBinaryName() {
var binaryName; var binaryName;
if (flags['--sass-binary-name']) { if (getArgument('--sass-binary-name')) {
binaryName = flags['--sass-binary-name']; binaryName = getArgument('--sass-binary-name');
} else if (process.env.SASS_BINARY_NAME) { } else if (process.env.SASS_BINARY_NAME) {
binaryName = process.env.SASS_BINARY_NAME; binaryName = process.env.SASS_BINARY_NAME;
} else if (process.env.npm_config_sass_binary_name) { } else if (process.env.npm_config_sass_binary_name) {
...@@ -63,7 +64,7 @@ function getBinaryName() { ...@@ -63,7 +64,7 @@ function getBinaryName() {
* *
* The default URL can be overriden using * The default URL can be overriden using
* the environment variable SASS_BINARY_SITE, * the environment variable SASS_BINARY_SITE,
* .npmrc variable sass_binary_site or * .npmrc variable sass_binary_site or
* or a command line option --sass-binary-site: * or a command line option --sass-binary-site:
* *
* node scripts/install.js --sass-binary-site http://example.com/ * node scripts/install.js --sass-binary-site http://example.com/
...@@ -81,27 +82,19 @@ function getBinaryName() { ...@@ -81,27 +82,19 @@ function getBinaryName() {
* v3.0.0/freebsd-x64-42_binding.node * v3.0.0/freebsd-x64-42_binding.node
* ... etc. for all supported versions and platforms * ... etc. for all supported versions and platforms
* *
* @api private * @api public
*/ */
function getBinaryUrl() { function getBinaryUrl() {
var site = flags['--sass-binary-site'] || var site = getArgument('--sass-binary-site') ||
process.env.SASS_BINARY_SITE || process.env.SASS_BINARY_SITE ||
process.env.npm_config_sass_binary_site || process.env.npm_config_sass_binary_site ||
(pkg.nodeSassConfig && pkg.nodeSassConfig.binarySite) || (pkg.nodeSassConfig && pkg.nodeSassConfig.binarySite) ||
'https://github.com/sass/node-sass/releases/download'; 'https://github.com/sass/node-sass/releases/download';
return [site, 'v' + pkg.version, sass.binaryName].join('/'); return [site, 'v' + pkg.version, getBinaryName()].join('/');
} }
collectArguments(process.argv.slice(2));
var sass = process.sass = {};
sass.binaryName = getBinaryName();
sass.binaryUrl = getBinaryUrl();
/** /**
* Get binary path. * Get binary path.
* If environment variable SASS_BINARY_PATH, * If environment variable SASS_BINARY_PATH,
...@@ -114,14 +107,14 @@ sass.binaryUrl = getBinaryUrl(); ...@@ -114,14 +107,14 @@ sass.binaryUrl = getBinaryUrl();
* returning. * returning.
* *
* @param {Boolean} throwIfNotExists * @param {Boolean} throwIfNotExists
* @api private * @api public
*/ */
sass.getBinaryPath = function(throwIfNotExists) { function getBinaryPath(throwIfNotExists) {
var binaryPath; var binaryPath;
if (flags['--sass-binary-path']) { if (getArgument('--sass-binary-path')) {
binaryPath = flags['--sass-binary-path']; binaryPath = getArgument('--sass-binary-path');
} else if (process.env.SASS_BINARY_PATH) { } else if (process.env.SASS_BINARY_PATH) {
binaryPath = process.env.SASS_BINARY_PATH; binaryPath = process.env.SASS_BINARY_PATH;
} else if (process.env.npm_config_sass_binary_path) { } else if (process.env.npm_config_sass_binary_path) {
...@@ -129,7 +122,7 @@ sass.getBinaryPath = function(throwIfNotExists) { ...@@ -129,7 +122,7 @@ sass.getBinaryPath = function(throwIfNotExists) {
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) {
binaryPath = pkg.nodeSassConfig.binaryPath; binaryPath = pkg.nodeSassConfig.binaryPath;
} else { } else {
binaryPath = path.join(__dirname, '..', 'vendor', sass.binaryName.replace(/_/, '/')); binaryPath = path.join(__dirname, '..', 'vendor', getBinaryName().replace(/_/, '/'));
} }
if (!fs.existsSync(binaryPath) && throwIfNotExists) { if (!fs.existsSync(binaryPath) && throwIfNotExists) {
...@@ -141,6 +134,22 @@ sass.getBinaryPath = function(throwIfNotExists) { ...@@ -141,6 +134,22 @@ sass.getBinaryPath = function(throwIfNotExists) {
} }
return binaryPath; return binaryPath;
}; }
/**
* Get Sass version information
*
* @api public
*/
function getVersionInfo(binding) {
return [
['node-sass', pkg.version, '(Wrapper)', '[JavaScript]'].join('\t'),
['libsass ', binding.libsassVersion(), '(Sass Compiler)', '[C/C++]'].join('\t'),
].join(eol);
}
sass.binaryPath = sass.getBinaryPath(); module.exports.getBinaryUrl = getBinaryUrl;
module.exports.getBinaryName = getBinaryName;
module.exports.getBinaryPath = getBinaryPath;
module.exports.getVersionInfo = getVersionInfo;
...@@ -2,31 +2,15 @@ ...@@ -2,31 +2,15 @@
* node-sass: lib/index.js * node-sass: lib/index.js
*/ */
var eol = require('os').EOL, var path = require('path'),
path = require('path'),
util = require('util'), util = require('util'),
pkg = require('../package.json'); sass = require('./extensions');
require('./extensions');
/** /**
* Require binding * Require binding
*/ */
var binding = require(process.sass.getBinaryPath(true)); var binding = require(sass.getBinaryPath(true));
/**
* Get Sass version information
*
* @api private
*/
function getVersionInfo(binding) {
return [
['node-sass', pkg.version, '(Wrapper)', '[JavaScript]'].join('\t'),
['libsass ', binding.libsassVersion(), '(Sass Compiler)', '[C/C++]'].join('\t'),
].join(eol);
}
/** /**
* Get input file * Get input file
...@@ -430,8 +414,7 @@ module.exports.renderSync = function(options) { ...@@ -430,8 +414,7 @@ module.exports.renderSync = function(options) {
* @api public * @api public
*/ */
process.sass.versionInfo = getVersionInfo(binding); module.exports.info = sass.getVersionInfo(binding);
module.exports.info = process.sass.versionInfo;
/** /**
* Expose sass types * Expose sass types
......
...@@ -7,9 +7,8 @@ var eol = require('os').EOL, ...@@ -7,9 +7,8 @@ var eol = require('os').EOL,
fs = require('fs'), fs = require('fs'),
mkdir = require('mkdirp'), mkdir = require('mkdirp'),
path = require('path'), path = require('path'),
spawn = require('cross-spawn-async'); spawn = require('cross-spawn-async'),
sass = require('../lib/extensions');
require('../lib/extensions');
/** /**
* After build * After build
...@@ -19,7 +18,7 @@ require('../lib/extensions'); ...@@ -19,7 +18,7 @@ require('../lib/extensions');
*/ */
function afterBuild(options) { function afterBuild(options) {
var install = process.sass.binaryPath; var install = sass.getBinaryPath();
var target = path.join(__dirname, '..', 'build', var target = path.join(__dirname, '..', 'build',
options.debug ? 'Debug' : process.config.target_defaults.default_configuration, options.debug ? 'Debug' : process.config.target_defaults.default_configuration,
'binding.node'); 'binding.node');
...@@ -197,13 +196,11 @@ function testBinary(options) { ...@@ -197,13 +196,11 @@ function testBinary(options) {
return build(options); return build(options);
} }
try { if (!sass.getBinaryPath()) {
process.sass.getBinaryPath(true);
} catch (e) {
return build(options); return build(options);
} }
console.log('`', process.sass.binaryPath, '` exists.', eol, 'testing binary.'); console.log('`', sass.getBinaryPath(), '` exists.', eol, 'testing binary.');
try { try {
require('../').renderSync({ require('../').renderSync({
......
...@@ -7,9 +7,8 @@ var fs = require('fs'), ...@@ -7,9 +7,8 @@ var fs = require('fs'),
mkdir = require('mkdirp'), mkdir = require('mkdirp'),
path = require('path'), path = require('path'),
got = require('got'), got = require('got'),
pkg = require('../package.json'); pkg = require('../package.json'),
sass = require('../lib/extensions');
require('../lib/extensions');
/** /**
* Download file, if succeeds save, if not delete * Download file, if succeeds save, if not delete
...@@ -82,24 +81,23 @@ function applyProxy(options, cb) { ...@@ -82,24 +81,23 @@ function applyProxy(options, cb) {
*/ */
function checkAndDownloadBinary() { function checkAndDownloadBinary() {
try { if (sass.getBinaryPath()) {
process.sass.getBinaryPath(true);
return; return;
} catch (e) { } }
mkdir(path.dirname(process.sass.binaryPath), function(err) { mkdir(path.dirname(sass.getBinaryPath()), function(err) {
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
download(process.sass.binaryUrl, process.sass.binaryPath, function(err) { download(sass.getBinaryUrl(), sass.getBinaryPath(), function(err) {
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
console.log('Binary downloaded and installed at', process.sass.binaryPath); console.log('Binary downloaded and installed at', sass.binaryPath());
}); });
}); });
} }
......
process.env.NODESASS_COV ? require('../lib-cov') : require('../lib'); process.env.NODESASS_COV ? require('../lib-cov') : require('../lib');
var assert = require('assert'), var assert = require('assert'),
binding = require(process.sass.binaryPath); sass = require('../lib/extensions'),
binding = require(sass.getBinaryPath());
describe('lowlevel', function() { describe('lowlevel', function() {
it('fail with options not an object', function(done) { it('fail with options not an object', function(done) {
......
var assert = require('assert'), var assert = require('assert'),
fs = require('fs'); fs = require('fs'),
extensionsPath = process.env.NODESASS_COV
? require.resolve('../lib-cov/extensions')
: require.resolve('../lib/extensions');
describe('runtime parameters', function() { describe('runtime parameters', function() {
var packagePath = require.resolve('../package'), var packagePath = require.resolve('../package'),
extensionsPath = process.env.NODESASS_COV
? require.resolve('../lib-cov/extensions')
: require.resolve('../lib/extensions'),
// Let's use JSON to fake a deep copy // Let's use JSON to fake a deep copy
savedArgv = JSON.stringify(process.argv), savedArgv = JSON.stringify(process.argv),
savedEnv = JSON.stringify(process.env); savedEnv = JSON.stringify(process.env);
...@@ -21,7 +21,6 @@ describe('runtime parameters', function() { ...@@ -21,7 +21,6 @@ describe('runtime parameters', function() {
process.argv = JSON.parse(savedArgv); process.argv = JSON.parse(savedArgv);
process.env = JSON.parse(savedEnv); process.env = JSON.parse(savedEnv);
require(packagePath); require(packagePath);
require(extensionsPath);
}); });
describe('configuration precedence should be respected', function() { describe('configuration precedence should be respected', function() {
...@@ -35,29 +34,29 @@ describe('runtime parameters', function() { ...@@ -35,29 +34,29 @@ describe('runtime parameters', function() {
}); });
it('command line argument', function() { it('command line argument', function() {
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryName, 'aaa_binding.node'); assert.equal(sass.getBinaryName(), 'aaa_binding.node');
}); });
it('environment variable', function() { it('environment variable', function() {
process.argv = []; process.argv = [];
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryName, 'bbb_binding.node'); assert.equal(sass.getBinaryName(), 'bbb_binding.node');
}); });
it('npm config variable', function() { it('npm config variable', function() {
process.argv = []; process.argv = [];
process.env.SASS_BINARY_NAME = null; process.env.SASS_BINARY_NAME = null;
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryName, 'ccc_binding.node'); assert.equal(sass.getBinaryName(), 'ccc_binding.node');
}); });
it('package.json', function() { it('package.json', function() {
process.argv = []; process.argv = [];
process.env.SASS_BINARY_NAME = null; process.env.SASS_BINARY_NAME = null;
process.env.npm_config_sass_binary_name = null; process.env.npm_config_sass_binary_name = null;
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryName, 'ddd_binding.node'); assert.equal(sass.getBinaryName(), 'ddd_binding.node');
}); });
}); });
...@@ -70,33 +69,33 @@ describe('runtime parameters', function() { ...@@ -70,33 +69,33 @@ describe('runtime parameters', function() {
}); });
it('command line argument', function() { it('command line argument', function() {
require(extensionsPath); var sass = require(extensionsPath);
var URL = 'http://aaa.example.com:9999'; var URL = 'http://aaa.example.com:9999';
assert.equal(process.sass.binaryUrl.substr(0, URL.length), URL); assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
}); });
it('environment variable', function() { it('environment variable', function() {
process.argv = []; process.argv = [];
require(extensionsPath); var sass = require(extensionsPath);
var URL = 'http://bbb.example.com:8888'; var URL = 'http://bbb.example.com:8888';
assert.equal(process.sass.binaryUrl.substr(0, URL.length), URL); assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
}); });
it('npm config variable', function() { it('npm config variable', function() {
process.argv = []; process.argv = [];
process.env.SASS_BINARY_SITE = null; process.env.SASS_BINARY_SITE = null;
require(extensionsPath); var sass = require(extensionsPath);
var URL = 'http://ccc.example.com:7777'; var URL = 'http://ccc.example.com:7777';
assert.equal(process.sass.binaryUrl.substr(0, URL.length), URL); assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
}); });
it('package.json', function() { it('package.json', function() {
process.argv = []; process.argv = [];
process.env.SASS_BINARY_SITE = null; process.env.SASS_BINARY_SITE = null;
process.env.npm_config_sass_binary_site = null; process.env.npm_config_sass_binary_site = null;
require(extensionsPath); var sass = require(extensionsPath);
var URL = 'http://ddd.example.com:6666'; var URL = 'http://ddd.example.com:6666';
assert.equal(process.sass.binaryUrl.substr(0, URL.length), URL); assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
}); });
}); });
...@@ -109,29 +108,29 @@ describe('runtime parameters', function() { ...@@ -109,29 +108,29 @@ describe('runtime parameters', function() {
}); });
it('command line argument', function() { it('command line argument', function() {
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryPath, 'aaa_binding.node'); assert.equal(sass.getBinaryPath(), 'aaa_binding.node');
}); });
it('environment variable', function() { it('environment variable', function() {
process.argv = []; process.argv = [];
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryPath, 'bbb_binding.node'); assert.equal(sass.getBinaryPath(), 'bbb_binding.node');
}); });
it('npm config variable', function() { it('npm config variable', function() {
process.argv = []; process.argv = [];
process.env.SASS_BINARY_PATH = null; process.env.SASS_BINARY_PATH = null;
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryPath, 'ccc_binding.node'); assert.equal(sass.getBinaryPath(), 'ccc_binding.node');
}); });
it('package.json', function() { it('package.json', function() {
process.argv = []; process.argv = [];
process.env.SASS_BINARY_PATH = null; process.env.SASS_BINARY_PATH = null;
process.env.npm_config_sass_binary_path = null; process.env.npm_config_sass_binary_path = null;
require(extensionsPath); var sass = require(extensionsPath);
assert.equal(process.sass.binaryPath, 'ddd_binding.node'); assert.equal(sass.getBinaryPath(), 'ddd_binding.node');
}); });
}); });
...@@ -140,12 +139,13 @@ describe('runtime parameters', function() { ...@@ -140,12 +139,13 @@ describe('runtime parameters', function() {
describe('library detection', function() { describe('library detection', function() {
it('should throw error when libsass binary is missing.', function() { it('should throw error when libsass binary is missing.', function() {
var originalBin = process.sass.binaryPath, var sass = require(extensionsPath),
originalBin = sass.getBinaryPath(),
renamedBin = [originalBin, '_moved'].join(''); renamedBin = [originalBin, '_moved'].join('');
assert.throws(function() { assert.throws(function() {
fs.renameSync(originalBin, renamedBin); fs.renameSync(originalBin, renamedBin);
process.sass.getBinaryPath(true); sass.getBinaryPath(true);
}, /The `libsass` binding was not found/); }, /The `libsass` binding was not found/);
fs.renameSync(renamedBin, originalBin); fs.renameSync(renamedBin, originalBin);
......
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