@pdfking/autoexport
v1.9.4
Published
CLI utility to automatically create index.ts files with exports
Downloads
489
Maintainers
Readme
Autoexport
A CLI utility to automatically create index.ts files with barrel exports.
What it does
This tool scans directories based on glob patterns and automatically generates index.ts files that export all found TypeScript files. It's perfect for creating barrel exports that re-export modules from subdirectories.
Installation
Since this is part of a pnpm workspace, you can install it locally:
pnpm install --filter autoexportOr install dependencies from the root:
pnpm installUsage
npx autoexport <output-file> <glob-patterns...>Arguments
<output-file>: The path where the index.ts file should be created (e.g.,src/index.ts)<glob-patterns...>: One or more glob patterns to match TypeScript files (e.g.,src/utils/**src/types/**)
Options
-v, --verbose: Enable verbose output to see what files are being processed-h, --help: Show help information--version: Show version information
Examples
Basic usage
Generate an index.ts file that exports all files from utils and types directories:
npx autoexport src/index.ts "src/utils/**" "src/types/**"With verbose output
npx autoexport src/index.ts "src/utils/**" "src/types/**" --verboseMore complex patterns
npx autoexport dist/index.ts "src/components/**" "src/hooks/**" "src/lib/**"Generated Output
Given a directory structure like:
src/
├── utils/
│ ├── format.ts
│ └── validate.ts
├── types/
│ ├── user.ts
│ └── api.ts
└── index.ts (generated)The generated src/index.ts would contain:
// This file is auto-generated by autoexport
// Do not modify manually
export * from "./types/api"
export * from "./types/user"
export * from "./utils/format"
export * from "./utils/validate"Features
- Automatic discovery: Finds all TypeScript files matching your patterns
- Relative imports: All generated imports are relative to the output file location
- Smart filtering: Ignores node_modules, dist, build directories and .d.ts files
- Self-exclusion: Won't include the output file itself in the exports
- Cross-platform: Works on Windows, macOS, and Linux
- Sorted output: Exports are alphabetically sorted for consistency
File Types Supported
.tsfiles.tsxfiles
Ignored Patterns
The tool automatically ignores:
**/node_modules/****/dist/****/build/****/*.d.ts- The output file itself
Use Cases
- Component libraries: Export all components from a single entry point
- Utility libraries: Create barrel exports for utility functions
- Type definitions: Export all types from a centralized location
- API modules: Export all API-related modules together
- Hooks: Export all custom React hooks from one place
Integration with Build Tools
You can integrate this into your build process:
{
"scripts": {
"build:exports": "autoexport src/index.ts 'src/components/**' 'src/hooks/**' 'src/utils/**'",
"prebuild": "npm run build:exports"
}
}Error Handling
The tool will exit with code 1 if:
- Invalid glob patterns are provided
- Output directory cannot be created
- File write permissions are insufficient
Use --verbose flag to get detailed information about what the tool is doing.
