-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
preprocessor.js
139 lines (118 loc) · 3.83 KB
/
preprocessor.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var fs = require('graceful-fs')
var crypto = require('crypto')
var mm = require('minimatch')
var isBinaryFile = require('isbinaryfile')
var combineLists = require('combine-lists')
var log = require('./logger').create('preprocess')
var sha1 = function (data) {
var hash = crypto.createHash('sha1')
hash.update(data)
return hash.digest('hex')
}
var createNextProcessor = function (preprocessors, file, done) {
return function nextPreprocessor (error, content) {
// normalize B-C
if (arguments.length === 1 && typeof error === 'string') {
content = error
error = null
}
if (error) {
file.content = null
file.contentPath = null
return done(error)
}
if (!preprocessors.length) {
file.contentPath = null
file.content = content
file.sha = sha1(content)
return done()
}
preprocessors.shift()(content, file, nextPreprocessor)
}
}
var createPreprocessor = function (config, basePath, injector) {
var alreadyDisplayedErrors = {}
var instances = {}
var patterns = Object.keys(config)
var emitter = injector.get('emitter')
var instantiatePreprocessor = function (name) {
if (alreadyDisplayedErrors[name]) {
return
}
var p
try {
p = injector.get('preprocessor:' + name)
} catch (e) {
if (e.message.indexOf('No provider for "preprocessor:' + name + '"') !== -1) {
log.error('Can not load "%s", it is not registered!\n ' +
'Perhaps you are missing some plugin?', name)
} else {
log.error('Can not load "%s"!\n ' + e.stack, name)
}
alreadyDisplayedErrors[name] = true
emitter.emit('load_error', 'preprocessor', name)
}
return p
}
var allPreprocessors = []
patterns.forEach(function (pattern) {
allPreprocessors = combineLists(allPreprocessors, config[pattern])
})
allPreprocessors.forEach(instantiatePreprocessor)
return function preprocess (file, done) {
patterns = Object.keys(config)
var retryCount = 0
var maxRetries = 3
function handleFile (err, buffer) {
if (err) {
log.warn(err)
if (retryCount < maxRetries) {
retryCount++
log.warn('retrying ' + retryCount)
fs.readFile(file.originalPath, handleFile)
return
} else {
done()
}
}
isBinaryFile(buffer, buffer.length, function (err, thisFileIsBinary) {
if (err) {
throw err
}
var preprocessorNames = []
for (var i = 0; i < patterns.length; i++) {
if (mm(file.originalPath, patterns[i], {dot: true})) {
if (thisFileIsBinary) {
log.warn('Ignoring preprocessing (%s) %s because it is a binary file.',
config[patterns[i]].join(', '), file.originalPath)
} else {
preprocessorNames = combineLists(preprocessorNames, config[patterns[i]])
}
}
}
var preprocessors = []
var nextPreprocessor = createNextProcessor(preprocessors, file, done)
preprocessorNames.forEach(function (name) {
var p = instances[name]
if (p == null) {
p = instantiatePreprocessor(name)
}
if (p == null) {
if (!alreadyDisplayedErrors[name]) {
alreadyDisplayedErrors[name] = true
log.error('Failed to instantiate preprocessor %s', name)
emitter.emit('load_error', 'preprocessor', name)
}
return
}
instances[name] = p
preprocessors.push(p)
})
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
})
}
return fs.readFile(file.originalPath, handleFile)
}
}
createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector']
exports.createPreprocessor = createPreprocessor