Skip to content

Commit

Permalink
fix: use eslint to format ts,js,json,md and yml, goodbye prettier
Browse files Browse the repository at this point in the history
prettier is great but introduces additional configuration(config, ignores and scripts), and packages. Eslint is pretty much capable of doing eveything and gives you much control over the opinionated nature of prettier.
Prettier describes itself to be "an opinionated code formatter".
Opinionated essentially means it’s not for everyone. Prettier makes a lot of hard-coded decisions to provide a minimal configuration interface. That makes it very easy to use (it’s excellent!) and the code consistent across projects. However, on the other hand, it also means you are losing the ability to have fine-grained tweaks to how your code should look like.
  • Loading branch information
rubiin committed Oct 3, 2023
1 parent 4da9edb commit 5941ee4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ LICENSE
.swcrc
.git
.husky
.prettierrc
makefile
justfile
test
46 changes: 19 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ env file
`Note`: Env files are kept in env folder. The config validation allows 4 environment ['dev', 'prod', 'test','stage'].
The env file name
should be of format .env.[environment] Ex. (.env.dev). The env to use should be provided while running any script as
NODE_ENV=dev yarn dev
NODE_ENV=dev npm run dev

Start local Postgres server and run `npx cross-env NODE_ENV=dev make migrate` to apply migrations
Start local Postgres server and run `npx cross-env NODE_ENV=dev just migrate` to apply migrations

Now you can start the application witt `npx cross-env NODE_ENV=dev yarn start`.
Now you can start the application witt `npx cross-env NODE_ENV=dev npm run start`.

---

Expand All @@ -115,27 +115,24 @@ Now you can start the application witt `npx cross-env NODE_ENV=dev yarn start`.
- 📃 **Swagger** - API Documentation
- 🐳 **Docker Compose** - Container Orchestration
- 🔐 **Helmet** - secure HTTP headers
- 😴 **Insomnia** - Insomnia config for endpoints
- 📏 **ESLint** — Pluggable JavaScript linter
- 💖 **Prettier** - Opinionated Code Formatter

## Available Scripts

- `yarn start` - Start application
- `yarn start:dev` - Start application in watch mode
- `yarn start:prod` - Start built application
- `yarn start:hmr` - Start application with hot module replacement
- `yarn format` - Formats all the files inside src using prettier with config provided in `.prettierrc`
- `yarn lint` - Uses eslint to lint all the files inside src with config provided in `.eslintrc.cjs`
- `yarn orm migration:create` - Uses Mikroorm to create a migration file
- `yarn orm migration:up` - This command is used to run availablexisting migration files.
- `yarn orm migration:down` - This command is used to rollback migration.
- `yarn orm seeder:run` - This command is used to run existing seeders in `src/common/database`.
- `npm run start` - Start application
- `npm run start:dev` - Start application in watch mode
- `npm run start:prod` - Start built application
- `npm run start:hmr` - Start application with hot module replacement
- `npm run lint` - Uses eslint to lint all the files inside src with config provided in `eslint.config.js`
- `npm run orm migration:create` - Uses Mikroorm to create a migration file
- `npm run orm migration:up` - This command is used to run availablexisting migration files.
- `npm run orm migration:down` - This command is used to rollback migration.
- `npm run orm seeder:run` - This command is used to run existing seeders in `src/common/database`.

All the scripts require `NODE_ENV` flag

Additionally, you can also see the scripts in `justfile` which is a cross platform task runner. You can use it by
installing [just](https://github.com/casey/just#packages) and then running `just <script>
installing [just](https://github.com/casey/just#packages) and then running `just <script>`. Ex. `just build`

---

Expand All @@ -146,26 +143,24 @@ installing [just](https://github.com/casey/just#packages) and then running `just
- Also remove the unwanted config variables from the env file
- Make sure you create a env file under `env` directory with name like `.env.something`.The portion after .env is
the `NODE_ENV` value which will be required while running the app
- Also make sure you have ssl files inside `src/resources/ssl` if you tend to use ssl. Replace the sample files with
your ssl files but keep the name same. Additionally

## Migration and seeding

Migrations are used to update the database schema. The migration files are stored in `migrations` directory.

```sh
npx cross-env NODE_ENV=dev yarn orm migration:up # applies migration for dev env
npx cross-env NODE_ENV=dev npm run orm migration:up # applies migration for dev env
```

Seeding is used to insert data into the database. The seeding files are stored in `common/database/seeders` directory.

```sh
npx cross-env USER_PASSWORD=Test@1234 NODE_ENV=dev yarn orm seeder:run # seeds data for dev env with all user password set as Test@1234
npx cross-env USER_PASSWORD=Test@1234 NODE_ENV=dev npm run orm seeder:run # seeds data for dev env with all user password set as Test@1234
```

## Start application

- `npx cross-env NODE_ENV=[env name] yarn start`
- `npx cross-env NODE_ENV=[env name] npm run start`
- View automatically generated swagger api docs by browsing to `http://localhost:[port]/docs`
- View automatically generated swagger stats dashboard by browsing to `http://localhost:[port]/stats`. The username and
password is the values set in the env file under `SWAGGER_USERNAME` and `SWAGGER_PASS` respectively
Expand All @@ -179,8 +174,8 @@ ultimate-nest
│ └── .env.dev * Configuration file for development environment.
│ └── .env.prod * Configuration file for production environment.
│ └── .env.test * Configuration file for test environment.
├── coverage * Coverage reports after running `yarn test:cov` command.
├── dist * Optimized code for production after `yarn build` is run.
├── coverage * Coverage reports after running `npm run test:cov` command.
├── dist * Optimized code for production after `npm run build` is run.
├── src
└── modules * Folder where specific modules all files are stored
└── <module>
Expand Down Expand Up @@ -209,16 +204,13 @@ authentication of the token.

# Deployment

You need to have `docker` and `docker-compose` (not the compose plugin) installed. Also since we are using `makefiles`
for deployment, you need to have `make` installed.
You need to have `docker` and `docker-compose` installed. The image environment variable values can be found at the compose file

```sh
ENV=dev sh ./scripts/deploy.sh # deploys dev environment (.env.dev used)
ENV=prod sh ./scripts/deploy.sh # deploys prod environment (.env.prod used)
```

The password for `redis` and `rabbitmq` is `Test@1234` can be changed in the make file under `deploy` script

More docs found at `docs` folder

<h2 align="center">Do you use this template?<br/>Don't be shy to give it a star! ★</h2>
Expand Down
14 changes: 7 additions & 7 deletions docs/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,35 @@ for [PostgreSQL](https://www.postgresql.org/), but you can use any database.
2. Next, generate migration file:

```bash
NODE_ENV=dev yarn orm migration:create
NODE_ENV=dev npm run orm migration:create
```

3. Apply this migration to database via [yarn run orm migration:up](#run-migration).
3. Apply this migration to database via [npm run run orm migration:up](#run-migration).

### Run migration

```bash
NODE_ENV=dev yarn orm migration:up
NODE_ENV=dev npm run orm migration:up
```

### Revert migration

```bash
NODE_ENV=dev yarn orm migration:down
NODE_ENV=dev npm run orm migration:down
```

More info for the migrations can be found at: https://mikro-orm.io/docs/migrations

### Drop the database and migrate up to the latest version

```bash
NODE_ENV=dev yarn orm migration:fresh
NODE_ENV=dev npm run orm migration:fresh
```

# Generate Seeds

```bash
NODE_ENV=dev yarn orm seeder:create AuthorSeeder # generates the class AuthorSeeder under src/common/database/seeders
NODE_ENV=dev npm run orm seeder:create AuthorSeeder # generates the class AuthorSeeder under src/common/database/seeders
```

```bash
Expand All @@ -112,7 +112,7 @@ export class AuthorSeeder extends Seeder {
To run the generated seeder:

```bash
NODE_ENV=dev yarn orm seeder:run AuthorSeeder # runs the authorseeder
NODE_ENV=dev npm run orm seeder:run AuthorSeeder # runs the authorseeder
```

More info for the seeding can be found at: https://mikro-orm.io/docs/seeding
Expand Down
17 changes: 9 additions & 8 deletions docs/linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
- [Configuration](#configuration)
- [FAQ](#faq)

This project uses Typescript Eslint, and Prettier to catch errors and avoid bike-shedding by enforcing a common code
This project uses Typescript Eslint to catch errors and avoid bike-shedding by enforcing a common code
style.

## Languages

- **TypeScript** is linted by Typescript Eslint and formatted by Prettier
- **JSON** is formatted by Prettier
- **TypeScript** is linted and formatted by Typescript Eslint.

## Scripts

Expand All @@ -24,12 +23,12 @@ There are a few different contexts in which the linters run.

```bash
# Lint all files without auto-fixing
yarn lint
npm run lint
```

```bash
# Lint all files, fixing many violations automatically
yarn lint:fix
npm run lint:fix
```

See `package.json` to update.
Expand All @@ -47,8 +46,10 @@ In supported editors, all files will be linted and show under the linter errors
This boilerplate ships with opinionated defaults, but you can edit each tools configuration in the following config
files:

- [ESLint](https://eslint.org/docs/user-guide/configuring)
- `.eslintrc.cjs`
- [ESLint](https://eslint.org/docs/latest/use/configure/configuration-files-new)
- `eslint.config.js`

Note: This uses eslint flat config, which is the recommended way to configure eslint.

## FAQ

Expand All @@ -60,5 +61,5 @@ files:
possible, because dynamic configurations are simply more powerful, able to respond to environment variables and much
more.

The linting rules can be found at [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) which
The linting rules can be found at [@rubiin/eslint-config](https://github.com/rubiin/eslint-config) which
has more than 100 powerful ESLint rules that help prevent bugs and enforce conventions.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ seed env="dev":

# test e2e with jest
test-e2e env="dev":
USER_PASSWORD=Test@1234 NODE_ENV={{env}} yarn test:e2e
USER_PASSWORD=Test@1234 NODE_ENV={{env}} npm run test:e2e

# will drop the database, run all migrations
seed-fresh env="dev":
Expand Down

0 comments on commit 5941ee4

Please sign in to comment.