-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
80 lines (65 loc) · 1.73 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
'use strict';
var path = require('path');
var es = require('event-stream');
var _ = require('lodash');
var vfs = require('vinyl-fs');
var sassGraph = require('sass-graph');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-better-sass-inheritance';
var stream;
function gulpBetterSassInheritance(options) {
options = options || {};
var files = [];
var filesPaths = [];
var graph;
if (!options.base) {
throw new PluginError(PLUGIN_NAME, 'Missing option `base`!');
}
var basePath = path.resolve(process.cwd(), options.base);
// gutil.log(basePath)
function writeStream(currentFile) {
if (currentFile && currentFile.contents.length) {
files.push(currentFile);
}
}
function check(_filePaths) {
_.forEach(_filePaths, function (filePath) {
filesPaths = _.union(filesPaths, [filePath]);
if (graph.index && graph.index[filePath]) {
var fullpaths = graph.index[filePath].importedBy;
if (options.debug) {
gutil.log('File \"', gutil.colors.magenta(path.relative(basePath, filePath)), '\"');
gutil.log(' - importedBy', fullpaths);
}
filesPaths = _.union(filesPaths, fullpaths);
}
if (fullpaths) {
return check(fullpaths);
}
});
return true;
}
function endStream() {
if (files.length) {
graph = sassGraph.parseDir(options.base, options);
check(_.map(files, function (item) {
return item.path;
}));
vfs.src(filesPaths, {'base': options.base})
.pipe(es.through(
function (f) {
stream.emit('data', f);
},
function () {
stream.emit('end');
}
));
} else {
stream.emit('end');
}
}
stream = es.through(writeStream, endStream);
return stream;
}
module.exports = gulpBetterSassInheritance;