source: trip-planner-front/node_modules/karma/lib/preprocessor.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.3 KB
Line 
1'use strict'
2
3const util = require('util')
4const fs = require('graceful-fs')
5// bind need only for mock unit tests
6const readFile = util.promisify(fs.readFile.bind(fs))
7const tryToRead = function (path, log) {
8 const maxRetries = 3
9 let promise = readFile(path)
10 for (let retryCount = 1; retryCount <= maxRetries; retryCount++) {
11 promise = promise.catch((err) => {
12 log.warn(err)
13 log.warn('retrying ' + retryCount)
14 return readFile(path)
15 })
16 }
17 return promise.catch((err) => {
18 log.warn(err)
19 return Promise.reject(err)
20 })
21}
22
23const mm = require('minimatch')
24const { isBinaryFile } = require('isbinaryfile')
25const _ = require('lodash')
26const CryptoUtils = require('./utils/crypto-utils')
27
28const log = require('./logger').create('preprocess')
29
30function executeProcessor (process, file, content) {
31 let done = null
32 const donePromise = new Promise((resolve, reject) => {
33 done = function (error, content) {
34 // normalize B-C
35 if (arguments.length === 1 && typeof error === 'string') {
36 content = error
37 error = null
38 }
39 if (error) {
40 reject(error)
41 } else {
42 resolve(content)
43 }
44 }
45 })
46
47 return (process(content, file, done) || Promise.resolve()).then((content) => {
48 if (content) {
49 // async process correctly returned content
50 return content
51 }
52 // process called done() (Either old sync api or an async function that did not return content)
53 return donePromise
54 })
55}
56
57async function runProcessors (preprocessors, file, content) {
58 try {
59 for (const process of preprocessors) {
60 content = await executeProcessor(process, file, content)
61 }
62 } catch (error) {
63 file.contentPath = null
64 file.content = null
65 throw error
66 }
67
68 file.contentPath = null
69 file.content = content
70 file.sha = CryptoUtils.sha1(content)
71}
72
73function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, instantiatePlugin) {
74 _.union.apply(_, Object.values(config)).forEach((name) => instantiatePlugin('preprocessor', name))
75 return async function preprocess (file) {
76 const buffer = await tryToRead(file.originalPath, log)
77 let isBinary = file.isBinary
78 if (isBinary == null) {
79 // Pattern did not specify, probe for it.
80 isBinary = await isBinaryFile(buffer, buffer.length)
81 }
82
83 const preprocessorNames = Object.keys(config).reduce((ppNames, pattern) => {
84 if (mm(file.originalPath, pattern, { dot: true })) {
85 ppNames = _.union(ppNames, config[pattern])
86 }
87 return ppNames
88 }, [])
89
90 // Apply preprocessor priority.
91 const preprocessors = preprocessorNames
92 .map((name) => [name, preprocessorPriority[name] || 0])
93 .sort((a, b) => b[1] - a[1])
94 .map((duo) => duo[0])
95 .reduce((preProcs, name) => {
96 const p = instantiatePlugin('preprocessor', name)
97
98 if (!isBinary || (p && p.handleBinaryFiles)) {
99 preProcs.push(p)
100 } else {
101 log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
102 }
103 return preProcs
104 }, [])
105
106 await runProcessors(preprocessors, file, isBinary ? buffer : buffer.toString())
107 }
108}
109
110createPriorityPreprocessor.$inject = ['config.preprocessors', 'config.preprocessor_priority', 'config.basePath', 'instantiatePlugin']
111exports.createPriorityPreprocessor = createPriorityPreprocessor
Note: See TracBrowser for help on using the repository browser.