UNPKG

ui5-middleware-testcafe-coverage

Version:
102 lines (96 loc) 2.96 kB
const log = require('@ui5/logger').getLogger('server:custommiddleware:code-coverage'); const path = require('path'); const url = require('url'); const fs = require('fs'); const im = require('istanbul-lib-instrument'); const utils = require('ui5-testcafe-selector-utils'); const config = utils.ui5Config; module.exports = function ({ options }) { const instrumenter = im.createInstrumenter(); const _config = config.coverage ? config.coverage : {}; // eslint-disable-next-line func-names return function (req, res, next) { if (_config.enabled !== true) { next(); return; } if (!req.url.match(/\.js$/) || req.url.match(/jquery/)) { next(); return; } if (req.url.indexOf('/sap/bc/ui5_ui5/ui2/ushell/resources') !== -1) { next(); return; } else if (req.url.indexOf('/sap/public/bc/ui2') !== -1) { next(); return; } else if (req.url.indexOf('sap-ui-core.js') !== -1) { if (_config.log === true) { log.info('skipped ' + req.url + ' as it is a a SAPUI5 file'); } next(); return; } else if (req.url.indexOf('library-preload.js') !== -1) { if (_config.log === true) { log.info('skipped ' + req.url + ' as it is a library-preload file - add to debug sources if this file should be instrumented'); } next(); return; } else if (req.url.indexOf('Component-preload') !== -1) { if (_config.log === true) { log.info('skipped ' + req.url + ' as it is a component-preload file - add to debug sources if this file should be instrumented'); } next(); return; } else if (req.url.indexOf(_config.basePath) === -1) { if (_config.log === true) { log.info('skipped ' + req.url + ' as it is not part of base Path ' + that.basePath); } next(); return; } //do we have any whitelist? if (_config.includePaths) { let bFound = false; for (let j = 0; j < _config.includePaths.length; j++) { if (req.url.indexOf(_config.includePaths[j]) !== -1) { bFound = true; break; } } if (bFound === false && _config.includePaths.length > 0) { if (_config.log === true) { log.info('skipped ' + req.url + ' as it is not part of include paths'); } next(); return; } } if (_config.excludePaths) { let bFound = false; for (let j = 0; j < _config.excludePaths.length; j++) { if (req.url.indexOf(_config.excludePaths[j]) !== -1) { bFound = true; break; } } if (bFound === true && _config.excludePaths.length > 0) { if (_config.log === true) { log.info('skipped ' + req.url + ' as it is part of exclude paths'); } next(); return; } } const file = path.join(process.cwd(), 'webapp', req.url); let code = ''; try { code = fs.readFileSync(file, 'utf8'); } catch (err) { next(); } if (code) { res.send(instrumenter.instrumentSync(code, file)); } } }