-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmap.js
80 lines (71 loc) · 1.88 KB
/
map.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';
const Filter = require('broccoli-persistent-filter');
const minimatch = require('minimatch');
const debug = require('debug')('broccoli-stew:map');
/**
* maps files, allow for simple content mutation
*
* @example
*
* const map = require('broccoli-stew').map;
*
* dist = map('lib', function(content, relativePath) {
* return 'new content';
* });
*
* dist = map('lib', function(content, relativePath) {
* return 'prepend' + content;
* });
*
* dist = map('lib', function(content, relativePath) {
* return mutateSomehow(content);
* });
*
* dist = map('lib', '*.js', function(content, relativePath) {
* // mutate only files that match *.js
* // leave the rest alone
* return mutateSomehow(content);
* });
*
*/
module.exports = Mapper;
function Mapper(inputTree, _filter, _fn) {
if (!(this instanceof Mapper)) {
return new Mapper(inputTree, _filter, _fn);
}
Filter.call(this, inputTree);
if (typeof _filter === 'function') {
this.fn = _filter;
this.filter = false;
} else {
this.filter = _filter;
this.fn = _fn;
}
this._matches = Object.create(null);
}
Mapper.prototype = Object.create(Filter.prototype);
Mapper.prototype.constructor = Mapper;
Mapper.prototype.canProcessFile = function(relativePath) {
if (this.filter) {
let can = this.match(relativePath);
debug('canProcessFile with filter: %s, %o', relativePath, can);
return can;
}
debug('canProcessFile', relativePath);
return true;
}
Mapper.prototype.getDestFilePath = function(path) {
return path;
};
Mapper.prototype.match = function(path) {
let cache = this._matches[path];
if (cache === undefined) {
return this._matches[path] = minimatch(path, this.filter);
} else {
return cache;
}
};
Mapper.prototype.processString = function (string, relativePath) {
debug('mapping: %s', relativePath);
return this.fn(string, relativePath);
};