-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
111 lines (72 loc) · 2.3 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
'use strict';
const
sassGraph = require('sass-graph'),
through = require('through2'),
vinylFile = require('vinyl-file'),
isWin = /^win/.test(process.platform);
const path = require('path');
function sass_partials_imported(scss_dir, loadPaths) {
scss_dir = scss_dir || './';
loadPaths = loadPaths
? Array.isArray(loadPaths)
? loadPaths
: [loadPaths]
: [scss_dir];
let graph = sassGraph.parseDir(scss_dir, { loadPaths: loadPaths }),
processedFiles = [];
return through.obj(function (file, enc, cb) {
let file_path = file.path,
files_to_sass = checkFiles(file_path, scss_dir, graph),
files = createVinylFileArray(files_to_sass, scss_dir);
if (files_to_sass.length > 0) {
files.forEach( f => {
if (processedFiles.indexOf(f.path) === -1) {
this.push(f);
processedFiles.push(f.path);
}
});
}
cb();
});
}
function checkFiles(file, project_path, graph) {
let files_to_sass = [],
file_path = isWin ? file.replace(/\//g, '\\') : file;
files_to_sass = getSassFileToUpdate(file_path, graph);
files_to_sass.sort();
return files_to_sass;
}
function getSassFileToUpdate(file_path, graph, files) {
files = files || [];
try {
if (!isPartial(file_path)) {
if (files.indexOf(file_path) === -1) {
files.push(file_path);
}
}
if (graph.index[file_path].importedBy.length > 0) {
// console.log("Parse", graph.index[file_path].importedBy);
graph.index[file_path].importedBy.forEach(function (file_path) {
getSassFileToUpdate(file_path, graph, files);
});
return files;
}
return files;
} catch(e) {
return [];
}
}
function isPartial(file_path) {
return path.basename(file_path).indexOf('_') === 0;
}
function createVinylFileArray(files, base) {
let filesArray = [];
files.forEach( value => {
let f = vinylFile.readSync(value, {
base: base
});
filesArray.push(f);
});
return filesArray;
}
module.exports = sass_partials_imported;