@droppedcode/index-generator

中文文档
1.0.4发布于 2024/04/29MITTypeScriptCommonJS近 30 天下载 2926

Create index files from a folder.

原始使用文档
文档目录5 节
  1. Usage
  2. Command line arguments
  3. From code
  4. Ignore files
  5. Modes

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';

版本与发布记录

@droppedcode/index-generator 共发布 5 个版本,首次发布于 2022/03/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.4latest2024/04/292 年前
1.0.32024/04/282 年前
1.0.22022/04/024 年前
1.0.12022/04/024 年前
1.0.02022/03/174 年前

关于 @droppedcode/index-generator 的常见问题

@droppedcode/index-generator 是什么?

Create index files from a folder.

如何安装 @droppedcode/index-generator?

在项目目录运行 npm install @droppedcode/index-generator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:lodash(^4.17.21)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 igen,可以用 npx igen 直接运行。

@droppedcode/index-generator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@droppedcode/index-generator 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@droppedcode/index-generator 可以商用吗?

@droppedcode/index-generator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@droppedcode/index-generator 的下载量有多少?

根据 npm 官方下载统计,@droppedcode/index-generator 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,926 次,日均约 98 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@droppedcode/index-generator 还在维护吗?

最新版本 1.0.4 发布于 2024/04/29(2 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。