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

Commit

Permalink
Merge pull request #1427 from xzyfer/feat/remove-process-sass
Browse files Browse the repository at this point in the history
Remove use of global process.sass
  • Loading branch information
xzyfer committed Mar 25, 2016
2 parents b0df78d + 90b6939 commit 41db8b9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 115 deletions.
3 changes: 2 additions & 1 deletion bin/node-sass
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Emitter = require('events').EventEmitter,
util = require('util'),
path = require('path'),
glob = require('glob'),
sass = require('../lib'),
render = require('../lib/render'),
stdin = require('get-stdin'),
fs = require('fs');
Expand All @@ -18,7 +19,7 @@ var Emitter = require('events').EventEmitter,

var cli = meow({
pkg: '../package.json',
version: process.sass.versionInfo,
version: sass.info,
help: [
'Usage:',
' node-sass [options] <input.scss>',
Expand Down
71 changes: 40 additions & 31 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
* node-sass: lib/extensions.js
*/

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

/**
* Collect Arguments
* Get the value of a CLI argument
*
* @param {String} name
* @param {Array} args
* @api private
*/

function collectArguments(args) {
for (var i = 0; i < args.length; i += 2) {
if (args[i].lastIndexOf('--', 0) !== 0) {
--i;
continue;
}
function getArgument(name, args) {
var flags = args || process.argv.slice(2),
index = flags.lastIndexOf(name);

flags[args[i]] = args[i + 1];
if (index === -1 || index + 1 >= flags.length) {
return null;
}

return flags[index + 1];
}

/**
Expand All @@ -33,14 +34,14 @@ function collectArguments(args) {
* return it as is, otherwise make default binary
* name: {platform}-{arch}-{v8 version}.node
*
* @api private
* @api public
*/

function getBinaryName() {
var binaryName;

if (flags['--sass-binary-name']) {
binaryName = flags['--sass-binary-name'];
if (getArgument('--sass-binary-name')) {
binaryName = getArgument('--sass-binary-name');
} else if (process.env.SASS_BINARY_NAME) {
binaryName = process.env.SASS_BINARY_NAME;
} else if (process.env.npm_config_sass_binary_name) {
Expand All @@ -63,7 +64,7 @@ function getBinaryName() {
*
* The default URL can be overriden using
* 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:
*
* node scripts/install.js --sass-binary-site http://example.com/
Expand All @@ -81,27 +82,19 @@ function getBinaryName() {
* v3.0.0/freebsd-x64-42_binding.node
* ... etc. for all supported versions and platforms
*
* @api private
* @api public
*/

function getBinaryUrl() {
var site = flags['--sass-binary-site'] ||
var site = getArgument('--sass-binary-site') ||
process.env.SASS_BINARY_SITE ||
process.env.npm_config_sass_binary_site ||
(pkg.nodeSassConfig && pkg.nodeSassConfig.binarySite) ||
'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.
* If environment variable SASS_BINARY_PATH,
Expand All @@ -114,22 +107,22 @@ sass.binaryUrl = getBinaryUrl();
* returning.
*
* @param {Boolean} throwIfNotExists
* @api private
* @api public
*/

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

if (flags['--sass-binary-path']) {
binaryPath = flags['--sass-binary-path'];
if (getArgument('--sass-binary-path')) {
binaryPath = getArgument('--sass-binary-path');
} else if (process.env.SASS_BINARY_PATH) {
binaryPath = process.env.SASS_BINARY_PATH;
} else if (process.env.npm_config_sass_binary_path) {
binaryPath = process.env.npm_config_sass_binary_path;
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) {
binaryPath = pkg.nodeSassConfig.binaryPath;
} else {
binaryPath = path.join(__dirname, '..', 'vendor', sass.binaryName.replace(/_/, '/'));
binaryPath = path.join(__dirname, '..', 'vendor', getBinaryName().replace(/_/, '/'));
}

if (!fs.existsSync(binaryPath) && throwIfNotExists) {
Expand All @@ -141,6 +134,22 @@ sass.getBinaryPath = function(throwIfNotExists) {
}

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;
39 changes: 18 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@
* node-sass: lib/index.js
*/

var eol = require('os').EOL,
path = require('path'),
var path = require('path'),
util = require('util'),
pkg = require('../package.json');

require('./extensions');
sass = require('./extensions');

/**
* Require binding
*/

var binding = require(process.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);
}
var binding = require(sass.getBinaryPath(true));

/**
* Get input file
Expand Down Expand Up @@ -430,8 +414,7 @@ module.exports.renderSync = function(options) {
* @api public
*/

process.sass.versionInfo = getVersionInfo(binding);
module.exports.info = process.sass.versionInfo;
module.exports.info = sass.getVersionInfo(binding);

/**
* Expose sass types
Expand All @@ -441,3 +424,17 @@ module.exports.types = binding.types;
module.exports.TRUE = binding.types.Boolean.TRUE;
module.exports.FALSE = binding.types.Boolean.FALSE;
module.exports.NULL = binding.types.Null.NULL;

/**
* Polyfill the old API
*
* TODO: remove for 4.0
*/

process.sass = process.sass || {
versionInfo: module.exports.info,
binaryName: sass.getBinaryName(),
binaryUrl: sass.getBinaryUrl(),
binaryPath: sass.getBinaryPath(),
getBinaryPath: sass.getBinaryPath,
};
13 changes: 5 additions & 8 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ var eol = require('os').EOL,
fs = require('fs'),
mkdir = require('mkdirp'),
path = require('path'),
spawn = require('cross-spawn-async');

require('../lib/extensions');
spawn = require('cross-spawn-async'),
sass = require('../lib/extensions');

/**
* After build
Expand All @@ -19,7 +18,7 @@ require('../lib/extensions');
*/

function afterBuild(options) {
var install = process.sass.binaryPath;
var install = sass.getBinaryPath();
var target = path.join(__dirname, '..', 'build',
options.debug ? 'Debug' : process.config.target_defaults.default_configuration,
'binding.node');
Expand Down Expand Up @@ -197,13 +196,11 @@ function testBinary(options) {
return build(options);
}

try {
process.sass.getBinaryPath(true);
} catch (e) {
if (!sass.getBinaryPath()) {
return build(options);
}

console.log('`', process.sass.binaryPath, '` exists.', eol, 'testing binary.');
console.log('`', sass.getBinaryPath(), '` exists.', eol, 'testing binary.');

try {
require('../').renderSync({
Expand Down
16 changes: 7 additions & 9 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ var fs = require('fs'),
mkdir = require('mkdirp'),
path = require('path'),
got = require('got'),
pkg = require('../package.json');

require('../lib/extensions');
pkg = require('../package.json'),
sass = require('../lib/extensions');

/**
* Download file, if succeeds save, if not delete
Expand Down Expand Up @@ -82,24 +81,23 @@ function applyProxy(options, cb) {
*/

function checkAndDownloadBinary() {
try {
process.sass.getBinaryPath(true);
if (sass.getBinaryPath()) {
return;
} catch (e) { }
}

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

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

console.log('Binary downloaded and installed at', process.sass.binaryPath);
console.log('Binary downloaded and installed at', sass.binaryPath());
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var assert = require('assert'),
LIBSASS_VERSION = null;

describe('cli', function() {

before(function(done) {
var bin = spawn(cli, ['-v']);
bin.stdout.setEncoding('utf8');
Expand Down Expand Up @@ -480,7 +480,7 @@ describe('cli', function() {
'--source-map-embed',
'--source-map', 'true'
]);

bin.stdout.on('data', function(data) {
result += data;
});
Expand Down
Loading

0 comments on commit 41db8b9

Please sign in to comment.