forked from teltploek/gulp-jade-find-affected
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (78 loc) · 2.8 KB
/
index.js
File metadata and controls
99 lines (78 loc) · 2.8 KB
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
'use strict';
var vfs = require('vinyl-fs');
var fs = require('fs');
var glob = require('glob');
var gs = require('glob-stream');
var gutil = require('gulp-util');
var _ = require('lodash');
var path = require('path');
var through = require('through2');
var map = require('map-stream');
var File = require('vinyl');
var foundFiles = [];
function findAffectedRecurse(filePath, filesBase, cb) {
if (typeof filePath === 'object') filePath = filePath.path;
var file = new File({
path: filePath
});
var changedFile = path.relative(filesBase, file.path).split('.jade')[0];
var filesPath = path.join(filesBase, '**/*.jade');
glob(filesPath , {}, function (er, files) {
_.each(files, function(path, i) {
var jadeFile = fs.readFileSync(path, 'utf8').replace(/\r\n|\r/g, '\n');
var testfile = changedFile.replace(/\\/g, '/');
var patterns = [];
patterns.push(new RegExp('include (?:\.\.\/)?('+testfile+')'));
patterns.push(new RegExp('include (?:\.\.\/\.\.\/)?('+testfile+')'));
patterns.push(new RegExp('extends (?:\.\.\/)?('+testfile+')'));
patterns.push(new RegExp('extends (?:\.\.\/\.\.\/)?('+testfile+')'));
var res =
patterns[0].test(jadeFile) ||
patterns[1].test(jadeFile) ||
patterns[2].test(jadeFile) ||
patterns[3].test(jadeFile);
// let's map out the paths we've found in where the changed file will affect changes
var foundPaths = _.map(foundFiles, 'path');
if (res && _.indexOf(foundPaths, path) === -1) {
foundFiles.unshift({
path : path,
content : jadeFile
});
findAffectedRecurse(foundFiles[0].path, filesBase, cb);
}
});
});
cb(foundFiles);
}
function logEvent(filepathAffected, filePathChanged) {
var msg = [gutil.colors.magenta(filePathChanged), 'was affected by the change of', gutil.colors.magenta(filepathAffected), 'and will be compiled.'];
gutil.log.apply(gutil, msg);
}
module.exports = function(){
function FindAffected(file, enc, cb){
foundFiles = [];
var base = path.resolve(file.cwd, file.base);
var that = this;
// now find files that were affected by the change
findAffectedRecurse(file, base, function(affectedFiles) {
_.each(affectedFiles, function(affectedFile) {
that.push(new File({
base: base,
path: affectedFile.path,
contents: new Buffer(affectedFile.content)
}));
// log event to the screen
logEvent(path.basename(file.path), path.relative(base, affectedFile.path));
});
});
// also compile yourself a long with the affected files
this.push(new File({
base: base,
path: file.path,
contents: file._contents
}))
return cb();
}
return through.obj(FindAffected);
};