# Configuration Module

Environment detection and path resolution utilities for the Chargebee Apps CLI.

## Overview

This module provides centralized configuration management for environment detection and path resolution across the CLI ecosystem. It eliminates the need for scattered try-catch blocks and provides a consistent, configuration-based approach to handling development and production environments.

## Components

### EnvironmentConfig

Detects and manages the current execution environment (development, production, or test).

**Key Features:**
- Automatic environment detection based on NODE_ENV
- Workspace structure analysis for implicit detection
- Simple boolean checks for environment state

**Usage Example:**
```typescript
import { EnvironmentConfig } from '@chargebee/chargebee-apps-shared';

const envConfig = new EnvironmentConfig();

if (envConfig.isDevelopment()) {
	console.log('Running in development mode');
}

console.log(`Environment: ${envConfig.getEnvironment()}`);
```

### PathResolver

Centralized path resolution for all packages based on the current environment.

**Key Features:**
- Environment-aware path resolution
- Support for all major package paths (private-application, public-libs, etc.)
- Type-safe path resolution with PathType enum
- Automatic handling of workspace vs installed package structures

**Supported Path Types:**
- `PathType.CLI_ROOT` - Workspace root directory
- `PathType.PRIVATE_APP_DIST` - Private application dist directory
- `PathType.PRIVATE_APP_ROOT` - Private application root directory
- `PathType.PUBLIC_LIBS_TEMPLATES` - Public libs templates directory
- `PathType.PUBLIC_LIBS_UI_WEB` - Public libs UI web directory

**Usage Example:**
```typescript
import { PathResolver, PathType } from '@chargebee/chargebee-apps-shared';

const pathResolver = new PathResolver();

// Get specific paths
const templatesPath = pathResolver.getPublicLibsTemplatesDir();
const cliRoot = pathResolver.getCliRoot();
const privateAppDist = pathResolver.getPrivateAppDistDir();

// Or use the generic resolver
const uiPath = pathResolver.resolvePath(PathType.PUBLIC_LIBS_UI_WEB);
```

## Architecture

### Environment Detection Logic

The environment is determined using the following priority:
1. **NODE_ENV environment variable** - Explicit environment setting
2. **Workspace structure** - Presence of workspaces field in package.json indicates development
3. **Default** - Falls back to production mode

### Path Resolution Logic

**Development Mode:**
- Uses workspace-relative paths
- Resolves from CLI_ROOT (workspace root)
- Example: `<CLI_ROOT>/packages/public-libs/templates`

**Production Mode:**
- Uses `require.resolve()` to find installed packages
- Works with published npm packages
- Example: Resolves from `node_modules/@chargebee/chargebee-apps-libs`

## Benefits

### Before (Error-based approach)
```typescript
let templatesPath: string;
try {
	const publicLibsPath = require.resolve('@chargebee/chargebee-apps-libs');
	const packageRoot = path.dirname(path.dirname(publicLibsPath));
	templatesPath = path.join(packageRoot, 'templates');
} catch (error) {
	templatesPath = path.join(CLI_ROOT, 'packages', 'public-libs', 'templates');
}
```

### After (Configuration-based approach)
```typescript
const pathResolver = new PathResolver();
const templatesPath = pathResolver.getPublicLibsTemplatesDir();
```

### Key Improvements
- **Cleaner code** - No try-catch blocks scattered across the codebase
- **Configuration-based** - Environment detection happens once, not on every path resolution
- **Centralized** - All path resolution logic in one place
- **Testable** - Easy to mock and test different environments
- **Maintainable** - Changes to path structure only need updating in one place
- **Type-safe** - PathType enum ensures valid path types

## Testing

The module can be tested by:
1. Setting NODE_ENV environment variable
2. Providing a custom EnvironmentConfig to PathResolver
3. Testing in different directory structures

```typescript
// Test with custom environment config
const mockEnvConfig = {
	getEnvironment: () => EnvironmentType.DEVELOPMENT,
	isDevelopment: () => true,
	isProduction: () => false,
	isTest: () => false
};

const pathResolver = new PathResolver(mockEnvConfig);
```

## Migration Guide

When migrating existing code to use this module:

1. **Import the PathResolver**
```typescript
import { PathResolver } from '@chargebee/chargebee-apps-shared';
```

2. **Create a singleton instance**
```typescript
const pathResolver = new PathResolver();
```

3. **Replace try-catch path resolution**
- Old: `try { require.resolve(...) } catch { fallback }`
- New: `pathResolver.getPublicLibsTemplatesDir()`

4. **Update function signatures if needed**
- Remove CLI_ROOT parameters where PathResolver can be used instead

## Files

- `environment.ts` - Environment detection and configuration
- `path-resolver.ts` - Centralized path resolution
- `README.md` - This file

## Related

- [`src/types/common.ts`](../types/README.md) - Uses PathResolver for CLI_ROOT
- [`packages/private-cli/src/commands/create.ts`](../../../../private-cli/src/commands/create.ts) - Example usage
- [`packages/public-cli/src/commands/create.ts`](../../../../public-cli/src/commands/create.ts) - Example usage

