# env-checker-strict

Ever crashed your server on a weekend due to a missing environment variable? env-checker-strict is a strict and developer-friendly environment variable checker that ensures all required env vars are validated before your app runs. Add the env reference to version control, and catch issues in production — not during your downtime.

## 🚀 Why use this?

Managing environment variables in large codebases can be error-prone:

- Missing env vars crash the app only during runtime ⚠️
- You don't get autocomplete or type checking 🧠
- Keeping track of all required keys is messy 📝

> `env-checker-strict` solves these with a CLI that auto-generates a `checkEnvAndThrowError()` function and a typed `ENVS` accessor.

## ✨ Features

- ✅ Throws on missing env vars **at startup**
- 🧠 Generates `ENVS` object with keys for **autocomplete** and **type safety**
- ⚙️ Supports both **JavaScript** and **TypeScript**
- 🧪 Fully customizable CLI: skip keys, comment block, output file path
- 📄 Auto-detects and parses `.env` file
- 🔌 Auto injects required import and function call to entry file (optional with `--inject` flag) [see CLI Options](#cli-options)

## 📦 Installation

```bash
npm install -D env-checker-strict
```

For global CLI usage:

```bash
npm install -g env-checker-strict
```

## 🧑‍💻 CLI Usage

```bash
env-checker-strict --input .env --output env_checker.js --skip "SECRET,API_KEY" --ts
```

### CLI Options

| Option        | Alias | Description                                                                                   | Default           |
|---------------|-------|-----------------------------------------------------------------------------------------------|--------------------|
| `--input`     | `-i`  | Path to `.env` file                                                                            | `.env`             |
| `--output`    | `-o`  | Output file path for the generated checker                                                     | `env_checker.js`   |
| `--skip`      | `-s`  | Comma-separated list of env keys to skip from validation                                       | *(none)*           |
| `--comment`   | `-c`  | Add a reminder comment at the top of `.env` to run `env-checker-strict`                       | `true`             |
| `--ts`        |       | Generate TypeScript output instead of JavaScript                                               | `false`            |
| `--inject`    |       | Inject `checkEnvAndThrowError()` into the entry file. Optionally pass a path to the entry file | Read from `package.json > main`  |

## 🔧 Example

### Given `.env`

```env
JWT_SECRET=supersecure
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
API_KEY=
```

### CLI Run

```bash
env-checker-strict -i .env -o env_checker.ts --ts -s API_KEY
```

### Output: `env_checker.ts`

```ts
// Auto-generated by env-checker-strict
// ⚠️ Do not modify manually

export const ENV_LIST: string[] = [
  'JWT_SECRET',
  'DB_HOST',
  'DB_USER',
  'DB_PASSWORD'
];

export interface ENVS {
  JWT_SECRET: string;
  DB_HOST: string;
  DB_USER: string;
  DB_PASSWORD: string;
}

export const ENVS: ENVS = {
  JWT_SECRET: process.env.JWT_SECRET as string,
  DB_HOST: process.env.DB_HOST as string,
  DB_USER: process.env.DB_USER as string,
  DB_PASSWORD: process.env.DB_PASSWORD as string
};

export const checkEnvAndThrowError = (): void => {
  for (const env of ENV_LIST) {
    if (!process.env[env]) {
      const message =
        `[ENV ERROR] Missing required environment variable: ${env}\n` +
        `➡️  Make sure '${env}' is defined in your .env file.\n` +
        `📄 Location: ${import.meta.url || 'env-checker.ts'}`;

      console.error(message);
      throw new Error(message);
    }
  }
};
```

## ✅ How to Use in Project

### JavaScript (index.js)
```js
const { checkEnvAndThrowError, ENVS } = require('./env_checker');
checkEnvAndThrowError();
console.log(ENVS.JWT_SECRET); // Autocomplete for all env vars
```

### TypeScript (main.ts)
```ts
import { checkEnvAndThrowError, ENVS } from './env_checker';
checkEnvAndThrowError();
console.log(ENVS.JWT_SECRET); // Fully typed access
```

## 💡 Tips

- Always run the CLI after updating your `.env` file.
- Keep the generated file in source control (optional, but helpful).
- Use the `ENVS` object across your app instead of `process.env`.

## 🪪 License

MIT — safe, free, and open.

---

Made with ❤️ to make your environment safer and your dev experience sharper.

---

PRs and suggestions welcome 🙌
