-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
57 lines (46 loc) · 1.38 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
/*
* broccoli-replace
*
* Copyright (c) 2014 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/broccoli-replace/blob/master/LICENSE-MIT
*/
'use strict';
// dependencies
var Filter = require('broccoli-filter');
var minimatch = require("minimatch");
var Applause = require('applause');
function ReplaceFilter (inputTree, options) {
if (!(this instanceof ReplaceFilter)) {
return new ReplaceFilter(inputTree, options);
}
Filter.call(this, inputTree, options);
this.inputTree = inputTree;
this.options = options || {};
this.files = this.options.files;
this.applause = Applause.create(options);
}
ReplaceFilter.prototype = Object.create(Filter.prototype);
ReplaceFilter.prototype.constructor = ReplaceFilter;
ReplaceFilter.prototype.getDestFilePath = function (relativePath) {
var files = this.options.files || [];
for (var i = 0; i < files.length; i++) {
var pattern = files[i];
var match = minimatch(relativePath, pattern);
/* console.log('>>> getDestFilePath: %s, pattern: %s, match: %s', relativePath,
pattern, match); */
if (match === true) {
return relativePath;
}
}
return null;
};
ReplaceFilter.prototype.processString = function (string) {
var result = this.applause.replace(string);
if (result === false) {
// no replacements
return string;
}
return result;
};
module.exports = ReplaceFilter;