indexgen-cli
v1.0.2
Published
A CLI tool that automatically generates index.ts files by scanning directory exports
Downloads
61
Maintainers
Readme
indexgen-cli
A universal tool that automatically scans folders to generate index.ts files.
🚀 Quick Start
# Install package
npm install indexgen-cli
# CLI usage (required: --paths option)
npx indexgencli --paths=src/components/**
# CLI usage (watch mode)
npx indexgencli --paths=src/components/** --watch
# Config file based usage
npx indexgencli --watch
# Show help
npx indexgencli --helpInstallation
npm install indexgen-cliUsage
1. CLI-based Usage
# Basic usage (--paths is required)
indexgencli --paths=src/components/**
# Multiple paths
indexgencli --paths=src/components/**,src/hooks/**
# Watch mode (auto-update on file changes)
indexgencli --paths=src/components/** --watch
# Output filename
indexgencli --paths=src/components/** --outputFile=exports.ts
# File extensions
indexgencli --paths=src/components/** --fileExtensions=.tsx,.ts
# Exclude file patterns
indexgencli --paths=src/components/** --excludes=*.d.ts,*.test.ts
# Export style
indexgencli --paths=src/components/** --exportStyle=named
# Naming convention
indexgencli --paths=src/components/** --namingConvention=PascalCase
# Include extension
indexgencli --paths=src/components/** --fromWithExtension=false2. Config File Based Usage
You can use it simply after creating a config file:
# Config file based watch mode
indexgen --watch
# Override config with CLI options
indexgencli --paths=src/components/** --watch --exportStyle=named
### 3. CLI Options
```bash
# Help
indexgencli --help
indexgen-cli - A tool that automatically scans folders to generate index.ts files
Usage:
indexgencli --paths=<path1,path2> [options]
Required Options:
--paths=<path1,path2> Folder paths to process (multiple paths can be specified with commas)
General Options:
--outputFile=<filename> Name of the index.ts file to generate (default: index.ts)
--fileExtensions=<ext> File extensions to watch (e.g., .tsx,.ts)
--excludes=<pattern1,pattern2> File patterns to exclude (e.g., *.d.ts,*.png)
--exportStyle=<style> Export style to generate (default, named, star, star-as, mixed, auto)
--namingConvention=<rule> Filename conversion rule (camelCase, original, PascalCase)
--fromWithExtension=<true|false> Include file extension in file path (default: false)
Logging Options:
--log=<true|false> Enable/disable log output (default: true)
Mode Options:
--watch Enable watch mode
-h, --help Show this help message
Examples:
indexgencli --paths=src/components/**
indexgencli --paths=src/components/**,src/**/ui/** --watch --exportStyle=named
indexgencli --paths=src/components/** --log=false --debug=true
indexgencli --watchConfiguration Options
Using Config Files
Like ESLint or Prettier, it uses a separate configuration file:
1. JSON Config File (.indexgen-cli)
Create a .indexgen-cli file in the project root:
{
"targets": [
{
"paths": ["src/components", "src/app/**/components"],
"fileExtensions": [".tsx", ".ts", ".jsx", ".js"],
"outputFile": "index.ts",
"exportStyle": "named",
"namingConvention": "PascalCase",
"excludes": ["*.d.ts", "*.test.ts", "*.stories.ts"]
},
{
"paths": ["src/hooks"],
"fileExtensions": [".tsx", ".ts", ".jsx", ".js"],
"outputFile": "index.ts",
"exportStyle": "named",
"namingConvention": "camelCase",
"excludes": ["*.d.ts"]
},
{
"paths": ["public/assets/icons"],
"fileExtensions": [".svg"],
"outputFile": "index.ts",
"exportStyle": "named",
"namingConvention": "PascalCase",
"fromWithExtension": true,
"excludes": ["*.png", "*.jpg", "*.gif"]
}
]
}2. JavaScript Config File (indexgen-cli.config.js)
You can use a JavaScript file for more complex configurations or dynamic settings:
module.exports = {
targets: [
{
paths: ['src/components', 'src/app/**/components'],
fileExtensions: ['.tsx', '.ts', '.jsx', '.js'],
outputFile: 'index.ts',
exportStyle: 'named',
namingConvention: 'PascalCase',
excludes: ['*.d.ts', '*.test.ts', '*.stories.ts'],
},
],
};3. Supported Config File Formats
Configuration files are searched in the following order:
.indexgen-cli(JSON).indexgen-cli.json(JSON)indexgen-cli.config.js(CommonJS)indexgen-cli.config.mjs(ES Module)indexgen-cli.config.ts(TypeScript)
Configuration Options Description
| Option | Type | Default | Description |
| --------- | ---------- | ------- | -------------------------------------------------------------- |
| targets | Target[] | - | Targets to process |
| log | boolean | true | Whether to output general logs (console.log, console.error, console.warn) |
Target Options
| Option | Type | Default | Description |
| ------------------- | ------------------------------------------------------------------ | -------------------------------- | --------------------------------- |
| paths | string[] | - | Paths to process (glob patterns supported) |
| fileExtensions | string[] | [".tsx", ".ts", ".jsx", ".js"] | File extensions to process |
| outputFile | string | "index.ts" | Output filename |
| exportStyle | "default" \| "named" \| "star" \| "star-as" \| "mixed" \| "auto" | "auto" | Export processing method |
| namingConvention | "camelCase" \| "PascalCase" \| "original" | "original" | Naming conversion rule |
| fromWithExtension | boolean | false | Whether to include file extension in from path |
| excludes | string[] | [] | File patterns to exclude |
Export Style Options
| Style | Description | Example |
| --------- | -------------------------- | --------------------------------------------------------------------- |
| default | Default export only | export { default } from './Component'; |
| named | Convert default to named | export { default as Component } from './Component'; |
| star | Use export * | export * from './Component'; |
| star-as | Use export * as | export * as Component from './Component'; |
| mixed | Default and named in one line | export { default as Component, named1, named2 } from './Component'; |
| auto | Auto-determine based on file content | Analyze file content and select appropriate style |
Mixed Style Detailed Description
The mixed style analyzes file content and works as follows:
- When default export exists:
export { default as [Alias], [named1], [named2] } from './path'; - When no default export:
export { [named1], [named2] } from './path';
Examples:
// Button.tsx (with default export)
export default function Button() { ... }
export const ButtonGroup = () => { ... }
export const ButtonSize = { ... }
// Generated index.ts
export { default as Button, ButtonGroup, ButtonSize } from './Button';
// utils.ts (no default export)
export const formatDate = () => { ... }
export const parseDate = () => { ... }
// Generated index.ts
export { formatDate, parseDate } from './utils';Naming Convention Examples
Filename: user-profile.tsx
| namingConvention | Result | Description |
| ---------------- | -------------- | ------------------------ |
| original | user_profile | Default, keep original filename |
| PascalCase | UserProfile | For React components |
| camelCase | userProfile | For utility functions |
fromWithExtension Option Examples
Filename: icon-logo.svg
| fromWithExtension | Result |
| ----------------- | -------------------------------------------------------- |
| false | export { default as IconLogo } from './icon-logo'; |
| true | export { default as IconLogo } from './icon-logo.svg'; |
Excludes Option Examples
Patterns: *.d.ts, *.test.ts, *.png
| Pattern | Description |
| ----------- | ------------------------- |
| *.d.ts | Exclude TypeScript declaration files |
| *.test.ts | Exclude test files |
| *.png | Exclude PNG image files |
Logging Configuration
You can control console output with the log option:
{
"log": true, // General logs (console.log, console.error, console.warn)
"targets": [...]
}Logging Levels:
log: true: Output general task progress, errors, and warning messageslog: false: Disable all general logs
Usage Examples:
# Disable logs
indexgen --paths=src/components --log=false
# Disable all logs
indexgen --paths=src/components --log=falseExamples
Component Folder Structure
src/components/
├── Button.tsx
├── Input.tsx
├── Modal.tsx
└── index.ts (auto-generated)Generated index.ts
export { Button } from './Button';
export { Input } from './Input';
export { Modal } from './Modal';SVG File Folder Structure
public/assets/icons/
├── icon-logo.svg
├── icon-menu.svg
└── index.ts (auto-generated)Generated index.ts (fromWithExtension: true)
export { default as IconLogo } from './icon-logo.svg';
export { default as IconMenu } from './icon-menu.svg';Configuration
Creating Config Files
Create configuration files in the project root:
# Create JSON config file
touch .indexgen-cli
# Or create JavaScript config file
touch indexgen-cli.config.jsAdding Scripts to package.json
{
"scripts": {
"generate:index": "indexgencli --paths=src/components/**",
"dev": "indexgencli --watch"
}
}Auto-detection During Development
{
"scripts": {
"dev:watch": "indexgencli --watch"
}
}Operation Modes
1. CLI-only Mode (cli-only)
- When
--pathsoption is provided and no config file exists - Operates only with CLI options
2. Config-based Mode (config-based)
- When no
--pathsoption is provided and config file exists - Operates only with config file settings
3. Hybrid Mode (hybrid)
- When both
--pathsoption is provided and config file exists - CLI options override config file settings
Notes
- The
--pathsoption is required when using CLI - Files specified in
outputFileare automatically excluded to prevent infinite loops excludespatterns support glob patterns (*.d.ts,*test*, etc.)- The
pathsfield is required when using config files
