1// Copyright 2021 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * @fileoverview Takes raw v8 coverage files and converts to IstanbulJS 7 * compliant coverage files. 8 */ 9 10// Relative path to the node modules. 11const NODE_MODULES = [ 12 '..', '..', '..', 'third_party', 'js_code_coverage', 'node_modules']; 13 14const {Worker} = require('worker_threads'); 15const {join} = require('path'); 16const {readFile, mkdir} = require('fs').promises; 17const {ArgumentParser} = require(join(...NODE_MODULES, 'argparse')); 18 19function createWorker(coverageDir, sourceDir, outputDir, urlToPathMap) { 20 return new Promise(function(resolve, reject) { 21 const worker = new Worker(join(__dirname, 'coverage_worker.js'), { 22 workerData: { 23 coverageDir: coverageDir, 24 sourceDir: sourceDir, 25 outputDir: outputDir, 26 urlToPathMap: urlToPathMap 27 }, 28 }); 29 worker.on('message', (data) => { 30 resolve(data); 31 }); 32 worker.on('error', (msg) => { 33 reject(`An error ocurred: ${msg}`); 34 }); 35 }); 36} 37 38/** 39 * The entry point to the function to enable the async functionality throughout. 40 * @param {Object} args The parsed CLI arguments. 41 * @return {!Promise<string>} Directory containing istanbul reports. 42 */ 43async function main(args) { 44 const urlToPathMapFile = 45 await readFile(join(args.source_dir, 'parsed_scripts.json')); 46 const urlToPathMap = JSON.parse(urlToPathMapFile.toString()); 47 const outputDir = join(args.output_dir, 'istanbul') 48 await mkdir(outputDir, {recursive: true}); 49 50 const workerPromises = []; 51 for (const coverageDir of args.raw_coverage_dirs) { 52 workerPromises.push( 53 createWorker(coverageDir, args.source_dir, outputDir, urlToPathMap)); 54 } 55 56 const results = await Promise.all(workerPromises); 57 console.log(`Result from workers: ${results}`) 58 return outputDir; 59} 60 61const parser = new ArgumentParser({ 62 description: 'Converts raw v8 coverage into IstanbulJS compliant files.', 63}); 64 65parser.add_argument('-s', '--source-dir', { 66 required: true, 67 help: 'Root directory where source files live. The corresponding ' + 68 'coverage files must refer to these files. Currently source ' + 69 'maps are not supported.', 70}); 71parser.add_argument('-o', '--output-dir', { 72 required: true, 73 help: 'Root directory to output all the converted istanbul coverage ' + 74 'reports.', 75}); 76parser.add_argument('-c', '--raw-coverage-dirs', { 77 required: true, 78 nargs: '*', 79 help: 'Directory that contains the raw v8 coverage files (files ' + 80 'ending in .cov.json)', 81}); 82 83const args = parser.parse_args(); 84main(args) 85 .then(outputDir => { 86 console.log(`Successfully converted from v8 to IstanbulJS: ${outputDir}`); 87 }) 88 .catch(error => { 89 console.error(error); 90 process.exit(1); 91 }); 92