From 75d0bda7c94eaba40a6158d4e5c5bd6470cff16c Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sun, 21 Apr 2024 19:23:52 +0100 Subject: [PATCH] Refactor CI workflow and add merge-lcov script --- .github/workflows/ci.yml | 31 ++++++++++++++++++------------- package.json | 4 +++- tools/merge-lcov-files.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 tools/merge-lcov-files.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a88ae5..8488587 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,27 +15,32 @@ permissions: contents: read jobs: - main: + test: runs-on: ubuntu-latest + steps: - - uses: actions/checkout@v4 + - name: Checkout Code Repository + uses: actions/checkout@v4 with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + - uses: nrwl/nx-set-shas@v3 + with: + main-branch-name: develop - # Cache node_modules - - uses: actions/setup-node@v3 + - name: Setup Node + uses: actions/setup-node@v3 with: node-version: 20 cache: 'yarn' - - run: yarn install --frozen-lockfile - - uses: nrwl/nx-set-shas@v4 - - run: git branch --track main origin/main - if: ${{ github.event_name == 'pull_request' }} + - run: yarn install + - run: npx nx affected --target=lint --parallel=3 + - run: npx nx affected --target=test --parallel=3 --code-coverage + - run: yarn merge-lcov - - run: yarn nx-cloud record -- nx format:check - - run: yarn nx affected -t lint test build + - name: Code Coverage + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./coverage/lcov.info diff --git a/package.json b/package.json index d03dd7e..9dbf222 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,9 @@ "name": "@valerymelou/source", "version": "0.0.0", "license": "MIT", - "scripts": {}, + "scripts": { + "merge-lcov": "node tools/merge-lcov-files.js" + }, "private": true, "dependencies": { "@angular/animations": "~17.3.0", diff --git a/tools/merge-lcov-files.js b/tools/merge-lcov-files.js new file mode 100644 index 0000000..2c9ce89 --- /dev/null +++ b/tools/merge-lcov-files.js @@ -0,0 +1,32 @@ +const glob = require('glob'); +const fs = require('fs'); +const path = require('path'); + +const getLcovFiles = function (src) { + return new Promise((resolve, reject) => { + glob(`${src}/**/lcov.info`, (error, result) => { + if (error) return reject(error); + resolve(result); + return null; + }); + }); +}; + +(async function () { + const files = await getLcovFiles('coverage'); + if (files && files instanceof Array) { + const mergedReport = files.reduce( + (report, currFile) => (report += fs.readFileSync(currFile)), + '', + ); + await fs.writeFile( + path.resolve('./coverage/lcov.info'), + mergedReport, + (err) => { + if (err) console.log(err); // skipcq: JS-0002 + console.log('The file has been saved!'); // skipcq: JS-0002 + return null; + }, + ); + } +})();