Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
[chore] eslint, jest CI (#5)
Browse files Browse the repository at this point in the history
* ESLint GitHub Actions

* Fix Directory Name workflow to workflows

* Fix yarn does not install dependencies

* Fix filter_mode

* Test for ESLint CI

* Reporter pr-review to pr-check

* Fix permission

* Revert "Test for ESLint CI"

This reverts commit daf968c.

* Create test.yml

* Create bundle-size-diff tools

* Remove name

* Fix over-jobs variable

* Fix Comment Settings

* Create Auto Approve Actions

* Fix Conditions
  • Loading branch information
AsPulse authored Mar 5, 2023
1 parent b066316 commit 2dbe46f
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 8 deletions.
7 changes: 3 additions & 4 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
'function-call-argument-newline': [ 'error', 'consistent' ],
'dot-location': [ 'error', 'property' ],
'dot-notation': [ 'error' ],
'@typescript-eslint/array-type': [ 'error', 'array-simple' ],
'@typescript-eslint/array-type': [ 'error', { default: 'array-simple', readonly: 'array-simple' }],
'@typescript-eslint/prefer-for-of': [ 'error' ],
'@typescript-eslint/prefer-includes': [ 'error' ],

Expand Down Expand Up @@ -100,10 +100,10 @@ module.exports = {
'unused-imports/no-unused-imports': [ 'error' ],

/* Maintainability */
'complexity': [ 'warn', 6 ],
'complexity': [ 'warn', 10 ],
'max-classes-per-file': [ 'warn', 1 ],
'max-depth': [ 'warn', 3 ],
'max-len': [ 'warn', { code: 50, ignoreComments: true, ignoreStrings: true, ignoreTemplateLiterals: true }],
'max-len': [ 'warn', { code: 100, ignoreComments: true, ignoreStrings: true, ignoreTemplateLiterals: true }],
'max-lines-per-function': [ 'warn', 50 ],
'no-multi-str': [ 'warn' ],
'object-shorthand': [ 'warn' ],
Expand All @@ -123,7 +123,6 @@ module.exports = {
'no-self-compare': [ 'error' ],
'no-unmodified-loop-condition': [ 'error' ],
'no-unused-private-class-members': [ 'error' ],
'no-use-before-define': [ 'error' ],
'no-param-reassign': [ 'error' ],
'no-return-assign': [ 'error' ],
'no-return-await': [ 'error' ],
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/auto-approve.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: auto-approve
on: [pull_request]

jobs:
auto-approve:
runs-on: ubuntu-latest
permissions:
pull-requests: write
if: github.event.pull_request.user.login == 'AsPulse'
steps:
- uses: hmarr/auto-approve-action@v3
79 changes: 79 additions & 0 deletions .github/workflows/bundle-size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: bundle-size
on: [pull_request]

jobs:
build_head:
runs-on: [ubuntu-latest]
permissions:
contents: read
outputs:
result: ${{ steps.export.outputs.HEAD_RESULT }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
- name: Install Dependencies
run: yarn install --immutable
- name: Build
run: yarn build
- name: Export Size Info
id: export
run: |
echo "HEAD_RESULT=$(yarn ts-node ./misc/bundle-size-diff.ts inspect)" >> $GITHUB_OUTPUT
build_base:
runs-on: [ubuntu-latest]
permissions:
contents: read
outputs:
result: ${{ steps.export.outputs.BASE_RESULT }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.base_re }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
- name: Install Dependencies
run: yarn install --immutable
- name: Build
run: yarn build
- name: Export Size Info
id: export
run: |
echo "BASE_RESULT=$(yarn ts-node ./misc/bundle-size-diff.ts inspect)" >> $GITHUB_OUTPUT
compare:
runs-on: [ubuntu-latest]
needs: [build_base, build_head]
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.base_re }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
- name: Install Dependencies
run: yarn install --immutable
- name: Export Markdown
env:
HEAD_RESULT: ${{ needs.build_head.outputs.result }}
BASE_RESULT: ${{ needs.build_base.outputs.result }}
run: yarn ts-node ./misc/bundle-size-diff.ts diff $HEAD_RESULT $BASE_RESULT
- name: Post Review Comment
uses: mshick/add-pr-comment@v2
with:
message-path: 'bundle-size/result.md'
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: lint
on: [pull_request]

jobs:
eslint:
runs-on: [ubuntu-latest]
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
- name: Install Dependencies
run: yarn install --immutable
- name: Run ESLint
uses: reviewdog/action-eslint@v1
with:
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
filter_mode: diff_context
fail_on_error: true
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test
on: [pull_request]

jobs:
jest:
runs-on: [ubuntu-latest]
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
- name: Install Dependencies
run: yarn install --immutable
- name: Run Jest
run: yarn test
119 changes: 119 additions & 0 deletions misc/bundle-size-diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* eslint-disable no-console */
import { rm, mkdir, readFile, writeFile } from 'fs/promises';

interface Diff {
head: number;
base: number;
diff: number;
}

interface DiffResult {
cjs: Diff;
esm: Diff;
package: Diff;
}

function getDiff(args: string[]): DiffResult {

const size = { head: args[3], base: args[4] };
if (size.head === undefined || size.base === undefined) {
console.error('Usage: bundle-size-diff diff <head> <base>');
process.exit(1);
}

const [ headCjs, headEsm, headPackage ] = size.head.split(':').map(v => parseInt(v, 10));
const [ baseCjs, baseEsm, basePackage ] = size.base.split(':').map(v => parseInt(v, 10));
if (
headCjs === undefined || headEsm === undefined || headPackage === undefined ||
baseCjs === undefined || baseEsm === undefined || basePackage === undefined
) {
console.error('Error: invalid size format.');
process.exit(1);
}

return {
cjs: { head: headCjs, base: baseCjs, diff: headCjs - baseCjs },
esm: { head: headEsm, base: baseEsm, diff: headEsm - baseEsm },
package: { head: headPackage, base: basePackage, diff: headPackage - basePackage },
};

}

async function exportToMarkdown(data: DiffResult): Promise<void> {
const result = [
'## Bundle Size Summary',
'||Base branch|HEAD branch|Diff|',
'|--:|:--:|:--:|:--:|',
[
'',
'CommonJS',
formatSize(data.cjs.base), formatSize(data.cjs.head),
`**${getSymbol(data.cjs.diff)} ${formatSize(Math.abs(data.cjs.diff))}**`,
'',
].join('|'),
[
'',
'ES Module',
formatSize(data.esm.base), formatSize(data.esm.head),
`**${getSymbol(data.esm.diff)} ${formatSize(Math.abs(data.esm.diff))}**`,
'',
].join('|'),
[
'',
'Package',
formatSize(data.package.base), formatSize(data.package.head),
`**${getSymbol(data.package.diff)} ${formatSize(Math.abs(data.package.diff))}**`,
'',
].join('|'),
].join('\n');
await rm('./bundle-size', { recursive: true, force: true });
await mkdir('./bundle-size', { recursive: true });
await writeFile('./bundle-size/result.md', result);
console.log('Result is exported to: bundle-size/result.md');
return;
}

export function getSymbol(diff: number): string {
return diff === 0 ? '+/-' : diff < 0 ? '-' : '+';
}
export function formatSize(byte: number): string {
return `${Math.round(byte / 10) / 100} kB`;
}

async function main(): Promise<void> {
const args = process.argv;
switch (args[2]) {
case 'inspect':
try {
const cjs = await readFile('./dist/cjs/index.js');
const esm = await readFile('./dist/esm/index.js');
const cjsDts = await readFile('./dist/cjs/index.d.ts');
const esmDts = await readFile('./dist/esm/index.d.ts');
const packageJson = await readFile('./package.json');
const license = await readFile('./LICENSE');
const readme = await readFile('./README.md');
console.log([
cjs.byteLength,
esm.byteLength,
[ cjs, esm, cjsDts, esmDts, packageJson, license, readme ]
.map(v => v.byteLength)
.reduce((a, b) => a + b, 0),
].join(':'));
} catch {
console.error('Error: some of build artifacts not found.');
process.exit(1);
}
break;

case 'diff':
await exportToMarkdown( getDiff(args) );
break;

default:
console.error('Usage: bundle-size-diff [inspect|diff]');
process.exit(1);
}
}


void main();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"rollup": "^3.18.0",
"rollup-plugin-dts": "^5.2.0",
"rollup-plugin-swc3": "^0.8.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
"typescript": "^4.9.5"
},
Expand Down
9 changes: 7 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true
},
"include": ["src", "tests", ".eslintrc.cjs", "rollup.config.mjs", "jest.config.cjs"],
"exclude": ["node_modules", "dist"]
"include": ["src", "tests", "misc", ".eslintrc.cjs", "rollup.config.mjs", "jest.config.cjs"],
"exclude": ["node_modules", "dist"],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
}
}
Loading

0 comments on commit 2dbe46f

Please sign in to comment.