ts-autoimport-generator
v1.0.6
Published
Generate files with imports based on globs
Readme
ts-autoimport-generator
A CLI tool that automatically generates TypeScript barrel files by collecting exports from files matching glob patterns. Configure output files once in tsag.ts, then run or watch — it handles the rest.
Install
npm install ts-autoimport-generatorQuick start
1. Create tsag.ts in your project root:
import type { Config } from 'ts-autoimport-generator';
export const config: Config = {
stripExtensions: true,
files: {
'./src/imports.generated.ts': {
patterns: ['src/**/*.module.ts'],
},
},
};2. Run:
npx tsagThis generates ./src/imports.generated.ts importing every file matched by the pattern.
Commands
| Command | Description |
| -------------- | -------------------------------------------------------- |
| tsag | Build all output files once and exit |
| tsag watch | Build all output files, then watch for additions/removals and rebuild automatically |
Configuration
The config file must be named tsag.ts and placed in the project root (the directory where you run tsag). It must export a config object.
import type { Config } from 'ts-autoimport-generator';
export const config: Config = {
stripExtensions: true, // optional
files: {
'<output file path>': {
patterns: ['<glob pattern>', ...],
exportName: 'collected', // optional
prefix: './', // optional
tree: false, // optional
},
},
};Config
| Field | Type | Default | Description |
| ----------------- | -------------------------------------- | ------- | -------------------------------------------------------------- |
| stripExtensions | boolean | false | When true, strips .ts/.tsx from generated import paths |
| files | { [outputPath: string]: FileConfig } | | Map of output file paths to their config |
FileConfig
| Field | Type | Default | Description |
| ------------ | ---------- | ------------- | --------------------------------------------------------------------------- |
| patterns | string[] | (required) | Glob patterns used to find input files |
| exportName | string | "collected" | Name of the exported constant in the generated file |
| prefix | string | "./" | Path prefix prepended to each import path |
| tree | boolean | false | When true, emits a nested object tree reflecting the directory structure |
Output modes
Flat (default)
All matched files are imported and spread into a single flat object.
Config:
'./src/imports.generated.ts': {
patterns: ['src/modules/**/*.module.ts'],
exportName: 'modules',
}Generated output:
/* GENERATED FILE, do not edit! */
import * as modules_auth_module from './auth.module';
import * as modules_user_module from './user.module';
export const modules = { ...modules_auth_module, ...modules_user_module };Tree
Files are nested into an object matching the directory structure of the matched files.
Config:
'./src/features.generated.ts': {
patterns: ['src/features/**/*.ts'],
exportName: 'features',
tree: true,
}Generated output:
/* GENERATED FILE, do not edit! */
import * as features_auth_login from './auth/login';
import * as features_auth_logout from './auth/logout';
import * as features_user_profile from './user/profile';
export const features = {
auth: {
login: { ...features_auth_login },
logout: { ...features_auth_logout },
},
user: {
profile: { ...features_user_profile },
},
};Example tsag.ts
import type { Config } from 'ts-autoimport-generator';
export const config: Config = {
stripExtensions: true,
files: {
// Flat barrel of all glob files
'./src/imports.generated.ts': {
patterns: ['src/**/*.glob.ts', 'src/**/*.actions.ts'],
},
// Nested tree of all actions, grouped by directory
'./src/features/actions.generated.ts': {
patterns: ['src/features/**/*.actions.ts'],
exportName: 'actions',
tree: true,
},
},
};How import names are derived
Each matched file gets an import identifier generated by:
- Taking the file path relative to the common root of all patterns
- Replacing every character that is not alphanumeric or
$with_
For example, features/auth/login.actions.ts becomes features_auth_login_actions_ts.
Set stripExtensions: true in the root config to strip .ts/.tsx from import paths. When omitted or false, the full filename including extension is used.
TypeScript types
The Config and FileConfig types are exported from the package and can be used to type your config file:
import type { Config, FileConfig } from 'ts-autoimport-generator';