forked from sindresorhus/gulp-ruby-sass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (107 loc) · 3.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'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), next);
});
}, cb);
});
}
module.exports = function (options) {
var compileDir = '_14139e58-9ebe-4c0f-beca-73a65bb01ce9';
var procDir = process.cwd();
options = options || {};
options.cacheLocation = options.cacheLocation || path.join(procDir, '.sass-cache');
options.update = '.:' + compileDir;
var args = dargs(options, ['bundleExec', 'watch', 'poll', 'sourcemapPath']);
var command;
// Error handling
var noLogMatcher = /execvp\(\): No such file or directory|spawn ENOENT/;
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`.');
if (options.bundleExec) {
command = 'bundle';
args.unshift('exec', 'sass');
} else {
command = 'sass';
}
return intermediate({
output: compileDir,
container: 'gulp-ruby-sass'
}, function (tempDir, cb, fileProps) {
if (process.argv.indexOf('--verbose') !== -1) {
gutil.log('gulp-ruby-sass:', 'Running command:',
chalk.blue(command, args.join(' ')));
}
var sass = spawn(command, args, {cwd: tempDir});
sass.stdout.setEncoding('utf8');
sass.stderr.setEncoding('utf8');
sass.stdout.on('data', function (data) {
var msg = data.trim();
if (bundleErrMatcher.test(msg)) {
gutil.log('gulp-ruby-sass:', bundleErr);
} else {
gutil.log('gulp-ruby-sass:', msg.replace(new RegExp(compileDir, 'g'), ''));
}
});
sass.stderr.on('data', function (data) {
var msg = data.trim();
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.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();
}
});
});
};