Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Created July 20, 2014 08:56
Show Gist options
  • Save samarpanda/1479f2e41d177a82a18e to your computer and use it in GitHub Desktop.
Save samarpanda/1479f2e41d177a82a18e to your computer and use it in GitHub Desktop.
Debugging an issue with gulp-ruby-sass. Index.js
'use strict';
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var dargs = require('dargs');
var slash = require('slash');
var gutil = require('gulp-util');
var spawn = require('win-spawn');
var eachAsync = require('each-async');
var intermediate = require('gulp-intermediate');
function rewriteSourcemapPaths (cssDir, relPath, cb) {
var glob = require('glob');
glob(path.join(cssDir, '**/*.map'), function (err, files) {
if (err) {
cb(err);
return;
}
eachAsync(files, function (file, i, next) {
fs.readFile(file, function (err, data) {
if (err) {
next(err);
return;
}
var sourceMap = JSON.parse(data);
var stepUp = path.relative(path.dirname(file), cssDir);
// rewrite sourcemaps to point to the original source files
sourceMap.sources = sourceMap.sources.map(function (source) {
var sourceBase = source.replace(/\.\.\//g, '');
// normalize to browser style paths if we're on windows
return slash(path.join(stepUp, relPath, sourceBase));
});
fs.writeFile(file, JSON.stringify(sourceMap, null, ' '), next);
});
}, cb);
});
}
module.exports = function (options) {
var compileDir = '_14139e58-9ebe-4c0f-beca-73a65bb01ce9';
var procDir = process.cwd();
// error handling
var noLogMatcher = /execvp\(\): No such file or directory/;
var bundleErrMatcher = /bundler: command not found|Could not find gem/;
var bundleErr = chalk.red('Gemfile version of Sass not found. Install missing gems with `bundle install`.');
// console.log(intermediate.toString());
return intermediate({
output: compileDir,
container: 'gulp-ruby-sass'
}, function (tempDir, cb, vinylFiles) {
if (process.argv.indexOf('--verbose') !== -1) {
gutil.log('gulp-ruby-sass:', 'Running command:',
chalk.blue(command, args.join(' ')));
}
options = options || {};
options.cacheLocation = options.cacheLocation || path.join(procDir, '.sass-cache');
options.update = tempDir + ':' + path.join(tempDir, compileDir);
options.loadPath = typeof options.loadPath === 'undefined' ? [] : [].concat(options.loadPath);
// add loadPaths for each temp file
vinylFiles.forEach(function (file) {
var relativeLoadPath = path.dirname(path.relative(procDir, file.path));
if (options.loadPath.indexOf(relativeLoadPath) === -1) {
options.loadPath.push(relativeLoadPath);
}
});
// TEMP: Sass error? Normalize loadPaths to local separators so sass
// staleness_checker doesn't get a mixed separator path.
options.loadPath = options.loadPath.map(function (loadPath) {
gutil.log('gulp-ruby-sass: loadpath ', loadPath);
gutil.log('gulp-ruby-sass: retun value ', loadPath.replace(/\//g, path.sep));
return loadPath.replace(/\//g, path.sep);
});
var args = dargs(options, ['bundleExec', 'watch', 'poll', 'sourcemapPath']);
gutil.log('args: ', args.toString());
var command;
if (options.bundleExec) {
command = 'bundle';
args.unshift('exec', 'sass');
} else {
command = 'sass';
}
gutil.log('command: ',command);
var sass = spawn(command, args);
sass.stdout.setEncoding('utf8');
sass.stderr.setEncoding('utf8');
sass.stdout.on('data', function (data) {
var compileDirMatcher = new RegExp(slash(path.join(tempDir, compileDir)) + '/?', 'g');
var msg = data.trim().replace(compileDirMatcher, '');
if (bundleErrMatcher.test(msg)) {
gutil.log('gulp-ruby-sass:', bundleErr);
} else {
gutil.log('gulp-ruby-sass:', msg);
}
});
sass.stderr.on('data', function (data) {
var intermediateDirMatcher = new RegExp(slash(tempDir) + '/?', 'g');
var msg = data.trim().replace(intermediateDirMatcher, '');
if (bundleErrMatcher.test(msg)) {
gutil.log('gulp-ruby-sass:', bundleErr);
} else if (!noLogMatcher.test(msg)) {
gutil.log('gulp-ruby-sass:', msg);
}
});
sass.on('error', function (err) {
var msg = err.message ? err.message.trim() : err.trim();
if (!noLogMatcher.test(msg)) {
gutil.log('gulp-ruby-sass:', chalk.red(msg));
}
});
sass.on('close', function (code) {
var dependencies = options.bundleExec ? 'Ruby, Bundler, and Sass' : 'Ruby and Sass';
if (code === -1) {
gutil.log('gulp-ruby-sass:', chalk.red('Missing dependencies. ' + dependencies + ' must be installed and available.'));
return cb();
}
if (options.sourcemap && options.sourcemapPath) {
var cssDir = path.join(tempDir, compileDir);
rewriteSourcemapPaths(cssDir, options.sourcemapPath, function (err) {
if (err) {
this.emit('error', new gutil.PluginError('gulp-ruby-sass', err));
}
cb();
}.bind(this));
} else {
cb();
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment