Skip to content

Commit

Permalink
Merge 4ac9dd8 into f8005c6
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox authored Oct 10, 2019
2 parents f8005c6 + 4ac9dd8 commit 2c80f8e
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,43 @@
const fs = require('graceful-fs')
const mm = require('minimatch')
const isBinaryFile = require('isbinaryfile')
const _ = require('lodash')
const CryptoUtils = require('./utils/crypto-utils')

const log = require('./logger').create('preprocess')

function createNextProcessor (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)
function executeProcessor (process, file, content) {
let done = null
const donePromise = new Promise((resolve, reject) => {
done = function (error, content) {
// normalize B-C
if (arguments.length === 1 && typeof error === 'string') {
content = error
error = null
}
if (error) {
reject(error)
} else {
resolve(content)
}
}
})
return process(content, file, done) || donePromise
}

if (!preprocessors.length) {
file.contentPath = null
file.content = content
file.sha = CryptoUtils.sha1(content)
return done()
async function runProcessors (preprocessors, file, content) {
try {
for (let process of preprocessors) {
content = await executeProcessor(process, file, content)
}

preprocessors.shift()(content, file, nextPreprocessor)
} catch (error) {
file.contentPath = null
file.content = null
throw error
}

file.contentPath = null
file.content = content
file.sha = CryptoUtils.sha1(content)
}

function createPriorityPreprocessor (config, preprocessorPriority, basePath, injector) {
Expand All @@ -44,7 +53,10 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
return
}

let p
let p = instances[name]
if (p) {
return p
}

try {
p = injector.get('preprocessor:' + name)
Expand All @@ -58,13 +70,19 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
emitter.emit('load_error', 'preprocessor', name)
}

if (!p && !alreadyDisplayedErrors[name]) {
alreadyDisplayedErrors[name] = true
log.error(`Failed to instantiate preprocessor ${name}`)
emitter.emit('load_error', 'preprocessor', name)
} else {
instances[name] = p
}

return p
}

let allPreprocessors = []
patterns.forEach((pattern) => {
allPreprocessors = _.union(allPreprocessors, config[pattern])
})
const allPreprocessors = new Set()
patterns.forEach((pattern) => (config[pattern] || []).forEach((proc) => allPreprocessors.add(proc)))
allPreprocessors.forEach(instantiatePreprocessor)

return function preprocess (file, done) {
Expand All @@ -89,42 +107,30 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
throw err
}

let preprocessorNames = []
const preprocessorNames = new Set()
patterns.forEach((pattern) => {
if (mm(file.originalPath, pattern, { dot: true })) {
preprocessorNames = _.union(preprocessorNames, config[pattern])
(config[pattern] || []).forEach((name) => preprocessorNames.add(name))
}
})

// Apply preprocessor priority.
let sortedPreprocessorNames = preprocessorNames
const preprocessors = Array.from(preprocessorNames)
.map((name) => [name, preprocessorPriority[name] || 0])
.sort((a, b) => b[1] - a[1])
.map((duo) => duo[0])
.reduce((res, name) => {
const p = instantiatePreprocessor(name)

let preprocessors = []
const nextPreprocessor = createNextProcessor(preprocessors, file, done)
sortedPreprocessorNames.forEach((name) => {
const p = instances[name] || instantiatePreprocessor(name)

if (p == null) {
if (!alreadyDisplayedErrors[name]) {
alreadyDisplayedErrors[name] = true
log.error(`Failed to instantiate preprocessor ${name}`)
emitter.emit('load_error', 'preprocessor', name)
if (!isBinary || (p && p.handleBinaryFiles)) {
res.push(p)
} else {
log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
}
return
}

instances[name] = p
if (!isBinary || p.handleBinaryFiles) {
preprocessors.push(p)
} else {
log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
}
})
return res
}, [])

nextPreprocessor(null, isBinary ? buffer : buffer.toString())
runProcessors(preprocessors, file, isBinary ? buffer : buffer.toString()).then(done, done)
})
}
return fs.readFile(file.originalPath, readFileCallback)
Expand Down

0 comments on commit 2c80f8e

Please sign in to comment.