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

Commit 41db8b9

Browse files
committed
Merge pull request #1427 from xzyfer/feat/remove-process-sass
Remove use of global process.sass
2 parents b0df78d + 90b6939 commit 41db8b9

File tree

8 files changed

+118
-115
lines changed

8 files changed

+118
-115
lines changed

bin/node-sass

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Emitter = require('events').EventEmitter,
88
util = require('util'),
99
path = require('path'),
1010
glob = require('glob'),
11+
sass = require('../lib'),
1112
render = require('../lib/render'),
1213
stdin = require('get-stdin'),
1314
fs = require('fs');
@@ -18,7 +19,7 @@ var Emitter = require('events').EventEmitter,
1819

1920
var cli = meow({
2021
pkg: '../package.json',
21-
version: process.sass.versionInfo,
22+
version: sass.info,
2223
help: [
2324
'Usage:',
2425
' node-sass [options] <input.scss>',

lib/extensions.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22
* node-sass: lib/extensions.js
33
*/
44

5-
var flags = {},
5+
var eol = require('os').EOL,
66
fs = require('fs'),
77
pkg = require('../package.json'),
88
path = require('path');
99

1010
/**
11-
* Collect Arguments
11+
* Get the value of a CLI argument
1212
*
13+
* @param {String} name
1314
* @param {Array} args
1415
* @api private
1516
*/
1617

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

24-
flags[args[i]] = args[i + 1];
22+
if (index === -1 || index + 1 >= flags.length) {
23+
return null;
2524
}
25+
26+
return flags[index + 1];
2627
}
2728

2829
/**
@@ -33,14 +34,14 @@ function collectArguments(args) {
3334
* return it as is, otherwise make default binary
3435
* name: {platform}-{arch}-{v8 version}.node
3536
*
36-
* @api private
37+
* @api public
3738
*/
3839

3940
function getBinaryName() {
4041
var binaryName;
4142

42-
if (flags['--sass-binary-name']) {
43-
binaryName = flags['--sass-binary-name'];
43+
if (getArgument('--sass-binary-name')) {
44+
binaryName = getArgument('--sass-binary-name');
4445
} else if (process.env.SASS_BINARY_NAME) {
4546
binaryName = process.env.SASS_BINARY_NAME;
4647
} else if (process.env.npm_config_sass_binary_name) {
@@ -63,7 +64,7 @@ function getBinaryName() {
6364
*
6465
* The default URL can be overriden using
6566
* the environment variable SASS_BINARY_SITE,
66-
* .npmrc variable sass_binary_site or
67+
* .npmrc variable sass_binary_site or
6768
* or a command line option --sass-binary-site:
6869
*
6970
* node scripts/install.js --sass-binary-site http://example.com/
@@ -81,27 +82,19 @@ function getBinaryName() {
8182
* v3.0.0/freebsd-x64-42_binding.node
8283
* ... etc. for all supported versions and platforms
8384
*
84-
* @api private
85+
* @api public
8586
*/
8687

8788
function getBinaryUrl() {
88-
var site = flags['--sass-binary-site'] ||
89+
var site = getArgument('--sass-binary-site') ||
8990
process.env.SASS_BINARY_SITE ||
9091
process.env.npm_config_sass_binary_site ||
9192
(pkg.nodeSassConfig && pkg.nodeSassConfig.binarySite) ||
9293
'https://github.com/sass/node-sass/releases/download';
9394

94-
return [site, 'v' + pkg.version, sass.binaryName].join('/');
95+
return [site, 'v' + pkg.version, getBinaryName()].join('/');
9596
}
9697

97-
98-
collectArguments(process.argv.slice(2));
99-
100-
var sass = process.sass = {};
101-
102-
sass.binaryName = getBinaryName();
103-
sass.binaryUrl = getBinaryUrl();
104-
10598
/**
10699
* Get binary path.
107100
* If environment variable SASS_BINARY_PATH,
@@ -114,22 +107,22 @@ sass.binaryUrl = getBinaryUrl();
114107
* returning.
115108
*
116109
* @param {Boolean} throwIfNotExists
117-
* @api private
110+
* @api public
118111
*/
119112

120-
sass.getBinaryPath = function(throwIfNotExists) {
113+
function getBinaryPath(throwIfNotExists) {
121114
var binaryPath;
122115

123-
if (flags['--sass-binary-path']) {
124-
binaryPath = flags['--sass-binary-path'];
116+
if (getArgument('--sass-binary-path')) {
117+
binaryPath = getArgument('--sass-binary-path');
125118
} else if (process.env.SASS_BINARY_PATH) {
126119
binaryPath = process.env.SASS_BINARY_PATH;
127120
} else if (process.env.npm_config_sass_binary_path) {
128121
binaryPath = process.env.npm_config_sass_binary_path;
129122
} else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) {
130123
binaryPath = pkg.nodeSassConfig.binaryPath;
131124
} else {
132-
binaryPath = path.join(__dirname, '..', 'vendor', sass.binaryName.replace(/_/, '/'));
125+
binaryPath = path.join(__dirname, '..', 'vendor', getBinaryName().replace(/_/, '/'));
133126
}
134127

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

143136
return binaryPath;
144-
};
137+
}
138+
139+
/**
140+
* Get Sass version information
141+
*
142+
* @api public
143+
*/
144+
145+
function getVersionInfo(binding) {
146+
return [
147+
['node-sass', pkg.version, '(Wrapper)', '[JavaScript]'].join('\t'),
148+
['libsass ', binding.libsassVersion(), '(Sass Compiler)', '[C/C++]'].join('\t'),
149+
].join(eol);
150+
}
145151

146-
sass.binaryPath = sass.getBinaryPath();
152+
module.exports.getBinaryUrl = getBinaryUrl;
153+
module.exports.getBinaryName = getBinaryName;
154+
module.exports.getBinaryPath = getBinaryPath;
155+
module.exports.getVersionInfo = getVersionInfo;

lib/index.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,15 @@
22
* node-sass: lib/index.js
33
*/
44

5-
var eol = require('os').EOL,
6-
path = require('path'),
5+
var path = require('path'),
76
util = require('util'),
8-
pkg = require('../package.json');
9-
10-
require('./extensions');
7+
sass = require('./extensions');
118

129
/**
1310
* Require binding
1411
*/
1512

16-
var binding = require(process.sass.getBinaryPath(true));
17-
18-
/**
19-
* Get Sass version information
20-
*
21-
* @api private
22-
*/
23-
24-
function getVersionInfo(binding) {
25-
return [
26-
['node-sass', pkg.version, '(Wrapper)', '[JavaScript]'].join('\t'),
27-
['libsass ', binding.libsassVersion(), '(Sass Compiler)', '[C/C++]'].join('\t'),
28-
].join(eol);
29-
}
13+
var binding = require(sass.getBinaryPath(true));
3014

3115
/**
3216
* Get input file
@@ -430,8 +414,7 @@ module.exports.renderSync = function(options) {
430414
* @api public
431415
*/
432416

433-
process.sass.versionInfo = getVersionInfo(binding);
434-
module.exports.info = process.sass.versionInfo;
417+
module.exports.info = sass.getVersionInfo(binding);
435418

436419
/**
437420
* Expose sass types
@@ -441,3 +424,17 @@ module.exports.types = binding.types;
441424
module.exports.TRUE = binding.types.Boolean.TRUE;
442425
module.exports.FALSE = binding.types.Boolean.FALSE;
443426
module.exports.NULL = binding.types.Null.NULL;
427+
428+
/**
429+
* Polyfill the old API
430+
*
431+
* TODO: remove for 4.0
432+
*/
433+
434+
process.sass = process.sass || {
435+
versionInfo: module.exports.info,
436+
binaryName: sass.getBinaryName(),
437+
binaryUrl: sass.getBinaryUrl(),
438+
binaryPath: sass.getBinaryPath(),
439+
getBinaryPath: sass.getBinaryPath,
440+
};

scripts/build.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ var eol = require('os').EOL,
77
fs = require('fs'),
88
mkdir = require('mkdirp'),
99
path = require('path'),
10-
spawn = require('cross-spawn-async');
11-
12-
require('../lib/extensions');
10+
spawn = require('cross-spawn-async'),
11+
sass = require('../lib/extensions');
1312

1413
/**
1514
* After build
@@ -19,7 +18,7 @@ require('../lib/extensions');
1918
*/
2019

2120
function afterBuild(options) {
22-
var install = process.sass.binaryPath;
21+
var install = sass.getBinaryPath();
2322
var target = path.join(__dirname, '..', 'build',
2423
options.debug ? 'Debug' : process.config.target_defaults.default_configuration,
2524
'binding.node');
@@ -197,13 +196,11 @@ function testBinary(options) {
197196
return build(options);
198197
}
199198

200-
try {
201-
process.sass.getBinaryPath(true);
202-
} catch (e) {
199+
if (!sass.getBinaryPath()) {
203200
return build(options);
204201
}
205202

206-
console.log('`', process.sass.binaryPath, '` exists.', eol, 'testing binary.');
203+
console.log('`', sass.getBinaryPath(), '` exists.', eol, 'testing binary.');
207204

208205
try {
209206
require('../').renderSync({

scripts/install.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ var fs = require('fs'),
77
mkdir = require('mkdirp'),
88
path = require('path'),
99
got = require('got'),
10-
pkg = require('../package.json');
11-
12-
require('../lib/extensions');
10+
pkg = require('../package.json'),
11+
sass = require('../lib/extensions');
1312

1413
/**
1514
* Download file, if succeeds save, if not delete
@@ -82,24 +81,23 @@ function applyProxy(options, cb) {
8281
*/
8382

8483
function checkAndDownloadBinary() {
85-
try {
86-
process.sass.getBinaryPath(true);
84+
if (sass.getBinaryPath()) {
8785
return;
88-
} catch (e) { }
86+
}
8987

90-
mkdir(path.dirname(process.sass.binaryPath), function(err) {
88+
mkdir(path.dirname(sass.getBinaryPath()), function(err) {
9189
if (err) {
9290
console.error(err);
9391
return;
9492
}
9593

96-
download(process.sass.binaryUrl, process.sass.binaryPath, function(err) {
94+
download(sass.getBinaryUrl(), sass.getBinaryPath(), function(err) {
9795
if (err) {
9896
console.error(err);
9997
return;
10098
}
10199

102-
console.log('Binary downloaded and installed at', process.sass.binaryPath);
100+
console.log('Binary downloaded and installed at', sass.binaryPath());
103101
});
104102
});
105103
}

test/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var assert = require('assert'),
1111
LIBSASS_VERSION = null;
1212

1313
describe('cli', function() {
14-
14+
1515
before(function(done) {
1616
var bin = spawn(cli, ['-v']);
1717
bin.stdout.setEncoding('utf8');
@@ -480,7 +480,7 @@ describe('cli', function() {
480480
'--source-map-embed',
481481
'--source-map', 'true'
482482
]);
483-
483+
484484
bin.stdout.on('data', function(data) {
485485
result += data;
486486
});

0 commit comments

Comments
 (0)