Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript : trigger "organizeImports" from command line

VSCode has an editor feature, which allows to clean and order imports in javascript and typescript files on saving ( "source.organizeImports": true ).

Question

How can I call this action on a file from the command line ?

something like :

tslint --fix [apply ordered-imports rule] file1 file2

but it seems tslint has its own implementation for "ordered-imports"

What I gathered so far

From what I understood, this feature triggers a call to the organizeImports function in typescript's codebase.

This functionnality is part of typescript's Language Service, but I don't know how to start a language service daemon, and how to interact with it.

Since the code is written in that function, there also probably is a way to call it synchronously from a ts script, but I couldn't find an example of how to setup the objects and variables from scratch to feed them to this function.

like image 376
LeGEC Avatar asked Jun 06 '19 07:06

LeGEC


1 Answers

The organize-imports-cli package does what you want: https://www.npmjs.com/package/organize-imports-cli

like image 190
thorn0 Avatar answered Nov 03 '22 18:11

thorn0