A universal tool that automatically scans folders to generate index.ts files.
# 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 --helpnpm install indexgen-cli# 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=falseYou 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 --watchLike ESLint or Prettier, it uses a separate configuration file:
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"]
}
]
}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'],
},
],
};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)
| Option | Type | Default | Description |
|---|---|---|---|
targets |
Target[] |
- | Targets to process |
log |
boolean |
true |
Whether to output general logs (console.log, console.error, console.warn) |
| 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 |
| 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 |
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';Filename: user-profile.tsx
| namingConvention | Result | Description |
|---|---|---|
original |
user_profile |
Default, keep original filename |
PascalCase |
UserProfile |
For React components |
camelCase |
userProfile |
For utility functions |
Filename: icon-logo.svg
| fromWithExtension | Result |
|---|---|
false |
export { default as IconLogo } from './icon-logo'; |
true |
export { default as IconLogo } from './icon-logo.svg'; |
Patterns: *.d.ts, *.test.ts, *.png
| Pattern | Description |
|---|---|
*.d.ts |
Exclude TypeScript declaration files |
*.test.ts |
Exclude test files |
*.png |
Exclude PNG image files |
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=falsesrc/components/
├── Button.tsx
├── Input.tsx
├── Modal.tsx
└── index.ts (auto-generated)
export { Button } from './Button';
export { Input } from './Input';
export { Modal } from './Modal';public/assets/icons/
├── icon-logo.svg
├── icon-menu.svg
└── index.ts (auto-generated)
export { default as IconLogo } from './icon-logo.svg';
export { default as IconMenu } from './icon-menu.svg';Create configuration files in the project root:
# Create JSON config file
touch .indexgen-cli
# Or create JavaScript config file
touch indexgen-cli.config.js{
"scripts": {
"generate:index": "indexgencli --paths=src/components/**",
"dev": "indexgencli --watch"
}
}{
"scripts": {
"dev:watch": "indexgencli --watch"
}
}- When
--pathsoption is provided and no config file exists - Operates only with CLI options
- When no
--pathsoption is provided and config file exists - Operates only with config file settings
- When both
--pathsoption is provided and config file exists - CLI options override config file settings
- 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