@droppedcode/index-generator
v1.0.4
Published
Create index files from a folder.
Downloads
2,892
Readme
Index file generator
Create index files from a folder.
The tool will collect all files with exports (recursively) and creates an export from these in the format:
export * from './file-1';
export * from './dir-1/file-1';
export * from './dir-1/file-2';
Only files with export-s will be added to the list (files containing any rows that starts with the export word).
Usage
igen [--exclude ...]
e.g. igen ./src ./src/index.ts --exclude (\.spec|\.test)\.ts$ ^docs\.ts$
This will create the index file "src/index.ts" for all files in the "src" folder except all files that ends with ".spec.ts" or ".test.ts" and the file "src/docs.ts".
Command line arguments
Path to the folder: the path that will be processed. Path to the created file: the path of the file that will be created.
Options
--path/-p (string, first argument by default): Specify a root folder to use. With this option (used multiple times or listing the folder after each other), you can specify multiple entry points.
--out/-o (string, second argument by default): Specify the output file name and path (relative to root) or the name of the output files (see modes).
--config/-c (string): Read a json file as the config.
--mode/-m (string): Set the generator mode:
- path (this is the default): Put the output file to the path defined, if not absolute then relative to the cwd.
- root: Put the output file into the root folder(s).
- folder: File per folder, including sub folder files from the individual files.
- sub: File per folder, including all sub folder index files, ignoring the individual files.
--include/-i (string): include files by regex. The regex will be matched against the relative path from the root of the processed folder. You can list any number of regex after the option or use it multiple times, e.g. -e regex1 regex2. Defaults to [/\.ts$/]
.
--exclude/-e (string): exclude files by regex. The regex will be matched against the relative path from the root of the processed folder. You can list any number of regex after the option or use it multiple times, e.g. -e regex1 regex2.
--eol/-l (string): The line ending used. Possible values: 'os' (the current operating system line ending), 'unix', 'lf', 'n' (use the unix line ending \n
), 'win', 'crlf', 'rn' (use the windows line ending \r\n
), 'r' (\r
), anything else (you can define anything else). Defaults to 'os'.
--eol-at-eof (boolean): Should an empty line added at the end of the file (defaults to true).
--header/-h (string): Set the header text at the beginning of the generated file. Defaults to:
This file was generated by a tool.
Do not modify it.
--header-mode (string): Set how the header should be generated, defaults to multi
:
- 'disabled': Do not add the header.
- 'raw': Add the header as is.
- 'single': Write each line as a single line comment
// header
. - 'multi': Write the header as a multiline comment, surrounded with
/* */
, each line stating with a*
.
--if-needed/-n (boolean): Should files without exports generated.
--format/-f (string): The format the export should be generated. Default to: export * from '{rel}/{name}';
You can use the following variables:
- {name}: The name of the file.
- {ext}: The extension of the file.
- {rel}: The relative path of the directory.
- {abs}: The absolute path of the directory.
From code
From code you can use it by importing the IndexGenerator
class from the package.
import { IndexGenerator, Options } from '@droppedcode/index-generator';
Ignore files
You can ignore files with adding "// index-generator-ignore" comment to the file (anywhere).
Modes
The different modes generate the following for this input:
src
|- f0
|- f1
|- b.ts
|- c.ts
|- a.ts
path
Put the output file to the path defined (with path ./src and no out defined), if not absolute then relative to the cwd (this is the default).
The generated file will not compile because the paths are not valid. You always should define the output path for the generated file (e.g. ./src/index.ts) or modify the format to contain the needed path.
src
|- f0
|- f1
|- b.ts
|- c.ts
|- a.ts
index.ts
export * from './f0/f1/b.ts';
export * from './f0/c.ts';
export * from './a.ts';
root
Put the output file into the root folder(s).
src
|- f0
|- f1
|- b.ts
|- c.ts
|- a.ts
|- index.ts
export * from './f0/f1/b';
export * from './f0/c';
export * from './a';
folder
File per folder, including sub folder files from the individual files.
src
|- f0
|- f1
|- b.ts
|- index.ts
export * from './b';
|- c.ts
|- index.ts
export * from './f1/b';
export * from './c';
|- a.ts
|- index.ts
export * from './f0/f1/b';
export * from './f0/c';
export * from './a';
sub
File per folder, including all sub folder index files, ignoring the individual files.
src
|- f0
|- f1
|- b.ts
|- index.ts
export * from './b';
|- c.ts
|- index.ts
export * from './f1/index';
export * from './c';
|- a.ts
|- index.ts
export * from './f0/index';
export * from './a';