Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ci): automatically format code #1035

Merged
merged 12 commits into from
Nov 19, 2023
38 changes: 18 additions & 20 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.222.0/containers/javascript-node
{
"name": "Node.js",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 16, 14, 12.
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local arm64/Apple Silicon.
"args": { "VARIANT": "16-bullseye" }
},
"name": "Node.js",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 16, 14, 12.
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local arm64/Apple Silicon.
"args": { "VARIANT": "16-bullseye" }
},

// Set *default* container specific settings.json values on container create.
"settings": {},
// Set *default* container specific settings.json values on container create.
"settings": {},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint"
],
// Add the IDs of extensions you want installed when the container is created.
"extensions": ["dbaeumer.vscode-eslint"],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pnpm install",
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pnpm install",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node"
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node"
}
50 changes: 50 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Fix

on:
push:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add pull_request:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't trigger this workflow for pull_request events intentionally, and that's also the reason why I wrote this workflow in a separate YAML file instead of existed ci.yml, as I want a different workflow trigger for this one.

For pull request events, GitHub actions won't run workflows on the development branch. It would first try to merge the development branch and the main branch, then run workflows on that merged commit. This means (a) we cannot run git push directly, as the upstream if not the development branch, and (b) if the main branch has some newer commit, pushing the formatted code to the development branch would also include these new content. Because of these two reasons, It seems adding pull_request is not a good idea.

My current implementation (i.e. run workflow on the push event) can already fix the PR. This PR itself is an example. See the commit history below:

image

branches-ignore:
- 'main'
- 'dev'

jobs:
format:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, 'ci skip')"
ocavue marked this conversation as resolved.
Show resolved Hide resolved

steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Cache ~/.pnpm-store
ocavue marked this conversation as resolved.
Show resolved Hide resolved
uses: actions/cache@v3
env:
cache-name: cache-pnpm-store
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ matrix.node-version }}-test-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node-version }}-test-${{ env.cache-name }}-
${{ runner.os }}-${{ matrix.node-version }}-test-
${{ runner.os }}-

- name: Install pnpm
run: corepack enable

- name: Install deps
run: pnpm i

- name: Format
run: pnpm run format

- name: Commit files and push
continue-on-error: true
if: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev' }}
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add --all
git commit -m "chore(ci): [bot] format code"
git push
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pnpm-lock.yaml
.cache
dist
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"semi": false
}
}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ Head over to the [discussions](https://github.com/egoist/tsup/discussions) to sh

## Sponsors


<p align="center">
<a href="https://chromatic.com" target="_blank"><picture>
<source media="(prefers-color-scheme: dark)" width="500" srcset="https://fastly.jsdelivr.net/gh/egoist-bot/images@main/uPic/Group 2 (2).png">
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"build": "tsup src/cli-*.ts src/index.ts src/rollup.ts --clean --splitting",
"prepublishOnly": "npm run build",
"test": "npm run build && npm run test-only",
"format": "prettier --write .",
"test-only": "vitest run",
"build-fast": "npm run build -- --no-dts"
},
Expand Down
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"extends": [
"config:base"
]
"extends": ["config:base"]
}
1 change: 0 additions & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
}
}
]

}
}
},
Expand Down
5 changes: 4 additions & 1 deletion src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export async function main(options: Options = {}) {
.option('--dts [entry]', 'Generate declaration file')
.option('--dts-resolve', 'Resolve externals types used for d.ts files')
.option('--dts-only', 'Emit declaration files only')
.option('--experimental-dts [entry]', 'Generate declaration file (experimental)')
.option(
'--experimental-dts [entry]',
'Generate declaration file (experimental)'
)
.option(
'--sourcemap [inline]',
'Generate external sourcemap, or inline source: --sourcemap inline'
Expand Down
27 changes: 15 additions & 12 deletions src/esbuild/node-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { Plugin } from 'esbuild'
* https://nodejs.org/api/esm.html#node-imports
*/
export const nodeProtocolPlugin = (): Plugin => {
const nodeProtocol = 'node:'
const nodeProtocol = 'node:'

return {
name: 'node-protocol-plugin',
setup({ onResolve }) {
onResolve({
filter: /^node:/,
}, ({ path }) => ({
path: path.slice(nodeProtocol.length),
external: true
}))
}
}
return {
name: 'node-protocol-plugin',
setup({ onResolve }) {
onResolve(
{
filter: /^node:/,
},
({ path }) => ({
path: path.slice(nodeProtocol.length),
external: true,
})
)
},
}
}
2 changes: 1 addition & 1 deletion src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function formatDistributionExports(
if (!importPath.match(/^\.+\//)) {
importPath = './' + importPath
}

let seen = {
named: new Set<string>(),
module: new Set<string>(),
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/tree-shaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const treeShakingPlugin = ({
name,
silent,
}: {
treeshake?: TreeshakingStrategy,
treeshake?: TreeshakingStrategy
name?: string
silent?: boolean
}): Plugin => {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const treeShakingPlugin = ({
format: this.format,
file: 'out.js',
sourcemap: !!this.options.sourcemap,
name
name,
})

for (const file of result.output) {
Expand Down
Loading