0.1.1 • Published 3 years ago

@lint-ts-index/cli v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@lint-ts-index/cli

GitHub tag (latest SemVer) License GitHub issues

Purpose of this tool is to check that every files or subdirectories are exported in their corresponding index.ts files.

$ lint-ts-index
foo.ts is not exported in index.ts
If you believe it's an error, please add an exclusion in .indexignore

Have you ever forget to export the content of a new source file into the index.ts file of it's parent directory? Not anymore!

:warning: This project is still experimental and subject to important changes. Use it at your own risk

Installation

Please note that lint-ts-index also exists as an eslint plugin. If you already use the linter in your project, it might be a better choice to use it.

Also, since you might prefer a to use a specific version of the compiler, the typescript package is not installed automatically as a dependency of @lint-ts-index/cli. Which means that you must also install it by yourself alongside the tool.

Global

npm install --global @lint-ts-index/cli

Append typescript at the end of the line if it's not already installed globally.

Project

Install the tool and add it to your package.json as a devDependency with:

npm install --save-dev @lint-ts-index/cli

Append typescript at the end of the line if it's not already installed in your project.

Then it's preferable to add this NPM script in your package.json file so that you can call the linter with npm run lint:ts-index:

{
  "scripts": {
    "lint:ts-index": "lint-ts-index",
    // You can also call the command from your existing lint script.
    "lint": "lint-ts-index && prettier"
    // ...
  }
  // ...
}

Usage

Usage: lint-ts-index [options] [directory]

Check that every files or subdirectories are exported in their corresponding index.ts files.

Arguments:
  directory            The directory to search recursively for index.ts files

Options:
  -c, --config <file>  Use this configuration overriding .lint-ts-index.*
                       config options if present
  -f, --fix            (Experimental) Automatically export the missing sources
                       in their index.ts
  --version            Output version information and exit
  -h, --help           Display help for command

NPX (requires no installation)

npx @lint-ts-index/cli

Examples

Simple example

Given the following directory:

examples/simple/fail
├── bar.ts
├── foo.ts
└── index.ts

And index.ts content:

export * from './bar';

Calling lint-ts-index would exit with a non-zero exit code and output the following:

$ lint-ts-index
foo.ts is not exported in index.ts
If you believe it's an error, please add an exclusion in .indexignore

To fix this error, you can either add the missing export * from './foo'; line in index.ts or add a .indexignore file to ignore the file from the list of modules which must be exported.

Let's say that, in our case, we don't want to export the file. Then, content of the .indexignore, would have the following content:

foo.ts

Once the corrections are applied, the linter should output nothing and have a successful exit code.

Advanced example

Given the following directory:

examples/advanced/fail
├── bar.ts
├── command-line
│   ├── helper.ts
│   ├── index.ts
│   └── plugin-api
│       ├── context.ts
│       ├── factory.ts
│       └── index.ts
├── foo.ts
├── index.ts
├── private
│   └── secret.ts
└── public
    ├── index.ts
    ├── private-file.ts
    └── public-file.ts

Specific requirements:

  • By default, content of advanced should be exported in index.ts.
  • foo.ts contains only private functions which we don't want to expose in the public API.
  • command-line is the code for a command-line tool.
    • command-line/index.ts contains the main entry point and should not be loaded as a library.
    • command-line/helper.ts is some internal utility functions used by command-line/index.ts.
    • command-line/plugin-api will be exposed publicly in a specific plugin API.
  • private has some secret functions in it which we don't want to expose in the public API.
  • public contains most of the public interfaces which must be exposed in the public API. We want to check that every file in it gets exported in public/index.ts
    • public/private-file.ts is a file which we don't want to expose in the public API although it is placed in the public directory.

As-is, the lint-ts-index output would look like this:

$ lint-ts-index
foo.ts is not exported in index.ts
command-line is not exported in index.ts
private is not exported in index.ts
command-line/helper.ts is not exported in command-line/index.ts
command-line/plugin-api is not exported in command-line/index.ts
public/private-file.ts is not exported in public/index.ts
If you believe it's an error, please add an exclusion in .indexignore

Because we need to respect the rules above, the solution here is not to export the reported files and directories. We have to write a .indexignore file with these lines:

foo.ts
private
command-line
command-line/index.ts
public/private-file.ts

Then, the linter should pass and still detect new additions in the directories whose content should be exported by default.

Configuration

See Configuration File Formats.

See also

License

This project is licensed under the MIT license which you can find a copy in the LICENSE file.