From fc9f0e49091365f4307466a48031840f0df8f8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Mon, 30 Sep 2024 14:56:48 +0800 Subject: [PATCH] feat: update --- packages/canyon-report/html-spa.js | 4 --- packages/canyon-report/lib/cli.js | 6 ++-- packages/canyon-report/lib/index.js | 4 +-- .../canyon-report/lib/reports/dynamic-data.js | 36 ++++++++++++++++--- .../canyon-report/lib/reports/template.js | 3 +- packages/canyon-report/package.json | 2 +- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/packages/canyon-report/html-spa.js b/packages/canyon-report/html-spa.js index a5b4ec28..d26750de 100644 --- a/packages/canyon-report/html-spa.js +++ b/packages/canyon-report/html-spa.js @@ -26,10 +26,6 @@ module.exports = class CustomReporter extends ReportBase { } const ccr = CCR({ - name: "My Coverage Report - 2024-02-28", - outputDir: "./coverage-reports", - reports: ["v8", "console-details"], - cleanCache: true, }); await ccr.add({}); await ccr.generate(); diff --git a/packages/canyon-report/lib/cli.js b/packages/canyon-report/lib/cli.js index f2a61b5d..722a58c7 100755 --- a/packages/canyon-report/lib/cli.js +++ b/packages/canyon-report/lib/cli.js @@ -12,10 +12,8 @@ const version = require('../package.json').version; const executeCommand = async (command, cliOptions) => { console.log(command,cliOptions) const ccr = CCR({ - name: 'My Coverage Report - 2024-02-28', - outputDir: './coverage-reports', - reports: ["v8", "console-details"], - cleanCache: true + name: command.name, + instrumentCwd:command.instrumentCwd, }); await ccr.add({}); await ccr.generate(); diff --git a/packages/canyon-report/lib/index.js b/packages/canyon-report/lib/index.js index 85e257ea..b14b1132 100644 --- a/packages/canyon-report/lib/index.js +++ b/packages/canyon-report/lib/index.js @@ -40,11 +40,11 @@ class CoverageReport { } })() // 5. 核心,动态生成dynamic-data数据 - generateDynamicData({coverage:cov}) + generateDynamicData({coverage:cov,instrumentCwd:this.options.instrumentCwd||''}); // 3. 复制dist目录到当前工作目录 copyDirectory(sourceDir, targetDir); // 4. 生成html文件,注入coverage-final.json的数据,需要一个summary列表 - const html = generateHtml({coverage:cov}); + const html = generateHtml({coverage:cov,name:this.options.name||'All files'}); fs.writeFileSync(path.join(targetDir, "index.html"), html); return {}; } diff --git a/packages/canyon-report/lib/reports/dynamic-data.js b/packages/canyon-report/lib/reports/dynamic-data.js index 1f1b5768..8c357ccd 100644 --- a/packages/canyon-report/lib/reports/dynamic-data.js +++ b/packages/canyon-report/lib/reports/dynamic-data.js @@ -5,10 +5,33 @@ function getDirectoryFromPath(filePath) { return filePath.substring(0, filePath.lastIndexOf("/")); } -const generateDynamicData = ({coverage}) => { - for (const key in JSON.parse(coverage)) {; +const getCommonPathPrefix = (paths) => { + if (paths.length === 0) return ''; + const splitPaths = paths.map(path => path.split('/')); + const minLength = Math.min(...splitPaths.map(p => p.length)); + + let commonPrefix = []; + for (let i = 0; i < minLength; i++) { + const segment = splitPaths[0][i]; + if (splitPaths.every(path => path[i] === segment)) { + commonPrefix.push(segment); + } else { + break; + } + } + return commonPrefix.join('/'); +}; + + +const generateDynamicData = ({coverage,_instrumentCwd}) => { + + const commonPath = getCommonPathPrefix(Object.keys(JSON.parse(coverage))); + + const instrumentCwd = _instrumentCwd || commonPath; + + for (const key in JSON.parse(coverage)) { // 示例 - const directory = getDirectoryFromPath(key); + const directory = getDirectoryFromPath(key.replaceAll(instrumentCwd+'/', "")); fs.mkdirSync(path.join(process.cwd(), `coverage/dynamic-data/${directory}`), { recursive: true }); const data = fs.readFileSync( `${key}`, @@ -16,11 +39,14 @@ const generateDynamicData = ({coverage}) => { ); const jsonData = { content: data, - coverage: coverage[key], + coverage: { + ...coverage[key], + path: key.replaceAll(instrumentCwd+'/', ""), + }, }; fs.writeFileSync( path.join(process.cwd(), `coverage/dynamic-data/${key}.js`), - `window["${key}"] = ${JSON.stringify(jsonData,null,2)}`, + `window["${key.replaceAll(instrumentCwd+'/', "")}"] = ${JSON.stringify(jsonData,null,2)}`, ); } diff --git a/packages/canyon-report/lib/reports/template.js b/packages/canyon-report/lib/reports/template.js index 7b73adaf..90491efe 100644 --- a/packages/canyon-report/lib/reports/template.js +++ b/packages/canyon-report/lib/reports/template.js @@ -1,6 +1,6 @@ const libCoverage = require('istanbul-lib-coverage'); -const generateHtml = ({coverage}) => { +const generateHtml = ({coverage,name}) => { var map = libCoverage.createCoverageMap(JSON.parse(coverage)); const obj = {} map.files().forEach(function(f) { @@ -24,6 +24,7 @@ const generateHtml = ({coverage}) => { Webpack App diff --git a/packages/canyon-report/package.json b/packages/canyon-report/package.json index 60aee2c9..ec1fda76 100755 --- a/packages/canyon-report/package.json +++ b/packages/canyon-report/package.json @@ -1,6 +1,6 @@ { "name": "canyon-report", - "version": "0.0.8-beta-26", + "version": "0.0.8-beta-27", "description": "My webpack project", "main": "./lib/index.js", "bin": "./lib/cli.js",