Skip to content

Commit

Permalink
feat: lint (#44)
Browse files Browse the repository at this point in the history
+ add lint as pre-commit hook and on pr workflow
+ add convenience scripts
+ lint and format
+ document in CONTRIBUTING.md
  • Loading branch information
yamcodes committed Sep 27, 2023
1 parent 1bb1c77 commit bc35136
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 154 deletions.
File renamed without changes.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Lint, format, and test

on:
pull_request:
branches: [main]

jobs:
lint:
name: Lint and format
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Bun
uses: oven-sh/setup-bun@v1

- name: Lint and format the code
run: bunx biome ci --linter-enabled=true --formatter-enabled=true .

test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Bun
uses: oven-sh/setup-bun@v1

- name: Test the code
run: bun test
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

bun lint-staged
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"editor.tabSize": 2
}
"editor.tabSize": 2
}
95 changes: 33 additions & 62 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,40 @@ Hey there! We're thrilled that you'd like to contribute to this project. Your he
> [!NOTE]
> **Windows users**: see [special note](#special-note-for-windows-users) below.
This project uses [Bun](https://bun.sh) as a runtime as well as a package manager. You'll need to install it and use as your package manager for this project. it We recommend using [antfu/ni](https://github.com/antfu/ni) to avoid worrying about package managers when switching projects.
This project uses [Bun](https://bun.sh) as a runtime as well as a package manager. It's a modern, fast, and lightweight alternative to [Node.js](https://nodejs.org/en/) and [npm](https://www.npmjs.com/). To install Bun, run the following command:

We will use `ni`'s commands in the following code snippets. If you are not using it, you can run `bun i` instead of `ni`, and `bun run` instead of `nr`.

* **Install the latest version of [Bun](https://bun.sh)**
```sh
curl -fsSL https://bun.sh/install | bash
```

* **Install [@antfu/ni](https://github.com/antfu/ni)**
```sh
bun i -g @antfu/ni
```

* **Install dependencies**
```sh
ni
```

### Special note for Windows users

This guide assumes you are using a Unix-like environment, since [Bun is working on a Windows port](https://bun.sh/docs/installation#windows). If you are using Windows, you can use [WSL](https://docs.microsoft.com/en-us/windows/wsl/install-win10) or [Git Bash](https://gitforwindows.org/).

## 💡 Commands

### `nr dev`

Start the development environment.

If it's a Node.js package, it will start the build process in watch mode, or [stub the passive watcher when using `unbuild`](https://antfu.me/posts/publish-esm-and-cjs#stubbing).
### `bun dev`

If it's a frontend project, it usually starts the dev server. You can then develop and see the changes in real time.
Start the development environment in watch mode.

### `nr play`
### `bun build`

If it's a Node.js package, it starts a dev server for the playground. The code is usually under `playground/`.
Build the project for production. The result is under `dist/`.

### `nr build`
### `bun lint`

Build the project for production. The result is usually under `dist/`.
We use [Biome](https://biomejs.dev/) for **both linting and formatting**. It is an ultra-fast, Rust based linter and formatter.
It also lints JSON.

### `nr lint`
We use [ESLint](https://eslint.org/) for **both linting and formatting**. It also lints for JSON, YAML and Markdown files if exists.
You can run `nr lint --fix` to let ESLint formats and lints the code.
Learn more about the [ESLint Setup](#eslint).
You can run `bun lint --apply` to apply any safe fixes automatically.

[**We don't use Prettier**](#no-prettier).
### `nr test`
### `bun test`
> [!NOTE]
> This is just a placeholder for now. We will add more details later once tests are formally added.
Run the tests. We mostly using [Vitest](https://vitest.dev/) - a replacement of [Jest](https://jestjs.io/).
Expand All @@ -75,13 +57,13 @@ Vitest runs in [watch mode by default](https://vitest.dev/guide/features.html#wa
For some projects, we might have multiple types of tests set up. For example `nr test:unit` for unit tests, `nr test:e2e` for end-to-end tests. `nr test` commonly run them together, you can run them separately as needed.
### `nr docs`
### `bun docs`
If the project contains documentation, you can run `nr docs` to start the documentation dev server. Use `nr docs:build` to build the docs for production.
Start the documentation dev server. Use `bun docs:build` to build the docs for production.
### `nr`
### `bun run`
For more, you can run bare `nr`, which will prompt a list of all available scripts.
Print a full list of available scripts.
## 🙌 The Road to a Great Pull Request
Expand Down Expand Up @@ -143,7 +125,7 @@ When ready to publish a new release, we run `nr release`. It prompts a list for

There are two kinds of publishing setups, both performed by `nr release`.

<table><tr><td width="500px" valign="top">
<table><tr><td>

#### Build Locally

Expand All @@ -161,7 +143,7 @@ In `package.json`, we usually have:

So whenever you run `npm publish`, it will make sure you have the latest change in the distribution.

</td><td width="500px" valign="top">
</td><td>

#### Build on CI

Expand All @@ -177,38 +159,41 @@ Changelogs are always generated by GitHub Actions.
## 📖 References
### ESLint
### Lint
We use [ESLint](https://eslint.org/) for both linting and formatting with [`antfu/eslint-config`](https://github.com/antfu/eslint-config).
We use [Biome](https://biomejs.dev/) for both linting and formatting with [a few custom rules](./biome.json). It is an ultra-fast, Rust based linter and formatter.
<table><tr><td width="500px" valign="top">
<table><tr><td>
#### IDE Setup
We recommend using [VS Code](https://code.visualstudio.com/) along with the [ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).
We recommend using [VS Code](https://code.visualstudio.com/) along with the [Biome extension](https://marketplace.visualstudio.com/items?itemName=biomejs.biome).
With the settings on the right, you can have auto fix and formatting when you save the code you are editing.
</td><td width="500px"><br>
</td><td><br>
VS Code's `settings.json`

```json
{
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": false,
"source.fixAll.eslint": true
"quickfix.biome": true
}
}
```

</td></tr></table>

### ⚠️ No Prettier
Since ESLint is already configured to format the code, there is no need to duplicate the functionality with Prettier ([*Why I don't Use Prettier*](https://antfu.me/posts/why-not-prettier)). To format the code, you can run `nr lint --fix` or referring the [ESLint section](#eslint) for IDE Setup.
### No Prettier

If you have Prettier installed in your editor, we recommend you disable it when working on the project to avoid conflict.
> [!WARNING]
> Since ESLint is already configured to format the code, there is no need to duplicate the functionality with Prettier ([*Why I don't Use Prettier*](https://antfu.me/posts/why-not-prettier)). To format the code, you can run `bun lint --apply` or refer to the [Lint section](#lint) for IDE Setup.
>
> If you have Prettier installed in your editor, we recommend you disable it when working on the project to avoid conflict.
> Instead, you may use the [Biome VS Code extension](https://marketplace.visualstudio.com/items?itemName=biomejs.biome).
## 🗒 Additional Info
Expand All @@ -226,17 +211,3 @@ In case you are interested in, here are some interesting tools, many of which in
- [esno](https://github.com/antfu/esno) - TypeScript runner
- [taze](https://github.com/antfu/taze) - dependency updater
- [bumpp](https://github.com/antfu/bumpp) - version bumpper

In addition of `ni`, here is a few shell aliases to be even lazier:

```bash
alias d="nr dev"
alias b="nr build"
alias t="nr test"
alias tu="nr test -u"
alias p="nr play"
alias c="nr typecheck"
alias lint="nr lint"
alias lintf="nr lint --fix"
alias release="nr release"
```
Binary file modified bun.lockb
Binary file not shown.
24 changes: 12 additions & 12 deletions db/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Config } from "drizzle-kit";
import type { Config } from 'drizzle-kit';
export const dbCredentials = {
host: process.env.POSTGRES_HOST || "0.0.0.0",
port: parseInt(process.env.POSTGRES_PORT || '5432'),
user: process.env.POSTGRES_USER || "postgres",
password: process.env.POSTGRES_PASSWORD || "postgres",
database: process.env.POSTGRES_DB || "medium"
}
host: process.env.POSTGRES_HOST || '0.0.0.0',
port: parseInt(process.env.POSTGRES_PORT || '5432'),
user: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || 'postgres',
database: process.env.POSTGRES_DB || 'medium',
};

export const dbCredentialsString = `postgres://${dbCredentials.user}:${dbCredentials.password}@${dbCredentials.host}:${dbCredentials.port}/${dbCredentials.database}`;

export default {
out: "./src/db/migrations",
schema: "**/*.schema.ts",
breakpoints: false,
driver: "pg",
dbCredentials
out: './src/db/migrations',
schema: '**/*.schema.ts',
breakpoints: false,
driver: 'pg',
dbCredentials,
} satisfies Config;
2 changes: 1 addition & 1 deletion db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@
"tables": {},
"columns": {}
}
}
}
2 changes: 1 addition & 1 deletion db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"breakpoints": false
}
]
}
}
10 changes: 6 additions & 4 deletions db/migrations/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {drizzle} from "drizzle-orm/postgres-js";
import {migrate} from "drizzle-orm/postgres-js/migrator";
import {migrationClient} from "@/database.providers";
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import { migrationClient } from '@/database.providers';

await migrate(drizzle(migrationClient), {migrationsFolder: `${import.meta.dir}`});
await migrate(drizzle(migrationClient), {
migrationsFolder: `${import.meta.dir}`,
});
24 changes: 12 additions & 12 deletions db/seed.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { exit } from 'process';
import { db } from '@/database.providers';
import {users} from "@/users/users.schema";
import { users } from '@/users/users.schema';

const data = {
id: users.id.default,
email: 'test@email.com',
username: 'test',
password: 'test',
bio: 'test',
image: 'test',
}
console.log("Inserting user: ", data)
await db.insert(users).values(data)
console.log("User inserted")
id: users.id.default,
email: 'test@email.com',
username: 'test',
password: 'test',
bio: 'test',
image: 'test',
};
console.log('Inserting user: ', data);
await db.insert(users).values(data);
console.log('User inserted');

const userResult = await db.select().from(users);
console.log("User result: ", userResult);
console.log('User result: ', userResult);

exit(0);
22 changes: 11 additions & 11 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { defineConfig } from 'vitepress'
import { defineConfig } from 'vitepress';

// https://vitepress.dev/reference/site-config
export default defineConfig({
title: "RealWorld Elysia Docs",
description: "A Vite Press docs of Real World",
title: 'RealWorld Elysia Docs',
description: 'A Vite Press docs of Real World',
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Home', link: '/' },
{ text: 'Examples', link: '/markdown-examples' }
{ text: 'Examples', link: '/markdown-examples' },
],

sidebar: [
{
text: 'Examples',
items: [
{ text: 'Markdown Examples', link: '/markdown-examples' },
{ text: 'Runtime API Examples', link: '/api-examples' }
]
}
{ text: 'Runtime API Examples', link: '/api-examples' },
],
},
],

socialLinks: [
{ icon: 'github', link: 'https://github.com/vuejs/vitepress' }
]
}
})
{ icon: 'github', link: 'https://github.com/vuejs/vitepress' },
],
},
});
Loading

0 comments on commit bc35136

Please sign in to comment.