Skip to content

Commit

Permalink
refactor(preprocessor): update lib/preprocessor.js to ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz committed Jun 22, 2018
1 parent 7b66e18 commit ad820a1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 103 deletions.
76 changes: 34 additions & 42 deletions lib/preprocessor.js
@@ -1,16 +1,12 @@
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')

function sha1 (data) {
var hash = crypto.createHash('sha1')
hash.update(data)
return hash.digest('hex')
}
'use strict'

const fs = require('graceful-fs')
const mm = require('minimatch')
const isBinaryFile = require('isbinaryfile')
const combineLists = require('combine-lists')
const CryptoUtils = require('./utils/crypto-utils')

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

function createNextProcessor (preprocessors, file, done) {
return function nextPreprocessor (error, content) {
Expand All @@ -29,7 +25,7 @@ function createNextProcessor (preprocessors, file, done) {
if (!preprocessors.length) {
file.contentPath = null
file.content = content
file.sha = sha1(content)
file.sha = CryptoUtils.sha1(content)
return done()
}

Expand All @@ -38,27 +34,25 @@ function createNextProcessor (preprocessors, file, done) {
}

function createPreprocessor (config, basePath, injector) {
var alreadyDisplayedErrors = {}
var instances = {}
var patterns = Object.keys(config)

var emitter = injector.get('emitter')
const emitter = injector.get('emitter')
const alreadyDisplayedErrors = {}
const instances = {}
let patterns = Object.keys(config)

function instantiatePreprocessor (name) {
if (alreadyDisplayedErrors[name]) {
return
}

var p
let 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)
if (e.message.indexOf(`No provider for "preprocessor:${name}"`) !== -1) {
log.error(`Can not load "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
} else {
log.error('Can not load "%s"!\n ' + e.stack, name)
log.error(`Can not load "${name}"!\n ` + e.stack)
}
alreadyDisplayedErrors[name] = true
emitter.emit('load_error', 'preprocessor', name)
Expand All @@ -67,16 +61,16 @@ function createPreprocessor (config, basePath, injector) {
return p
}

var allPreprocessors = []
patterns.forEach(function (pattern) {
let allPreprocessors = []
patterns.forEach((pattern) => {
allPreprocessors = combineLists(allPreprocessors, config[pattern])
})
allPreprocessors.forEach(instantiatePreprocessor)

return function preprocess (file, done) {
patterns = Object.keys(config)
var retryCount = 0
var maxRetries = 3
let retryCount = 0
let maxRetries = 3
function readFileCallback (err, buffer) {
if (err) {
log.warn(err)
Expand All @@ -90,25 +84,22 @@ function createPreprocessor (config, basePath, injector) {
}
}

isBinaryFile(buffer, buffer.length, function (err, thisFileIsBinary) {
isBinaryFile(buffer, buffer.length, function (err, isBinary) {
if (err) {
throw err
}

var preprocessorNames = []
for (var i = 0; i < patterns.length; i++) {
if (mm(file.originalPath, patterns[i], {dot: true})) {
preprocessorNames = combineLists(preprocessorNames, config[patterns[i]])
patterns.forEach((pattern) => {
if (mm(file.originalPath, pattern, {dot: true})) {
preprocessorNames = combineLists(preprocessorNames, config[pattern])
}
}
})

var preprocessors = []
var nextPreprocessor = createNextProcessor(preprocessors, file, done)
preprocessorNames.forEach(function (name) {
var p = instances[name]
if (p == null) {
p = instantiatePreprocessor(name)
}
let preprocessors = []
const nextPreprocessor = createNextProcessor(preprocessors, file, done)
preprocessorNames.forEach((name) => {
const p = instances[name] || instantiatePreprocessor(name)

if (p == null) {
if (!alreadyDisplayedErrors[name]) {
Expand All @@ -120,20 +111,21 @@ function createPreprocessor (config, basePath, injector) {
}

instances[name] = p
if (!thisFileIsBinary || p.handleBinaryFiles) {
if (!isBinary || p.handleBinaryFiles) {
preprocessors.push(p)
} else {
log.warn('Ignored preprocessing %s because %s has handleBinaryFiles=false.',
file.originalPath, name)
}
})

nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
nextPreprocessor(null, isBinary ? buffer : buffer.toString())
})
}
return fs.readFile(file.originalPath, readFileCallback)
}
}

createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector']

exports.createPreprocessor = createPreprocessor
14 changes: 14 additions & 0 deletions lib/utils/crypto-utils.js
@@ -0,0 +1,14 @@
'use strict'

const crypto = require('crypto')

const CryptoUtils = {
sha1 (data) {
return crypto
.createHash('sha1')
.update(data)
.digest('hex')
}
}

module.exports = CryptoUtils

0 comments on commit ad820a1

Please sign in to comment.