NetanelBasal / import-conductor

Automatically organize your TypeScript import statements to keep your code clean and readable.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Logo

All Contributors

import-conductor

Automatically organize your TypeScript imports to keep them clean and readable.

Demo

What it does

Import conductor will order all imports into the following blocks:

1. Block - third party libraries

2. Block - user / company libraries

3. Block - imports from other modules or directories in your codebase

4. Block - imports fro the same module

Take a look at the following source file. It's hard to distinguish between third-party imports, company wide imports and files from same module.

import { Component, OnInit } from '@angular/core';
import {CustomerService} from './customer.service';
import {Customer} from './customer.model';
import {Order} from '../order/order.model';
import {LoggerService} from '@myorg/logger';
import {Observable} from 'rxjs';

A cleaner version that is easy scannable would look like this:

import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs';

import {LoggerService} from '@myorg/logger';

import {Order} from '../order/order.model';

import {CustomerService} from './customer.service';
import {Customer} from './customer.model';

Of course, it's a lot of work to order all import statements in existing code bases. Furthermore, in bigger development teams it's hard to enforce this syntax so that every developer orders their imports accordingly. Especially with AutoImports in IDEs.

That's where import-conductor comes into play. Import-conductor can reorder all imports in your project, and combined with tools like husky you can automatically reorder imports of changed files in a pre commit hook.

Usage

  • Run in the command line:
npx import-conductor -s customer.component.ts -p @myorg
  • Run as a npm script:
 "scripts": {
    "import-conductor": "import-conductor -p @myorg"
 },
  • Integrate with tools like husky:
  "lint-staged": {
    "*.{ts,tsx}": [
      "import-conductor --staged -p @myorg",
      "prettier --write",
      "eslint --fix",
      "git add"
    ]
  },

Options

Option Description Default value
-V --version Display the current version -
-h --help Show a help menu -
-s --source Regex to that matches the source files ./src/**/*.ts
-p --userLibPrefixes The prefix of custom user libraries - this prefix is used to distinguish between third party libraries and company libs []
-d --autoAdd Disable automatically adding the committed files when the staged option is used true
-m --autoMerge Automatically merge 2 import statements from the same source true
--staged Run against staged files false
--silent Run with minimal log output true

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Kevin Kreuzer

πŸ’» 🎨 πŸ“– πŸ€” πŸš‡ 🚧 ⚠️

Shahar Kazaz

πŸ’» πŸ“– πŸ€” πŸš‡ ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

About

Automatically organize your TypeScript import statements to keep your code clean and readable.

License:MIT License


Languages

Language:TypeScript 82.6%Language:JavaScript 17.4%