A command line utility for organizing imports within TypeScript files.
This utility is available as an NPM package through GitHub Package Registry or Imagine Learning's private MyGet NPM repository.
npm install --save-dev @imaginelearning/ts-imports-organizer
> npx ts-imports-organizer --help
Usage: ts-imports-organizer [options] files
Organizes the imports within the specified TypeScript files. Files can be listed individually
or as glob patterns.
Options:
--help, -h
Displays help information about this script
'ts-imports-organizer -h' or 'ts-imports-organizer --help'
--version
Displays version info
ts-imports-organizer --version
--stage, -s
Stage files in Git after processing
'ts-imports-organizer --stage=true' or 'ts-imports-organizer -s true'
--help
: Displays help information.--version
: Displays current version.--stage [boolean]
: Stage files in Git after processing. Useful when using in a pre-commit hook.
You can run ts-imports-organizer
as a pre-commit hook using husky
and lint-staged
.
npm install --save-dev husky lint-staged
In package.json
add:
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.ts": "ts-imports-organizer --stage=true"
}
}
If you want to include a code formatter in your pre-commit hook, such as pretty-quick
,
you can include ts-imports-organizer
as the first subtask so it runs before the code formatter.
{
"lint-staged": {
"*.(ts|js|json|scss|css|htm|html)": [
"ts-imports-organizer --stage=true",
"pretty-quick --staged"
]
}
}
Or you can configure them with separate tasks, but be sure to pass the --concurrent false
flag to lint-staged
so each task is run sequentially
(since you'll want ts-imports-organizer
to run prior to your code formatter or linter).
{
"husky": {
"hooks": {
"pre-commit": "lint-staged --concurrent false"
}
},
"lint-staged": {
"*.ts": "ts-imports-organizer --stage=true",
"*.(ts|js|json|scss|css|htm|html)": "pretty-quick --staged"
}
}
The heavy lifting for this project was already done, thanks to these libraries:
argv
: Node based command line argument parser.minimatch
: A minimal matching utility.simple-git
: A light weight interface for running git commands in any node.js application.ts-morph
: TypeScript Compiler API wrapper. Provides an easier way to programmatically navigate and manipulate TypeScript and JavaScript code.