npm package: @scaffit/env
Description: Environment variable setup with Zod validation for Next.js, React, Vue, Angular, Svelte, Express, Fastify, Node.js projects
Author: Santosh Hanumaiah Ranganatha
Latest version: 1.0.5

# @scaffit/env

Environment variable setup with Zod validation.

## Features

- **Type-safe environment variables** with Zod validation
- **Multi-framework support** - Next.js, React, Vue, Angular, Svelte, Express, Fastify, Node.js
- **Automatic framework detection** - Adapts configuration based on your project
- **Example variables** included for common use cases
- **Feature-based configuration** - Choose what to include (database, auth, APIs, etc.)

## Installation

### Option 1: Using Scaffit CLI (Recommended)
```bash
# Add environment scaffold (no installation needed!)
npx scaffit add env
```

### Alternative: Global Installation
```bash
# Install CLI globally
npm install -g scaffit

# Add environment scaffold
scaffit add env
```

### Option 2: Direct npm package usage
```bash
# Install scaffold directly
npm install @scaffit/env

# Use in your code
import { setupEnv, previewEnv } from '@scaffit/env';

// Setup environment variables with custom options
const result = await setupEnv({
  includeExamples: true,
  useZod: true,
  features: ['database', 'auth', 'apis'],
  projectRoot: './my-project'
});

// Preview changes before applying
const preview = await previewEnv({
  useZod: true,
  features: ['database', 'auth']
});
```

**Note**: Both approaches require `@scaffit/core` to be installed (automatically handled).

## Usage

After installation, you can immediately use environment variables:

```bash
# Copy the example file and fill in your values
cp .env.example .env.local

# Your environment variables are now validated and type-safe
```

**Note**: Environment setup is ready to use immediately after installation.

## Configuration Options

- **Use Zod validation**: Add Zod schema validation for environment variables
- **Include examples**: Add common environment variable examples
- **Select features**: Choose from database, auth, APIs, email, storage, analytics
- **Framework**: Automatically detected (Next.js, React, Vue, Angular, Svelte, Express, Fastify, Node.js)

## Generated Files

### .env.example
Template file with example environment variables (framework-specific):

**Next.js:**
```bash
# Next.js Configuration
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="your-secret-key-here"

# Database
DATABASE_URL="postgresql://username:password@localhost:5432/database"

# API Keys
OPENAI_API_KEY="your-openai-key-here"
STRIPE_SECRET_KEY="your-stripe-secret-key"
```

**React/Vue/Svelte:**
```bash
# React/Vue/Svelte Configuration
VITE_API_URL="http://localhost:3000"

# Database
DATABASE_URL="postgresql://username:password@localhost:5432/database"

# API Keys
OPENAI_API_KEY="your-openai-key-here"
STRIPE_SECRET_KEY="your-stripe-secret-key"
```

**Express/Fastify/Node.js:**
```bash
# Express/Fastify Configuration
PORT=3000
NODE_ENV="development"

# Database
DATABASE_URL="postgresql://username:password@localhost:5432/database"

# Authentication
JWT_SECRET="your-jwt-secret-here"
SESSION_SECRET="your-session-secret-here"
```

### env.ts / env.js
Type-safe environment validation (framework-specific):

**Next.js:**
```typescript
import { z } from 'zod';

const envSchema = z.object({
  NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
  NEXTAUTH_URL: z.string().url(),
  NEXTAUTH_SECRET: z.string().min(32),
  DATABASE_URL: z.string().url(),
  OPENAI_API_KEY: z.string().optional(),
  STRIPE_SECRET_KEY: z.string().optional(),
});

export const env = envSchema.parse(process.env);
export type Env = z.infer<typeof envSchema>;
```

**React/Vue/Svelte:**
```typescript
import { z } from 'zod';

const envSchema = z.object({
  NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
  VITE_API_URL: z.string().url(),
  DATABASE_URL: z.string().url(),
  OPENAI_API_KEY: z.string().optional(),
  STRIPE_SECRET_KEY: z.string().optional(),
});

export const env = envSchema.parse(process.env);
export type Env = z.infer<typeof envSchema>;
```

**Express/Fastify/Node.js:**
```typescript
import { z } from 'zod';

const envSchema = z.object({
  NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
  PORT: z.string().default('3000'),
  DATABASE_URL: z.string().url(),
  JWT_SECRET: z.string().min(32),
  SESSION_SECRET: z.string().min(32),
  OPENAI_API_KEY: z.string().optional(),
  STRIPE_SECRET_KEY: z.string().optional(),
});

export const env = envSchema.parse(process.env);
export type Env = z.infer<typeof envSchema>;
```

## Dependencies Added

- `zod` - Schema validation for environment variables

## Next Steps

1. Copy `.env.example` to `.env.local`
2. Fill in your actual environment variables
3. Import `env` in your application files
4. Use type-safe environment variables throughout your app

## Example Usage

```typescript
import { env } from './env';

// Type-safe access to environment variables
const dbUrl = env.DATABASE_URL; // string
const secret = env.NEXTAUTH_SECRET; // string
```