Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 30, 2017
1 parent be9483b commit 1d0044d
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 294 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
yarn.lock
.sass-cache
fixture/maps
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
6 changes: 2 additions & 4 deletions .travis.yml
@@ -1,10 +1,8 @@
sudo: false
language: node_js
node_js:
- '5'
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
before_install:
- gem update --system
- gem install sass
138 changes: 68 additions & 70 deletions index.js
@@ -1,25 +1,24 @@
'use strict';
var fs = require('fs');
var path = require('path');
var Readable = require('stream').Readable;
var assign = require('object-assign');
var convert = require('convert-source-map');
var dargs = require('dargs');
var eachAsync = require('each-async');
var glob = require('glob');
var gutil = require('gulp-util');
var osTmpdir = require('os-tmpdir');
var pathExists = require('path-exists');
var rimraf = require('rimraf');
var spawn = require('cross-spawn');
var logger = require('./lib/logger');
var utils = require('./lib/utils');

var emitErr = utils.emitErr;
var replaceLocation = utils.replaceLocation;
var createIntermediatePath = utils.createIntermediatePath;

var defaults = {
const fs = require('fs');
const path = require('path');
const Readable = require('stream').Readable;
const convert = require('convert-source-map');
const dargs = require('dargs');
const eachAsync = require('each-async');
const glob = require('glob');
const gutil = require('gulp-util');
const osTmpdir = require('os-tmpdir');
const pathExists = require('path-exists');
const rimraf = require('rimraf');
const spawn = require('cross-spawn');
const logger = require('./lib/logger');
const utils = require('./lib/utils');

const emitErr = utils.emitErr;
const replaceLocation = utils.replaceLocation;
const createIntermediatePath = utils.createIntermediatePath;

const defaults = {
tempDir: path.join(osTmpdir(), 'gulp-ruby-sass'),
verbose: false,
sourcemap: false,
Expand All @@ -31,78 +30,78 @@ if (typeof process.getuid === 'function') {
}

function gulpRubySass(sources, options) {
var stream = new Readable({objectMode: true});
const stream = new Readable({objectMode: true});

// redundant but necessary
// Redundant but necessary
stream._read = function () {};

options = assign({}, defaults, options);
options = Object.assign({}, defaults, options);

// alert user that `container` is deprecated
// Alert user that `container` is deprecated
if (options.container) {
gutil.log(gutil.colors.yellow('The container option has been deprecated. Simultaneous tasks work automatically now!'));
}

// error if user tries to watch their files with the Sass gem
// Error if user tries to watch their files with the Sass gem
if (options.watch || options.poll) {
emitErr(stream, '`watch` and `poll` are not valid options for gulp-ruby-sass. Use `gulp.watch` to rebuild your files on change.');
}

// error if user tries to pass a Sass option to sourcemap
// Error if user tries to pass a Sass option to sourcemap
if (typeof options.sourcemap !== 'boolean') {
emitErr(stream, 'The sourcemap option must be true or false. See the readme for instructions on using Sass sourcemaps with gulp.');
}

options.sourcemap = options.sourcemap === true ? 'file' : 'none';
options.update = true;

// simplified handling of array sources, like gulp.src
// Simplified handling of array sources, like gulp.src
if (!Array.isArray(sources)) {
sources = [sources];
}

var matches = [];
var bases = [];
const matches = [];
const bases = [];

sources.forEach(function (source) {
sources.forEach(source => {
matches.push(glob.sync(source));
bases.push(options.base || utils.calculateBase(source));
});

// log and return stream if there are no file matches
// Log and return stream if there are no file matches
if (matches[0].length < 1) {
gutil.log('No files matched your Sass source.');
stream.push(null);
return stream;
}

var intermediateDir = createIntermediatePath(sources, matches, options);
var compileMappings = [];
var baseMappings = {};
const intermediateDir = createIntermediatePath(sources, matches, options);
const compileMappings = [];
const baseMappings = {};

matches.forEach(function (matchArray, i) {
var base = bases[i];
matches.forEach((matchArray, i) => {
const base = bases[i];

matchArray.filter(function (match) {
// remove _partials
matchArray.filter(match => {
// Remove _partials
return path.basename(match).indexOf('_') !== 0;
})
.forEach(function (match) {
var dest = gutil.replaceExtension(
.forEach(match => {
const dest = gutil.replaceExtension(
replaceLocation(match, base, intermediateDir),
'.css'
);
var relative = path.relative(intermediateDir, dest);
const relative = path.relative(intermediateDir, dest);

// source:dest mappings for the Sass CLI
// Source:dest mappings for the Sass CLI
compileMappings.push(match + ':' + dest);

// store base values by relative file path
// Store base values by relative file path
baseMappings[relative] = base;
});
});

var args = dargs(options, [
const args = dargs(options, [
'bundleExec',
'watch',
'poll',
Expand All @@ -113,7 +112,7 @@ function gulpRubySass(sources, options) {
'container'
]).concat(compileMappings);

var command;
let command;

if (options.bundleExec) {
command = 'bundle';
Expand All @@ -123,71 +122,71 @@ function gulpRubySass(sources, options) {
command = 'sass';
}

// plugin logging
// Plugin logging
if (options.verbose) {
logger.verbose(command, args);
}

var sass = spawn(command, args);
const sass = spawn(command, args);

sass.stdout.setEncoding('utf8');
sass.stderr.setEncoding('utf8');

sass.stdout.on('data', function (data) {
sass.stdout.on('data', data => {
logger.stdout(stream, intermediateDir, data);
});

sass.stderr.on('data', function (data) {
sass.stderr.on('data', data => {
logger.stderr(stream, intermediateDir, data);
});

sass.on('error', function (err) {
sass.on('error', err => {
logger.error(stream, err);
});

sass.on('close', function (code) {
sass.on('close', code => {
if (options.emitCompileError && code !== 0) {
emitErr(stream, 'Sass compilation failed. See console output for more information.');
}

glob(path.join(intermediateDir, '**', '*'), function (err, files) {
glob(path.join(intermediateDir, '**', '*'), (err, files) => {
if (err) {
emitErr(stream, err);
}

eachAsync(files, function (file, i, next) {
eachAsync(files, (file, i, next) => {
if (fs.statSync(file).isDirectory() || path.extname(file) === '.map') {
next();
return;
}

var relative = path.relative(intermediateDir, file);
var base = baseMappings[relative];
const relative = path.relative(intermediateDir, file);
const base = baseMappings[relative];

fs.readFile(file, function (err, data) {
fs.readFile(file, (err, data) => {
if (err) {
emitErr(stream, err);
next();
return;
}

// rewrite file paths so gulp thinks the file came from cwd, not the
// Rewrite file paths so gulp thinks the file came from cwd, not the
// intermediate directory
var vinylFile = new gutil.File({
const vinylFile = new gutil.File({
cwd: process.cwd(),
base: base,
base,
path: replaceLocation(file, intermediateDir, base)
});

// sourcemap integration
// Sourcemap integration
if (options.sourcemap === 'file' && pathExists.sync(file + '.map')) {
// remove sourcemap comment; gulp-sourcemaps will add it back in
data = new Buffer(convert.removeMapFileComments(data.toString()));
var sourceMapObject = JSON.parse(fs.readFileSync(file + '.map', 'utf8'));
// Remove sourcemap comment; gulp-sourcemaps will add it back in
data = Buffer.from(convert.removeMapFileComments(data.toString()));
const sourceMapObject = JSON.parse(fs.readFileSync(file + '.map', 'utf8'));

// create relative paths for sources
sourceMapObject.sources = sourceMapObject.sources.map(function (sourcePath) {
var absoluteSourcePath = decodeURI(path.resolve(
// Create relative paths for sources
sourceMapObject.sources = sourceMapObject.sources.map(sourcePath => {
const absoluteSourcePath = decodeURI(path.resolve(
'/',
sourcePath.replace('file:///', '')
));
Expand All @@ -200,9 +199,8 @@ function gulpRubySass(sources, options) {
vinylFile.contents = data;
stream.push(vinylFile);
next();
return;
});
}, function () {
}, () => {
stream.push(null);
});
});
Expand All @@ -212,7 +210,7 @@ function gulpRubySass(sources, options) {
}

gulpRubySass.logError = function (err) {
var message = new gutil.PluginError('gulp-ruby-sass', err);
const message = new gutil.PluginError('gulp-ruby-sass', err);
process.stderr.write(message + '\n');
this.emit('end');
};
Expand Down
14 changes: 7 additions & 7 deletions lib/logger.js
@@ -1,13 +1,13 @@
'use strict';
var gutil = require('gulp-util');
var escapeStringRegexp = require('escape-string-regexp');
var emitErr = require('./utils').emitErr;
const gutil = require('gulp-util');
const escapeStringRegexp = require('escape-string-regexp');
const emitErr = require('./utils').emitErr;

var logger = {};
const logger = {};

// Remove intermediate directory for more Sass-like logging
logger.prettifyDirectoryLogging = function (msg, intermediateDir) {
var escapedDir = escapeStringRegexp(intermediateDir);
const escapedDir = escapeStringRegexp(intermediateDir);
return msg.replace(new RegExp(escapedDir + '/?', 'g'), './');
};

Expand Down Expand Up @@ -40,8 +40,8 @@ logger.stdout = function (stream, intermediateDir, data) {
};

logger.stderr = function (stream, intermediateDir, data) {
var bundlerMissing = /Could not find 'bundler' \((.*?)\)/.exec(data);
var sassVersionMissing = /Could not find gem 'sass \((.*?)\) ruby'/.exec(data);
const bundlerMissing = /Could not find 'bundler' \((.*?)\)/.exec(data);
const sassVersionMissing = /Could not find gem 'sass \((.*?)\) ruby'/.exec(data);

// Ruby error: Bundler gem not installed
if (bundlerMissing) {
Expand Down
10 changes: 5 additions & 5 deletions lib/utils.js
@@ -1,9 +1,9 @@
'use strict';
var path = require('path');
var glob = require('glob');
var glob2base = require('glob2base');
var gutil = require('gulp-util');
var md5Hex = require('md5-hex');
const path = require('path');
const glob = require('glob');
const glob2base = require('glob2base');
const gutil = require('gulp-util');
const md5Hex = require('md5-hex');

exports.emitErr = function (stream, err) {
stream.emit('error', new gutil.PluginError('gulp-ruby-sass', err));
Expand Down
20 changes: 4 additions & 16 deletions license
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 1d0044d

Please sign in to comment.