'use strict';
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var path = require('path');
var fs = require('fs');
var _require = require('source-map');
var SourceMapConsumer = _require.SourceMapConsumer;
var sourceMapRegEx = /(?:\/{2}[#@]{1,2}|\/\*)\s+sourceMappingURL\s*=\s*(data:(?:[^;]+;)+base64,)?(\S+)/;
var getMapping = require('./getMapping');
var MappingProvider = function () {
function MappingProvider(options, sparceCoverageCollector) {
(0, _classCallCheck3.default)(this, MappingProvider);
this.sparceCoverageCollector = sparceCoverageCollector;
this.warn = options.warn || console.warn;
this.readJSON = options.readJSON || function readJSON(filePath) {
Iif (!fs.existsSync(filePath)) {
throw new Error('Could not find file: "' + filePath + '"');
}
return JSON.parse(fs.readFileSync(filePath));
};
this.readFile = options.readFile || function readFile(filePath) {
if (!fs.existsSync(filePath)) {
this.warn(new Error('Could not find file: "' + filePath + '"'));
return '';
}
return fs.readFileSync(filePath);
};
this.sourceStore = options.sources;
}
(0, _createClass3.default)(MappingProvider, [{
key: 'getMappingResolver',
value: function getMappingResolver(filePath) {
var _this = this;
var code = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
/* coverage.json can sometimes include the code inline */
var codeIsArray = true;
var jsText = code || this.readFile(filePath);
if (Array.isArray(jsText)) {
/* sometimes the source is an array */
jsText = jsText.join('\n');
} else {
codeIsArray = false;
}
var match = sourceMapRegEx.exec(jsText);
var sourceMapDir = path.dirname(filePath);
var rawSourceMap = void 0;
if (!match) {
return false;
}
if (match[1]) {
rawSourceMap = JSON.parse(new Buffer(match[2], 'base64').toString('utf8'));
} else {
var sourceMapPath = path.join(sourceMapDir, match[2]);
rawSourceMap = this.readJSON(sourceMapPath);
sourceMapDir = path.dirname(sourceMapPath);
}
// replace relative paths in source maps with absolute
rawSourceMap.sources = rawSourceMap.sources.map(function (srcPath) {
return path.resolve(sourceMapDir, srcPath);
});
var sourceMap = new SourceMapConsumer(rawSourceMap);
/* if there are inline sources and a store to put them into, we will populate it */
if (sourceMap.sourcesContent) {
sourceMap.sourcesContent.forEach(function (source, idx) {
_this.sparceCoverageCollector.setSourceCode(sourceMap.sources[idx], codeIsArray ? source.split('\n') : source);
if (_this.sourceStore) {
_this.sourceStore.set(sourceMap.sources[idx], source);
}
});
}
return function (location) {
return getMapping(sourceMap, location);
};
}
}]);
return MappingProvider;
}();
module.exports.MappingProvider = MappingProvider;
//# sourceMappingURL=MappingProvider.js.map |