From 09ddd765eae14c296f1360d7cd42d79b33931f89 Mon Sep 17 00:00:00 2001 From: mmisty Date: Thu, 13 Jun 2024 17:19:21 +0300 Subject: [PATCH] fix --- .scripts/merge.js | 139 +++++++++++++++++++++++++++++++++++++++++++ .scripts/remove.sh | 5 ++ package-publish.json | 18 ------ package.json | 2 +- 4 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 .scripts/merge.js create mode 100644 .scripts/remove.sh delete mode 100644 package-publish.json diff --git a/.scripts/merge.js b/.scripts/merge.js new file mode 100644 index 0000000..43b628b --- /dev/null +++ b/.scripts/merge.js @@ -0,0 +1,139 @@ +#!/usr/bin/env node +const yargs = require("yargs"); +const path = require("path"); +const istanbul = require('istanbul-lib-coverage'); +const { existsSync, mkdirSync, rmSync, writeFileSync, readFileSync} = require("fs"); + +/** + * this taken from @cypress/code-coverage + * @param coverage object + */ +function fixSourcePaths(coverage) { + Object.values(coverage).forEach((file) => { + const { path: absolutePath, inputSourceMap } = file + const fileName = /([^\/\\]+)$/.exec(absolutePath)[1] + if (!inputSourceMap || !fileName) return + + if (inputSourceMap.sourceRoot) inputSourceMap.sourceRoot = '' + inputSourceMap.sources = inputSourceMap.sources.map((source) => + source.includes(fileName) ? absolutePath : source + ) + }) +} + +/** + * this partly taken from @cypress/code-coverage + * @param tempDir directory where to merge + * @param fileWithCoverage file containing coverage + */ +function combineCoverage(tempDir, fileWithCoverage) { + const fileToSave = `${tempDir}/combined.json`; + + const coverage = existsSync(fileToSave) + ? JSON.parse(readFileSync(fileToSave, 'utf8')) + : {}; + + fixSourcePaths(coverage) + + const previousCoverage = existsSync(fileWithCoverage) + ? JSON.parse(readFileSync(fileWithCoverage, 'utf8')) + : {} + + const coverageMap = istanbul.createCoverageMap(previousCoverage) + coverageMap.merge(coverage) + + writeFileSync(fileToSave, JSON.stringify(coverageMap, null, 2)) + console.log('combined coverage from `%s` with %s', fileToSave, fileWithCoverage) +} + +/** + * Create report by NYC library, + * Will not read nyc config and temp dic from nyc.config.js + * @param tempDir dir where json files located + * @param reportDir dir where to put report + * @param reporterArr array with reporters like ['json', 'lcov', 'text'] + */ +function createReport(tempDir, reportDir, reporterArr){ + const NYC = require('nyc'); + const nycReportOptions = { + reportDir: reportDir, + tempDir: tempDir, + reporter: reporterArr ?? ['json', 'lcov', 'text'], + }; + + const nyc = new NYC(nycReportOptions) + + nyc.report().then(()=> { + console.log("Report created"); + }) +} + +/** + * Remove directory sync + * @param dir + */ +const removeDir = (dir) => { + const pathResolved = path.resolve(dir) + if(existsSync(pathResolved)){ + rmSync(pathResolved, {recursive: true}); + } +} + +/** + * Clear directory sync + * @param dir + */ +const clearDir = (dir) => { + const pathResolved = path.resolve(dir) + if(existsSync(pathResolved)){ + rmSync(pathResolved, {recursive: true}); + } + mkdirSync(pathResolved, {recursive: true}); +} + +const argv = yargs(process.argv.slice(2)) + .options({ + cypress: { + type: 'string', + demandOption: true, + default: 'reports/coverage-cypress', + describe: `Path to coverage reports directory (relative to current working directory) + Path with directories - each of them should contain coverage report (coverage-final.json)`, + }, + jest: { + type: 'string', + demandOption: true, + default: 'reports/coverage-jest', + describe: `Path to jet coverage report, should contain coverage report (coverage-final.json)`, + }, + out: { + type: 'string', + demandOption: true, + default: 'reports/coverage-temp', + describe: `Path to final report`, + }, + report: { + type: 'string', + demandOption: true, + default: 'reports/coverage-full', + describe: `Path to final report`, + }, + }) + .alias('c', 'cypress') + .alias('j', 'jest') + .alias('h', 'help') + .help('help') + .parseSync(); + +console.log(' ======== MERGE COVERAGE REPORTS'); + +const { jest, cypress, out, report } = argv; +const outDir = path.resolve(out); +const reportDir = path.resolve(report); + +removeDir(reportDir); +clearDir(outDir); + +combineCoverage(outDir, `${cypress}/coverage-final.json`); +combineCoverage(outDir, `${jest}/coverage-final.json`); +createReport(outDir, reportDir, ['json', 'lcov', 'text', 'cobertura', 'clover']); \ No newline at end of file diff --git a/.scripts/remove.sh b/.scripts/remove.sh new file mode 100644 index 0000000..cfadbe5 --- /dev/null +++ b/.scripts/remove.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +sed '/"scripts":/,/},/d' lib/package.json > lib/temp.json + +mv lib/temp.json lib/package.json \ No newline at end of file diff --git a/package-publish.json b/package-publish.json deleted file mode 100644 index 2dd1a41..0000000 --- a/package-publish.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "allure-js-parser", - "version": "0.0.1", - "description": "The package allows you to parse allure-results raw files and convert them into a typed object", - "main": "./index", - "repository": "git@github.com:mmisty/allure-js-parser.git", - "publishConfig": { - "registry": "https://registry.npmjs.org/" - }, - "author": "Taisia Pitko (mmisty) ", - "license": "MIT", - "keywords": ["allure", "parser", "json", "results", "tests", "statistics", "allure-js-parser"], - "dependencies": { - "allure-js-commons": "^2.15.1", - "debug": "^4.3.5", - "fast-glob": "^3.3.2" - } -} diff --git a/package.json b/package.json index ddff8a1..5f5f843 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "test:cov": "CONSOLE=false jest --coverage", "lint": "eslint --ext '.js,.ts' . --ignore-path .gitignore", "prepublish": "npm run test:cov && npm run build", - "build": "rm -rf dist && tsc --project tsconfig.build.json && cp README-pack.md ./dist/README.md", + "build": "rm -rf dist && tsc --project tsconfig.build.json && cp package.json ./dist/package.json && cp README-pack.md ./dist/README.md && cp CHANGELOG.md ./dist/CHANGELOG.md && sh .scripts/remove.sh && cd dist && npm version $ver --no-git-tag-version ", "pack": "npm run build && cd dist && npm pack", "publish": "npm run publish:patch", "prepublishOnly": "echo 'USE script \"npm run publish:patch\" (or minor/major)\n' && false",